message stringlengths 2 30.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 237 109k | cluster float64 10 10 | __index_level_0__ int64 474 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
N people are waiting in a single line in front of the Takahashi Store. The cash on hand of the i-th person from the front of the line is a positive integer A_i.
Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer P indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.
At each step, when a product is shown to a customer, if price P is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than P decreases by P, and the next step begins.
Mr. Takahashi can set the value of positive integer P independently at each step.
He would like to sell as many products as possible. However, if a customer were to end up with 0 cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with 0 cash.
Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.
Constraints
* 1 β¦ | N | β¦ 100000
* 1 β¦ A_i β¦ 10^9(1 β¦ i β¦ N)
* All inputs are integers.
Input
Inputs are provided from Standard Inputs in the following form.
N
A_1
:
A_N
Output
Output an integer representing the maximum number of products Mr. Takahashi can sell.
Examples
Input
3
3
2
5
Output
3
Input
15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
Output
18
Submitted Solution:
```
import sys
read = sys.stdin.read
readline = sys.stdin.readline
N, *A = map(int, read().split())
if N == 1:
print(A[0] - 1)
exit()
answer = A[0] - 1
P = 2
for i in A[1:]:
if i < P:
continue
elif i == P:
P += 1
continue
answer += (i - 1) // P
print(answer)
``` | instruction | 0 | 67,135 | 10 | 134,270 |
Yes | output | 1 | 67,135 | 10 | 134,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
N people are waiting in a single line in front of the Takahashi Store. The cash on hand of the i-th person from the front of the line is a positive integer A_i.
Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer P indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.
At each step, when a product is shown to a customer, if price P is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than P decreases by P, and the next step begins.
Mr. Takahashi can set the value of positive integer P independently at each step.
He would like to sell as many products as possible. However, if a customer were to end up with 0 cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with 0 cash.
Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.
Constraints
* 1 β¦ | N | β¦ 100000
* 1 β¦ A_i β¦ 10^9(1 β¦ i β¦ N)
* All inputs are integers.
Input
Inputs are provided from Standard Inputs in the following form.
N
A_1
:
A_N
Output
Output an integer representing the maximum number of products Mr. Takahashi can sell.
Examples
Input
3
3
2
5
Output
3
Input
15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
Output
18
Submitted Solution:
```
N=int(input())
A=[int(input()) for _ in range(N)]
max_rem=0
ans=0
for a in A:
if a >= max_rem+2:
h = max_rem+1
c,rem = divmod(a-1, h)
max_rem = max(max_rem, rem)
ans += c
else:
max_rem = max(max_rem, a)
print(ans)
``` | instruction | 0 | 67,136 | 10 | 134,272 |
No | output | 1 | 67,136 | 10 | 134,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
N people are waiting in a single line in front of the Takahashi Store. The cash on hand of the i-th person from the front of the line is a positive integer A_i.
Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer P indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.
At each step, when a product is shown to a customer, if price P is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than P decreases by P, and the next step begins.
Mr. Takahashi can set the value of positive integer P independently at each step.
He would like to sell as many products as possible. However, if a customer were to end up with 0 cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with 0 cash.
Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.
Constraints
* 1 β¦ | N | β¦ 100000
* 1 β¦ A_i β¦ 10^9(1 β¦ i β¦ N)
* All inputs are integers.
Input
Inputs are provided from Standard Inputs in the following form.
N
A_1
:
A_N
Output
Output an integer representing the maximum number of products Mr. Takahashi can sell.
Examples
Input
3
3
2
5
Output
3
Input
15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
Output
18
Submitted Solution:
```
N = int(input())
A = [int(input()) for i in range(N)]
ans = 0
X = 0
for a in A:
# δΈγ€γε£²γγͺγ
if a - 1 < X + 1:
X = max(X, a)
continue
sell = (a - 1) // (X + 1)
ans += sell
next_X_1 = a % ((X + 1) * sell)
p = X + (a % (X + 1)) // sell
if p:
next_X_2 = a % p
else:
next_X_2 = float('inf')
X = max(X, min(next_X_1, next_X_2))
print(ans)
``` | instruction | 0 | 67,137 | 10 | 134,274 |
No | output | 1 | 67,137 | 10 | 134,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
N people are waiting in a single line in front of the Takahashi Store. The cash on hand of the i-th person from the front of the line is a positive integer A_i.
Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer P indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.
At each step, when a product is shown to a customer, if price P is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than P decreases by P, and the next step begins.
Mr. Takahashi can set the value of positive integer P independently at each step.
He would like to sell as many products as possible. However, if a customer were to end up with 0 cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with 0 cash.
Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.
Constraints
* 1 β¦ | N | β¦ 100000
* 1 β¦ A_i β¦ 10^9(1 β¦ i β¦ N)
* All inputs are integers.
Input
Inputs are provided from Standard Inputs in the following form.
N
A_1
:
A_N
Output
Output an integer representing the maximum number of products Mr. Takahashi can sell.
Examples
Input
3
3
2
5
Output
3
Input
15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
Output
18
Submitted Solution:
```
N=int(input())
a=[0]*N
for i in range(N):
a[i]=int(input())
minimum=1
count=0
for i in a:
if(minimum<i):
if i%minimum:
count+= i//minimum
print("a:",i,"minimum:",minimum,"count:",count)
minimum=max(2,minimum)
else :
count+= (i//minimum-1)
print("a:",i,"minimum:",minimum,"count:",count)
minimum=max(2,minimum)
else:
minimum=max(minimum,i+1)
print("a:",i,"minimum:",minimum,"count:",count)
print(count)
``` | instruction | 0 | 67,138 | 10 | 134,276 |
No | output | 1 | 67,138 | 10 | 134,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
N people are waiting in a single line in front of the Takahashi Store. The cash on hand of the i-th person from the front of the line is a positive integer A_i.
Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer P indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.
At each step, when a product is shown to a customer, if price P is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than P decreases by P, and the next step begins.
Mr. Takahashi can set the value of positive integer P independently at each step.
He would like to sell as many products as possible. However, if a customer were to end up with 0 cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with 0 cash.
Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.
Constraints
* 1 β¦ | N | β¦ 100000
* 1 β¦ A_i β¦ 10^9(1 β¦ i β¦ N)
* All inputs are integers.
Input
Inputs are provided from Standard Inputs in the following form.
N
A_1
:
A_N
Output
Output an integer representing the maximum number of products Mr. Takahashi can sell.
Examples
Input
3
3
2
5
Output
3
Input
15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
Output
18
Submitted Solution:
```
# def makelist(n, m):
# return [[0 for i in range(m)] for j in range(n)]
n = int(input())
A = [0]*N
for i in range(N):
A[i] = int(input())
ans = A[0] - 1
A[0] = 1
cnt = 1
# yobi = 1
for i in range(1, N):
now = A[i]
if now > cnt:
if now == cnt + 1: ## 0 ni nattyau
cnt += 1
else: ## now >= cnt + 2
ans += now // (cnt + 1)
if now % (cnt + 1) == 0:
ans -= 1
print(ans)
``` | instruction | 0 | 67,139 | 10 | 134,278 |
No | output | 1 | 67,139 | 10 | 134,279 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | instruction | 0 | 67,551 | 10 | 135,102 |
Tags: greedy, sortings
Correct Solution:
```
n, m, k=input().split(" ")
n=int(n)
m=int(m)
k=int(k)
mex=0
class Obj:
def __init__(self, index, costs, sells, lims):
self.i=index
self.costs=costs
self.sells=sells
self.limits=lims
l=[Obj(i, [], [], []) for i in range(n)]
for i in range(n):
planet=input()
for j in range(m):
a, b, c=tuple(map(int, input().split(" ")))
l[i].costs.append(a)
l[i].sells.append(b)
l[i].limits.append(c)
for i in range(n):
for j in range(n):
l1=l[i].costs
l2=l[j].sells
ll=[]
for pro in range(m):
ll.append([l2[pro]-l1[pro], pro])
ll.sort(key=lambda x:x[0])
quant=0
pr=-1
c=0
while quant<k and pr>-len(ll)-1:
if quant+l[i].limits[ll[pr][1]]<=k and ll[pr][0]>0:
quant+=l[i].limits[ll[pr][1]]
c+=l[i].limits[ll[pr][1]]*ll[pr][0]
elif ll[pr][0]>0:
c+=(k-quant)*ll[pr][0]
quant=k
else:
break
pr-=1
mex=max(c, mex)
print(mex)
``` | output | 1 | 67,551 | 10 | 135,103 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | instruction | 0 | 67,552 | 10 | 135,104 |
Tags: greedy, sortings
Correct Solution:
```
import math
n,m,k=map(int,input().split())
q,z,y,pp={},-math.inf,[],k
for i in range(n):
input();q[i]=[]
for j in range(m):
q[i].append(list(map(int,input().split())))
for i in q:
r=[]
for j in q:
o=[]
if i!=j:
for p in range(m):
o.append([(q[j][p][1]-q[i][p][0]),q[i][p][2]])
r.append(o)
y.append(r)
for i in y:
for j in i:
j.sort(reverse=True)
h,k=0,pp
for p in j:
if p[0]>0 and k>0:h+=p[0]*min(p[1],k);k-=p[1]
z=max(z,h)
print(z)
``` | output | 1 | 67,552 | 10 | 135,105 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | instruction | 0 | 67,553 | 10 | 135,106 |
Tags: greedy, sortings
Correct Solution:
```
import sys
profit=0
initial=(list(map(int,sys.stdin.readline().split())))
num_planet=initial[0]
num_goods=initial[1]
capacity=initial[2]
stonks=[]
for i in range (0, num_planet):
name_planet=str(sys.stdin.readline())
planet=[]
for j in range (0, num_goods):
elem=(list(map(int,sys.stdin.readline().split())))
planet.append(elem)
stonks.append(planet)
def Solution():
global profit
pair=[]
for i in range (0, len(stonks)):
for j in range (0, len(stonks)):
tmp=[]
for k in range (0, num_goods):
if stonks[i][k][0]<stonks[j][k][1]:
res=stonks[j][k][1]-stonks[i][k][0]
a=(res, stonks[i][k][2])
tmp.append(a)
else:
pass
if len(tmp)>0:
sort_tmp=sorted(tmp, key = lambda x: (-x[0]))
y=0
for y in range (0, capacity):
local_profit=0
i_in_list=0
if y == capacity:
break
for x in range (0, len(sort_tmp)):
for z in range (0, sort_tmp[i_in_list][1]):
if sort_tmp[i_in_list][1]==0:
break
local_profit+=sort_tmp[i_in_list][0]
y+=1
if z==sort_tmp[i_in_list][1]:
break
if y > capacity-1 or x==len(sort_tmp):
break
if y > capacity-1 or x==len(sort_tmp):
break
i_in_list+=1
profit = local_profit if local_profit > profit else profit
Solution()
print(profit)
``` | output | 1 | 67,553 | 10 | 135,107 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | instruction | 0 | 67,554 | 10 | 135,108 |
Tags: greedy, sortings
Correct Solution:
```
import sys
profit = 0
initial = (list(map(int, sys.stdin.readline().split())))
num_planet = initial[0]
num_goods = initial[1]
capacity = initial[2]
stonks = []
for i in range(0, num_planet):
#print('Name planetu')
name_Planet = str(sys.stdin.readline())
planet = []
for j in range (0, num_goods):
elem = (list(map(int,sys.stdin.readline().split())))
planet.append(elem)
stonks.append(planet)
def Solution():
global profit
for i in range(0, len(stonks)):
for j in range(0, len(stonks)):
tmp = []
for k in range(0, num_goods):
if stonks[i][k][0] < stonks[j][k][1]:
res = stonks[j][k][1] - stonks[i][k][0]
a = (res, stonks[i][k][2])
tmp.append(a)
else:
pass
if len(tmp) > 0:
sort_tmp = sorted(tmp, key = lambda x: (-x[0]))
y = 0
for y in range(0, capacity):
local_profit = 0
i_in_list = 0
if y == capacity:
break
for x in range(0, len(sort_tmp)):
for z in range(0, sort_tmp[i_in_list][1]):
if sort_tmp[i_in_list][1] == 0:
break
local_profit += sort_tmp[i_in_list][0]
y+=1
if z == sort_tmp[i_in_list][1]:
break
if y > capacity -1 or x == len(sort_tmp):
break
if y > capacity -1 or x == len(sort_tmp):
break
i_in_list += 1
profit = local_profit if local_profit > profit else profit
Solution()
print(profit)
``` | output | 1 | 67,554 | 10 | 135,109 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | instruction | 0 | 67,555 | 10 | 135,110 |
Tags: greedy, sortings
Correct Solution:
```
from itertools import permutations
def main():
n, m, k = map(int, input().split())
l, res = [], []
for _ in range(n):
input()
l.append(list(tuple(map(int, input().split())) for _ in range(m)))
for sb in permutations(l, 2):
t = [(b - a, c) for (a, _, c), (_, b, _) in zip(*sb)]
v, x = k, 0
for d, c in sorted(t, reverse=True):
if d <= 0:
break
if v >= c:
x += d * c
v -= c
else:
x += d * v
break
res.append(x)
print(max(res))
if __name__ == '__main__':
main()
``` | output | 1 | 67,555 | 10 | 135,111 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | instruction | 0 | 67,556 | 10 | 135,112 |
Tags: greedy, sortings
Correct Solution:
```
n, m, k = map(int, input().split())
planets = []
for i in range(n):
name = input()
planets.append([])
for j in range(m):
item = list(map(int, input().split()))
planets[-1].append(item)
res = float("-inf")
from itertools import permutations
for p1, p2 in permutations(planets, 2):
sis = sorted(range(m), key=lambda i :p2[i][1] - p1[i][0], reverse=True)
cl = k
t = 0
for i in sis:
if cl == 0 or p2[i][1] - p1[i][0] <= 0:
break
taken = min(cl, p1[i][2])
t += (p2[i][1] - p1[i][0]) * taken
cl -= taken
res = max(res, t)
print(res)
``` | output | 1 | 67,556 | 10 | 135,113 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | instruction | 0 | 67,557 | 10 | 135,114 |
Tags: greedy, sortings
Correct Solution:
```
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def MAP2():return map(float,input().split())
def LIST(): return list(map(int, input().split()))
def STRING(): return input()
import string
import sys
from heapq import heappop , heappush
from bisect import *
from collections import deque , Counter , defaultdict
from math import *
from itertools import permutations , accumulate
dx = [-1 , 1 , 0 , 0 ]
dy = [0 , 0 , 1 , - 1]
#visited = [[False for i in range(m)] for j in range(n)]
# primes = [2,11,101,1009,10007,100003,1000003,10000019,102345689]
#sys.stdin = open(r'input.txt' , 'r')
#sys.stdout = open(r'output.txt' , 'w')
#for tt in range(INT()):
#arr.sort(key=lambda x: (-d[x], x)) Sort with Freq
#Code
def solve(i , j ):
l = []
for x in range(m):
l.append([(sell_price[j][x]-buy_price[i][x]) ,number_items[i][x]])
l.sort(key=lambda x : x[0] , reverse=True)
profit = 0
count = 0
for item in l :
if count >= k:
break
profit += min(item[1] , k - count) * max(0 , item[0])
if item[0] > 0 :
count+=min(item[1],k-count)
return profit
n , m , k = MAP()
buy_price = []
sell_price = []
number_items = []
for i in range(n):
s = input()
v1 = []
v2 = []
v3 = []
for j in range(m):
l = LIST()
v1.append(l[0])
v2.append(l[1])
v3.append(l[2])
buy_price.append(v1)
sell_price.append(v2)
number_items.append(v3)
ans = 0
for i in range(n):
for j in range(n):
if i != j :
ans = max(ans,solve(i,j))
print(ans)
``` | output | 1 | 67,557 | 10 | 135,115 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | instruction | 0 | 67,558 | 10 | 135,116 |
Tags: greedy, sortings
Correct Solution:
```
import math
n,m,k=map(int,input().split())
q,z,y,pp={},-math.inf,[],k
for i in range(n):
input();q[i]=[]
for j in range(m):
q[i].append(list(map(int,input().split())))
for i in q:
r=[]
for j in q:
o=[]
for p in range(m):
o.append([(q[j][p][1]-q[i][p][0]),q[i][p][2]])
r.append(o)
y.append(r)
for i in y:
for j in i:
j.sort(reverse=True)
h,k=0,pp
for p in j:
if p[0]>0 and k>0:h+=p[0]*min(p[1],k);k-=p[1]
z=max(z,h)
print(z)
``` | output | 1 | 67,558 | 10 | 135,117 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
Submitted Solution:
```
import collections
import itertools
from functools import reduce
mod = (10 ** 9) + 7
def permutationbysum():
for _ in range(int(input())):
num, l, r, achieve = map(int, input().split())
k = r - l + 1
if (k * (k + 1)) // 2 <= achieve <= (k * (num * 2 + 1 - k)) // 2:
outpos = (r) % num
inpos = l - 1
ans = [0] * num
for i in range(num, 0, -1):
if achieve - i > 0 or (achieve - i == 0 and inpos == r - 1):
achieve -= i
ans[inpos] = i
inpos += 1
else:
ans[outpos] = i
outpos += 1
outpos %= num
print(*ans)
else:
print(-1)
# permutationbysum()
def peaks():
for _ in range(int(input())):
num, peak = map(int, input().split())
pos = 1
rpos = num
ans = []
now = 0
if num == 1:
if not peak:
print(1)
else:
print(-1)
continue
if num == 2:
if peak:
print(-1)
else:
print(1, 2)
continue
added = 0
while rpos + 1 != pos:
if not peak:
while pos <= rpos:
ans.append(pos)
pos += 1
break
if not now:
ans.append(pos)
pos += 1
else:
ans.append(rpos)
rpos -= 1
if num - added != 1:
peak -= 1
added += 1
now = 1 - now
if peak:
print(-1)
continue
print(*ans)
# peaks()
import sys
input = sys.stdin.readline
def addone():
for _ in range(int(input())):
num, changes = input().split()
l = collections.deque(sorted([int(i) for i in num]))
perm = collections.deque()
changes = int(changes)
cd = 0
ans = len(num)
while True:
nextnum = l.pop()
while perm and perm[-1] == nextnum:
l.append(perm.pop())
c = (10 - nextnum) - cd
changes -= c
cd += c
if changes >= 0:
if not c:
perm.appendleft(1 - cd)
l.appendleft(-cd)
else:
l.appendleft(1 - cd)
l.appendleft(-cd)
ans += 1
else:
break
print(ans % 1000000007)
# addone()
import math
def andsequences():
def mapping(num):
nonlocal mnum
nonlocal c
num = int(num)
if num < mnum:
mnum = num
c = 1
elif num == mnum:
c += 1
return num
mod = 1000000007
for _ in range(int(input())):
mnum = float('inf')
c = 0
num = int(input())
l = list(map(mapping, input().split()))
for i in l:
if mnum & i != mnum:
print(0)
break
else:
if c == 1:
print(0)
else:
print((math.factorial(num - 2) * (c - 1) * c) % mod)
# andsequences()
def numberdigit():
n = ((10 ** 5) * 2)
l = [0] * 11
mod = 10 ** 9 + 7
l[0] = l[1] = l[2] = l[3] = l[4] = l[5] = l[6] = l[7] = l[8] = 2
l[9] = 3
l[10] = 4
for i in range(11, n):
l.append((l[i - 10] + l[i - 9]) % mod)
for _ in range(int(input())):
num, c = input().split()
c = int(c)
ans = 0
for i in num:
i = int(i)
if 10 - i > c:
ans += 1
else:
ans += l[c - (10 - i)]
print(ans % mod)
# numberdigit()
def mushroom():
people, t1, t2, percent = map(int, input().split())
l = []
percent = 1 - percent * 0.01
for i in range(1, people + 1):
s, s1 = map(int, input().split())
l.append([i, max(s * t1 * percent + s1 * t2, s1 * t1 * percent + s * t2)])
l.sort(key=lambda x: (x[1]), reverse=True)
for i in l:
i[1] = "{:.2f}".format(i[1])
print(*i)
# mushroom()
def escape():
prins = int(input())
dra = int(input())
start = int(input())
pack = int(input())
c = int(input())
speeddiff = dra - prins
if speeddiff <= 0:
return 0
pd = start * prins
ans = 0
while pd < c:
hs = pd / speeddiff
pd += prins * hs
if pd >= c:
break
time = pd / dra
time += pack
pd += prins * time
ans += 1
return ans
# print(escape())
def perm():
def high(n, k):
return k * (2 * n - k + 1) // 2
def low(k):
return k * (k + 1) // 2
for _ in range(int(input())):
num, lef, rig, s = map(int, input().split())
k = rig - lef + 1
rig, lef = rig - 1, lef - 1
if not high(num, k) >= s >= low(k):
print(-1)
continue
l = [0] * num
lp = lef
rp = lef - 1
for i in range(num, 0, -1):
if high(i, k) >= s and s - i >= low(k - 1) and k:
l[lp] = i
lp += 1
s -= i
k -= 1
else:
l[rp] = i
rp -= 1
if k:
print(-1)
else:
print(*l)
# perm()
def newcom():
for _ in range(int(input())):
days, price = map(int, input().split())
dl = input().split()
worth = input().split()
worth.append(0)
ans = float('inf')
req = 0
left = 0
for i in range(days):
a = int(dl[i])
w = int(worth[i])
ans = min(ans, req + max(0, price - left + a - 1) // a)
ns = max(0, w - left + a - 1) // a
req += ns + 1
left += a * ns - w
print(ans)
# newcom()
def perfectsq():
for _ in range(int(input())):
n = input()
for i in input().split():
sq = math.sqrt(int(i))
if sq != int(sq):
print("YES")
break
else:
print("NO")
# perfectsq()
def and0big():
for _ in range(int(input())):
l, k = map(int, input().split())
print(l ** k % mod)
# and0big()
import math
def mod1p():
n = int(input())
ans = dict()
p = 1
for i in range(1, n):
if math.gcd(i, n) == 1:
ans[str(i)] = True
p = (p * i) % n
if p == 1:
print(len(ans))
print(' '.join(ans.keys()))
else:
ans.pop(str(p))
print(len(ans))
print(' '.join(ans.keys()))
# mod1p()
def shorttask():
num = 10000100
l = [-1] * (num + 2)
s = [-1] * (num + 2)
l[1] = 1
for i in range(2, int(math.sqrt(num + 1)) + 2):
if l[i] == -1:
l[i] = i
for x in range(i * i, num + 1, i):
if l[x] == -1:
l[x] = i
s[1] = 1
for i in range(2, num + 1):
if l[i] == -1:
l[i] = i
s[i] = i + 1
else:
i1 = i
s[i] = 1
while i1 % l[i] == 0:
i1 //= l[i]
s[i] = s[i] * l[i] + 1
s[i] *= s[i1]
ans = [-1] * (num + 1)
for i in range(num, 0, -1):
if s[i] < num:
ans[s[i]] = i
for _ in range(int(input())):
print(ans[int(input())])
# shorttask()
def review():
for _ in range(int(input())):
n = int(input())
ans = 0
for i in input().split():
i = int(i)
if i == 1 or i == 3:
ans += 1
print(ans)
# review()
def GCDleng():
po10 = [0] * 11
po10[1] = 1
for i in range(2, 11):
po10[i] = po10[i - 1] * 10
for _ in range(int(input())):
n, n1, res = map(int, input().split())
print(po10[n], po10[n1] + po10[res])
# GCDleng()
def anothercarddeck():
n, q = map(int, input().split())
l = input().split()
d = {l[i]: i for i in range(n - 1, -1, -1)}
ans = []
for i in input().split():
now = d[i]
ans.append(now + 1)
for key in d:
if d[key] < now:
d[key] += 1
d[i] = 0
print(*ans)
# anothercarddeck()
def mincoststring():
n, letters = map(int, input().split())
l = [chr(i + 97) for i in range(letters)]
ans = []
real = letters - 1
if not n:
print(*ans, sep='')
return
if n == 1:
print(*ans, sep='', end='')
print(l[1 % (real + 1)])
return
while n:
for i in range(len(l)):
for i1 in range(i, len(l)):
if i1 != real:
ans.append(l[i1])
ans.append(l[i])
n -= 2
else:
ans.append(l[i1])
n -= 1
if not n:
print(*ans, sep='')
return
if n == 1:
print(*ans, sep='', end='')
print(l[(i1 + 1) % (real + 1)])
return
print(*ans)
# mincoststring()
def mincost2():
n, letters = map(int, input().split())
l = [chr(i + 97) for i in range(letters)]
comb = []
if letters == 1 or n == 1:
print('a' * n)
return
for i in range(letters):
for i1 in range(i, letters):
comb.append(l[i1] + l[i])
lc = len(comb)
while True:
for i in range(lc):
if ord(comb[i][0]) - 97 == letters - 1:
n -= 1
print(comb[i][0], end='')
else:
n -= 2
print(comb[i], end='')
if n == 1:
pos = ord(comb[i][0]) - 97 + 1
print(l[pos % letters])
return
if not n:
return
# mincost2()
def Tittat():
for _ in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
for i in range(n):
if not k:
break
while k and l[i]:
l[i] -= 1
l[-1] += 1
k -= 1
print(*l)
# Tittat()
def xorq2():
for _ in range(int(input())):
n = int(input())
s = 0
num = -1
nc = 0
c = 0
for i in input().split():
i = int(i)
s ^= i
c += 1
if num == -1 and s == i:
num = s
s = 0
nc += c
c = 0
if num != -1 and (s == num or not s):
s = 0
nc += c
c = 0
print(['NO', 'YES'][nc == n])
# xorq2()
def xorq2re():
n = int(input())
s = input().split()
xor = 0
for i in range(n):
s[i] = int(s[i])
xor ^= s[i]
if not xor:
print("YES")
else:
new = 0
ans = 0
for i in s:
new ^= i
if new == xor:
new = 0
ans += 1
print(["NO", "YES"][ans > 1])
import sys
sys.setrecursionlimit(2300)
def q3partition():
def partition():
if tol % 2:
return 0
up = tol
rec = [[False] * (up + 2) for _ in range(n + 1)]
rec[0][0] = True
for i in range(1, n + 1):
v = l[i - 1]
for j in range(up + 1):
if rec[i - 1][j]:
if v + j <= up:
rec[i][j + v] = True
rec[i][j] = True
return rec[n][tol // 2]
def ints(x):
nonlocal tol
nonlocal gcf
x = int(x)
tol += x
gcf = math.gcd(gcf, x) if gcf != float('inf') else x
return x
n = int(input())
tol = 0
gcf = float('inf')
l = list(map(ints, input().split()))
if partition():
pos = 1
for i in l:
if i // gcf % 2:
print(1)
print(pos)
return
pos += 1
else:
print(0)
def permu(n, r):
return math.factorial(n) // math.factorial(n - r)
def comb(n, r):
return math.factorial(n) // math.factorial(r) * math.factorial(n - r)
def calc():
n = int(input())
ans = permu(n, n)
for i in range(1, n + 1):
ans -= comb(n, i)
print(ans)
# calc()
def order():
for _ in range(int(input())):
n = int(input())
odd = []
even = []
for i in input().split():
i = int(i)
if i % 2:
odd.append(i)
else:
even.append(i)
print(*odd + even)
# order()
def TMTdoc():
n = int(input())
l = input().rstrip()
tts = 0
tms = 0
for i in l:
if i == 'T':
tts += 1
else:
tms += 1
if tms * 2 == tts:
nowts = 0
nowms = 0
for i in l:
if i == 'T':
nowts += 1
else:
nowms += 1
if nowts >= nowms and tms - nowms + 1 <= tts - nowts:
continue
return "NO"
else:
return "NO"
return 'YES'
def relayrace():
def solve(n):
if n:
s = -1
else:
s = 1
nowmax = -float('inf')
nowmin = -nowmax
ans = 0
for i, v in sorted(l, key=lambda x: (x[1], s * int(x[0])), reverse=True):
i = int(i)
if i > nowmax:
nowmax = i
if i < nowmin:
nowmin = i
ans += v * (nowmax - nowmin)
return ans
n = int(input())
# l = list(collections.Counter(input().split()).items())
l = -1
ans = 0
for i in sorted(input().split(), key=lambda x: int(x)):
if l == -1:
l = int(i)
else:
ans += int(i) - l
return ans
def relayraces():
n = int(input())
l = sorted(map(int, input().split()))
minv = float("INF")
minpos = 0
for i in range(len(l)):
if i > 0 and 0 <= l[i] - l[i - 1] <= minv:
minv = l[i] - l[i - 1]
minpos = i
if i < n - 1 and l[i + 1] - l[i] <= minv:
minv = l[i + 1] - l[i]
minpos = i
lpos = minpos - 1
rpos = minpos + 1
ans = 0
while lpos >= 0 and rpos < n:
diffl = -(l[lpos] - l[minpos])
diffr = l[rpos] - l[minpos]
if diffl < diffr:
minpos = lpos
lpos -= 1
ans += diffl
else:
minpos = rpos
rpos += 1
ans += diffr
if lpos < 0:
m = l[0]
for i in range(rpos, n):
ans += l[i] - m
else:
h = l[-1]
for i in range(minpos, -1, -1):
ans += h - l[i]
return ans
# sys.setrecursionlimit(2004)
def relayracess():
def recur(l, r):
if l == r:
return 0
if d[(l, r)] != -1:
return d[(l, r)]
ans = nums[r] - nums[l] + min(recur(l + 1, r), recur(l, r - 1))
d[(l, r)] = ans
return ans
d = collections.defaultdict(lambda: -1)
n = int(input())
nums = sorted(map(int, input().split()))
return recur(0, n - 1)
def relayracesss():
# def recur(l,r):
# if l == r:
# d[l][r] = 0
# return 0
# if d[l][r] != -1:
# return d[l][r]
# ans = nums[r]-nums[l] + min(recur(l+1,r),recur(l,r-1))
# d[l][r] = ans
# return ans
# n = int(input())
# m = n + 20
# d = [[-1]*m for _ in range(m)]
n = int(input())
l = sorted(map(int, input().split()))
dp = [[0] * n for _ in range(n)]
for i in range(n - 2, -1, -1):
for i1 in range(i + 1, n):
dp[i][i1] = l[i1] - l[i] + min(dp[i + 1][i1], dp[i][i1 - 1])
return dp[0][n - 1]
def relayracessss():
n = int(input())
l = sorted(map(int, input().split()))
dp = [0] * n
for le in range(n - 2, -1, -1):
for ri in range(le + 1, n):
dp[ri] = l[ri] - l[le] + min(dp[ri], dp[ri - 1])
return dp[-1]
def binli():
n = int(input())
n2 = n * 2
s = input().rstrip()
s1 = input().rstrip()
s2 = input().rstrip()
posl = [0, 0, 0]
ans = ''
while max(posl) < n2:
l = [[], []]
l[int(s[posl[0]])].append(0)
l[int(s1[posl[1]])].append(1)
l[int(s2[posl[2]])].append(2)
if len(l[0]) >= 2:
for i in l[0]:
posl[i] += 1
ans += '0'
else:
for i in l[1]:
posl[i] += 1
ans += '1'
if posl[0] == n2:
if posl[1] >= posl[2]:
ans += s1[posl[1]:]
else:
ans += s2[posl[2]:]
elif posl[1] == n2:
if posl[0] >= posl[2]:
ans += s[posl[0]:]
else:
ans += s2[posl[2]:]
else:
if posl[0] >= posl[1]:
ans += s[posl[0]:]
else:
ans += s1[posl[1]:]
print(ans)
def almostsort():
n, k = map(int, input().split())
ul = 2 ** (n - 1)
if k > ul:
return [-1]
permen = []
bi = bin(k - 1)[2:]
if bi == '0':
return range(1, n + 1)
pos = n - len(bi)
ans = list(range(1, pos))
for i in bi:
permen.append(pos)
if not int(i):
ans += permen[::-1]
permen = []
pos += 1
permen.append(n)
ans += permen[::-1]
return ans
def q1_2050():
for i in range(int(input())):
s = int(input())
if s % 2050:
print(-1)
else:
ab = 2050
while ab <= s:
ab *= 10
ab //= 10
c = 0
while s and s != 1:
c += s // ab
s = s % ab
while s != 1 and s and s < ab:
ab //= 10
print(c)
def q2jog():
n, m = map(int, input().split())
sortbyval = []
rowsv = [collections.defaultdict(int) for _ in range(n)]
for row in range(n):
for i in input().split():
i = int(i)
sortbyval.append([i, row])
rowsv[row][i] += 1
sortbyval.sort(key=lambda x: x[0])
larl = []
for i in range(0, n * m, m):
larl.append(sortbyval[i:i + m])
ans = [[0] * m for _ in range(n)]
person = 0
for i in larl:
for v, pos in i:
ans[pos][person] = v
rowsv[pos][v] -= 1
person += 1
if person == m:
break
else:
continue
break
for r in range(n):
for c in range(m):
if not ans[r][c]:
for key in list(rowsv[r].keys()):
if not rowsv[r][key]:
rowsv[r].pop(key)
continue
rowsv[r][key] -= 1
ans[r][c] = key
break
for i in ans:
print(*i)
def q3fillomino(n, inp):
def gorow():
nonlocal nowp
nonlocal col
nonlocal totake
t = 0
if nowp and col < n and totake and not l[col][nowp - 1]:
for back in range(nowp, -1, -1):
if l[col][back]:
nowp += 1
col += 1
return t
if not totake:
return t
l[col][back] = i
totake -= 1
nowp -= 1
t = 1
nowp += 1
col += 1
return t
l = [[0] * i for i in range(1, n + 1)]
p = 0
for i in inp:
totake = int(i)
nowp = p
col = p
while col < n:
if not totake:
break
for _ in range(n - p):
if not gorow():
break
if not totake or col == n or nowp == n:
break
l[col][nowp] = i
totake -= 1
col += 1
p += 1
for i in l:
print(*i)
def q3fillominoclear(n, inp):
l = [[0] * i for i in range(1, n + 1)]
p = 0
for i in inp:
take = int(i) - 1
l[p][p] = i
x, y = p, p
while take:
if y and not l[x][y - 1]:
y -= 1
else:
x += 1
l[x][y] = i
take -= 1
p += 1
for i in l:
print(*i)
def q4explor():
def gen(r, c):
res = float('inf')
if r:
res = min(res, his[r - 1][c] + 2 * colstep[r - 1][c])
if r < rows - 1:
res = min(res, his[r + 1][c] + 2 * colstep[r][c])
if c:
res = min(res, his[r][c - 1] + 2 * rowstep[r][c - 1])
if c < cols - 1:
res = min(res, his[r][c + 1] + 2 * rowstep[r][c])
return res
rows, cols, k = map(int, input().split())
if k % 2:
for i in range(rows):
print(*[-1] * cols)
return
rowstep = [list(map(int, input().split())) for _ in range(rows)]
colstep = [list(map(int, input().split())) for _ in range(rows - 1)]
his = [[0] * cols for _ in range(rows)]
for _ in range(k // 2):
temp = [[gen(r, c) for c in range(cols)] for r in range(rows)]
his = temp
for i in his:
print(*i)
def cvacations():
def de(v):
nonlocal last
if v == 2:
last = 2
elif v == 1:
last = 1
n = int(input())
ans = 0
last = 0 # 0 = none, 1 = gym, 2 = contest
l = input().split()
for i1 in range(n):
i = int(l[i1])
if not i:
last = 0
ans += 1
continue
if i == 1:
if last != 2:
last = 2
continue
ans += 1
last = 0
elif i == 2:
if last != 1:
last = 1
continue
ans += 1
last = 0
else:
if not last:
if i1 < n - 1:
de(int(l[i1 + 1]))
if int(l[i1 + 1]) == 3:
for pos in range(i1 + 2, n):
if l[pos] != 3:
r = l[pos]
break
else:
last = 1
continue
de(r)
else:
break
elif last == 1:
last = 2
elif last == 2:
last = 1
return ans
def brackets():
s = input().rstrip()
lpos, rpos = 0, len(s) - 1
ans = []
while lpos + 1 <= rpos:
if s[lpos] == '(':
if s[rpos] == ')':
ans.append(lpos + 1)
ans.append(rpos + 1)
lpos += 1
rpos -= 1
else:
rpos -= 1
else:
lpos += 1
if ans:
print(1)
print(len(ans))
print(*sorted(ans))
else:
print(0)
def In():
return map(int, input().split())
def complibook():
n, q = In()
nums = list(In())
for _ in range(q):
l, r, x = In()
num = nums[x - 1]
les = 0
for i in range(l - 1, r):
if nums[i] < num:
les += 1
print(["No", "Yes"][les + l - 1 == x - 1])
# complibook()
def bstr():
for _ in range(int(input())):
n, k = In()
now = 1
times = 2
while k > now:
k -= now
now += 1
times += 1
fir = n - times
sec = n - k
for i in range(n):
if i == fir or i == sec:
print('b', end='')
else:
print('a', end='')
print()
# bstr()
def avgsuper():
n, k, oper = In()
l = sorted(In(), reverse=True)
nowavg = sum(l) / n
addpos, addnum = 0, 0
for i in range(oper):
if n == 1 and addpos != 0:
break
new = (nowavg * n - l[n - 1]) / max((n - 1), 1)
if 1 / n > new - nowavg and addpos < n:
addnum += 1
if addnum >= k:
addpos += 1
addnum = 0
nowavg += 1 / n
else:
nowavg = new
n -= 1
print(nowavg)
# avgsuper()
def avgsuper2():
n, k, oper = In()
l = sorted(In(), reverse=True)
prefix = [0] * (n + 1)
for i in range(1, n + 1):
prefix[i] = l[i - 1] + prefix[i - 1]
prefix.pop(0)
ans = 0
for i in range(n - 1, max(-1, n - oper - 2), -1):
ans = max(ans, (prefix[i] + min(k * (i + 1), oper - (n - i - 1))) / (i + 1))
return ans
# print(avgsuper2())
def tanna():
n, k, A, B = int(input()), int(input()), int(input()), int(input())
if k == 1:
return (n - 1) * A
x = n
ans = 0
while x != 1:
if x < k:
return ans + A * (x - 1)
if not x % k:
nexts = x // k
ans += min(B, A * (x - nexts))
x = nexts
else:
new = (x % k)
ans += new * A
x = x - new
return ans
# print(tanna())
def segsqu():
square = 1
num = int(input())
while num > square * square:
square += 1
cols = square
rows = math.ceil(num / cols)
print(cols + rows)
# segsqu()
def ParallelUniverses():
n, k, m, t = In()
for _ in range(t):
sta, pos = In()
if sta: # insert
n += 1
if pos - 1 < k:
k += 1
else:
if pos < k:
k -= pos
n -= pos
else:
n = pos
print(n, k)
# ParallelUniverses()
def treasure():
num = int(input())
choose = max(list(In()) for _ in range(num))
ch2 = min(list(In()) for _ in range(num))
print(choose[0] + ch2[0], choose[1] + ch2[1])
# treasure()
def tradingbus():
plan, m, cap = In()
l = []
for _ in range(plan):
name = input()
l.append([list(In()) for _ in range(m)])
ans = 0
for i in range(plan):
fix = l[i]
for i1 in range(i + 1, plan):
fix1 = l[i1]
sellnew = [] # 1
buynew = [] # buy from planet 2
for x in range(m):
fnow = fix[x]
f1now = fix1[x]
sellnew.append([max(f1now[1] - fnow[0],0), fnow[2]])
buynew.append([max(fnow[1] - f1now[0],0), f1now[2]])
sellnew.sort(reverse=True)
buynew.sort(reverse=True)
sellc = cap
buyc = cap
sa = 0
ba = 0
for x in range(m):
left = sellnew[x][1]
if left > sellc:
sa += sellnew[x][0] * sellc
break
else:
sa += sellnew[x][0] * left
sellc -= left
for x in range(m):
left = buynew[x][1]
if left > buyc:
ba += buynew[x][0] * buyc
break
else:
ba += buynew[x][0] * left
buyc -= left
ans = max(ans, max(ba, sa))
return ans
print(tradingbus())
``` | instruction | 0 | 67,559 | 10 | 135,118 |
Yes | output | 1 | 67,559 | 10 | 135,119 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
Submitted Solution:
```
class Planeta:
def __init__(self):
self.nome = input()
self.precoDeComprar = {}
self.precoDeVender = {}
self.estoqueNoPlaneta = {}
for i in range(0, m):
s = list(map(int, input().split()))
self.precoDeComprar[i] = s[0]
self.precoDeVender[i] = s[1]
self.estoqueNoPlaneta[i] = s[2]
def __hash__(self):
return hash(self.nome)
n, m, k = list(map(int, input().split()))
planetas = []
for i in range(0, n):
planetas.append(Planeta())
maximumProfit = 0
for i in range(0, n):
for j in range(0, n):
if (i != j):
p1 = planetas[i]
p2 = planetas[j]
lucroTabela = {}
l = 0
holding = 0
for x in range(0, m):
lucroTabela[x] = p2.precoDeVender[x] - p1.precoDeComprar[x]
lucroOrdenado = list(lucroTabela.keys())
lucroOrdenado.sort(key = lambda x: lucroTabela[x], reverse=True)
for x in lucroOrdenado:
if (lucroTabela[x] > 0):
quantoComprar = min(p1.estoqueNoPlaneta[x], k - holding)
l += lucroTabela[x] * quantoComprar
holding += quantoComprar
if (l > maximumProfit):
maximumProfit = l
print(maximumProfit)
``` | instruction | 0 | 67,560 | 10 | 135,120 |
Yes | output | 1 | 67,560 | 10 | 135,121 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
Submitted Solution:
```
I=lambda:map(int,input().split())
R=range
n,m,k=I()
def r(a,b,c=k):
q=0
for a,b in sorted((b[i][1]-a[i][0],a[i][2])for i in R(m))[::-1]:
if a<1or c<1:break
q+=a*min(b,c);c-=b
return q
w=[]
for _ in '0'*n:I();w+=[[list(I())for _ in '0'*m]]
print(max(r(w[i],w[j])for i in R(n)for j in R(n)))
``` | instruction | 0 | 67,561 | 10 | 135,122 |
Yes | output | 1 | 67,561 | 10 | 135,123 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
Submitted Solution:
```
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int,minp().split())
n,m,k = mints()
planets = [0]*n
for i in range(n):
name = minp()
costs = [0]*m
for j in range(m):
costs[j] = tuple(mints())
planets[i] = costs
def profit(a,b):
offers = [0]*m
for i in range(m):
offers[i] = (b[i][1]-a[i][0],a[i][2])
#print(offers)
offers.sort(reverse=True)
z = 0
r = 0
for i in range(m):
if offers[i][0] > 0:
w = min(k-z,offers[i][1])
z += w
#print(w, offers[i][0])
r += w*offers[i][0]
return r
mp = 0
for i in range(n):
for j in range(n):
if i != j:
mp = max(mp, profit(planets[i],planets[j]))
print(mp)
``` | instruction | 0 | 67,562 | 10 | 135,124 |
Yes | output | 1 | 67,562 | 10 | 135,125 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
Submitted Solution:
```
import collections
import itertools
from functools import reduce
mod = (10 ** 9) + 7
def permutationbysum():
for _ in range(int(input())):
num, l, r, achieve = map(int, input().split())
k = r - l + 1
if (k * (k + 1)) // 2 <= achieve <= (k * (num * 2 + 1 - k)) // 2:
outpos = (r) % num
inpos = l - 1
ans = [0] * num
for i in range(num, 0, -1):
if achieve - i > 0 or (achieve - i == 0 and inpos == r - 1):
achieve -= i
ans[inpos] = i
inpos += 1
else:
ans[outpos] = i
outpos += 1
outpos %= num
print(*ans)
else:
print(-1)
# permutationbysum()
def peaks():
for _ in range(int(input())):
num, peak = map(int, input().split())
pos = 1
rpos = num
ans = []
now = 0
if num == 1:
if not peak:
print(1)
else:
print(-1)
continue
if num == 2:
if peak:
print(-1)
else:
print(1, 2)
continue
added = 0
while rpos + 1 != pos:
if not peak:
while pos <= rpos:
ans.append(pos)
pos += 1
break
if not now:
ans.append(pos)
pos += 1
else:
ans.append(rpos)
rpos -= 1
if num - added != 1:
peak -= 1
added += 1
now = 1 - now
if peak:
print(-1)
continue
print(*ans)
# peaks()
import sys
input = sys.stdin.readline
def addone():
for _ in range(int(input())):
num, changes = input().split()
l = collections.deque(sorted([int(i) for i in num]))
perm = collections.deque()
changes = int(changes)
cd = 0
ans = len(num)
while True:
nextnum = l.pop()
while perm and perm[-1] == nextnum:
l.append(perm.pop())
c = (10 - nextnum) - cd
changes -= c
cd += c
if changes >= 0:
if not c:
perm.appendleft(1 - cd)
l.appendleft(-cd)
else:
l.appendleft(1 - cd)
l.appendleft(-cd)
ans += 1
else:
break
print(ans % 1000000007)
# addone()
import math
def andsequences():
def mapping(num):
nonlocal mnum
nonlocal c
num = int(num)
if num < mnum:
mnum = num
c = 1
elif num == mnum:
c += 1
return num
mod = 1000000007
for _ in range(int(input())):
mnum = float('inf')
c = 0
num = int(input())
l = list(map(mapping, input().split()))
for i in l:
if mnum & i != mnum:
print(0)
break
else:
if c == 1:
print(0)
else:
print((math.factorial(num - 2) * (c - 1) * c) % mod)
# andsequences()
def numberdigit():
n = ((10 ** 5) * 2)
l = [0] * 11
mod = 10 ** 9 + 7
l[0] = l[1] = l[2] = l[3] = l[4] = l[5] = l[6] = l[7] = l[8] = 2
l[9] = 3
l[10] = 4
for i in range(11, n):
l.append((l[i - 10] + l[i - 9]) % mod)
for _ in range(int(input())):
num, c = input().split()
c = int(c)
ans = 0
for i in num:
i = int(i)
if 10 - i > c:
ans += 1
else:
ans += l[c - (10 - i)]
print(ans % mod)
# numberdigit()
def mushroom():
people, t1, t2, percent = map(int, input().split())
l = []
percent = 1 - percent * 0.01
for i in range(1, people + 1):
s, s1 = map(int, input().split())
l.append([i, max(s * t1 * percent + s1 * t2, s1 * t1 * percent + s * t2)])
l.sort(key=lambda x: (x[1]), reverse=True)
for i in l:
i[1] = "{:.2f}".format(i[1])
print(*i)
# mushroom()
def escape():
prins = int(input())
dra = int(input())
start = int(input())
pack = int(input())
c = int(input())
speeddiff = dra - prins
if speeddiff <= 0:
return 0
pd = start * prins
ans = 0
while pd < c:
hs = pd / speeddiff
pd += prins * hs
if pd >= c:
break
time = pd / dra
time += pack
pd += prins * time
ans += 1
return ans
# print(escape())
def perm():
def high(n, k):
return k * (2 * n - k + 1) // 2
def low(k):
return k * (k + 1) // 2
for _ in range(int(input())):
num, lef, rig, s = map(int, input().split())
k = rig - lef + 1
rig, lef = rig - 1, lef - 1
if not high(num, k) >= s >= low(k):
print(-1)
continue
l = [0] * num
lp = lef
rp = lef - 1
for i in range(num, 0, -1):
if high(i, k) >= s and s - i >= low(k - 1) and k:
l[lp] = i
lp += 1
s -= i
k -= 1
else:
l[rp] = i
rp -= 1
if k:
print(-1)
else:
print(*l)
# perm()
def newcom():
for _ in range(int(input())):
days, price = map(int, input().split())
dl = input().split()
worth = input().split()
worth.append(0)
ans = float('inf')
req = 0
left = 0
for i in range(days):
a = int(dl[i])
w = int(worth[i])
ans = min(ans, req + max(0, price - left + a - 1) // a)
ns = max(0, w - left + a - 1) // a
req += ns + 1
left += a * ns - w
print(ans)
# newcom()
def perfectsq():
for _ in range(int(input())):
n = input()
for i in input().split():
sq = math.sqrt(int(i))
if sq != int(sq):
print("YES")
break
else:
print("NO")
# perfectsq()
def and0big():
for _ in range(int(input())):
l, k = map(int, input().split())
print(l ** k % mod)
# and0big()
import math
def mod1p():
n = int(input())
ans = dict()
p = 1
for i in range(1, n):
if math.gcd(i, n) == 1:
ans[str(i)] = True
p = (p * i) % n
if p == 1:
print(len(ans))
print(' '.join(ans.keys()))
else:
ans.pop(str(p))
print(len(ans))
print(' '.join(ans.keys()))
# mod1p()
def shorttask():
num = 10000100
l = [-1] * (num + 2)
s = [-1] * (num + 2)
l[1] = 1
for i in range(2, int(math.sqrt(num + 1)) + 2):
if l[i] == -1:
l[i] = i
for x in range(i * i, num + 1, i):
if l[x] == -1:
l[x] = i
s[1] = 1
for i in range(2, num + 1):
if l[i] == -1:
l[i] = i
s[i] = i + 1
else:
i1 = i
s[i] = 1
while i1 % l[i] == 0:
i1 //= l[i]
s[i] = s[i] * l[i] + 1
s[i] *= s[i1]
ans = [-1] * (num + 1)
for i in range(num, 0, -1):
if s[i] < num:
ans[s[i]] = i
for _ in range(int(input())):
print(ans[int(input())])
# shorttask()
def review():
for _ in range(int(input())):
n = int(input())
ans = 0
for i in input().split():
i = int(i)
if i == 1 or i == 3:
ans += 1
print(ans)
# review()
def GCDleng():
po10 = [0] * 11
po10[1] = 1
for i in range(2, 11):
po10[i] = po10[i - 1] * 10
for _ in range(int(input())):
n, n1, res = map(int, input().split())
print(po10[n], po10[n1] + po10[res])
# GCDleng()
def anothercarddeck():
n, q = map(int, input().split())
l = input().split()
d = {l[i]: i for i in range(n - 1, -1, -1)}
ans = []
for i in input().split():
now = d[i]
ans.append(now + 1)
for key in d:
if d[key] < now:
d[key] += 1
d[i] = 0
print(*ans)
# anothercarddeck()
def mincoststring():
n, letters = map(int, input().split())
l = [chr(i + 97) for i in range(letters)]
ans = []
real = letters - 1
if not n:
print(*ans, sep='')
return
if n == 1:
print(*ans, sep='', end='')
print(l[1 % (real + 1)])
return
while n:
for i in range(len(l)):
for i1 in range(i, len(l)):
if i1 != real:
ans.append(l[i1])
ans.append(l[i])
n -= 2
else:
ans.append(l[i1])
n -= 1
if not n:
print(*ans, sep='')
return
if n == 1:
print(*ans, sep='', end='')
print(l[(i1 + 1) % (real + 1)])
return
print(*ans)
# mincoststring()
def mincost2():
n, letters = map(int, input().split())
l = [chr(i + 97) for i in range(letters)]
comb = []
if letters == 1 or n == 1:
print('a' * n)
return
for i in range(letters):
for i1 in range(i, letters):
comb.append(l[i1] + l[i])
lc = len(comb)
while True:
for i in range(lc):
if ord(comb[i][0]) - 97 == letters - 1:
n -= 1
print(comb[i][0], end='')
else:
n -= 2
print(comb[i], end='')
if n == 1:
pos = ord(comb[i][0]) - 97 + 1
print(l[pos % letters])
return
if not n:
return
# mincost2()
def Tittat():
for _ in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
for i in range(n):
if not k:
break
while k and l[i]:
l[i] -= 1
l[-1] += 1
k -= 1
print(*l)
# Tittat()
def xorq2():
for _ in range(int(input())):
n = int(input())
s = 0
num = -1
nc = 0
c = 0
for i in input().split():
i = int(i)
s ^= i
c += 1
if num == -1 and s == i:
num = s
s = 0
nc += c
c = 0
if num != -1 and (s == num or not s):
s = 0
nc += c
c = 0
print(['NO', 'YES'][nc == n])
# xorq2()
def xorq2re():
n = int(input())
s = input().split()
xor = 0
for i in range(n):
s[i] = int(s[i])
xor ^= s[i]
if not xor:
print("YES")
else:
new = 0
ans = 0
for i in s:
new ^= i
if new == xor:
new = 0
ans += 1
print(["NO", "YES"][ans > 1])
import sys
sys.setrecursionlimit(2300)
def q3partition():
def partition():
if tol % 2:
return 0
up = tol
rec = [[False] * (up + 2) for _ in range(n + 1)]
rec[0][0] = True
for i in range(1, n + 1):
v = l[i - 1]
for j in range(up + 1):
if rec[i - 1][j]:
if v + j <= up:
rec[i][j + v] = True
rec[i][j] = True
return rec[n][tol // 2]
def ints(x):
nonlocal tol
nonlocal gcf
x = int(x)
tol += x
gcf = math.gcd(gcf, x) if gcf != float('inf') else x
return x
n = int(input())
tol = 0
gcf = float('inf')
l = list(map(ints, input().split()))
if partition():
pos = 1
for i in l:
if i // gcf % 2:
print(1)
print(pos)
return
pos += 1
else:
print(0)
def permu(n, r):
return math.factorial(n) // math.factorial(n - r)
def comb(n, r):
return math.factorial(n) // math.factorial(r) * math.factorial(n - r)
def calc():
n = int(input())
ans = permu(n, n)
for i in range(1, n + 1):
ans -= comb(n, i)
print(ans)
# calc()
def order():
for _ in range(int(input())):
n = int(input())
odd = []
even = []
for i in input().split():
i = int(i)
if i % 2:
odd.append(i)
else:
even.append(i)
print(*odd + even)
# order()
def TMTdoc():
n = int(input())
l = input().rstrip()
tts = 0
tms = 0
for i in l:
if i == 'T':
tts += 1
else:
tms += 1
if tms * 2 == tts:
nowts = 0
nowms = 0
for i in l:
if i == 'T':
nowts += 1
else:
nowms += 1
if nowts >= nowms and tms - nowms + 1 <= tts - nowts:
continue
return "NO"
else:
return "NO"
return 'YES'
def relayrace():
def solve(n):
if n:
s = -1
else:
s = 1
nowmax = -float('inf')
nowmin = -nowmax
ans = 0
for i, v in sorted(l, key=lambda x: (x[1], s * int(x[0])), reverse=True):
i = int(i)
if i > nowmax:
nowmax = i
if i < nowmin:
nowmin = i
ans += v * (nowmax - nowmin)
return ans
n = int(input())
# l = list(collections.Counter(input().split()).items())
l = -1
ans = 0
for i in sorted(input().split(), key=lambda x: int(x)):
if l == -1:
l = int(i)
else:
ans += int(i) - l
return ans
def relayraces():
n = int(input())
l = sorted(map(int, input().split()))
minv = float("INF")
minpos = 0
for i in range(len(l)):
if i > 0 and 0 <= l[i] - l[i - 1] <= minv:
minv = l[i] - l[i - 1]
minpos = i
if i < n - 1 and l[i + 1] - l[i] <= minv:
minv = l[i + 1] - l[i]
minpos = i
lpos = minpos - 1
rpos = minpos + 1
ans = 0
while lpos >= 0 and rpos < n:
diffl = -(l[lpos] - l[minpos])
diffr = l[rpos] - l[minpos]
if diffl < diffr:
minpos = lpos
lpos -= 1
ans += diffl
else:
minpos = rpos
rpos += 1
ans += diffr
if lpos < 0:
m = l[0]
for i in range(rpos, n):
ans += l[i] - m
else:
h = l[-1]
for i in range(minpos, -1, -1):
ans += h - l[i]
return ans
# sys.setrecursionlimit(2004)
def relayracess():
def recur(l, r):
if l == r:
return 0
if d[(l, r)] != -1:
return d[(l, r)]
ans = nums[r] - nums[l] + min(recur(l + 1, r), recur(l, r - 1))
d[(l, r)] = ans
return ans
d = collections.defaultdict(lambda: -1)
n = int(input())
nums = sorted(map(int, input().split()))
return recur(0, n - 1)
def relayracesss():
# def recur(l,r):
# if l == r:
# d[l][r] = 0
# return 0
# if d[l][r] != -1:
# return d[l][r]
# ans = nums[r]-nums[l] + min(recur(l+1,r),recur(l,r-1))
# d[l][r] = ans
# return ans
# n = int(input())
# m = n + 20
# d = [[-1]*m for _ in range(m)]
n = int(input())
l = sorted(map(int, input().split()))
dp = [[0] * n for _ in range(n)]
for i in range(n - 2, -1, -1):
for i1 in range(i + 1, n):
dp[i][i1] = l[i1] - l[i] + min(dp[i + 1][i1], dp[i][i1 - 1])
return dp[0][n - 1]
def relayracessss():
n = int(input())
l = sorted(map(int, input().split()))
dp = [0] * n
for le in range(n - 2, -1, -1):
for ri in range(le + 1, n):
dp[ri] = l[ri] - l[le] + min(dp[ri], dp[ri - 1])
return dp[-1]
def binli():
n = int(input())
n2 = n * 2
s = input().rstrip()
s1 = input().rstrip()
s2 = input().rstrip()
posl = [0, 0, 0]
ans = ''
while max(posl) < n2:
l = [[], []]
l[int(s[posl[0]])].append(0)
l[int(s1[posl[1]])].append(1)
l[int(s2[posl[2]])].append(2)
if len(l[0]) >= 2:
for i in l[0]:
posl[i] += 1
ans += '0'
else:
for i in l[1]:
posl[i] += 1
ans += '1'
if posl[0] == n2:
if posl[1] >= posl[2]:
ans += s1[posl[1]:]
else:
ans += s2[posl[2]:]
elif posl[1] == n2:
if posl[0] >= posl[2]:
ans += s[posl[0]:]
else:
ans += s2[posl[2]:]
else:
if posl[0] >= posl[1]:
ans += s[posl[0]:]
else:
ans += s1[posl[1]:]
print(ans)
def almostsort():
n, k = map(int, input().split())
ul = 2 ** (n - 1)
if k > ul:
return [-1]
permen = []
bi = bin(k - 1)[2:]
if bi == '0':
return range(1, n + 1)
pos = n - len(bi)
ans = list(range(1, pos))
for i in bi:
permen.append(pos)
if not int(i):
ans += permen[::-1]
permen = []
pos += 1
permen.append(n)
ans += permen[::-1]
return ans
def q1_2050():
for i in range(int(input())):
s = int(input())
if s % 2050:
print(-1)
else:
ab = 2050
while ab <= s:
ab *= 10
ab //= 10
c = 0
while s and s != 1:
c += s // ab
s = s % ab
while s != 1 and s and s < ab:
ab //= 10
print(c)
def q2jog():
n, m = map(int, input().split())
sortbyval = []
rowsv = [collections.defaultdict(int) for _ in range(n)]
for row in range(n):
for i in input().split():
i = int(i)
sortbyval.append([i, row])
rowsv[row][i] += 1
sortbyval.sort(key=lambda x: x[0])
larl = []
for i in range(0, n * m, m):
larl.append(sortbyval[i:i + m])
ans = [[0] * m for _ in range(n)]
person = 0
for i in larl:
for v, pos in i:
ans[pos][person] = v
rowsv[pos][v] -= 1
person += 1
if person == m:
break
else:
continue
break
for r in range(n):
for c in range(m):
if not ans[r][c]:
for key in list(rowsv[r].keys()):
if not rowsv[r][key]:
rowsv[r].pop(key)
continue
rowsv[r][key] -= 1
ans[r][c] = key
break
for i in ans:
print(*i)
def q3fillomino(n, inp):
def gorow():
nonlocal nowp
nonlocal col
nonlocal totake
t = 0
if nowp and col < n and totake and not l[col][nowp - 1]:
for back in range(nowp, -1, -1):
if l[col][back]:
nowp += 1
col += 1
return t
if not totake:
return t
l[col][back] = i
totake -= 1
nowp -= 1
t = 1
nowp += 1
col += 1
return t
l = [[0] * i for i in range(1, n + 1)]
p = 0
for i in inp:
totake = int(i)
nowp = p
col = p
while col < n:
if not totake:
break
for _ in range(n - p):
if not gorow():
break
if not totake or col == n or nowp == n:
break
l[col][nowp] = i
totake -= 1
col += 1
p += 1
for i in l:
print(*i)
def q3fillominoclear(n, inp):
l = [[0] * i for i in range(1, n + 1)]
p = 0
for i in inp:
take = int(i) - 1
l[p][p] = i
x, y = p, p
while take:
if y and not l[x][y - 1]:
y -= 1
else:
x += 1
l[x][y] = i
take -= 1
p += 1
for i in l:
print(*i)
def q4explor():
def gen(r, c):
res = float('inf')
if r:
res = min(res, his[r - 1][c] + 2 * colstep[r - 1][c])
if r < rows - 1:
res = min(res, his[r + 1][c] + 2 * colstep[r][c])
if c:
res = min(res, his[r][c - 1] + 2 * rowstep[r][c - 1])
if c < cols - 1:
res = min(res, his[r][c + 1] + 2 * rowstep[r][c])
return res
rows, cols, k = map(int, input().split())
if k % 2:
for i in range(rows):
print(*[-1] * cols)
return
rowstep = [list(map(int, input().split())) for _ in range(rows)]
colstep = [list(map(int, input().split())) for _ in range(rows - 1)]
his = [[0] * cols for _ in range(rows)]
for _ in range(k // 2):
temp = [[gen(r, c) for c in range(cols)] for r in range(rows)]
his = temp
for i in his:
print(*i)
def cvacations():
def de(v):
nonlocal last
if v == 2:
last = 2
elif v == 1:
last = 1
n = int(input())
ans = 0
last = 0 # 0 = none, 1 = gym, 2 = contest
l = input().split()
for i1 in range(n):
i = int(l[i1])
if not i:
last = 0
ans += 1
continue
if i == 1:
if last != 2:
last = 2
continue
ans += 1
last = 0
elif i == 2:
if last != 1:
last = 1
continue
ans += 1
last = 0
else:
if not last:
if i1 < n - 1:
de(int(l[i1 + 1]))
if int(l[i1 + 1]) == 3:
for pos in range(i1 + 2, n):
if l[pos] != 3:
r = l[pos]
break
else:
last = 1
continue
de(r)
else:
break
elif last == 1:
last = 2
elif last == 2:
last = 1
return ans
def brackets():
s = input().rstrip()
lpos, rpos = 0, len(s) - 1
ans = []
while lpos + 1 <= rpos:
if s[lpos] == '(':
if s[rpos] == ')':
ans.append(lpos + 1)
ans.append(rpos + 1)
lpos += 1
rpos -= 1
else:
rpos -= 1
else:
lpos += 1
if ans:
print(1)
print(len(ans))
print(*sorted(ans))
else:
print(0)
def In():
return map(int, input().split())
def complibook():
n, q = In()
nums = list(In())
for _ in range(q):
l, r, x = In()
num = nums[x - 1]
les = 0
for i in range(l - 1, r):
if nums[i] < num:
les += 1
print(["No", "Yes"][les + l - 1 == x - 1])
# complibook()
def bstr():
for _ in range(int(input())):
n, k = In()
now = 1
times = 2
while k > now:
k -= now
now += 1
times += 1
fir = n - times
sec = n - k
for i in range(n):
if i == fir or i == sec:
print('b', end='')
else:
print('a', end='')
print()
# bstr()
def avgsuper():
n, k, oper = In()
l = sorted(In(), reverse=True)
nowavg = sum(l) / n
addpos, addnum = 0, 0
for i in range(oper):
if n == 1 and addpos != 0:
break
new = (nowavg * n - l[n - 1]) / max((n - 1), 1)
if 1 / n > new - nowavg and addpos < n:
addnum += 1
if addnum >= k:
addpos += 1
addnum = 0
nowavg += 1 / n
else:
nowavg = new
n -= 1
print(nowavg)
# avgsuper()
def avgsuper2():
n, k, oper = In()
l = sorted(In(), reverse=True)
prefix = [0] * (n + 1)
for i in range(1, n + 1):
prefix[i] = l[i - 1] + prefix[i - 1]
prefix.pop(0)
ans = 0
for i in range(n - 1, max(-1, n - oper - 2), -1):
ans = max(ans, (prefix[i] + min(k * (i + 1), oper - (n - i - 1))) / (i + 1))
return ans
# print(avgsuper2())
def tanna():
n, k, A, B = int(input()), int(input()), int(input()), int(input())
if k == 1:
return (n - 1) * A
x = n
ans = 0
while x != 1:
if x < k:
return ans + A * (x - 1)
if not x % k:
nexts = x // k
ans += min(B, A * (x - nexts))
x = nexts
else:
new = (x % k)
ans += new * A
x = x - new
return ans
# print(tanna())
def segsqu():
square = 1
num = int(input())
while num > square * square:
square += 1
cols = square
rows = math.ceil(num / cols)
print(cols + rows)
# segsqu()
def ParallelUniverses():
n, k, m, t = In()
for _ in range(t):
sta, pos = In()
if sta: # insert
n += 1
if pos - 1 < k:
k += 1
else:
if pos < k:
k -= pos
n -= pos
else:
n = pos
print(n, k)
# ParallelUniverses()
def treasure():
num = int(input())
choose = max(list(In()) for _ in range(num))
ch2 = min(list(In()) for _ in range(num))
print(choose[0] + ch2[0], choose[1] + ch2[1])
# treasure()
def tradingbus():
plan, m, cap = In()
l = []
for _ in range(plan):
name = input()
l.append([list(In()) for _ in range(m)])
ans = 0
for i in range(plan):
fix = l[i]
for i1 in range(i + 1, plan):
fix1 = l[i1]
sellnew = [] # 1
buynew = [] # buy from planet 2
for x in range(m):
fnow = fix[x]
f1now = fix1[x]
sellnew.append([f1now[1] - fnow[0], fnow[2]])
buynew.append([fnow[1] - f1now[0], f1now[2]])
sellnew.sort(reverse=True)
buynew.sort(reverse=True)
sellc = cap
buyc = cap
sa = 0
ba = 0
for x in range(m):
left = sellnew[x][1]
if left > sellc:
sa += sellnew[x][0] * sellc
break
else:
sa += sellnew[x][0] * left
sellc -= left
for x in range(m):
left = buynew[x][1]
if left > buyc:
ba += buynew[x][0] * buyc
else:
ba += buynew[x][0] * left
buyc -= left
ans = max(ans, max(ba, sa))
return ans
print(tradingbus())
``` | instruction | 0 | 67,563 | 10 | 135,126 |
No | output | 1 | 67,563 | 10 | 135,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
Submitted Solution:
```
n, m, k=input().split(" ")
n=int(n)
m=int(m)
k=int(k)
mex=0
class Obj:
def __init__(self, index, costs, sells, lims):
self.i=index
self.costs=costs
self.sells=sells
self.limits=lims
l=[Obj(i, [], [], []) for i in range(n)]
for i in range(n):
planet=input()
for j in range(m):
a, b, c=tuple(map(int, input().split(" ")))
l[i].costs.append(a)
l[i].sells.append(b)
l[i].limits.append(c)
for i in range(n):
for j in range(i+1, n):
l1=l[i].costs
l2=l[j].sells
ll=[]
for pro in range(m):
ll.append([l2[pro]-l1[pro], pro])
ll.sort(key=lambda x:x[0])
quant=0
pr=-1
c=0
while quant<k and pr>-len(ll)-1:
if quant+l[i].limits[ll[pr][1]]<=k and ll[pr][0]>0:
quant+=l[i].limits[ll[pr][1]]
c+=l[i].limits[ll[pr][1]]*ll[pr][0]
elif ll[pr][0]>0:
c+=(k-quant)*ll[pr][0]
quant=k
else:
break
pr-=1
mex=max(c, mex)
print(mex)
``` | instruction | 0 | 67,564 | 10 | 135,128 |
No | output | 1 | 67,564 | 10 | 135,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
Submitted Solution:
```
import sys
profit = 0
initial = (list(map(int, sys.stdin.readline().split())))
num_planet = initial[0]
num_goods = initial[1]
capacity = initial[2]
stonks = []
for i in range(0, num_planet):
#print('Name planetu')
name_Planet = str(sys.stdin.readline())
planet = []
for j in range (0, num_goods):
elem = (list(map(int,sys.stdin.readline().split())))
planet.append(elem)
stonks.append(planet)
def Solution():
global profit
for i in range(0, len(stonks)):
for j in range(0, len(stonks)):
tmp = []
for k in range(0, num_goods):
if stonks[i][k][0] < stonks[j][k][1]:
res = stonks[j][k][1] - stonks[i][k][0]
a = (res, stonks[i][k][2])
tmp.append(a)
else:
pass
if len(tmp) > 0:
sort_tmp = sorted(tmp, key = lambda x: (-x[0]))
y = 0
for y in range(0, capacity):
local_profit = 0
i_in_list = 0
if y == capacity:
break
for x in range(0, len(sort_tmp)):
for z in range(0, sort_tmp[i_in_list][1]):
if sort_tmp[i_in_list][1] == 0:
break
local_profit += sort_tmp[i_in_list][0]
y+=1
if z == sort_tmp[i_in_list][1]:
break
if y > capacity -1 or x == len(sort_tmp):
break
i_in_list += 1
profit = local_profit if local_profit > profit else profit
Solution()
print(profit)
``` | instruction | 0 | 67,565 | 10 | 135,130 |
No | output | 1 | 67,565 | 10 | 135,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following:
* aij β the cost of buying an item;
* bij β the cost of selling an item;
* cij β the number of remaining items.
It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
Input
The first line contains three space-separated integers n, m and k (2 β€ n β€ 10, 1 β€ m, k β€ 100) β the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow n blocks describing each planet.
The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 β€ bij < aij β€ 1000, 0 β€ cij β€ 100) β the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different.
Output
Print a single number β the maximum profit Qwerty can get.
Examples
Input
3 3 10
Venus
6 5 3
7 6 5
8 6 10
Earth
10 9 0
8 6 4
10 9 3
Mars
4 3 0
8 4 12
7 2 5
Output
16
Note
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3Β·6 + 7Β·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3Β·9 + 7Β·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
Submitted Solution:
```
n,m,k = [int(s) for s in input().split()]
BuyingPrice = []
SellingPrice = []
Number_of_items = []
for i in range(n):
input()
x = []
y = []
z = []
for j in range(m):
a,b,c = [int(s) for s in input().split()]
x.append(a)
y.append(b)
z.append(c)
BuyingPrice.append(x)
SellingPrice.append(y)
Number_of_items.append(z)
def getProfit(i,j):
def takeFirst(z):
return z[0]
n = []
for w in range(m):
n.append((SellingPrice[j][w]-BuyingPrice[i][w],Number_of_items[i][w]))
n.sort(key=takeFirst,reverse=True)
count = 0
profit = 0
for w in n:
if count>=k:
break
profit += min(w[1],k-count)*max(w[0],0)
if(w[0] > 0):
count += min(w[1],k-count)
return profit
profit = 0
for i in range(n):
for j in range(m):
profit = max(profit,getProfit(i,j))
print(profit)
``` | instruction | 0 | 67,566 | 10 | 135,132 |
No | output | 1 | 67,566 | 10 | 135,133 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person. | instruction | 0 | 67,738 | 10 | 135,476 |
Tags: binary search, greedy
Correct Solution:
```
import sys
inp = sys.stdin.read().splitlines()
n,k = list(map(int,inp[0].split()))
lst = list(map(int,inp[1].split()))
lst.sort()
total = sum(lst)
lower = int(total/n)
nupper = total%n
if nupper == 0:
upper = lower;
else:
upper = lower+1;
nlower = n - nupper;
i = 0;
while i<n and lst[i]<lower:
i+=1
low1st = i;
i = n-1;
while i>=0 and lst[i]>upper:
i-=1
uplast = i;
lowerfill = low1st*lower - sum(lst[:low1st])
upperfill = sum(lst[uplast+1:]) - (n-uplast-1)*upper
totalsteps = (lowerfill+upperfill)/2
'''
print("nlower = %d"%nlower)
print("nupper = %d"%nupper)
print("lower = %d"%lower)
print("upper = %d"%upper)
print("lowerfill = %d"%lowerfill)
print("upperfill = %d"%upperfill)
print("totalsteps = %f"%totalsteps)
'''
def filllower():
kk = k
cur = lst[0]
i = 0
while (kk>0):
while (lst[i]==cur):
i+=1
#print("i=%d,lst[i]=%d"%(i,lst[i]))
diff = lst[i] - lst[i-1]
kk -= i*diff
#print("lower kk = %d",kk)
if kk == 0:
cur = lst[i]
break
elif kk<0:
cur = lst[i]-int(-kk/i)-1
#print("-kk/i = %d",int(-kk/i))
if (-kk%i) ==0:
cur += 1
break
cur = lst[i]
#print("min = ",cur)
return cur
def fillupper():
kk = k
i = n-1
cur = lst[i]
while (kk>0):
while (lst[i]==cur):
i-=1
#print("i=%d,lst[i]=%d"%(i,lst[i]))
diff = lst[i+1] - lst[i]
kk -= (n-i-1)*diff
#print("upper kk = ",kk)
if kk == 0:
cur = lst[i-1]
break
elif kk<0:
cur = lst[i]+int(-kk/(n-i-1))
if (-kk%(n-i-1)!=0):
cur += 1;
break
cur = lst[i]
#print("max = ",cur)
return cur
if totalsteps>=k:
print(fillupper()-filllower())
else:
print(upper-lower)
'''
def sortmax():
v = lst[-1]
i = n-2
while(i>=0):
if lst[i]<=v:
lst[-1]=lst[i+1]
lst[i+1]=v
return
i-=1
lst[-1]=lst[0]
lst[0]=v
def sortmin():
v = lst[0]
i = 1
while(i<n):
if lst[i]>=v:
lst[0]=lst[i-1]
lst[i-1]=v
return
i+=1
lst[0]=lst[-1]
lst[-1]=v
lst.sort()
while k:
lst[-1]-=1
sortmax()
#print(lst)
lst[0]+=1
sortmin()
if (lst[-1]-lst[0])<=1:
break
#print(lst)
k-=1
print(lst[-1]-lst[0])
'''
``` | output | 1 | 67,738 | 10 | 135,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person. | instruction | 0 | 67,739 | 10 | 135,478 |
Tags: binary search, greedy
Correct Solution:
```
from sys import stdin
n,k = [int(x) for x in stdin.readline().split()]
oK = k
c = sorted([int(x) for x in stdin.readline().split()])
l = 0
r = n-1
low = c[0]
high = c[-1]
lowBuffer = 0
highBuffer = 0
while low < high:
if low == c[l]:
l += 1
if high == c[r]:
r -= 1
if l*(c[l]-low)-lowBuffer > (n-r-1)*(high-c[r])-highBuffer <= k:
#print(low,high,l,r,'highDown', (n-r-1)*(high-c[r])-highBuffer)
k -= (n-r-1)*(high-c[r])-highBuffer
lowBuffer += (n-r-1)*(high-c[r])-highBuffer
highBuffer = 0
high = c[r]
elif (n-r-1)*(high-c[r])-highBuffer >= l*(c[l]-low)-lowBuffer <= k:
#print(low,high,l,r,'lowUp',l*(c[l]-low)-lowBuffer)
k -= l*(c[l]-low)-lowBuffer
highBuffer += l*(c[l]-low)-lowBuffer
lowBuffer = 0
low = c[l]
else:
low += (lowBuffer+k)//l
high -= (highBuffer+k)//(n-r-1)
k = 0
break
if sum(c)%n == 0:
print(max(0,high-low))
else:
print(max(1,high-low))
``` | output | 1 | 67,739 | 10 | 135,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person. | instruction | 0 | 67,740 | 10 | 135,480 |
Tags: binary search, greedy
Correct Solution:
```
import sys
inp = sys.stdin.read().splitlines()
n,k = list(map(int,inp[0].split()))
lst = list(map(int,inp[1].split()))
lst.sort()
total = sum(lst)
lower = int(total/n)
nupper = total%n
if nupper == 0:
upper = lower;
else:
upper = lower+1;
nlower = n - nupper;
i = 0;
while i<n and lst[i]<lower:
i+=1
low1st = i;
i = n-1;
while i>=0 and lst[i]>upper:
i-=1
uplast = i;
lowerfill = low1st*lower - sum(lst[:low1st])
upperfill = sum(lst[uplast+1:]) - (n-uplast-1)*upper
totalsteps = (lowerfill+upperfill)/2
'''
print("nlower = %d"%nlower)
print("nupper = %d"%nupper)
print("lower = %d"%lower)
print("upper = %d"%upper)
print("lowerfill = %d"%lowerfill)
print("upperfill = %d"%upperfill)
print("totalsteps = %f"%totalsteps)
'''
def filllower():
kk = k
cur = lst[0]
i = 0
while (kk>0):
while (lst[i]==cur):
i+=1
#print("i=%d,lst[i]=%d"%(i,lst[i]))
diff = lst[i] - lst[i-1]
kk -= i*diff
#print("lower kk = %d",kk)
if kk == 0:
cur = lst[i]
break
elif kk<0:
cur = lst[i]-int(-kk/i)-1
#print("-kk/i = %d",int(-kk/i))
if (-kk%i) ==0:
cur += 1
break
cur = lst[i]
#print("min = ",cur)
return cur
def fillupper():
kk = k
i = n-1
cur = lst[i]
while (kk>0):
while (lst[i]==cur):
i-=1
#print("i=%d,lst[i]=%d"%(i,lst[i]))
diff = lst[i+1] - lst[i]
kk -= (n-i-1)*diff
#print("upper kk = ",kk)
if kk == 0:
cur = lst[i-1]
break
elif kk<0:
cur = lst[i]+int(-kk/(n-i-1))
if (-kk%(n-i-1)!=0):
cur += 1;
break
cur = lst[i]
#print("max = ",cur)
return cur
if totalsteps>=k:
print(fillupper()-filllower())
else:
print(upper-lower)
'''
def sortmax():
v = lst[-1]
i = n-2
while(i>=0):
if lst[i]<=v:
lst[-1]=lst[i+1]
lst[i+1]=v
return
i-=1
lst[-1]=lst[0]
lst[0]=v
def sortmin():
v = lst[0]
i = 1
while(i<n):
if lst[i]>=v:
lst[0]=lst[i-1]
lst[i-1]=v
return
i+=1
lst[0]=lst[-1]
lst[-1]=v
lst.sort()
while k:
lst[-1]-=1
sortmax()
#print(lst)
lst[0]+=1
sortmin()
if (lst[-1]-lst[0])<=1:
break
#print(lst)
k-=1
print(lst[-1]-lst[0])
'''
# Made By Mostafa_Khaled
``` | output | 1 | 67,740 | 10 | 135,481 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person. | instruction | 0 | 67,741 | 10 | 135,482 |
Tags: binary search, greedy
Correct Solution:
```
# https://codeforces.com/problemset/problem/671/B
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
def solve(a, k):
b = a[::-1]
S = sum(a)
n = len(a)
div = S // n
re = S % n
T = 0
num = re
for i in range(n-1, -1, -1):
thres = div
if num > 0:
thres += 1
num -= 1
if a[i] > thres:
T += a[i] - thres
if k >= T:
return 1 if re > 0 else 0
l_arr = [0] * n
r_arr = [0] * n
for i in range(1, n):
l_arr[i] = (a[i] - a[i-1]) * i
for i in range(1, n):
r_arr[i] = (b[i-1] - b[i]) * i
remain = k
l, u = a[0], b[0]
for i in range(1, n):
if remain >= l_arr[i]:
remain -= l_arr[i]
l = a[i]
else:
l += remain // i
break
remain = k
for i in range(1, n):
if remain >= r_arr[i]:
remain -= r_arr[i]
u = b[i]
else:
u -= remain // i
break
return u - l#, a, l_arr, r_arr
print(solve(a, k))
#4 1
#1 1 4 2
``` | output | 1 | 67,741 | 10 | 135,483 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person. | instruction | 0 | 67,742 | 10 | 135,484 |
Tags: binary search, greedy
Correct Solution:
```
def main():
n, k = map(int, input().split())
l = sorted(map(int, input().split()))
lo, hi = l[0], l[-1]
while lo < hi - 1:
mid = (lo + hi) // 2
t = k
for x in l:
if x > mid:
lo = mid
break
t -= mid - x
if t < 0:
hi = mid
break
else:
lo = mid
m1 = lo
lo, hi = l[0], l[-1]
l.reverse()
while lo < hi - 1:
mid = (lo + hi) // 2
t = k
for x in l:
if x < mid:
hi = mid
break
t -= x - mid
if t < 0:
lo = mid
break
else:
hi = mid
print(hi - m1 if hi > m1 else 1 if sum(l) % n else 0)
if __name__ == '__main__':
main()
``` | output | 1 | 67,742 | 10 | 135,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person. | instruction | 0 | 67,743 | 10 | 135,486 |
Tags: binary search, greedy
Correct Solution:
```
# [https://ideone.com/WT1j4B <- https://codeforces.com/blog/entry/44821 <- https://codeforces.com/problemset/problem/671/B <- https://algoprog.ru/material/pc671pB]
N = 500005
C = [0] * N
def doit():
global k
C[1:n + 1] = sorted(C[1:n + 1])
last = C[1]
temp = k
for i in range(2, n + 1):
if (C[i] - last) * (i - 1) <= k:
k -= (C[i] - last) * (i - 1)
last = C[i]
j = 0
while j + 1 <= n and C[j + 1] <= last:
j += 1
go = k // j
left = k % j
for i in range(1, j + 1):
C[i] = last + go + (i <= left)
k = temp
def rev():
for i in range(1, n + 1):
C[i] = -C[i]
(n, k) = map(int, input().split())
c = list(map(int, input().split()))
for i in range(1, n+1):
C[i] = c[i-1]
doit()
rev()
doit()
rev()
C[1:n + 1] = sorted(C[1:n + 1])
print(C[n] - C[1])
``` | output | 1 | 67,743 | 10 | 135,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person. | instruction | 0 | 67,744 | 10 | 135,488 |
Tags: binary search, greedy
Correct Solution:
```
from sys import *
f = lambda: map(int, stdin.readline().split())
n, k = f()
t = sorted(f())
s = [0] * (n + 1)
for i in range(n): s[i + 1] = s[i] + t[i]
t = [0] + t
d = s[n]
l, r = 0, n
while l < r:
m = l + r + 1 >> 1
if t[m] * m - s[m] > k: r = m - 1
else: l = m
x = l
l, r = 0, n
while l < r:
m = l + r >> 1
if d - s[m] - t[m] * (n - m) > k: l = m + 1
else: r = m
y = r
q = (d - s[y - 1] - k + n - y) // (n - y + 1) - (s[x] + k) // x
print(max(q, int(d % n > 0)))
``` | output | 1 | 67,744 | 10 | 135,489 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person. | instruction | 0 | 67,745 | 10 | 135,490 |
Tags: binary search, greedy
Correct Solution:
```
import sys
inp = sys.stdin.read().splitlines()
n,k = list(map(int,inp[0].split()))
lst = list(map(int,inp[1].split()))
lst.sort()
total = sum(lst)
lower = int(total/n)
nupper = total%n
if nupper == 0:
upper = lower;
else:
upper = lower+1;
nlower = n - nupper;
i = 0;
while i<n and lst[i]<lower:
i+=1
low1st = i;
i = n-1;
while i>=0 and lst[i]>upper:
i-=1
uplast = i;
lowerfill = low1st*lower - sum(lst[:low1st])
upperfill = sum(lst[uplast+1:]) - (n-uplast-1)*upper
totalsteps = (lowerfill+upperfill)/2
def filllower():
kk = k
cur = lst[0]
i = 0
while (kk>0):
while (lst[i]==cur):
i+=1
diff = lst[i] - lst[i-1]
kk -= i*diff
if kk == 0:
cur = lst[i]
break
elif kk<0:
cur = lst[i]-int(-kk/i)-1
if (-kk%i) ==0:
cur += 1
break
cur = lst[i]
return cur
def fillupper():
kk = k
i = n-1
cur = lst[i]
while (kk>0):
while (lst[i]==cur):
i-=1
diff = lst[i+1] - lst[i]
kk -= (n-i-1)*diff
if kk == 0:
cur = lst[i-1]
break
elif kk<0:
cur = lst[i]+int(-kk/(n-i-1))
if (-kk%(n-i-1)!=0):
cur += 1;
break
cur = lst[i]
return cur
if totalsteps>=k:
print(fillupper()-filllower())
else:
print(upper-lower)
``` | output | 1 | 67,745 | 10 | 135,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
Submitted Solution:
```
n, k = map(int, input().split())
nums = list(map(int, input().split()))
total = sum(nums)
avg = int(total / n)
def check1(nums, target, K):
for x in nums:
if K < 0:
return False
if x < target:
K -= target - x
return K >= 0
def check2(nums, target, K):
for x in nums:
if K < 0:
return False
if x > target:
K -= x - target
return K >= 0
l1, r1 = min(nums), avg + 1
while l1 + 1 < r1:
mid = (l1 + r1) >> 1
if check1(nums, mid, k):
l1 = mid
else:
r1 = mid
if check2(nums, avg + (0 if total % n == 0 else 1), k):
r2 = avg + (0 if total % n == 0 else 1)
else:
l2, r2 = avg + (0 if total % n == 0 else 1), max(nums)
while l2 + 1 < r2:
mid = (l2 + r2) >> 1
if check2(nums, mid, k):
r2 = mid
else:
l2 = mid
print(r2 - l1)
``` | instruction | 0 | 67,746 | 10 | 135,492 |
Yes | output | 1 | 67,746 | 10 | 135,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
Submitted Solution:
```
import math
n,k = map(int,input().split(" "))
arr = sorted(map(int,input().split(" ")))
totalNeed = 0
poorest = 0
richest = 0
foundP = 1
foundR =0
if k==0:
print(max(arr)-min(arr))
else:
for i in range(n-1):
num = arr[i]
prevT = totalNeed
totalNeed += (arr[i+1]-num)*(i+1)
#print(totalNeed)
if k<=totalNeed:
poorest = num+math.floor((k-prevT)/(i+1))
foundP = i
#print('foundP',i)
break
#print('richest:')
stolen =0
for i in range(1,n)[::-1]:
num = arr[i]
prevS = stolen
stolen += (num-arr[i-1])*(n-i)
#print(stolen)
if k<=stolen:
richest = num-math.floor((k-prevS)/(n-i))#ceil?
foundR=i
#print('foundR',i)
break
#print(richest,poorest)
if foundR-1<=foundP:
if foundR-1==foundP and sum(arr)%len(arr)==0:
print(max(richest-poorest,0))
elif foundR-1==foundP and sum(arr)%len(arr)>0:
print(max(richest-poorest,1))
elif sum(arr)%len(arr)==0:
print(0)
else:
print(1)#either 1 left over or zero
else:
print(richest-poorest)
``` | instruction | 0 | 67,747 | 10 | 135,494 |
Yes | output | 1 | 67,747 | 10 | 135,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
Submitted Solution:
```
def main():
n, k = map(int, input().split())
l = sorted(map(int, input().split()))
lo, hi = l[0], l[-1]
while lo < hi - 1:
mid = (lo + hi) // 2
t = k
for x in l:
if x > mid:
lo = mid
break
t -= mid - x
if t < 0:
hi = mid
break
else:
lo = mid
n = lo
lo, hi = l[0], l[-1]
l.reverse()
while lo < hi - 1:
mid = (lo + hi) // 2
t = k
for x in l:
if x < mid:
hi = mid
break
t -= x - mid
if t < 0:
lo = mid
break
else:
hi = mid
print(hi - n if hi > n else abs(sum(l)) % 2)
if __name__ == '__main__':
main()
``` | instruction | 0 | 67,748 | 10 | 135,496 |
No | output | 1 | 67,748 | 10 | 135,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
Submitted Solution:
```
s = input().split(' ')
n = int(s[0])
k = int(s[1])
l = list(map(int, input().split( ' ')))
l.sort()
pb = 0
pe = n-1
while pe > 1 and l[pe-1] == l[pe]:
pe -= 1
while pb < n-1 and l[pb+1] == l[pb]:
pb += 1
while k > 0:
if pb == n-1 or pe == 0:
break
l[pb] +=1
l[pe] -=1
pb -= 1
pe += 1
if pb == 0:
while pb < n-1 and l[pb+1] == l[pb]:
pb += 1
if pe == n-1:
while pe > 1 and l[pe-1] == l[pe]:
pe -= 1
k-=1
print(l[-1] - l[1])
``` | instruction | 0 | 67,749 | 10 | 135,498 |
No | output | 1 | 67,749 | 10 | 135,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
Submitted Solution:
```
import math
n,k = map(int,input().split(" "))
arr = sorted(map(int,input().split(" ")))
totalNeed = 0
poorest = 0
richest = 0
foundP = 1
foundR =0
if k==0:
print(max(arr)-min(arr))
else:
for i in range(n-1):
num = arr[i]
prevT = totalNeed
totalNeed += (arr[i+1]-num)*i
#print(totalNeed)
if k<=totalNeed:
poorest = num+math.floor((k-prevT)/(i+1))
foundP = i
#print('found',i)
break
#print('richest:')
stolen =0
for i in range(1,n)[::-1]:
num = arr[i]
prevS = stolen
stolen += (num-arr[i-1])*(n-i)
#print(stolen)
if k<=stolen:
richest = num-math.floor((k-prevS)/(n-i))#ceil?
foundR=i
#print('foundR',i)
break
#print(richest,poorest)
if foundR<foundP:
if sum(arr)%len(arr)==0:
print(0)
else:
print(1)#either 1 left over or zero
else:
print(richest-poorest)
``` | instruction | 0 | 67,750 | 10 | 135,500 |
No | output | 1 | 67,750 | 10 | 135,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 500 000, 0 β€ k β€ 109) β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 β€ ci β€ 109) β initial wealth of the i-th person.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
Input
4 1
1 1 4 2
Output
2
Input
3 1
2 2 2
Output
0
Note
Lets look at how wealth changes through day in the first sample.
1. [1, 1, 4, 2]
2. [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
Submitted Solution:
```
# https://codeforces.com/problemset/problem/671/B
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
def solve(a, k):
S = sum(a)
n = len(a)
div = S // n
re = S % n
T = 0
num = re
for i in range(n-1, -1, -1):
thres = div
if num > 0:
thres += 1
num -= 1
if a[i] > thres:
T += a[i] - thres
if k >= T:
return 1 if re > 0 else 0
l_arr = [x for x in a]
r_arr = [x for x in a[::-1]]
for i in range(1, n):
l_arr[i] = (l_arr[i] - l_arr[i-1]) * i
for i in range(1, n):
r_arr[i] = (r_arr[i-1] - r_arr[i]) * i
remain = k
l, u = a[0], a[-1]
for i in range(1, n):
if remain >= l_arr[i]:
remain -= l_arr[i]
l = a[i]
else:
l += remain // i
break
remain = k
for i in range(1, n):
if remain >= r_arr[i]:
remain -= r_arr[i]
u = a[i]
else:
u -= remain // i
break
return u - l
print(solve(a, k))
#4 1
#1 1 4 2
``` | instruction | 0 | 67,751 | 10 | 135,502 |
No | output | 1 | 67,751 | 10 | 135,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins. | instruction | 0 | 67,878 | 10 | 135,756 |
Tags: dp, greedy, sortings
Correct Solution:
```
nb = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
ame = [0 for i in range(nb[0])]
amo = [0 for i in range(nb[0])]
for i in range(nb[0]):
if(a[i] % 2 == 0):
if(i == 0):
ame[i] = 1
amo[i] = 0
ame[i] = ame[i-1]+1
amo[i] = amo[i-1]
else:
if(i == 0):
ame[i] = 0
amo[i] = 1
ame[i] = ame[i-1]
amo[i] = amo[i-1]+1
sums = []
for i in range(nb[0]-1):
if(ame[i] == amo[i]):
sums.append(abs(a[i+1]-a[i]))
sums = sorted(sums)
ans = 0
cur = 0
#print(sums, ame, amo)
while nb[1] > 0:
if(cur == len(sums)):
break
if(nb[1]-sums[cur] >= 0):
ans+=1
nb[1] = nb[1]-sums[cur]
cur+=1
else:
break
print(ans)
``` | output | 1 | 67,878 | 10 | 135,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins. | instruction | 0 | 67,879 | 10 | 135,758 |
Tags: dp, greedy, sortings
Correct Solution:
```
input=__import__('sys').stdin.readline
n,b = map(int,input().split())
lis = list(map(int,input().split()))
pre=[[0,0] for i in range(n)]
cut=[]
for i in range(n-1):
if lis[i]%2==0:
pre[i][0]=pre[i-1][0]+1
pre[i][1]=pre[i-1][1]
else:
pre[i][1]=pre[i-1][1]+1
pre[i][0]=pre[i-1][0]
if pre[i][0]==pre[i][1]:
cut.append(abs(lis[i]-lis[i+1]))
ans=0
cut.sort()
for i in range(len(cut)):
if b>=cut[i]:
b-=cut[i]
ans+=1
print(ans)
``` | output | 1 | 67,879 | 10 | 135,759 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins. | instruction | 0 | 67,880 | 10 | 135,760 |
Tags: dp, greedy, sortings
Correct Solution:
```
n,m=map(int,input().split())
a=list(map(int,input().split()))
odd=0
even=0
s=-1
b=[]
for i in range(n-1):
if(a[i]%2):
# if(s==-1):
# s=i
odd+=1
else:
# if(s==-1):
# s=i
even+=1
if(odd==even):
# print(i,s,abs(a[i]-a[i+1]))
# if(i!=n-1):
b.append(abs(a[i]-a[i+1]))
odd=0
even=0
s=-1
# print(b)
b.sort()
ans=0
s=0
for i in b:
if(s+i<=m):
ans+=1
s+=i
else:
break
print(ans)
``` | output | 1 | 67,880 | 10 | 135,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins. | instruction | 0 | 67,881 | 10 | 135,762 |
Tags: dp, greedy, sortings
Correct Solution:
```
def main():
n, B = [int(c) for c in input().split()]
a = [int(c) for c in input().split()]
even = odd = 0
costs = []
for i, e in enumerate(a[:-1]):
if e % 2 == 0:
even += 1
else:
odd += 1
if even == odd:
costs.append(abs(a[i] - a[i+1]))
ans = 0
for cost in sorted(costs):
if cost <= B:
ans += 1
B -= cost
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 67,881 | 10 | 135,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins. | instruction | 0 | 67,882 | 10 | 135,764 |
Tags: dp, greedy, sortings
Correct Solution:
```
n,b = map(int,input().split())
a = list(map(int,input().split()))
if(a[0]%2==0):
odd1=0
even1=1
else:
odd1=1
even1 = 0
odd2=0
even2=0
for val in range(1,len(a)):
if(a[val]%2==0):
even2+=1
else:
odd2+=1
l1=[]
if(even2==odd2 and even1==odd1):
if(abs(a[1]-a[0])<=b):
l1.append(abs(a[1]-a[0]))
for val in range(1,len(a)-1):
if(a[val]%2==0):
even1+=1
even2-=1
else:
odd1+=1
odd2-=1
if(even1==odd1 and even2==odd2):
if(abs(a[val+1]-a[val])<=b):
l1.append(abs(a[val+1]-a[val]))
l1.sort()
sp=0
money=0
if(len(l1)>0):
for val in range(0,len(l1)):
money+=l1[val]
if(money>b):
break
else:
sp+=1
print(sp)
``` | output | 1 | 67,882 | 10 | 135,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins. | instruction | 0 | 67,883 | 10 | 135,766 |
Tags: dp, greedy, sortings
Correct Solution:
```
n,bi=input().split()
n=int(n)
bi=int(bi)
a=list(map(int, input().split()))
cnt1=0
cnt2=0
i=0
b=[]
flag=0
while i<len(a):
if a[i]%2==0:
cnt1+=1
else:
cnt2+=1
if cnt1==cnt2:
if i==len(a)-1:
flag=1
else:
b.append(abs(a[i+1]-a[i]))
i+=1
if flag==0:
print(0)
else:
b.sort()
val=0
cnt=0
i=0
while i<len(b):
if val+b[i]<=bi:
cnt+=1
val+=b[i]
else:
break
i+=1
print(cnt)
'''else:
subset=[]
for i in range(len(b)+1):
subset.append([])
for i in range(len(b)+1):
for j in range(bi+1):
subset[i].append(0)
for i in range(len(b)+1):
subset[i][0]=1
for i in range(1,len(b)+1):
for j in range(1,bi+1):
if j<b[i-1]:
subset[i][j]=subset[i-1][j]
else:
subset[i][j]=subset[i-1][j] || subset[i-1][j-b[j-1]]
i=bi
while i>=0:
if subset[len(b)][i]==1:
print(bi-i)
break
i-=1'''
``` | output | 1 | 67,883 | 10 | 135,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins. | instruction | 0 | 67,884 | 10 | 135,768 |
Tags: dp, greedy, sortings
Correct Solution:
```
n, b = list(map(int, input().split()))
a = list(map(int, input().split()))
balance = [0] * n
for i in range(1, n):
if a[i - 1] % 2 == 0:
balance[i] = balance[i - 1] + 1
else:
balance[i] = balance[i - 1] - 1
s = [10000]
for i in range(1, n):
if balance[i] == 0:
s.append(abs(a[i] - a[i - 1]))
s.sort()
c = 0
ans = 0
for i in s:
c += i
if c > b:
print(ans)
break
else:
ans += 1
``` | output | 1 | 67,884 | 10 | 135,769 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins. | instruction | 0 | 67,885 | 10 | 135,770 |
Tags: dp, greedy, sortings
Correct Solution:
```
n,b = list(map(int,input().split()))
a = list(map(int,input().split()))
costs = []
balance = [False]*n
odds, evens = 0,0
for x in range(n):
if a[x] %2 == 0:
evens+=1
else:
odds+=1
i,j =0,0
for x in range(n-1):
if a[x] %2 == 0:
i+=1
else:
j+=1
if i==j and (evens-i)==(odds-j):
balance[x] = True
for x in range(n-1):
if balance[x]:
costs.append(abs(a[x]-a[x+1]))
costSum = 0
costs = sorted(costs)
ans = 0
for x in costs:
if costSum + x <= b:
costSum+=x
ans+=1
print(ans)
``` | output | 1 | 67,885 | 10 | 135,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins.
Submitted Solution:
```
buf=input()
m=buf.split()
n=int(m[0])
B=int(m[1])
a=[]
N=[]
C=[]
arr=input()
a=arr.split(' ')
for i in range(0,n):
a[i]=int(a[i])
if a[0]%2==0:
N.append(0)
C.append(1)
else:
N.append(1)
C.append(0)
for i in range(1,n):
if a[i]%2==0:
N.append(N[i-1])
C.append(C[i-1]+1)
else:
N.append(N[i-1]+1)
C.append(C[i-1])
k=0
s=0
c=[]
for i in range(1,n-1,2):
if C[i]==N[i]:
c.append(abs(a[i+1]-a[i]))
c.sort()
for i in c:
if(s+i<=B):
k=k+1
s+=i
print(k)
``` | instruction | 0 | 67,886 | 10 | 135,772 |
Yes | output | 1 | 67,886 | 10 | 135,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins.
Submitted Solution:
```
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
sys.setrecursionlimit(10000)
def read_int():
return int(input())
def read_int_n():
return list(map(int, input().split()))
def read_float():
return float(input())
def read_float_n():
return list(map(float, input().split()))
def read_str():
return input()
def read_str_n():
return list(map(str, input().split()))
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, 'sec')
return ret
return wrap
@mt
def slv(N, B, A):
ans = 0
# error_print(A)
A = [[(-1 if a%2 == 1 else 1, a) for a in A]]
# error_print(A)
while True:
div_cnd = []
for i, a in enumerate(A):
s = 0
for j in range(1, len(a)):
if sum(map(lambda x:x[0], a[:j])) == 0 and sum(map(lambda x:x[0], a[j:])) == 0:
div_cnd.append((abs(a[j][1] - a[j-1][1]), i, j))
if len(div_cnd) == 0:
break
d = min(div_cnd, key=lambda x: x[0])
if d[0] <= B:
B -= d[0]
a = A.pop(d[1])
A.append(a[:d[2]])
A.append(a[d[2]:])
ans += 1
else:
break
return ans
def main():
N, B = read_int_n()
A = read_int_n()
# N = 100
# B = 100
# A = [random.randint(1, 100) for _ in range(N)]
print(slv(N, B, A))
if __name__ == '__main__':
main()
``` | instruction | 0 | 67,887 | 10 | 135,774 |
Yes | output | 1 | 67,887 | 10 | 135,775 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins.
Submitted Solution:
```
n,k=list(map(int,input().split()))
m=list(map(int,input().split()))
a=[]
p=0
for i in range(n):
if(m[i]%2):
p+=1
elif(m[i]%2==0):
p-=1
if(p==0 and i!=n-1):
a.append(abs(m[i+1]-m[i]))
if p==0:
a.sort()
l=0
for i in range(len(a)):
if(k>=a[i]):
k-=a[i]
l+=1
else:
break
print(l)
else:
print(0)
``` | instruction | 0 | 67,888 | 10 | 135,776 |
Yes | output | 1 | 67,888 | 10 | 135,777 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins.
Submitted Solution:
```
n,k=map(int,input().split())
l=list(map(int,input().split()))
cost=0
c=0
ans=0
sumc=[]
for i in range(n-2):
if l[i]%2==1:
c+=1
else:
c-=1
if c==0:
cost=abs(l[i+1]-l[i])
if cost<=k:
sumc.append(cost)
sumc.sort()
su=0
for i in range(len(sumc)):
su+=sumc[i]
if su<=k:
ans+=1
print(ans)
``` | instruction | 0 | 67,889 | 10 | 135,778 |
Yes | output | 1 | 67,889 | 10 | 135,779 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins.
Submitted Solution:
```
n,b=map(int,input().split())
lst=list(map(int,input().split()))
c=[]
t=0
for i in range(len(lst)-1):
if lst[i]%2==0:
t+=1
else:
t-=1
if t==0:
c.append(lst[i+1]-lst[i])
c.sort()
j=0
while j<len(c) and b>=0:
b-=c[j]
if b>=0:
j+=1
print(j)
``` | instruction | 0 | 67,890 | 10 | 135,780 |
No | output | 1 | 67,890 | 10 | 135,781 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins.
Submitted Solution:
```
n,b=[int(i) for i in input().split()]
lst=[int(i) for i in input().split()]
co=0
ce=0
cost=[]
for i in range(n):
if lst[i]%2==0:
co+=1
else:ce+=1
if co==ce and i<n-1:
cost.append(abs(lst[i+1] - lst[i]))
cost.sort()
print(cost)
sum=0
i=0
while(sum<=b and i<len(cost)):
sum+=cost[i]
i+=1
if sum>b:print(i-1)
else:print(i)
``` | instruction | 0 | 67,891 | 10 | 135,782 |
No | output | 1 | 67,891 | 10 | 135,783 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are a lot of things which could be cut β trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] β two cuts β [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.
Input
First line of the input contains an integer n (2 β€ n β€ 100) and an integer B (1 β€ B β€ 100) β the number of elements in the sequence and the number of bitcoins you have.
Second line contains n integers: a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β elements of the sequence, which contains the equal number of even and odd numbers
Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.
Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1 + 1 = 2 bitcoins.
Submitted Solution:
```
n,b = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
even = 0
odd = 0
cuts = []
for i in range(0,n):
if a[i]%2 == 0:
even+=1
else:
odd += 1
if even == odd:
cuts.append(i)
even = 0
odd = 0
cuts.pop()
print (cuts)
l = len(cuts)
B= 0
cost = []
for i in range(0,l):
B+= abs(a[cuts[i]]-a[cuts[i]+1])
cost.append(abs(a[cuts[i]]-a[cuts[i]+1]))
print(B)
L = l
r = l-1
l = 0
while B > b:
if cost[l] > cost[r]:
B -= cost[l]
l += 1
else:
B -= cost[r]
r -= 1
print(cost[l:r])
if l>r :
print("L",L)
break
L -= 1
print(B)
if l>r:
print(0)
else:
print(L)
``` | instruction | 0 | 67,892 | 10 | 135,784 |
No | output | 1 | 67,892 | 10 | 135,785 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.