description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
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, r = 0, n
ac = 0
while l < r:
k = l + r + 1 >> 1
at = [(x + (i + 1) * k) for i, x in enumerate(arr)]
at.sort()
cost = sum(at[:k])
if cost <= S:
l = k
ac = cost
else:
r = k - 1
print(l, ac)
|
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 BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN 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.
|
def check(n, s, f):
a = []
for i in range(len(l)):
a.append(l[i] + n * (i + 1))
a.sort()
return sum(a[0:n]) <= s if f == 1 else sum(a[0:n])
n, s = map(int, input().split())
l = list(map(int, input().split()))
i = 0
while i <= n:
mid = (i + n) // 2
if check(mid, s, 1):
if i == mid:
break
i = mid
else:
n = mid - 1
if check(n, s, 1):
print(n, check(n, s, 0))
else:
print(i, check(i, s, 0))
|
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 RETURN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR 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 NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR 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 getcost(m):
summ = 0
global n
cost = [(0) for _ in range(n)]
for i in range(n):
cost[i] = m * (i + 1) + a[i]
cost.sort()
for i in range(m):
summ += cost[i]
return summ
n, s = map(int, input().split())
a = list(map(int, input().split()))
cost = [0] * n
summ = 0
for i in range(n):
cost[i] = n * (i + 1) + a[i]
summ += cost[i]
if summ <= s:
print(n, summ)
exit(0)
u = n
l = 0
m = 0
c = 0
cc = 0
mm = 0
while l <= u:
m = (l + u) // 2
c = getcost(m)
if c <= s:
cc = c
mm = m
if l == u:
if c > s:
c = cc
m = mm
break
if c < s:
l = m + 1
else:
u = m - 1
print(mm, cc)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER 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 IF 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.
|
def proverka(k, a, s):
b = []
for i in a:
b.append(i)
for i in range(len(b)):
b[i] = b[i] + k * (i + 1)
b.sort()
cnt = 0
index = 0
while cnt < k and s - b[index] >= 0:
cnt += 1
s -= b[index]
index += 1
if cnt < k:
return False
else:
return True
n, s = list(map(int, input().split()))
a = list(map(int, input().split()))
l = 0
r = n + 1
while l + 1 < r:
if proverka((l + r) // 2, a, s):
l = (l + r) // 2
else:
r = (l + r) // 2
for i in range(n):
a[i] = a[i] + l * (i + 1)
a.sort()
qwer = 0
index = 0
while index < l and qwer + a[index] <= s:
qwer += a[index]
index += 1
print(l, qwer)
|
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER 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 BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR 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.
|
def get(k, li):
a = [0] * len(li)
for i in range(len(li)):
a[i] = li[i] + (i + 1) * k
a.sort()
summ = 0
for i in range(k):
summ += a[i]
return summ
def b_search(n, li, k):
l = 0
h = n + 1
while h - l > 1:
m = (l + h) // 2
if get(m, li) <= k:
l = m
else:
h = m
print(l, get(l, li))
n, m = map(int, input().split())
li = list(map(int, input().split()))
b_search(n, li, m)
|
FUNC_DEF 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 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 VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR 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 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()))
nums = list(map(int, input().split()))
def findcost(k):
prices = [(nums[i] + k * (i + 1)) for i in range(n)]
prices.sort()
return sum(prices[:k])
low, high = 0, n
ans, cos = 0, 0
while low <= high:
mid = (low + high) // 2
cost = findcost(mid)
if cost > s:
high = mid - 1
else:
low = mid + 1
ans = mid
cos = cost
print(ans, cos)
|
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 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 NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER 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.
|
import sys
from itertools import accumulate
def solve():
def check(m):
if m == 0:
return 0
b = [(ai + (i + 1) * m) for i, ai in enumerate(a)]
b.sort()
return sum(b[:m])
n, S = map(int, input().split())
a = [int(i) for i in input().split()]
btm = -1
top = n + 1
while top - btm > 1:
mid = (top + btm) // 2
if check(mid) <= S:
btm = mid
else:
top = mid
k = btm
T = check(k)
print(k, T)
solve()
|
IMPORT FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP 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 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 BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL 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.
|
import sys
input = sys.stdin.readline
def judge(x):
b = [(a[i] + (i + 1) * x) for i in range(n)]
b.sort()
s = sum(b[:x])
return s <= S
def binary_search():
l, r = 0, n
while l <= r:
mid = (l + r) // 2
if judge(mid):
l = mid + 1
else:
r = mid - 1
return r
n, S = map(int, input().split())
a = list(map(int, input().split()))
k = binary_search()
b = [(a[i] + (i + 1) * k) for i in range(n)]
b.sort()
T = sum(b[:k])
print(k, T)
|
IMPORT ASSIGN VAR 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 FUNC_DEF ASSIGN VAR VAR NUMBER 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 BIN_OP VAR NUMBER 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 FUNC_CALL 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 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.
|
n, s = map(int, input().split())
A = list(map(int, input().split()))
l = 0
r = n + 1
while r - l > 1:
m = l + (r - l) // 2
B = A.copy()
for j in range(n):
B[j] = A[j] + (j + 1) * m
B.sort()
if sum(B[:m]) > s:
r = m
else:
l = m
B = A.copy()
for j in range(n):
B[j] = A[j] + (j + 1) * l
B.sort()
print(l, sum(B[: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 BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL 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 IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL 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 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 f(k):
cur = sum(sorted([(a[i] + (i + 1) * k) for i in range(n)])[:k])
return cur, cur <= S
read = lambda: map(int, input().split())
n, S = read()
a = list(read())
l, r = 0, n + 1
while r - l > 1:
m = l + r >> 1
if f(m)[1]:
l = m
else:
r = m
print(l, f(l)[0])
|
FUNC_DEF 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 RETURN VAR VAR VAR 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 NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR 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.
|
from sys import stderr, stdin
def readInts():
return map(int, stdin.readline().strip().split())
def print_err(*args, **kwargs):
print(*args, file=stderr, **kwargs)
def best_souvenirs(n, s, ns):
def attemptf(k):
ns2 = []
for i, v in enumerate(ns):
ns2.append(ns[i] + (i + 1) * k)
ns2.sort()
buy = sum(ns2[:k])
if buy > s:
return None
return buy
def binary_search(lo, hi, f):
best_v = 0
best_mid = 0
while lo + 1 < hi:
mid = (lo + hi) // 2
v = f(mid)
if v:
lo = mid
best_v = v
best_mid = mid
else:
hi = mid
return best_mid, best_v
return binary_search(0, len(ns) + 1, attemptf)
def run():
n, s = readInts()
ns = list(readInts())
print(" ".join(map(str, best_souvenirs(n, s, ns))))
run()
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR 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 RETURN NONE RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR 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 RETURN VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING 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.
|
def calc(cnt):
return sum(sorted(num + cnt * ind for ind, num in enumerate(price, 1))[:cnt])
n, money = map(int, input().split())
price = [int(num) for num in input().split()]
can = 0
cant = n + 1
while cant - can > 1:
mid = (can + cant) // 2
if calc(mid) <= money:
can = mid
else:
cant = mid
print(can, calc(can))
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER 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 BIN_OP VAR NUMBER WHILE BIN_OP VAR 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
|
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 ispossible(a, k):
l = [0] * len(a)
for i in range(len(a)):
l[i] = a[i] + (i + 1) * k
l.sort()
return sum(l[:k])
n, S = map(int, input().split())
a = list(map(int, input().split()))
nn = [int(i) for i in range(1, n + 1)]
l, h = 0, n + 1
while h - l > 1:
m = (l + h) // 2
if ispossible(a, m) <= S:
l = m
else:
h = m
print(l, ispossible(a, l))
|
FUNC_DEF 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 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 FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR 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
|
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())
a = [int(i) for i in input().split()]
l, r = 0, n
while r - l > 1:
mid = (l + r) // 2
subarr = [(0) for _ in range(n)]
for i in range(n):
subarr[i] = a[i] + (i + 1) * mid
subarr.sort()
pref = 0
for i in range(mid):
pref += subarr[i]
if pref <= s:
l = mid
else:
r = mid
ans = -1
subarr = [(0) for _ in range(n)]
for i in range(n):
subarr[i] = a[i] + (i + 1) * r
subarr.sort()
pref = 0
for i in range(r):
pref += subarr[i]
if pref <= s:
ans = r
else:
ans = l
subarr = [(0) for _ in range(n)]
for i in range(n):
subarr[i] = a[i] + (i + 1) * l
subarr.sort()
pref = 0
for i in range(l):
pref += subarr[i]
print(ans, pref)
main()
|
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 VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER 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 IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER 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 IF VAR VAR ASSIGN VAR VAR ASSIGN 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 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.
|
from sys import stdin
def cost_of(data, k):
kl = []
for index, item in enumerate(data):
kl.append(item + (index + 1) * k)
kl.sort()
accu = 0
for i in range(0, k):
accu += kl[i]
return accu
n, s = map(int, stdin.readline().rstrip().split())
data = list(map(int, stdin.readline().rstrip().split()))
left, right = 0, n
cleft, cright = 0, cost_of(data, n)
if s >= cright:
print("{0} {1}".format(n, cright))
exit()
last_center = -1
center = -1
while left <= right:
last_center = center
center = (left + right) // 2
ccenter = cost_of(data, center)
if center == last_center:
rs = right if s >= cright else left
left = rs
break
if s < ccenter:
right = center - 1
cright = cost_of(data, right)
else:
left = center
cleft = cost_of(data, left)
print(left, cost_of(data, left))
|
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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR 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 ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR 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.
|
vals = input().split()
n = int(vals[0])
S = int(vals[1])
a = [int(v) for v in input().split()]
answer1 = 0
answer2 = 0
left = 0
right = n - 1
while left <= right:
mid = (left + right) // 2
c = [(x + (index + 1) * (mid + 1)) for index, x in enumerate(a)]
c.sort()
s = sum(c[: mid + 1])
if s == S:
answer1 = mid + 1
answer2 = s
break
elif s > S:
right = mid - 1
else:
answer1 = mid + 1
answer2 = s
left = mid + 1
print(answer1, answer2)
|
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 VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP 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 FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER 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 check(x, a, u):
s = 0
a.sort(key=lambda o: o[0] + o[1] * x)
for i in range(0, x):
s += a[i][0] + a[i][1] * x
if s > u:
return [False]
return [True, s]
def binary_search(a, n):
l = 0
r = len(a)
while r - l > 1:
m = (r + l) // 2
if a[m] > n:
r = m
else:
l = m
return a[l]
n, s = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(0, len(a)):
b.append([a[i], i + 1])
a = b
l = -1
r = n + 1
max1 = 0
while r - l > 1:
m = (r + l) // 2
if check(m, a, s)[0] == True:
l = m
max1 = check(m, a, s)[1]
else:
r = m
print(l, max1)
|
FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN LIST NUMBER RETURN LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN 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 LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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 NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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.
|
n, S = map(int, input().split())
st = input().split(" ")
cost = [int(num) for num in st]
l, r, Smin, k = 0, n + 1, 0, 0
for _ in range(20):
k = int((l + r) / 2)
a = list()
for i in range(n):
a.append(cost[i] + k * (i + 1))
a.sort()
s = 0
for i in range(k):
s += a[i]
if s <= S:
l = k
Smin = s
else:
r = k
print(k, int(Smin))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN 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.
|
def binarySearch(k, x, ls):
l = 0
r = k
while l <= r:
mid = (l + r) // 2
b = price(mid, ls)
if b == x:
return mid
elif b < x:
l = mid + 1
else:
r = mid - 1
return l - 1
def price(k, ls):
ar = []
for i in range(len(ls)):
p = ls[i] + (i + 1) * k
ar.append(p)
ar.sort()
return sum(ar[:k])
k, n = map(int, input().split())
ls = list(map(int, input().split()))
mx = 0
cnt = 0
s = 0
t = binarySearch(k, n, ls)
print(f"{t} {price(t, ls)}")
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING 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 calc(k):
m = [(a[i] + k * (i + 1)) for i in range(n)]
m = sorted(m)
return sum(m[:k]) <= S, sum(m[:k])
l = 0
r = n
while l < r:
mid = (l + r + 1) // 2
f, s = calc(mid)
if f:
l = mid
else:
r = mid - 1
f, s = calc(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 FUNC_DEF ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR 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 FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN 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.
|
def f(arr, k):
suma = 0
arr1 = []
for i in range(0, len(arr)):
arr1.append(arr[i] + k * (i + 1))
arr1.sort()
if k == 1:
return arr1[0] + 1
for i in range(0, k):
try:
suma = suma + arr1[i]
except:
adfasdf = 0
return suma
n, S = map(int, input().split())
A = [int(i) for i in input().split()]
low = 0
high = n
while low < high:
mid = (low + high) // 2
cost = f(A, mid)
if cost < S:
low = mid + 1
elif cost >= S:
high = mid
ans = 0
if f(A, high + 1) <= S:
ans = high + 1
elif f(A, high) <= S:
ans = high
elif f(A, low + 1) <= S:
ans = low + 1
elif f(A, low) <= S:
ans = low
else:
ans = low - 1
if ans > n:
ans = n
suma = 0
arr1 = []
for i in range(0, len(A)):
arr1.append(A[i] + ans * (i + 1))
arr1.sort()
for i in range(0, ans):
suma = suma + arr1[i]
print(ans, suma)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN 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 VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP 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(arr, k):
arr1 = arr[:]
for i in range(n):
arr1[i] = arr1[i] + (i + 1) * k
arr1 = sorted(arr1)
return sum(arr1[:k])
n, s = map(int, input().split())
arr = list(map(int, input().split()))
l = 1
r = n
ans1, ans2 = 0, int(2000000000.0)
while l <= r:
mid = (l + r) // 2
cost = check(arr, mid)
if cost <= s and mid > ans1:
ans1, ans2 = mid, cost
l = mid + 1
else:
r = mid - 1
print(ans1, end=" ")
if ans2 == int(2000000000.0):
print(0)
else:
print(ans2)
|
FUNC_DEF 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 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 VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL 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.
|
def can_buy(num, n, budget, base_costs):
costs = [(base_costs[i] + (i + 1) * num) for i in range(n)]
costs.sort()
temp = sum(costs[:num])
if temp <= budget:
return True, temp
else:
return False, 0
def solve(n, budget, base_costs):
start = 0
end = n
k = 0
cost = 0
while start <= end:
mid = start + (end - start) // 2
check, total = can_buy(mid, n, budget, base_costs)
if check:
start = mid + 1
k = mid
cost = total
else:
end = mid - 1
return k, cost
n, s = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
k, total = solve(n, s, a)
print(k, total)
|
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 NUMBER VAR RETURN NUMBER NUMBER FUNC_DEF 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 VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR 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 ASSIGN VAR VAR 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, res = 0, n, 0
while l <= r:
m = (l + r) // 2
f = sorted([(a[i] + (i + 1) * m) for i in range(n)])
g = sum(f[:m])
if g <= s:
l, res = m + 1, g
else:
r = m - 1
print(l - 1, res)
|
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 NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR 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 VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP 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.
|
from itertools import count
n, m = list(map(int, input().split()))
p = [int(num) for num in input().split()]
def calc(cnt):
new_p = list(map(lambda ind, num: num + cnt * ind, count(1), p))
return sum(sorted(new_p)[:cnt])
def can(cnt):
return calc(cnt) <= m
l = 0
r = n + 1
while r - l > 1:
mid = (l + r) // 2
if can(mid):
l = mid
else:
r = mid
print(l, calc(l))
|
ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR 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 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.
|
for _ in range(1):
n, budget = map(int, input().split())
costs = list(map(int, input().split()))
start = 0
end = n + 1
while start < end - 1:
mid = start + (end - start) // 2
binary = [(0) for _ in range(n)]
for i in range(n):
binary[i] = costs[i] + (i + 1) * mid
binary.sort()
total = 0
for i in range(mid):
total += binary[i]
if total <= budget:
start = mid
else:
end = mid
binary = [(0) for _ in range(n)]
for i in range(n):
binary[i] = costs[i] + (i + 1) * start
binary.sort()
total = 0
for i in range(start):
total += binary[i]
print(start, total)
|
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 BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER 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 IF VAR VAR ASSIGN VAR VAR ASSIGN 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 EXPR FUNC_CALL VAR VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
cnt1, cnt2, x, y = map(int, input().split())
upper_limit = (cnt1 + cnt2) * (x * y) // (x * y - x - y + 1)
def bin_search(low, high):
if low >= high - 1:
return high
mid = (low + high) // 2
best_way = max(0, cnt1 - mid // y + mid // (x * y)) + max(
0, cnt2 - mid // x + mid // (x * y)
)
leftovers = mid - mid // x - mid // y + mid // (x * y)
if best_way <= leftovers:
return bin_search(low, mid)
return bin_search(mid, high)
print(bin_search(1, upper_limit + 10))
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
def spliter():
d = input()
a = d.split()
r = []
for i in a:
k = int(i)
r.append(k)
return r
def counter(a, b):
i = a // b
j = i
while i > 0:
a += i
i = a // b - j
j += i
return a
lst = spliter()
l1 = lst[0]
l2 = lst[1]
l3 = lst[2]
l4 = lst[3]
count1 = counter(l1, l3)
count2 = counter(l2, l4)
count3 = counter(l1 + l2, l3 * l4)
count = max(count1, count2, count3)
print(count)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
cnt1, cnt2, x, y = list(map(int, input().split()))
def present(v, x, y, cnt1, cnt2):
div_x = v // x
div_y = v // y
div_xy = v // (x * y)
div_x -= div_xy
div_y -= div_xy
left = v - (div_x + div_y + div_xy)
cnt1 -= div_y
cnt2 -= div_x
cnt1 = cnt1 if cnt1 > 0 else 0
cnt2 = cnt2 if cnt2 > 0 else 0
return left >= cnt1 + cnt2
l = 0
r = 1e18
while r - l > 1:
mid = l + (r - l) // 2
if present(mid, x, y, cnt1, cnt2):
r = mid
else:
l = mid
print(int(r))
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
cnt1, cnt2, x, y = list(map(int, input().split(" ")))
lo = 1
hi = 10**10
while lo < hi:
xx = cnt1
yy = cnt2
mid = (lo + hi) // 2
intx = mid // x
inty = mid // y
intxy = mid // (x * y)
notxy = mid - intx - inty + intxy
notx = mid - intx - notxy
noty = mid - inty - notxy
xx -= notx
yy -= noty
if max(xx, 0) + max(yy, 0) > notxy:
lo = mid + 1
else:
hi = mid
print(lo)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
a, b, c, d = map(int, input().split())
l = 1
r = 10**10
while r - l > 1:
m = (r + l) // 2
both = m // (c * d)
f = [m // c, m // d]
other = m - f[0] - f[1] + both
f[0] -= both
f[1] -= both
if max(0, b - f[0]) + max(0, a - f[1]) <= other:
r = m
else:
l = m
print(r)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
cnt_1, cnt_2, x, y = map(int, input().split())
l = 0
r = 10**18
lcm = x * y
while r > l + 1:
mid = (l + r) // 2
if mid - mid // x < cnt_1 or min(mid - cnt_1 - mid // lcm, mid - mid // y) < cnt_2:
l = mid
else:
r = mid
print(r)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
def check(v):
a = v // y - v // (x * y)
b = v // x - v // (x * y)
c = v - (a + b + v // (x * y))
fa = c + a
fb = min(c, fa - cnt1) + b
return fa >= cnt1 and fb >= cnt2
cnt1, cnt2, x, y = list(map(int, input().split()))
l, r = 0, (cnt1 + cnt2) * 2
while l <= r:
mid = (l + r) // 2
if check(mid):
ans = mid
r = mid - 1
else:
l = mid + 1
print(ans)
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR 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 EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
c1, c2, x, y = map(int, input().split())
l, r = 0, 1 << 179
while r - l > 1:
m = l + r >> 1
if m - m // x >= c1 and m - m // y - max(0, c1 - m // y + m // x // y) >= c2:
r = m
else:
l = m
print(r)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
cnt1, cnt2, x, y = map(int, input().split())
do = cnt1 + cnt2
up = 10**12
def checker(mid):
if (
mid - mid // x >= cnt1
and mid - mid // y - max(cnt1 - mid // y + mid // (y * x), 0) >= cnt2
):
return True
elif (
mid - mid // y >= cnt2
and mid - mid // x - max(cnt2 - mid // x + mid // (y * x), 0) >= cnt1
):
return True
return False
while up >= do:
mid = (up + do) // 2
if checker(mid):
up = mid - 1
an = mid
else:
do = mid + 1
print(an)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER 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 EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
R = lambda: map(int, input().split())
c1, c2, x, y = R()
l, r = c1 + c2, 2 * 10**9 + 7
while l < r:
m = (l + r) // 2
r1 = max(0, c1 - m // y + m // (x * y))
r2 = max(0, c2 - m // x + m // (x * y))
if m - m // x - m // y + m // (x * y) < r1 + r2:
l = m + 1
else:
r = m
print(l)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
def valid(n1, n2, p1, p2, mid):
x = mid - mid // p1
y = mid - mid // p2
rem = mid - mid // (p1 * p2)
return x < n1 or y < n2 or rem < n1 + n2
def binary_search(n1, n2, p1, p2):
l, r = 0, 1 << 61
while l <= r:
mid = l + r >> 1
if valid(n1, n2, p1, p2, mid):
l = mid + 1
else:
r = mid - 1
return l
def main():
n1, n2, p1, p2 = map(int, input().split())
print(binary_search(n1, n2, p1, p2))
main()
|
FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
from sys import stdin
lines = list([_f for _f in stdin.read().split("\n") if _f])
def parseline(line):
return list(map(int, line.split()))
lines = list(map(parseline, lines))
def search(f, v, l, r):
while l + 1 < r:
p = l + (r - l) // 2
if f(p) <= v:
l = p
else:
r = p
return l
c1, c2, x, y = lines[0]
def check(v, debug=False):
m1 = max(c1 - v // y + v // (x * y), 0)
m2 = max(c2 - v // x + v // (x * y), 0)
f = v - v // x - v // y + v // (x * y)
if debug:
print(v, ":", m1, m2, f)
if m2 <= f - m1:
return 1
else:
return 0
print(1 + search(check, 0, 1, y * c1 + x * c2 + 1))
|
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR VAR IF VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
def Solve(n, m, sum):
cnt = int(sum / x / y)
if x == y:
cnt = int(sum / x)
n = max(0, n - int(sum / y) + cnt)
m = max(0, m - int(sum / x) + cnt)
if sum - int(sum / x) - int(sum / y) + cnt >= n + m:
tmp = 0
else:
tmp = m + n - (sum - int(sum / x) - int(sum / y) + cnt)
return tmp
n, m, x, y = map(int, input().split())
sum = n + m
while True:
temp = Solve(n, m, sum)
if temp == 0:
break
sum += temp
print(sum)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
line = input().split()
cnt1 = int(line[0])
cnt2 = int(line[1])
x = int(line[2])
y = int(line[3])
maxNum = 100000000000
def solve(n):
a = n // x
b = n // y
both = n // (x * y)
only_a = a - both
only_b = b - both
curr_cnt1 = max(0, cnt1 - only_b)
curr_cnt2 = max(0, cnt2 - only_a)
shared = curr_cnt1 + curr_cnt2
remaining = n - only_a - only_b - both
if remaining > shared:
return 1
elif remaining == shared:
return 0
else:
return -1
def binary_search(left, right):
middle = (left + right) // 2
if solve(middle) == 0:
return middle
elif solve(middle) == 1:
return binary_search(left, middle - 1)
else:
return binary_search(middle + 1, right)
ans = binary_search(1, maxNum)
while True:
if solve(ans - 1) == 0:
ans -= 1
else:
break
print(ans)
|
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 VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR WHILE NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
c1, c2, x, y = map(int, input().split())
l = c1 + c2
temp = l
h = pow(10, 10)
while l < h:
ans = (l + h) // 2
if c1 > ans - ans // x or c2 > ans - ans // y or temp > ans - ans // (x * y):
l = ans + 1
else:
h = ans
print(l)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
def works(v):
shared = v // (x * y)
cnt_1loc, cnt_2loc = v // x, v // y
split = v - cnt_1loc - cnt_2loc + shared
cnt_1loc, cnt_2loc = cnt_1loc - shared, cnt_2loc - shared
return max(cnt1 - cnt_2loc, 0) + max(cnt2 - cnt_1loc, 0) <= split
cnt1, cnt2, x, y = map(int, input().split())
low, mid, high = 0, -1, 10**18
while high - low > 1:
mid = (high + low) // 2
if works(mid):
high = mid
else:
low = mid
print(high)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER 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 EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
cnt1, cnt2, x, y = map(int, input().split())
l = 0
r = (cnt1 // (x - 1) + 1) * x + (cnt2 // (y - 1) + 1) * y + 5
while r - l != 1:
m = l + (r - l) // 2
t_cnt1 = m // y - m // (x * y)
t_cnt2 = m // x - m // (x * y)
if max(0, cnt1 - t_cnt1) + max(0, cnt2 - t_cnt2) <= m - (
m // x + m // y - m // (x * y)
):
r = m
else:
l = m
print(r)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
c1, c2, x, y = map(int, input().split())
xy = x * y
n = 1
while n - n // x < c1 or n - n // y < c2 or n - n // xy < c1 + c2:
n = n << 1
l = 1
r = n
while r - l > 1:
m = r + l >> 1
if m - m // x < c1 or m - m // y < c2 or m - m // xy < c1 + c2:
l = m
else:
r = m
m = l
if m - m // x < c1 or m - m // y < c2 or m - m // xy < c1 + c2:
print(r)
else:
print(l)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
c1, c2, x, y = map(int, input().split())
def fn(val):
f = [val // x, val // y]
both = val // (x * y)
f = [(i - both) for i in f]
oth = val - f[0] - f[1] - both
cnt = [c1 - f[1], c2 - f[0]]
if cnt[0] < 0:
cnt[0] = 0
if cnt[1] < 0:
cnt[1] = 0
return sum(cnt) <= oth
l = 0
r = int(1e18)
while r - l > 1:
m = (r + l) // 2
if fn(m):
r = m
else:
l = m
print(r)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR LIST BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
cnt1, cnt2, x, y = [int(x) for x in input().split()]
lo, hi = 1, 10 * (cnt1 + cnt2)
while lo < hi:
mid = (lo + hi) // 2
div_by_both = mid // (x * y)
div_by_x_only = mid // x - div_by_both
div_by_y_only = mid // y - div_by_both
total_wasted = div_by_both
total_wasted += div_by_y_only - min(cnt1, div_by_y_only)
total_wasted += div_by_x_only - min(cnt2, div_by_x_only)
is_enough = True
if cnt1 + cnt2 + total_wasted > mid:
is_enough = False
if is_enough:
hi = mid
else:
lo = mid + 1
print(lo)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
c1, c2, x, y = map(int, input().split())
def valid(m):
a = m - m // x
b = m - m // y - max(0, c1 - m // y + m // x // y)
return a >= c1 and b >= c2
l, r = 0, 1 << 62
while l + 1 < r:
m = l + r >> 1
if valid(m):
r = m
else:
l = m
print(r)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
a, b, x, y = map(int, input().split())
l, r = a + b, (a + b) * 5
while l < r:
v = (l + r) // 2
if v - v // x >= a and v - v // y >= b and v - v // (x * y) >= a + b:
r = v
else:
l = v + 1
print(r)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
c1, c2, x, y = list(map(int, input().split()))
def get(n):
if n - n // (x * y) >= c1 + c2 and n - n // x >= c1 and n - n // y >= c2:
return True
else:
return False
l = 0
r = c1 * x + c2 * y
while r - l > 1:
m = (l + r) // 2
if get(m):
r = m
else:
l = m
print(r)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR 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 EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
c1, c2, x, y = map(int, input().split())
def poss(v):
global c1, c2, x, y
divbyx = v // x
divbyy = v // y
divbyxy = v // (x * y)
cc1 = c2 - divbyx + divbyxy
cc2 = c1 - divbyy + divbyxy
cc1 = max(cc1, 0)
cc2 = max(cc2, 0)
remaining = v - divbyx - divbyy + divbyxy
if cc1 + cc2 <= remaining:
return True
return False
lo, hi = 1, int(1e19)
ans = int(1e19)
while lo <= hi:
mid = (lo + hi) // 2
if poss(mid):
ans = min(ans, mid)
hi = mid - 1
else:
lo = mid + 1
print(ans)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
n, m, x, y = map(int, input().split())
tmp = [1]
def judge(n, m, sum, tmp):
cnt = int(sum / x / y)
if x == y:
cnt = int(sum / x)
n = max(0, n - int(sum / y) + cnt)
m = max(0, m - int(sum / x) + cnt)
if sum - int(sum / x) - int(sum / y) + cnt >= n + m:
tmp = sum
re = 0
else:
tmp = m + n - (sum - int(sum / x) - int(sum / y) + cnt)
re = tmp
return re
sum = n + m
while True:
temp = judge(n, m, sum, tmp)
if temp == 0:
break
sum += temp
print(sum)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
a, b, x, y = map(int, input().split())
l = 0
r = 10000000000000
res = 0
def judge(n, a, b, x, y):
return n - n // (x * y) >= a + b and n - n // x >= a and n - n // y >= b
while l <= r:
m = (l + r) // 2
if judge(m, a, b, x, y):
r = m - 1
res = m
else:
l = m + 1
print(res)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
def gcd(a, b):
return b if a % b == 0 else gcd(b, a % b)
cntx, cnty, x, y = list(map(int, input().split()))
d = x * y // gcd(x, y)
v = cnt = 0
while cntx + cnty > cnt:
dxy = cntx + cnty - cnt
cx = (v + dxy) // x - v // x
cy = (v + dxy) // y - v // y
cxy = (v + dxy) // d - v // d
cnt += dxy - cx - cy + cxy
cntx -= min(cy - cxy, cntx)
cnty -= min(cx - cxy, cnty)
v += dxy
print(v)
|
FUNC_DEF RETURN BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
class FriendsPresents:
def __init__(self, c1, c2, x, y):
self.c1, self.c2, self.x, self.y = c1, c2, x, y
def check(self, v):
xval = int(v / self.x)
yval = int(v / self.y)
xyval = int(v / (self.x * self.y))
c1rem = max(0, self.c1 - (yval - xyval))
c2rem = max(0, self.c2 - (xval - xyval))
if c1rem + c2rem <= v - xval - yval + xyval:
return True
else:
return False
def min_val(self):
mn = self.c1 + self.c2 - 1
mx = self.c1 * self.c2
while not self.check(mx):
mx = mx * 2
while mx > mn + 1:
md = int((mn + mx) / 2)
if self.check(md):
mx = md
else:
mn = md
return mx
c1, c2, x, y = list(map(int, input().strip(" ").split(" ")))
fp = FriendsPresents(c1, c2, x, y)
print(fp.min_val())
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
def main():
cnt1, cnt2, x, y = map(int, input().split())
lo = cnt3 = cnt1 + cnt2
hi, z = 10**10, x * y
while lo < hi:
v = (lo + hi) // 2
if v - v // x < cnt1 or v - v // y < cnt2 or v - v // z < cnt3:
lo = v + 1
else:
hi = v
print(lo)
main()
|
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
import sys
__author__ = "neki"
words = input().split()
cnt1 = int(words[0])
cnt2 = int(words[1])
x = int(words[2])
y = int(words[3])
def determine(num, x, y, cnt1, cnt2):
like1 = num - num // x
like2 = num - num // y
like1and2 = like1 + like2 - num + num // (x * y)
friend1 = like1 - like1and2
friend2 = like2 - like1and2
both = like1and2
cond1 = 0
cond2 = 0
if friend1 + both >= cnt1 and friend2 + both >= cnt2:
cond1 += 1
if friend1 + friend2 + both >= cnt1 + cnt2:
cond2 += 1
if cond1 > 0 and cond2 > 0:
return 1
return 0
lim = 0
lim1 = 1
lim2 = -1
while 1:
if lim2 < 0:
lim = lim1 * 2
else:
lim = (lim1 + lim2) // 2
if lim == lim1:
break
if determine(lim, x, y, cnt1, cnt2) == 1:
lim2 = lim
else:
lim1 = lim
print(lim2)
|
IMPORT ASSIGN VAR STRING 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 VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
s = input()
c1, c2, x, y = list(map(int, s.split()))
l = 0
r = int(1e18)
while r - l > 1:
m = (l + r) // 2
d1 = m // x
d2 = m // y
b = m // (x * y)
k1 = d2 - b
k2 = d1 - b
rr = m - d1 - d2 + b
r1 = max(0, c1 - k1)
r2 = max(0, c2 - k2)
if rr >= r1 + r2:
r = m
else:
l = m
print(r)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
s = input()
arrIn = s.split()
n = int(arrIn[0])
m = int(arrIn[1])
x = int(arrIn[2])
y = int(arrIn[3])
l = 1
r = 2000000000.0
while l < r:
mid = int((r + l) / 2)
if (
n <= mid - int(mid / x)
and m <= mid - int(mid / y)
and m + n <= mid - int(mid / (x * y))
):
r = mid
else:
l = mid + 1
else:
print(r)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
-----Input-----
The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≤ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
-----Note-----
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
n0, n1, x, y = map(int, input().split())
def f(m, n, x, y):
return max(0, n - (m // y - m // (x * y)))
lo = -1
hi = x * y * (n0 + n1)
while lo + 1 < hi:
mid = lo + (hi - lo) // 2
if f(mid, n0, x, y) + f(mid, n1, y, x) <= mid - mid // x - mid // y + mid // (
x * y
):
hi = mid
else:
lo = mid
print(hi)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, k, m = map(int, input().split())
l = [int(i) for i in input().split()]
def ispzbl(l, n, m, k, mid):
z = l[mid:]
if not z:
return True
s = 0
box = 1
for i in z:
s += i
if s > m:
box += 1
s = i
return box <= k
low = 0
high = n - 1
while low <= high:
mid = (low + high) // 2
if ispzbl(l, n, m, k, mid):
ans = mid
high = mid - 1
else:
low = mid + 1
print(n - ans)
|
ASSIGN VAR 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 VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN 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 VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = [int(x) for x in input().split()]
ns = [int(x) for x in input().split()]
ns.reverse()
rem = k
num = 0
ans = []
for i in range(n):
if rem >= ns[i]:
rem -= ns[i]
num += 1
else:
ans.append(num)
rem = k - ns[i]
num = 1
if num > 0:
ans.append(num)
if len(ans) <= m:
print(sum(ans))
else:
a = [sum(ans[:m])]
print(max(a))
|
ASSIGN VAR 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 ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
s = [int(i) for i in input().split()]
n, m, k = s[0], s[1], s[2]
arr = [int(i) for i in input().split()]
present = 0
box = 1
index = -1
for o in range(n - 1, -1, -1):
if present + arr[o] > k:
present = arr[o]
box += 1
else:
present += arr[o]
if box > m:
index = o
break
print(n - 1 - index)
|
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = list(map(int, input().split()))
cnt = 0
count = 0
sum = 0
a = [int(s) for s in input().split()]
for i in range(n - 1, -1, -1):
sum += a[i]
if sum > k:
sum = a[i]
count += 1
if count >= m:
break
cnt += 1
print(cnt)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
def check(a, m, mx):
t = mx
for i in a:
if t - i >= 0:
t -= i
elif m - 1 > 0:
m -= 1
t = mx - i
else:
return False
return True
def f(m, mx, a):
hi = len(a)
lo = 0
res = 0
while lo <= hi:
mid = (lo + hi) // 2
if check(a[mid:], m, mx):
hi = mid - 1
res = len(a[mid:])
else:
lo = mid + 1
return res
n, m, mx = map(int, input().strip().split())
lst = list(map(int, input().strip().split()))
print(f(m, mx, lst))
|
FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN 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 ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
ar = []
read = input().split(" ")
n = int(read[0])
m = int(read[1])
k = int(read[2])
read = input().split(" ")
for i in range(n):
ar.append(int(read[i]))
cap = k
i = n - 1
while m > 0 and i >= 0:
if cap - ar[i] >= 0:
cap -= ar[i]
i -= 1
elif m > 0:
m -= 1
cap = k
else:
break
print(n - i - 1)
|
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
w, m, k = map(int, input().split())
a = list(map(int, input().split()))
i = w
s = k
while i > 0 and m > 0:
i -= 1
if s < a[i]:
m -= 1
s = k
s -= a[i]
if m == 0:
print(w - i - 1)
else:
print(w - i)
|
ASSIGN VAR 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 ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, M, K = map(int, input().split())
l = list(map(int, input().split()))
num = 0
k = K
m = M
for j in range(n - 1, -1, -1):
if m <= 0:
break
elif k >= l[j]:
k -= l[j]
num += 1
else:
m -= 1
if m <= 0:
break
else:
k = K
k -= l[j]
num += 1
print(num)
|
ASSIGN VAR 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 VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = list(map(int, input().split()))
arr = list(map(int, input().split()))
low, high = 0, n - 1
while low < high:
mid = (low + high) // 2
ans = [k for i in range(m)]
flag = 0
ind = 0
for j in range(mid, n):
cur = arr[j]
prev = ind
while True:
if ans[ind] < cur:
ind += 1
else:
ans[ind] -= cur
break
if ind >= m:
flag = 1
break
if flag == 1:
break
if flag == 1:
low = mid
flag = 0
else:
high = mid
flag = 0
if high - low == 1:
break
ans = [k for i in range(m)]
flag = 0
ind = 0
for j in range(low, n):
cur = arr[j]
prev = ind
while True:
if ans[ind] < cur:
ind += 1
else:
ans[ind] -= cur
break
if ind >= m:
flag = 1
break
if flag == 1:
break
if flag == 1:
print(n - high)
else:
print(n - low)
|
ASSIGN VAR 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 VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
n = n - 1
count = 0
while m > 0 and n >= 0:
ktemp = k
while ktemp >= a[n] and n >= 0:
ktemp = ktemp - a[n]
n = n - 1
count = count + 1
m = m - 1
print(count)
|
ASSIGN VAR 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 WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
def check(num):
left = m
curr = k
for i in range(n - num, n):
if curr >= a[i]:
curr -= a[i]
else:
if left == 1:
return False
left -= 1
curr = k - a[i]
return True
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
low = 0
high = n
while low < high:
mid = (low + high) // 2
if check(mid):
low = mid + 1
else:
high = mid - 1
if check(low):
print(low)
else:
print(low - 1)
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR 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 ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
obj, n_caixas, size = input().split()
n_caixas, size = int(n_caixas), int(size)
objts = [int(x) for x in input().split()]
objts.reverse()
current_box_size = size
count_obj = 0
for o in objts:
if n_caixas < 1:
break
if o <= current_box_size:
current_box_size -= o
count_obj += 1
continue
n_caixas -= 1
if n_caixas < 1 or size < o:
break
current_box_size = size - o
count_obj += 1
print(count_obj)
|
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a = a[::-1]
boxes = 0
ans = 0
loc = 0
for i in range(n):
loc += a[i]
if loc > k:
boxes += 1
loc = a[i]
if boxes == m:
break
ans += 1
print(ans)
|
ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
a = [int(i) for i in input().split()]
def cond(x):
box_cnt = m
capacity = k
for i in range(n - x - 1, n):
if capacity >= a[i]:
capacity -= a[i]
else:
box_cnt -= 1
capacity = k - a[i]
return box_cnt <= 0
lo, hi = 0, n
while lo < hi:
mi = (lo + hi) // 2
if cond(mi):
hi = mi
else:
lo = mi + 1
print(lo)
|
ASSIGN VAR 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 VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR NUMBER VAR 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 EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
import sys
input = sys.stdin.readline
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
a = a[::-1]
cnt_box = 1
ca = 0
s = 0
for i in range(n):
if s + a[i] <= k:
s += a[i]
ca += 1
else:
s = 0
cnt_box += 1
if cnt_box <= m:
s += a[i]
ca += 1
if cnt_box > m:
break
print(ca)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
import sys
def minp():
return sys.stdin.readline().strip()
n, mm, k = map(int, minp().split())
a = list(map(int, minp().split()))
def tt(x):
m = mm - 1
c = 0
for j in range(x, n):
i = a[j]
if c + i > k:
if m == 0 or i > k:
return False
c = i
m -= 1
else:
c += i
return True
l = 0
r = n
while l < r:
c = (l + r) // 2
if tt(c):
r = c
else:
l = c + 1
print(n - r)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR 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 EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
def get_input_list():
return list(map(int, input().split()))
n, m, k = get_input_list()
a = get_input_list()
b = 0
c = k
i = n - 1
r = 0
while True:
if c < a[i]:
b += 1
if b == m:
break
c = k
c -= a[i]
r += 1
i -= 1
if i < 0:
break
print(r)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
import sys
input = sys.stdin.readline
def main():
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if min(a) > k:
return 0
a = a[::-1]
current = 0
answer = 0
for i in range(n):
if current + a[i] <= k:
current += a[i]
answer += 1
else:
current = 0
m -= 1
if m == 0:
break
elif current + a[i] < k:
current += a[i]
answer += 1
elif current + a[i] == k:
answer += 1
m -= 1
if m == 0:
break
else:
continue
return answer
def __starting_point():
print(main())
__starting_point()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR 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 IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
def is_ok(x):
cnt = 1
cur = 0
for i in range(n - x, n):
if cur + A[i] <= k:
cur += A[i]
else:
cnt += 1
cur = A[i]
if cnt <= m:
return True
else:
return False
ok = 0
ng = n + 1
while ok + 1 < ng:
c = (ok + ng) // 2
if is_ok(c):
ok = c
else:
ng = c
print(ok)
|
ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER 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 ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
u = list(map(int, input().split()))
def chek(M):
p = k
t = m
for i in range(M, n):
if p - u[i] >= 0:
p -= u[i]
elif t > 1:
t -= 1
p = k - u[i]
if p < 0:
return False
else:
return False
return True
L = 0
R = n
while R - L > 1:
M = (L + R) // 2
if chek(M):
R = M
else:
L = M
if chek(L):
print(n - L)
else:
print(n - R)
|
ASSIGN VAR 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 VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
a = list(reversed(a))
cnt = 0
K = k
ans = 0
for i in range(n):
if a[i] <= K:
K -= a[i]
else:
cnt += 1
K = k - a[i]
if cnt == m:
break
ans += 1
print(ans)
|
ASSIGN VAR 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 VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
kk = input().split()
n = int(kk[0])
m = int(kk[1])
k = int(kk[2])
objs = list(map(int, input().split()))
if m >= n:
print(n)
else:
objs = objs[::-1]
sum_ = 0
items = 0
l = list()
for obj in objs:
if sum_ + obj <= k:
sum_ += obj
items += 1
else:
l.append(items)
sum_ = obj
items = 1
if len(l) == m:
break
l.append(items)
s = 0
for i in range(m):
if i < len(l):
s += l[i]
else:
break
print(s)
|
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 VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
import sys
mod = 10**9 + 7
INF = float("inf")
def inp():
return int(sys.stdin.readline())
def inpl():
return list(map(int, sys.stdin.readline().split()))
n, m, k = inpl()
a = inpl()
a = a[::-1]
now = k
for i, x in enumerate(a):
if x > k:
break
if now >= x:
now -= x
else:
m -= 1
if m == 0:
break
now = k - x
else:
print(n)
quit()
print(i)
|
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = list(map(int, input().split()))
list_a = list(map(int, input().split()))
list_a.reverse()
count_b = 0
sum_now = 0
ans = 0
i = 0
while i < n:
if count_b >= m:
ans = i - 1
break
sum_now += list_a[i]
if sum_now > k:
sum_now = 0
count_b += 1
else:
i += 1
else:
ans = n - 1
print(ans + 1)
|
ASSIGN VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
def main():
nobj, nbox, boxsize = list(map(int, input().split()))
obj = list(map(int, input().split()))
box = boxsize
curr = 0
ans = 0
i = nobj - 1
while i >= 0:
o = obj[i]
if o <= box:
box -= o
ans += 1
else:
curr += 1
box = boxsize
if curr >= nbox:
break
continue
i -= 1
print(ans)
def __starting_point():
main()
__starting_point()
|
FUNC_DEF ASSIGN VAR 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 VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
import sys
input = sys.stdin.readline
def judge(x):
cnt = 1
remain = k
for i in range(n - x, n):
if remain - a[i] >= 0:
remain -= a[i]
else:
if a[i] > k:
return False
cnt += 1
remain = k - a[i]
return cnt <= m
def binary_search():
l, r = 0, n
while l <= r:
mid = (l + r) // 2
if judge(mid):
l = mid + 1
else:
r = mid - 1
return r
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
print(binary_search())
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR 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 ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
from sys import stdin, stdout
en, m, k = [int(x) for x in stdin.readline().split()]
a = [int(x) for x in stdin.readline().split()]
n = []
c = k
boxes = 1
for x in a:
if c >= x:
c -= x
else:
boxes += 1
c = k - x
n.append(boxes)
totalB = n[-1]
if totalB <= m:
print(en)
else:
low = n.index(totalB - m)
high = n.index(totalB - m + 1)
while low <= high:
mid = (low + high) // 2
n = []
c = k
boxes = 1
for x in a[mid:]:
if c >= x:
c -= x
else:
boxes += 1
c = k - x
n.append(boxes)
if n[-1] <= m:
high = mid - 1
else:
low = mid + 1
print(en - low)
|
ASSIGN VAR 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 FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
def fit(a, m, k, res):
k1 = k
for ai in a[-res:]:
if ai <= k1:
k1 -= ai
else:
k1 = k - ai
if m == 1:
return False
else:
m -= 1
return True
def bisect(n, m, k, a):
left, right = 0, n + 1
while left + 1 < right:
middle = (left + right) // 2
if fit(a, m, k, middle):
left = middle
else:
right = middle
return left
def main():
n, m, k = list(map(int, input().split()))
a = tuple(map(int, input().split()))
print(bisect(n, m, k, a))
main()
|
FUNC_DEF ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF 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 VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
count = 0
curr = 0
i = n
while count < m and i >= 0:
i -= 1
if curr + a[i] <= k:
curr += a[i]
else:
count += 1
curr = a[i]
print(n - i - 1)
|
ASSIGN VAR 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 NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, k, m = map(int, input().split())
l = list(map(int, input().split()))
tot = 0
ans = 1
s = 0
for i in range(n - 1, -1, -1):
s += l[i]
if s > m:
s = l[i]
ans += 1
if ans > k:
break
tot += 1
print(tot)
|
ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
import sys
def minp():
return sys.stdin.readline().strip()
n, mm, k = list(map(int, minp().split()))
a = list(map(int, minp().split()))
def tt(x):
m = mm - 1
c = 0
res = 0
for i in reversed(a):
if c + i > k:
if m == 0 or i > k:
return res
c = i
m -= 1
else:
c += i
res += 1
return res
print(tt(0))
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
b = k
count = 0
for obj in a[::-1]:
if obj > k:
break
if obj > b:
if m > 1:
m -= 1
b = k - obj
count += 1
else:
break
else:
b -= obj
count += 1
print(count)
|
ASSIGN VAR 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 FOR VAR VAR NUMBER IF VAR VAR IF VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
import sys
def test(pos):
current_pack = k
count = 1
for i in range(pos, n):
if a[i] > current_pack:
count += 1
current_pack = k - a[i]
else:
current_pack -= a[i]
if count > m:
return False
else:
return True
def bs(l, r):
l1 = l
r1 = r
res = 0
while l1 <= r1:
mid = (l1 + r1) // 2
if test(mid) == True:
res = mid
r1 = mid - 1
else:
l1 = mid + 1
return res
n, m, k = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
print(n - bs(0, n - 1))
|
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
t = list(map(int, input().split()))
tmp_sum = 0
wyn = 0
z = 0
for x in range(len(t) - 1, -1, -1):
if m == 0:
break
tmp_sum += t[x]
if x != 0:
wyn += 1
if tmp_sum + t[x - 1] > k:
m -= 1
tmp_sum = 0
elif tmp_sum <= k:
wyn += 1
print(wyn)
|
ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
a = input().split()
a = [int(x) for x in a]
a.reverse()
box = 0
fruits = 0
m -= 1
for i, x in enumerate(a):
if box + x <= k:
box += x
continue
m -= 1
if m < 0:
fruits = i
break
box = x
else:
print(n)
exit()
print(i)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
def main():
n, m, k = [int(c) for c in input().split()]
a = [int(c) for c in input().split()]
box = 0
b_id = 0
count = 0
for i in range(len(a) - 1, -1, -1):
if box + a[i] <= k:
box += a[i]
count += 1
else:
if b_id + 1 >= m:
break
b_id += 1
box = a[i]
count += 1
print(count)
main()
|
FUNC_DEF ASSIGN VAR 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
input1 = input().split()
num_objects = int(input1[0])
num_boxes = int(input1[1])
box_size = int(input1[2])
objects = [int(x) for x in input().split()]
max_objects_packed = 0
current_box_size = box_size
remaining_boxes = num_boxes
objects_packed = 0
i = num_objects - 1
while i >= 0 and remaining_boxes != 0:
if objects[i] < current_box_size:
current_box_size -= objects[i]
objects_packed += 1
i -= 1
elif objects[i] == current_box_size:
current_box_size = box_size
objects_packed += 1
remaining_boxes -= 1
i -= 1
elif objects[i] > current_box_size:
current_box_size = box_size - objects[i]
remaining_boxes -= 1
i -= 1
if remaining_boxes != 0:
objects_packed += 1
print(objects_packed)
|
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 VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, b, k = map(int, input().split())
a = [int(i) for i in input().split()]
j, m, s, ans = 0, 1, 0, 0
for i in range(len(a) - 1, -1, -1):
if m <= b and s + a[i] <= k:
ans += 1
s += a[i]
elif m + 1 <= b and a[i] <= k:
ans += 1
s = a[i]
m += 1
else:
break
print(ans)
|
ASSIGN VAR 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 VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
i = n - 1
tec = k
res = 0
while i >= 0 and m > 0:
if a[i] <= tec:
tec -= a[i]
res += 1
i -= 1
else:
m -= 1
tec = k
print(res)
|
ASSIGN VAR 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 BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = map(int, input().split())
arr = list(reversed(list(map(int, input().split()))))
has = cnt = 0
for v in arr:
if v + has <= k:
has += v
elif v > k:
print(0)
exit(0)
else:
has = v
m -= 1
if m == 0:
break
else:
cnt += 1
print(cnt)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
-----Input-----
The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.
-----Output-----
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
-----Examples-----
Input
5 2 6
5 2 1 4 2
Output
4
Input
5 1 4
4 2 3 4 1
Output
1
Input
5 3 3
1 2 3 1 1
Output
5
-----Note-----
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
|
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
summ = []
cur = 0
for item in a[::-1]:
cur += item
summ.append(cur)
def helper(x, summ, l, r):
while l < r:
mid = l + (r - l + 1) // 2
if summ[mid] > x:
r = mid - 1
else:
l = mid
if summ[l] <= x:
return l
else:
return l - 1
res = []
l = 0
z = k
while len(res) < m and l < n:
x = helper(z, summ, l, n - 1)
res.append(x)
z = summ[x] + k
l = x + 1
print(res[-1] + 1)
|
ASSIGN VAR 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 LIST ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.