description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, n, m, l, h, a):
def speed(h, a, t):
return h + a * t
def is_final_speed_good(t):
tot = 0
for i in range(n):
s = speed(h[i], a[i], t)
if s >= l:
tot += s
return tot >= m
low = 0
high = 10**9
mid = 0
while low < high:
mid = (low + high) // 2
if is_final_speed_good(mid):
high = mid
else:
low = mid + 1
return high
|
CLASS_DEF FUNC_DEF FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
low = 0
high = max(L, M)
mid = 0
ret = 0
while low <= high:
mid = int((low + high) / 2)
t_speed = 0
for i in range(N):
h_speed = H[i] + A[i] * mid
if h_speed >= L:
t_speed += h_speed
if t_speed >= M:
ret = mid
high = mid - 1
else:
low = mid + 1
return ret
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
def check(val):
s = 0
for i in range(N):
x = H[i] + val * A[i]
if x >= L:
s += x
return s >= M
l, r = 0, min(L, M)
ans = 0
while l <= r:
mid = (l + r) // 2
x = check(mid)
if x:
ans = mid
r = mid - 1
else:
l = mid + 1
return ans
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Bikers:
def __init__(self, bikers, alarm, fspeed):
self.bikers = bikers
self.speed_alarm = alarm
self.fast_speed = fspeed
def alarm(self, speed, accel):
l = 0
r = 0
x = max(self.fast_speed, self.speed_alarm)
for i in range(self.bikers):
if (x - speed[i]) % accel[i] == 0:
r = max(r, x - speed[i] // accel[i])
else:
r = max(r, x - speed[i] // accel[i] + 1)
while l <= r:
mid = (l + r) // 2
sum = 0
for i in range(self.bikers):
if speed[i] + accel[i] * mid >= self.fast_speed:
sum += speed[i] + accel[i] * mid
if sum >= self.speed_alarm:
r = mid - 1
else:
l = mid + 1
return l
class Solution:
def buzzTime(self, N, M, L, H, A):
bk = Bikers(N, M, L)
return bk.alarm(H, A)
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
r = 0
l = max(L, M)
def check(mid):
speed = 0
for i in range(N):
s = H[i] + A[i] * mid
if s >= L:
speed += s
return speed >= M
ans = 0
while r <= l:
mid = (l + r) // 2
if check(mid):
ans = mid
l = mid - 1
else:
r = mid + 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def alarm(self, H, A, M, L, hour):
tot = 0
for i in range(len(H)):
ti = H[i] + A[i] * hour
if ti > L:
tot += ti
return tot >= M
def binarysearch(self, l, r, H, A, M, L):
if l <= r:
mid = (l + r) // 2
if self.alarm(H, A, M, L, mid):
return self.binarysearch(l, mid - 1, H, A, M, L)
else:
return self.binarysearch(mid + 1, r, H, A, M, L)
return l
def buzzTime(self, N, M, L, H, A):
return self.binarysearch(1, max(M, L), H, A, M, L)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
strt = 0
end = max(M, L)
while strt <= end:
mid = strt + (end - strt) // 2
if self.TS(H, A, N, M, L, mid):
end = mid - 1
else:
strt = mid + 1
return end
def TS(self, H, A, N, M, L, T):
t = 0
for i in range(0, N):
x = H[i] + (T - 1) * A[i]
if x >= L:
t += x
if t >= M:
return True
return False
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def check(h, N, M, L, H, A):
track_speed = 0
for i in range(N):
Biker_speed = H[i] + h * A[i]
if Biker_speed >= L:
track_speed += Biker_speed
if track_speed >= M:
return True
return False
def buzzTime(self, N, M, L, H, A):
low = 0
high = 10**9
ans = 0
while low <= high:
mid = (low + high) // 2
if Solution.check(mid, N, M, L, H, A):
high = mid - 1
ans = mid
else:
low = mid + 1
return int(ans)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
lTime = 0
rTime = 9223372036854775807
speed = 0
for i in range(N):
if H[i] >= L:
speed = speed + H[i]
if speed >= M:
return 0
while lTime <= rTime:
mTime = int((lTime + rTime) / 2)
mSpeed = 0
prevSpeed = 0
for i in range(N):
if H[i] + mTime * A[i] >= L:
mSpeed = mSpeed + (H[i] + mTime * A[i])
if H[i] + (mTime - 1) * A[i] >= L:
prevSpeed = prevSpeed + (H[i] + (mTime - 1) * A[i])
if mSpeed >= M and prevSpeed < M:
return mTime
if mSpeed >= M:
rTime = mTime
else:
lTime = mTime
return lTime
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def isvalid(self, N, M, L, H, A, m):
speedoftrack = 0
for i in range(N):
if H[i] + m * A[i] >= L:
speedoftrack += H[i] + m * A[i]
if speedoftrack >= M:
return True
return False
def buzzTime(self, N, M, L, H, A):
s = 0
e = max(L, M)
while s <= e:
m = s + (e - s) // 2
if self.isvalid(N, M, L, H, A, m):
ans = m
e = m - 1
else:
s = m + 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
def isPossible(m):
fspeed = 0
for i in range(N):
speed = H[i] + m * A[i]
if speed >= L:
fspeed += speed
if fspeed >= M:
return True
else:
return False
ans = -1
s = 0
e = max(L, M)
while s <= e:
m = (s + e) // 2
if isPossible(m):
e = m - 1
ans = m
else:
s = m + 1
return ans
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
low, high = 0, max(L, M)
def isvalid(N, M, L, H, A, time):
fast_bike_sum = 0
for i in range(len(H)):
speed = H[i] + A[i] * time
if speed >= L:
fast_bike_sum += speed
if fast_bike_sum >= M:
return True
else:
return False
while low < high:
mid = (low + high) // 2
if isvalid(N, M, L, H, A, mid):
high = mid
else:
low = mid + 1
return high
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def check(self, N, M, L, H, A, mid):
pass
def buzzTime(self, N, M, L, H, A):
s = 0
e = 10000000000.0
mid = (s + e) // 2
ans = -1
while s <= e:
speed = 0
for i in range(N):
if H[i] + mid * A[i] >= L:
speed += H[i] + mid * A[i]
if speed >= M:
ans = mid
e = mid - 1
else:
s = mid + 1
mid = (s + e) // 2
return int(ans)
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
def currScore(N, H, A, L, ind):
curr = 0
for i in range(N):
c = H[i] + A[i] * ind
if c >= L:
curr += c
return curr
class Solution:
def buzzTime(self, N, M, L, H, A):
st, en = 0, max(M, L) // max(H) + 1
while st < en:
mid = (st + en) // 2
sc = currScore(N, H, A, L, mid)
if sc == M:
return mid
elif sc > M:
en = mid
else:
st = mid + 1
return en
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def check(self, N, total, M, L, H, A):
count = 0
for i in range(N):
temp = A[i] * (total - 1) + H[i]
if temp >= L:
count += temp
if count >= M:
return True
return False
def buzzTime(self, N, M, L, H, A):
low = 0
minvalue = min(A)
high = M + max(H) // minvalue + 1
ans = 0
while low <= high:
mid = low + (high - low) // 2
if self.check(N, mid, M, L, H, A):
high = mid - 1
ans = mid
else:
low = mid + 1
return ans - 1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def isPossible(self, N, M, L, H, A, mid):
net_speed = 0
for i in range(N):
a = H[i]
d = A[i]
speed = a + mid * d
if speed >= L:
net_speed += speed
if net_speed >= M:
return True
else:
return False
def buzzTime(self, N, M, L, H, A):
start = 0
end = M
ans = -1
mid = start + (end - start) // 2
while start <= end:
if self.isPossible(N, M, L, H, A, mid):
ans = mid
end = mid - 1
else:
start = mid + 1
mid = start + (end - start) // 2
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
low = 0
high = (max(M, L) - min(H)) // min(A)
while low < high:
mid = (low + high) // 2
summa = 0
for i in range(N):
speed = H[i] + mid * A[i]
if speed > L:
summa += speed
if summa == M:
return mid
if summa < M:
low = mid + 1
else:
high = mid
return low
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
high = min(L, M)
low = 0
while low < high:
mid = (low + high) // 2
if bool_(mid, N, M, L, H, A):
high = mid
else:
low = mid + 1
return high
def bool_(mid, N, M, L, H, A):
sumCount = 0
for i in range(N):
temp = H[i] + A[i] * mid
if temp > L:
sumCount += temp
if sumCount > M:
return True
else:
return False
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
def check(A, L, M, H, lo, hi, mid):
s = 0
for i in range(len(A)):
if mid * A[i] + H[i] >= L:
s += mid * A[i] + H[i]
return s >= M
class Solution:
def buzzTime(self, N, M, L, H, A):
lo = 0
hi = max(M, L)
mid = lo + (hi - lo) // 2
ans = -1
while lo <= hi:
mid = lo + (hi - lo) // 2
if check(A, L, M, H, lo, hi, mid):
ans = mid
hi = mid - 1
else:
lo = mid + 1
return ans
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N, M, L = [int(x) for x in input().split()]
H = [(0) for x in range(N)]
A = [(0) for x in range(N)]
for i in range(N):
H[i], A[i] = [int(y) for y in input().split()]
ob = Solution()
print(ob.buzzTime(N, M, L, H, A))
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
lo, hi = 0, 10**9
potential_alarm_hour = 0
while lo <= hi:
mid = lo + (hi - lo) // 2
speeds = (h + mid * a for h, a in zip(H, A))
fast_speeds = filter(lambda x: x >= L, speeds)
if sum(fast_speeds) > M:
potential_alarm_hour = mid
hi = mid - 1
else:
lo = mid + 1
return potential_alarm_hour
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
totalTrackSpeed = 0
hr = -1
firstMin = False
for i in range(N):
val = (L - H[i]) // A[i]
if not firstMin:
hr = val
firstMin = True
if hr > val:
hr = val
while totalTrackSpeed < M:
totalTrackSpeed = 0
bikerSpeed = 0
for i in range(N):
bikerSpeed = H[i] + A[i] * hr
if bikerSpeed >= L:
totalTrackSpeed += bikerSpeed
if totalTrackSpeed < M:
hr += 1
return hr
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
l, r, mid, sum = 0, 0, 0, 0
x = max(M, L)
for i in range(N):
if (x - H[i]) % A[i] == 0:
r = max(r, (x - H[i]) // A[i])
else:
r = max(r, (x - H[i]) // A[i] + 1)
while l <= r:
mid = (l + r) // 2
sum = 0
for i in range(N):
if H[i] + A[i] * mid >= L:
sum += H[i] + A[i] * mid
if sum >= M:
r = mid - 1
else:
l = mid + 1
return l
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, n, M, L, s, a):
def check(e):
c = 0
for i in range(n):
if s[i] + e * a[i] >= L:
c += s[i] + e * a[i]
return c >= M
i = 0
j = 10**9
ans = 0
while i <= j:
m = (i + j) // 2
if check(m):
ans = m
j = m - 1
else:
i = m + 1
return ans
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Geek is organising a bike race with N bikers. The initial speed of the ith biker is denoted by H_{i} Km/hr and the acceleration of ith biker as A_{i} Km/Hr2. A biker whose speed is 'L' or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is 'M' kilometers per hour or more, the safety alarm turns on.
Find the minimum number of hours after which the safety alarm will start.
Example 1:
Input:
N = 3, M = 400, L = 120
H = {20, 50, 20}
A = {20, 70, 90}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [20 40 60 80 100]
Biker2= [50 120 190 260 330]
Biker3= [20 110 200 290 380]
Initial Speed on track = 0
because none of the biker's speed is fast enough.
Speed on track after 1st Hour= 120
Speed on track after 2nd Hour= 190+200=390
Speed on track after 3rd Hour= 260+290=550
Alarm will start at 3rd Hour.
Example 2:
Input:
N = 2, M = 60, L = 120
H = {50, 30}
A = {20, 40}
Output: 3
Explaination:
Speeds of all the Bikers at ith hour
Biker1= [50 70 90 110 130]
Biker2= [30 70 110 150 190]
Initial Speed on track = 0 because none of the
biker's speed is fast enough.
Speed on track at 1st Hour= 0
Speed on track at 2nd Hour= 0
Speed on track at 3rd Hour= 150
Alarm will buzz at 3rd Hour.
Your Task:
You do not need to read input or print anything. Your task is to complete the function buzzTime() which takes N, M, L and array H and array A as input parameters and returns the time when alarm buzzes.
Expected Time Complexity: O(N*log(max(L,M)))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L, M ≤ 10^{10}
1 ≤ H_{i}, A_{i} ≤ 10^{9}
|
class Solution:
def buzzTime(self, N, M, L, H, A):
low = 0
high = M
res = 0
while low <= high:
mid = (low + high) // 2
Speed = 0
for i in range(N):
tmp = A[i] * mid + H[i]
if tmp >= L:
Speed += tmp
if Speed >= M:
high = mid - 1
res = mid
else:
low = mid + 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
import sys
n = int(input())
l = list(map(int, input().split()))
s = sum(l)
m = max(l)
n = n - 1
if s % n == 0:
res = s // n
else:
res = s // n + 1
if res < m:
res = m
print(res)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
n = int(input())
a = [int(x) for x in input().split()]
res = max((sum(a) + (n - 2)) // (n - 1), max(a))
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
def ceil(x):
if int(x) != x:
return int(x + 1)
else:
return int(x)
def mafia():
n = int(input())
rounds = list(map(int, str(input()).split()))
count = 0
rounds.sort(reverse=True)
for i in range(n - 1, 0, -1):
if rounds[i - 1] == rounds[i]:
continue
else:
steps = n - i
count += steps * (rounds[i - 1] - rounds[i])
rounds[0] -= count
if rounds[0] > 0:
k = ceil(rounds[0] / (n - 1))
count += n * k
rounds[0] += k * (1 - n)
if rounds[0] <= 0:
count += rounds[0]
print(count)
mafia()
|
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
n = int(input())
a = [int(s) for s in input().split()]
sum, b = 0, 0
for i in a:
b = max(b, i)
sum += i
if b * n - sum >= b:
print(b)
else:
print((sum + n - 2) // (n - 1))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
n = int(input())
numbers = input().split()
num = [int(x) for x in numbers]
total = sum(num)
max_num = max(num)
rounds = int((total + n - 2) / (n - 1))
print(max(max_num, rounds))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
n = int(input())
a = list(map(int, input().split()))
mx = max(a)
if sum(a) % (n - 1) == 0:
mx2 = sum(a) // (n - 1)
else:
mx2 = sum(a) // (n - 1) + 1
print(max(mx, mx2))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
n = int(input())
a = list(map(int, input().split()))
x = sum(a) // (n - 1) + (1 if sum(a) % (n - 1) != 0 else 0)
print(x if x >= max(a) else max(a))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
import sys
n = int(input())
l = list(map(int, input().split()))
s = sum(l)
n = n - 1
res = s % n == 0 and s // n or s // n + 1
res = max(res, max(l))
print(res)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
n = int(input())
sez = [int(i) for i in input().split()]
rez = max(sez)
sez.sort()
mini = sez[0]
for i in range(n):
sez[i] -= mini
fora = 0
for i in range(1, n):
fora += sez[n - 1] - sez[i]
mini -= fora
if mini > 0:
rez += mini // (n - 1)
mini %= n - 1
if mini != 0:
rez += 1
print(rez)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?
Input
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
Output
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
3 2 2
Output
4
Input
4
2 2 2 2
Output
3
Note
You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
|
n = int(input())
array = list(map(int, input().split()))
maxVal = max(array)
summation = sum(array)
val = summation // (n - 1)
if summation % (n - 1) != 0:
val = val + 1
print(max(val, max(array)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
import sys
try:
sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")
except:
pass
input = sys.stdin.readline
n, s = map(int, input().split())
a = list(map(int, input().split()))
l, r = 0, n
new_s = 0
ans = 0
while l < r:
mid = (l + r + 1) // 2
new_a = sorted([(a[i] + (i + 1) * mid) for i in range(n)])
new_s = sum(new_a[:mid])
if new_s > s:
r = mid - 1
else:
ans = new_s
l = mid
print(l, ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
from sys import stdin
def check(n, s, a):
a.sort(key=lambda x: x[1] * n + x[0])
r = 0
for i in a[:n]:
r += i[0] + i[1] * n
return r
def main():
n, s = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
for i in range(n):
a[i] = [a[i], i + 1]
l = 0
res = 0
r = n + 1
while r - l > 1:
m = (l + r) // 2
b = a[:]
res_new = check(m, s, b)
if res_new <= s:
l = m
res = res_new
else:
r = m
print(l, res)
main()
|
FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def f(li, items):
if items > len(li):
items = len(li)
price = []
for i in range(len(li)):
price.append(li[i] + items * (i + 1))
price.sort()
min_cost = 0
for i in range(items):
min_cost += price[i]
return min_cost
def solution(li, amount_have):
l, r = 0, len(li)
while l <= r:
mid = (l + r) // 2
out = f(li, mid)
out1 = f(li, mid + 1)
if out <= amount_have and out1 > amount_have:
return [mid, out]
elif out > amount_have:
r = mid - 1
else:
l = mid + 1
return [mid, out]
def input_test():
n, amount_have = map(int, input().split())
li = [int(x) for x in input().split()]
ans = solution(li, amount_have)
print(" ".join(map(str, ans)))
input_test()
|
FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN LIST VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN LIST VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
lo, hi = 0, n + 1
while lo < hi:
mid = (lo + hi) // 2
if mid > n:
hi = mid
continue
if sum(sorted(x + (i + 1) * mid for i, x in enumerate(a))[:mid]) <= S:
lo = mid + 1
else:
hi = mid
lo -= 1
print(lo, sum(sorted(x + (i + 1) * lo for i, x in enumerate(a))[:lo]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
from sys import stdin
inFile = stdin
tokens = []
tokens_next = 0
def next_str():
nonlocal tokens, tokens_next
while tokens_next >= len(tokens):
tokens = inFile.readline().split()
tokens_next = 0
tokens_next += 1
return tokens[tokens_next - 1]
def nextInt():
return int(next_str())
def min_cost(a, k):
if k > len(a):
return False
new_a = [(a[i] + (i + 1) * k) for i in range(len(a))]
new_a = sorted(new_a)
return sum(new_a[:k])
n = nextInt()
S = nextInt()
a = [nextInt() for _ in range(n)]
low = 0
high = n + 1
while low + 1 < high:
mid = (low + high) // 2
if min_cost(a, mid) <= S:
low = mid
else:
high = mid
print(low, min_cost(a, low))
|
ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
def tr(m):
bb = [(x + (i + 1) * m) for i, x in enumerate(b)]
bb = sorted(bb)
res = sum(bb[:m])
if res <= s:
return res
else:
return None
res = None
lo = 0
hi = n
d = {}
maxgood = -1
while lo < hi:
mid = (lo + hi) // 2
temp = tr(mid)
d[mid] = temp
if temp is not None:
lo = mid + 1
maxgood = max(maxgood, mid)
else:
hi = mid
temp = tr(hi)
d[hi] = temp
if temp is not None:
maxgood = max(maxgood, hi)
temp = tr(lo)
d[lo] = temp
if temp is not None:
maxgood = max(maxgood, lo)
temp = tr(mid)
d[mid] = temp
if temp is not None:
maxgood = max(maxgood, mid)
print(maxgood, d[maxgood])
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, k = map(int, input().split())
arr = list(map(int, input().split()))
def answer(x):
arr_new = [0]
for i in range(1, n + 1):
arr_new.append(arr[i - 1] + i * x)
arr_new.sort()
ans = 0
for j in range(0, x + 1):
ans = ans + arr_new[j]
return ans
fin_ans = answer(n)
l = 1
r = n
items = n
if fin_ans > k:
while r <= n and l <= r and l >= 1:
mid = l + (r - l) // 2
temp_ans = answer(mid)
if temp_ans <= k:
fin_ans = temp_ans
items = mid
if temp_ans == k:
break
elif temp_ans > k:
r = mid - 1
else:
l = mid + 1
if fin_ans > k:
print("0 0")
else:
print(str(items) + " " + str(fin_ans))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = map(int, input().split())
v = list(map(int, input().split()))
r = n + 1
l = 0
o = 0
while r - l != 1:
s1 = s
b = []
t = True
for i in range(n):
b.append(v[i] + (i + 1) * ((r + l) // 2))
b.sort()
i = 0
sum = 0
for i in range((r + l) // 2):
sum += b[i]
if sum <= s:
l = (r + l) // 2
o = sum
else:
r = (r + l) // 2
s = s1
print(l, o)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def getCost(n, k, a):
cost = [(a[i] + k * (i + 1)) for i in range(n)]
cost.sort()
return sum(cost[:k])
n, s = map(int, input().split())
a = list(map(int, input().split()))
l = 0
r = n + 1
temp = 0
ans = 0
while l < r - 1:
m = (l + r) // 2
temp = getCost(n, m, a)
if temp <= s:
ans = temp
l = m
else:
r = m
print(l, ans)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = map(int, input().split())
a = list(map(int, input().split()))
beg = 0
end = n
def trier(k):
p = sorted([(a[i] + k * (i + 1)) for i in range(n)])
som = sum(p[0:k])
return 0 if som > s else som
l = 0
r = n
while l < r:
k = (l + r + 1) // 2
if trier(k):
l = k
else:
r = k - 1
print(l, trier(l))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def f(k, a: [int]):
b = [0] * len(a)
for i in range(len(a)):
b[i] = a[i] + (i + 1) * k
b.sort()
ans = 0
for i in range(k):
ans += b[i]
return ans
def main():
n, S = map(int, input().split())
a = list(map(int, input().split()))
l = 0
r = n
while True:
k = (r - l) // 2 + l
T = f(k, a)
if T > S:
r = k - 1
elif k == n:
print(k, T)
break
else:
T2 = f(k + 1, a)
if T2 > S:
print(k, T)
break
else:
l = k + 1
main()
|
FUNC_DEF LIST VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def can(a, s, k):
global cost
costs = [(a[i] + (i + 1) * k) for i in range(n)]
costs.sort()
cost = sum(costs[:k])
return cost <= s
cost = 0
n, s = map(int, input().split())
a = list(map(int, input().split()))
lt, rt = 0, n + 1
while lt + 1 < rt:
mid = (lt + rt) // 2
if can(a, s, mid):
lt = mid
else:
rt = mid
can(a, s, lt)
print(lt, cost)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = map(int, input().split())
a = list(map(int, input().split()))
l, r, m, cost = 0, n + 1, -1, -1
while r - l > 1:
m = (l + r) // 2
cost = sum(sorted([(a[i] + (i + 1) * m) for i in range(n)])[:m])
l, r = (l, m) if cost > S else (m, r)
print(l, sum(sorted([(a[i] + (i + 1) * l) for i in range(n)])[:l]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def main():
n, s = map(int, input().split())
aa = list(map(int, input().split()))
lo, hi = 0, n
helper = lambda e: sum(sorted(i * e + a for i, a in enumerate(aa, 1))[:e])
while lo < hi:
mid = (lo + hi + 1) // 2
t = helper(mid)
if s < t:
hi = mid - 1
else:
lo = mid
print(lo, helper(lo))
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = map(int, input().strip().split())
A = list(map(int, input().strip().split()))
def cost(k):
if k == 0:
return 0
costs = []
for i in range(n):
costs.append(A[i] + (i + 1) * k)
costs = sorted(costs)
return sum(costs[:k])
def cls():
l = 0
r = n + 1
while l < r - 1:
m = (l + r) // 2
if cost(m) <= S:
l = m
else:
r = m
print(l, cost(l))
cls()
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def cheapestPrice(A, k):
prices = []
for i, a in enumerate(A):
prices.append(a + (i + 1) * k)
prices.sort()
return sum(prices[:k])
def ok(A, S, k):
return cheapestPrice(A, k) <= S
n, S = map(int, input().split())
A = [int(a) for a in input().split()]
k = -1
skip = n
while skip >= 1:
while k + skip <= n and ok(A, S, k + skip):
k += skip
skip //= 2
print(k, cheapestPrice(A, k))
|
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def attempt_buy_n_items(n, budget, prices):
prices = list(prices)
for i, price in enumerate(prices):
prices[i] = price + n * (i + 1)
prices = sorted(prices)
if sum(prices[:n]) <= budget:
return sum(prices[:n])
n, b = [int(n) for n in input().split()]
prices = [int(n) for n in input().split()]
items_to_buy = n
max_items = 0
min_price = 0
l, r = 1, n
while l <= r:
mid = (l + r) // 2
bought = attempt_buy_n_items(mid, b, prices)
if bought is not None:
max_items = mid
min_price = bought
l = mid + 1
else:
r = mid - 1
print(max_items, min_price)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
import sys
n, s = map(int, sys.stdin.readline().split())
l = list(map(int, sys.stdin.readline().split()))
t = list(map(lambda x: x[1] + n * (x[0] + 1), enumerate(l)))
upper = n - 1
lower = 0
while upper > lower:
k = (upper + lower) // 2
t = list(map(lambda x: x[1] + k * (x[0] + 1), enumerate(l)))
h = sum(sorted(t)[:k])
if h == s:
break
if h < s:
lower = k + 1
else:
upper = k - 1
t = list(map(lambda x: x[1] + upper * (x[0] + 1), enumerate(l)))
if sum(sorted(t)[:upper]) <= s:
z = list(map(lambda x: x[1] + (upper + 1) * (x[0] + 1), enumerate(l)))
if sum(sorted(z)[: upper + 1]) < s:
print(upper + 1, sum(sorted(z)[: upper + 1]))
else:
print(upper, sum(sorted(t)[:upper]))
else:
t = list(map(lambda x: x[1] + (upper - 1) * (x[0] + 1), enumerate(l)))
print(upper - 1, sum(sorted(t)[: upper - 1]))
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, m1 = map(int, input().split())
lp = list(map(int, input().split()))
l = 0
r = n
while l < r:
m = (l + r + 1) // 2
l1 = lp[:]
for i in range(n):
l1[i] = lp[i] + (i + 1) * m
l1 = sorted(l1)
s = sum(l1[:m])
if s > m1:
r = m - 1
else:
l = m
l1 = lp[:]
for i in range(n):
l1[i] = lp[i] + (i + 1) * l
l1 = sorted(l1)
s = sum(l1[:l])
print(l, s)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def check(k):
return sum(sorted([(a[i] + (i + 1) * k) for i in range(n)])[:k])
def binary(p, q):
if p >= q:
return p if check(p) <= s else p - 1
mid = (p + q) // 2
if check(mid) <= s:
return binary(mid + 1, q)
else:
return binary(p, mid - 1)
n, s = map(int, input().split())
a = [*map(int, input().split())]
ans = binary(0, n)
print(ans, check(ans))
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def getCost(a, k):
b = a.copy()
for i in range(0, len(a)):
b[i] += k * (i + 1)
b.sort()
cost = 0
for i in range(0, int(k)):
cost += b[i]
return cost
n, s = list(map(int, input().split()))
a = list(map(int, input().split()))
left = 0
right = n
num = 0
spent = 0
while left <= right:
mid = int(left + right) // 2
cost = getCost(a, mid)
if cost <= s:
left = mid + 1
num = mid
spent = cost
else:
right = mid - 1
print(num, " ", spent)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def bSearch(left, right, money, a):
if right == left:
if canBuy(left, money, a) != -1:
print(left, canBuy(left, money, a))
return left
if right - 1 == left:
if canBuy(right, money, a) != -1:
print(right, canBuy(right, money, a))
return right
if canBuy(left, money, a) != -1:
print(left, canBuy(left, money, a))
return left
mid = (right + left) // 2
if canBuy(mid, money, a) != -1:
return bSearch(mid, right, money, a)
else:
return bSearch(left, mid - 1, money, a)
return -1
def canBuy(k, m, x):
arr = x[:]
s = 0
for i in range(len(arr)):
arr[i] += k * (i + 1)
arr.sort()
s = sum(arr[:k])
if s <= m:
return s
return -1
n, money = input("").split(" ")
n, money = int(n), int(money)
prices = [int(x) for x in input("").split(" ")]
bSearch(0, n, money, prices)
|
FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR NUMBER VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def sag_nub(n, s, arr):
N = int(n)
S = int(s)
ans, total, _min, _max = 0, 0, 0, N
while _min <= _max:
b = []
K = (_min + _max) // 2
for i in range(N):
b.append(int(arr[i]) + (i + 1) * K)
b = sorted(b)
cost = 0
for i in range(K):
cost += b[i]
if cost <= S:
ans = K
total = cost
_min = K + 1
else:
_max = K - 1
print(str(ans) + " " + str(total))
N, S = str(input()).split(" ")
a = input().split(" ")
sag_nub(N, S, a)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR WHILE VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = list(map(int, input().split()))
a = list(map(int, input().split()))
l = 0
r = n + 1
while r > l + 1:
k = -(-(l + r) // 2)
cost = 0
a_new = [(a[i] + (i + 1) * k) for i in range(n)]
a_new.sort()
for i in range(k):
cost += a_new[i]
if cost <= S:
l = k
else:
r = k
a_new = [(a[i] + (i + 1) * l) for i in range(n)]
a_new.sort()
cost = 0
for i in range(l):
cost += a_new[i]
print(l, cost)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def good(k):
newp = []
for i in range(len(prices)):
newp.append(prices[i] + (i + 1) * k)
newp.sort()
mincosts[k] = sum(newp[:k])
return mincosts[k] <= s
def binsearch():
l = 0
r = n + 1
while r - l > 1:
m = (l + r) // 2
if good(m):
l = m
else:
r = m
if r < n + 1 and good(r):
return r
return l
n, s = map(int, input().split())
prices = list(map(int, input().split()))
mincosts = [(0) for i in range(n + 1)]
b = binsearch()
print(b, mincosts[b])
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def canBuy(k):
fullCost = [((i + 1) * k + cost[i]) for i in range(0, n)]
fullCost = sorted(fullCost)
fullSum = sum(fullCost[:k])
return fullSum <= money
def canBuyCost(k):
fullCost = [((i + 1) * k + cost[i]) for i in range(0, n)]
fullCost = sorted(fullCost)
fullSum = sum(fullCost[:k])
return fullSum if fullSum <= money else -1
n, money = [int(x) for x in input().split()]
cost = [int(x) for x in input().split()]
left = 0
right = n
while left < right - 1:
mid = (left + right) // 2
if canBuy(mid):
left = mid
else:
right = mid
rightRes = canBuyCost(right)
if rightRes == -1:
print(left, canBuyCost(left))
else:
print(right, rightRes)
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
import sys
n, s = list(map(int, input().split()))
price = list(map(int, input().split()))
lo = 0
hi = n
ans = 0
cst = sys.maxsize
while lo <= hi:
total = s
k = (lo + hi) // 2
newPrice = []
for i in range(n):
newPrice.append(price[i] + k * (i + 1))
newPrice.sort()
cnt = 0
cost = 0
for i in range(k):
total -= newPrice[i]
if total < 0:
break
cost += newPrice[i]
cnt += 1
if cnt == k:
lo = k + 1
if cnt > ans:
ans = cnt
cst = cost
if cnt == ans:
cst = min(cst, cost)
else:
if cnt > ans:
ans = cnt
cst = cost
if cnt == ans:
cst = min(cst, cost)
hi = k - 1
print(ans, cst)
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def cnt(arr, n, mid):
costs = [(arr[i] + (i + 1) * mid) for i in range(n)]
costs.sort()
cost = sum(costs[:mid])
return cost
def binary_search(arr, n, s):
l, r = 0, n
while l <= r:
mid = l + r >> 1
if cnt(arr, n, mid) <= s:
l = mid + 1
else:
r = mid - 1
return l - 1
def main():
n, s = map(int, input().split())
a = list(map(int, input().split()))
res = binary_search(a, n, s)
print(res, cnt(a, n, res))
main()
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
info = input().split()
a = [int(item) for item in input().split()]
n = int(info[0])
S = int(info[1])
left = 0
right = len(a) - 1
numItem = 0
minCost = 0
while left <= right:
mid = (left + right) // 2
c = [(x + (ind + 1) * (mid + 1)) for ind, x in enumerate(a)]
c.sort()
total = 0
for i in range(mid + 1):
total += c[i]
if total <= S:
numItem = mid + 1
minCost = total
left = mid + 1
else:
right = mid - 1
print(numItem, minCost)
|
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, c = map(int, input().split())
items = list(map(int, input().split()))
global oldCost
global oldm
cost = [0] * n
def calc_cost(m):
sum_to_m = 0
for i in range(n):
cost[i] = items[i] + (i + 1) * m
cost.sort()
for i in range(m):
sum_to_m += cost[i]
return sum_to_m
l = 0
r = n
m = 0
while l <= r:
m = (l + r) // 2
a = calc_cost(m)
if a <= c:
oldCost = a
oldm = m
l = m + 1
else:
r = m - 1
print(oldm, oldCost)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def newArr(a, k):
b = a
i = 0
n = len(a)
while i < n:
b[i] += (i + 1) * k
i += 1
return b
def minSum(a, k):
a.sort()
sm = 0
cnt = 0
i = 0
while i < k:
sm += a[i]
i += 1
return sm
inp = input().split()
n = int(inp[0])
S = int(inp[1])
inp = input().split()
a = []
for x in inp:
a.append(int(x))
l = 0
r = n
while l < r:
mid = (l + r) // 2
if l + 1 == r:
mid = r
b = newArr(list(a), mid)
if minSum(b, mid) <= S:
l = mid
else:
r = mid - 1
ans = l
print(str(ans) + " " + str(minSum(newArr(a, ans), ans)))
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def read():
return [int(c) for c in input().split()]
def solve(k, a):
price = [(e + i * k) for i, e in enumerate(a, 1)]
price.sort()
cost = sum(e for i, e in enumerate(price) if i < k)
return cost
def main():
n, S = read()
a = read()
lo, hi = 0, n + 1
while lo < hi - 1:
mid = (lo + hi) // 2
if solve(mid, a) <= S:
lo = mid
else:
hi = mid
print(lo, solve(lo, a))
main()
|
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def market(li, S):
start = 0
end = len(li)
ans = -1
new = [(0) for v in range(len(li))]
while start <= end:
mid = (start + end) // 2
arr = [(0) for b in range(mid)]
if ispossible(mid, S, li, new) is True:
ans = mid
start = mid + 1
else:
end = mid - 1
return ans
def ispossible(k, S, li, new):
for q in range(len(li)):
new[q] = li[q] + (q + 1) * k
new.sort()
total = 0
for i in range(0, k):
total = total + new[i]
if total <= S:
return True
else:
return False
li1 = [int(x) for x in input().split()]
S = li1[1]
li = [int(x) for x in input().split()]
a = market(li, S)
summ = 0
final = [(0) for c in range(len(li))]
for i in range(len(li)):
final[i] = li[i] + (i + 1) * a
final.sort()
for y in range(a):
summ = summ + final[y]
print(a, end=" ")
print(summ)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def total_cost(k):
cost = [(a[i] + (i + 1) * k) for i in range(len(a))]
cost.sort()
return sum(cost[:k])
n, S = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
l, h = 0, n + 1
while l < h - 1:
mid = (l + h) // 2
if total_cost(mid) <= S:
l = mid
else:
h = mid
print("{0} {1}".format(l, total_cost(l)))
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def find(k):
new = sorted([(a[i] + (i + 1) * k) for i in range(n)])
tot = sum(new[:k])
if tot <= s:
return tot
return 0
n, s = map(int, input().split())
a = list(map(int, input().split()))
low, high = 0, n
while low < high:
mid = low + (high - low + 1) // 2
if find(mid):
low = mid
else:
high = mid - 1
print(low, find(low))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def check(k):
anew = [(a[i] + (i + 1) * k) for i in range(n)]
anew.sort()
asum = sum(anew[:k])
if asum <= s:
return asum
else:
return 0
n, s = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
min_sum = 0
L = 0
R = n + 1
while R - L > 1:
m = (L + R) // 2
res = check(m)
if res:
L = m
min_sum = res
else:
R = m
print(L, min_sum)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def func(c, k, s, n):
nc = []
for i in range(n):
nc.append(c[i] + (i + 1) * (k + 1))
nc.sort()
val = sum(nc[: k + 1])
if val <= s:
return val
else:
return -1
inp = input().split()
n = int(inp[0])
s = int(inp[1])
c = []
val = input().split()
i = 1
for v in val:
c.append(int(v))
i += 1
begin = 0
end = n - 1
flag = 0
while begin <= end:
mid = (begin + end) // 2
if func(c, mid, s, n) != -1:
if mid == n - 1:
print(mid + 1, func(c, mid, s, n))
flag = 1
break
if func(c, mid + 1, s, n) == -1:
print(mid + 1, func(c, mid, s, n))
flag = 1
break
else:
begin = mid + 1
else:
end = mid - 1
if flag == 0:
print("0 0")
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
R = lambda: map(int, input().split())
n, S = R()
a = list(R())
l, r, s = 0, n, 0
while l < r:
m = (l + r + 1) // 2
t = sum(sorted(x + (i + 1) * m for i, x in enumerate(a))[:m])
if t <= S:
l = m
s = t
else:
r = m - 1
print(l, s)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = []
k = S
t = False
def Merge(array):
if len(array) == 1:
return
else:
R = array[len(array) // 2 :]
L = array[: len(array) // 2]
Merge(R)
Merge(L)
i = j = k = 0
while i < len(R) and j < len(L):
if R[i] < L[j]:
array[k] = R[i]
i += 1
k += 1
else:
array[k] = L[j]
j += 1
k += 1
while j < len(L):
array[k] = L[j]
j += 1
k += 1
while i < len(R):
array[k] = R[i]
i += 1
k += 1
def KK(lower, upper):
global new_k, b, t, n
new_k = (lower + upper) // 2
if new_k == lower or new_k == upper:
if t:
if new_k > n:
new_k = n
b = list(a)
for po in range(1, len(b) + 1):
b[po - 1] = b[po - 1] + new_k * po
Merge(b)
print(new_k, sum(b[:new_k:1]), sep=" ")
return
t = True
b = list(a)
for po in range(1, len(b) + 1):
b[po - 1] = b[po - 1] + new_k * po
Merge(b)
if sum(b[:new_k]) <= S:
KK(new_k, upper)
else:
KK(lower, new_k)
KK(0, k)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = list(map(int, input().split()))
a = list(map(int, input().split()))
st = 1
end = n
ans = -1
while st <= end:
k = (st + end) // 2
b = []
for i in range(n):
b.append(a[i] + (i + 1) * k)
b.sort()
d = sum(b[:k])
if d == s:
ans = k
break
end = k - 1
elif d > s:
end = k - 1
else:
st = k + 1
if ans == -1:
if sum(b[:k]) > s:
b = []
for i in range(n):
b.append(a[i] + (i + 1) * (k - 1))
b.sort()
print(k - 1, sum(b[: k - 1]))
else:
print(k, sum(b[:k]))
else:
print(ans, d)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def Solution(arr, N, S):
l = 0
r = N
ans = [0, 0]
while l <= r:
mid = (l + r) // 2
res = isValid(arr, S, mid)
if res[0]:
l = mid + 1
ans = mid, res[1]
else:
r = mid - 1
return ans
def isValid(arr, S, k):
tempArr = []
for j in range(len(arr)):
tempArr.append(arr[j] + (j + 1) * k)
tempArr = sorted(tempArr)
tempSum = 0
for i in range(k):
tempSum += tempArr[i]
if tempSum > S:
return [False, 0]
return [True, tempSum]
N, S = map(int, input().split())
arr = list(map(int, input().split()))
res = Solution(arr, N, S)
print(res[0], res[1])
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN LIST NUMBER NUMBER RETURN LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def budget(o, data):
coast = 0
arr = []
for i in range(len(data)):
arr.append((i + 1) * o + data[i])
arr = sorted(arr)
for j in range(o):
coast += arr[j]
return coast
def getTotalItems(mid, data, money):
total = 0
for i in range(mid):
total += (i + 1) * mid + data[i]
if total <= money:
return True
else:
for j in range(mid, len(data)):
total -= (j - mid + 1) * mid + data[j - mid]
total += (j + 1) * mid + data[j]
if total <= money:
return True
return False
def bsItems(data, mon):
r = len(data)
l = 0
money = mon
lCoast = 0
while r - l > 0:
m = (l + r + 1) // 2
if budget(m, data) > money:
r = m - 1
else:
l = m
minmumCoast = budget(l, data)
return l, minmumCoast
def getMinTotalCoast(data, mi, money, items2):
total2 = mi
n2 = len(data)
print(f"m is {mi}")
for i in range(items2):
total2 -= (i + 1) * items2 + data[i]
if total2 >= 0:
return True
else:
print(total2)
for j in range(items2, n2):
indx = j - items2
total2 += (indx + 1) * items2 + data[indx]
print(total2)
total2 -= (j + 1) * items2 + data[j]
print(total2)
if total2 >= 0:
return True
return False
def bsTotalCoast(data, money, items2):
r = money
l = 0
while r - l > 0:
min_coast = (l + r) // 2
if getMinTotalCoast(data, min_coast, money, items2):
r = min_coast
else:
l = min_coast + 1
return r
n, s = map(int, input().split(" "))
items = list(map(int, input().split(" ")))
k, t = bsItems(items, s)
print(k, t, end=" ")
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
import sys
input = sys.stdin.readline
def check(k, money):
aux = []
for i in range(n):
aux.append(l[i] + (i + 1) * k)
aux.sort()
ans = [0, 0]
if ans[0] == k:
return ans
for e in aux:
if money - e < 0:
break
ans[0] += 1
ans[1] += e
if ans[0] == k:
break
money -= e
if ans[0] < k:
ans[0] = -1
return ans
for _ in range(1):
n, s = map(int, input().split())
l = list(map(int, input().split()))
low = 0
high = n
g = [0, 0]
while low <= high:
mid = low + (high - low) // 2
ans = check(mid, s)
if ans[0] == mid:
g = ans
low = mid + 1
else:
high = mid - 1
print(*g)
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER VAR RETURN VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = map(int, input().split())
l = list(map(int, input().split()))
l2, r = 0, n
v = 0
while l2 != r:
l1 = []
m = (l2 + r + 1) // 2
for i in range(n):
l1.append(l[i] + (i + 1) * m)
l1 = sorted(l1)
for i in range(1, n):
l1[i] += l1[i - 1]
if l1[m - 1] > S:
r = m - 1
else:
l2 = m
v = l1[m - 1]
print(l2, v)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def check(x, A, s):
n = len(A)
B = [(0) for i in range(n)]
for i in range(n):
B[i] = A[i] + (i + 1) * x
B.sort()
c = 0
for i in range(x):
c += B[i]
return c
n, s = map(int, input().split())
A = list(map(int, input().split()))
l = 0
r = n + 1
ans = 0
while l < r - 1:
mid = (l + r) // 2
if check(mid, A, s) <= s:
l = mid
ans = mid
else:
r = mid
print(ans, check(ans, A, s))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = map(int, input().strip().split())
t = input()
a = list(map(int, t.strip().split()))
def check(x):
b = []
for i in range(n):
temp = (i + 1) * x + a[i]
b.append(temp)
b.sort()
sum = 0
x = int(x)
for i in range(x):
sum += b[i]
if sum <= s:
return 1, sum
else:
return 0, sum
L = 1
R = n
ans = 0
sum = 0
ans1 = 0
while L <= R:
mid = int((L + R) / 2)
temp, sum = check(mid)
if temp == 0:
R = mid - 1
else:
L = mid + 1
ans = mid
ans1 = sum
print(ans, end=" ")
print(ans1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER VAR RETURN NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = map(int, input().split())
a = list(map(int, input().split()))
def check(m):
ac = sorted([(a[i - 1] + i * m) for i in range(1, n + 1)])
w = sum(ac[:m])
if w <= s:
return True, w
else:
return False, 0
l, r = 0, len(a) + 1
last = -1
lastw = 0
while l < r:
m = l + (r - l) // 2
st, w = check(m)
if st:
last = m
lastw = w
if l == m:
break
l = m
else:
r = m
print(last, lastw)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER VAR RETURN NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = map(int, input().split())
a = list(map(int, input().split()))
def cout(x):
b = list(a)
for i, y in enumerate(b):
b[i] = (i + 1) * x + y
b.sort()
return sum(b[:x])
i, j = 0, n + 1
while i + 1 < j:
mid = (i + j) // 2
if cout(mid) <= s:
i = mid
else:
j = mid
result = cout(i)
if result > s:
print(0, 0)
else:
print(i, result)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def check(k, a, n):
res = 0
c = []
for i in range(0, n):
c.append(a[i] + (i + 1) * k)
c.sort()
for i in range(0, k):
res += c[i]
return res
n, s = map(int, input().split())
a = list(map(int, input().split()))
l = 1
r = n
tot = 0
ans = 0
while l <= r:
mid = int((l + r) / 2)
x = check(mid, a, n)
if x <= s:
tot = mid
ans = x
l = mid + 1
else:
r = mid - 1
print(tot, ans)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def check(li, mid, c):
s = []
for i in range(len(li)):
s.append(li[i] + mid * (i + 1))
s.sort()
x = 0
for i in range(mid):
x += s[i]
if x > c:
return False, x
return True, x
def main(li, c):
start = 0
end = len(li)
ans = -1
sol = -1
while start <= end:
mid = (start + end) // 2
z, y = check(li, mid, c)
if z == True:
ans = mid
sol = y
start = mid + 1
else:
end = mid - 1
print(ans, sol)
n, c = [int(x) for x in input().split()]
li = [int(x) for x in input().split()]
main(li, c)
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER VAR RETURN NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def check(A, S, k):
prices = []
for i, a in enumerate(A):
prices.append(a + (i + 1) * k)
prices.sort()
total = sum(prices[:k])
return total <= S, total
n, S = map(int, input().split())
A = [int(a) for a in input().split()]
k = -1
p = 0
skip = n
while skip >= 1:
while k + skip <= n and check(A, S, k + skip)[0]:
p = check(A, S, k + skip)[1]
k += skip
skip //= 2
print(k, p)
|
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = [int(i) for i in input().split()]
ll = [int(i) for i in input().split()]
curr = 0
l = 0
h = n
while l <= h:
mid = (h + l) // 2
k = ll[:]
for i in range(n):
k[i] = (i + 1) * mid + ll[i]
k.sort()
sm = sum(k[:mid])
if sm <= s:
curr = mid
l = mid + 1
ans = sm
else:
h = mid - 1
print(curr, ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def cost_total(num_souvenir, budget, costs_base, k):
costs_total = [(costs_base[i] + (k + 1) * (i + 1)) for i in range(num_souvenir)]
costs_total.sort()
suma = 0
for i in range(k + 1):
suma = costs_total[i] + suma
return suma
def busqueda_binaria(num_souvenir, budget, costs_base):
minimo = 0
maximo = num_souvenir - 1
ultimo_valido = -1
ultima_suma = -1
flag = False
while minimo <= maximo:
medio = int((minimo + maximo) / 2)
suma = cost_total(num_souvenir, budget, costs_base, medio)
if suma == budget:
flag = True
print(str(medio + 1) + " " + str(suma))
break
elif suma < budget:
ultima_suma = suma
ultimo_valido = medio
minimo = medio + 1
else:
maximo = medio - 1
if flag == False:
if ultima_suma == -1:
print("0 0")
else:
print(str(ultimo_valido + 1) + " " + str(ultima_suma))
def main():
num_souvenir, budget = map(int, input().split(" "))
costs_base = list(map(int, input().split(" ")))
busqueda_binaria(num_souvenir, budget, costs_base)
main()
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def check(num):
cost = []
for i in range(n):
cost.append(a[i] + (i + 1) * num)
cost.sort()
if sum(cost[0:num]) <= s:
return True, sum(cost[0:num])
return False, -1
n, s = map(int, input().split())
a = list(map(int, input().split()))
low = 0
high = n
while low < high:
mid = (low + high) // 2
if check(mid)[0]:
low = mid + 1
else:
high = mid - 1
if check(low)[0]:
print(low, check(low)[1])
else:
print(low - 1, check(low - 1)[1])
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = map(int, input().split())
arr = list(map(int, input().split()))
l = 0
r = n
mxnumber = 0
mincost = 0
while l <= r:
mid = l + (r - l) // 2
b = []
value = 0
for i in range(n):
b.append(arr[i] + (i + 1) * mid)
b.sort()
for i in range(mid):
value += b[i]
if value <= s:
mxnumber = mid
mincost = value
if s <= value:
r = mid - 1
else:
l = mid + 1
print(mxnumber, mincost)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
from sys import stdin as fin
n, S = map(int, fin.readline().split())
arr = list(map(int, fin.readline().split()))
def test(k):
nonlocal arr, n, S
costs = sorted([(arr[i] + (i + 1) * k) for i in range(n)])
csum = sum(costs[:k])
if csum <= S:
return csum
else:
return 0
l, r, m = 0, n + 1, 0
ans, ansm = 0, 0
while l + 1 != r:
m = (l + r) // 2
csum = test(m)
if not csum:
r = m
else:
ans = csum
ansm = m
l = m
print(ansm if ans else 0, ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = map(int, input().split())
a = [int(x) for x in input().split()]
def calc(k):
prices = [(a[i] + (i + 1) * k) for i, x in enumerate(a)]
prices.sort()
return sum(prices[:k])
l, r = 0, n
while l != r:
m = (l + r + 1) // 2
if calc(m) <= S:
l = m
else:
r = m - 1
print(l, calc(l))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = [int(i) for i in input().split(" ")]
a = [int(i) for i in input().split(" ")]
l, r = 1, n
ans_k, ans_cost = 0, 0
while l <= r:
k = int((l + r) / 2)
cost = [(a[i] + (i + 1) * k) for i in range(n)]
cost = sorted(cost)
cost_sum = sum(cost[:k])
if cost_sum <= S:
ans_k = k
ans_cost = cost_sum
l = k + 1
else:
r = k - 1
print("{} {}".format(ans_k, ans_cost))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
import sys
def main():
n, s = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
l = 0
r = n
bestk = 0
bests = 0
while l < r:
mid = (l + r) // 2
x = [(a[i] + (i + 1) * mid) for i in range(n)]
x.sort()
t = 0
for i in range(mid):
t += x[i]
if t <= s:
bests = t
bestk = mid
l = mid + 1
else:
r = mid - 1
mid = l
x = [(a[i] + (i + 1) * mid) for i in range(n)]
x.sort()
t = 0
for i in range(mid):
t += x[i]
if t <= s:
bestk = mid
bests = t
print(bestk, bests)
main()
|
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, mm = list(map(int, input().split()))
ar = list(map(int, input().split()))
pr = list(ar)
def check(curr):
for i in range(0, n):
pr[i] = ar[i] + curr * (i + 1)
pr.sort()
return sum(pr[0:curr])
def BS(l, r):
if r == l + 1:
if check(r) <= mm:
return r
return l
m = l + r
m = m // 2
if check(m) <= mm:
return BS(m, r)
return BS(l, m)
ans = BS(0, n)
print(ans, end=" ")
print(check(ans))
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
import sys
n, S = tuple(map(int, sys.stdin.readline().split(" ")))
baseCosts = list(map(int, sys.stdin.readline().split(" ")))
def costTaking(k: int) -> int:
costs = list(map(lambda tup: tup[1] + (tup[0] + 1) * k, enumerate(baseCosts)))
sortedCosts = sorted(costs)
return sum(sortedCosts[0:k])
left = 0
right = n + 1
while left < right:
mid = (left + right) // 2
if costTaking(mid) > S:
right = mid
else:
left = mid + 1
print(right - 1, costTaking(right - 1))
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = [int(i) for i in input().split()]
array = [int(i) for i in input().split()]
sum = 0
def chkProcess(k):
map1 = []
sumx = 0
for i in range(1, n + 1):
map1.append(array[i - 1] + i * k)
map1 = sorted(map1)
for i in range(k):
sumx += map1[i]
return sumx
upper, lower = 0, n + 1
while upper < lower - 1:
mid = (upper + lower) // 2
sumx = chkProcess(mid)
if sumx <= s:
upper = mid
else:
lower = mid
print(upper, chkProcess(upper))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, S = map(int, input().split())
a = list(map(int, input().split()))
start = 0
end = n
T = 0
Tc = 0
Kc = 0
found = False
while start <= end and not found:
k = (start + end) // 2
tc = a[:]
for i in range(n):
tc[i] = tc[i] + (i + 1) * k
tc.sort()
T = 0
for i in range(k):
T += tc[i]
if T > S:
end = k - 1
elif T < S:
start = k + 1
Tc = T
Kc = k
else:
found = True
Tc = T
Kc = k
print(Kc, Tc)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
cost = 0
n, s = map(int, input().split())
a = list(map(int, input().split()))
def funcMonotonic(a, s, k):
global cost
costs = [(a[i] + (i + 1) * k) for i in range(n)]
costs.sort()
cost = sum(costs[:k])
return cost <= s
left, right = 0, n + 1
while left + 1 < right:
middle = (left + right) // 2
if funcMonotonic(a, s, middle):
left = middle
else:
right = middle
funcMonotonic(a, s, left)
print(left, cost)
|
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
def func(m):
global a
a1 = [(a[i] + (1 + i) * m) for i in range(len(a))]
a1.sort()
s = 0
for i in range(m):
s += a1[i]
return s
l = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
low = 0
high = l[0]
mid = (low + high) // 2
while low <= high:
mid = (low + high) // 2
x = func(mid)
if x > l[1]:
high = mid - 1
elif x < l[1]:
low = mid + 1
else:
low = mid
break
if func(mid) > l[1]:
mid -= 1
print(mid, func(mid))
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.
Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?
-----Input-----
The first line contains two integers n and S (1 ≤ n ≤ 10^5 and 1 ≤ S ≤ 10^9) — the number of souvenirs in the market and Sagheer's budget.
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the base costs of the souvenirs.
-----Output-----
On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.
-----Examples-----
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
-----Note-----
In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.
In the second example, he can buy all items as they will cost him [5, 10, 17, 22].
In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
|
n, s = map(int, input().split())
arr = list(map(int, input().split()))
low, high = 0, n + 1
ans = [0, 0]
b = [0] * n
def canWeBuyTillMid(arr, k):
ans = 0
for i in range(n):
b[i] = arr[i] + (i + 1) * k
b.sort()
for i in range(k):
ans += b[i]
return ans
while low < high - 1:
mid = (low + high) // 2
sumTillMid = canWeBuyTillMid(arr, mid)
if sumTillMid <= s:
ans = [mid, sumTillMid]
low = mid
else:
high = mid
print(ans[0], ans[1])
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.