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.
The only difference between easy and hard versions is constraints.
The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show, the episode of which will be shown in i-th day.
The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately.
How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows d (1 β€ d β€ n) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of d consecutive days in which all episodes belong to the purchased shows.
Input
The first line contains an integer t (1 β€ t β€ 100) β the number of test cases in the input. Then t test case descriptions follow.
The first line of each test case contains three integers n, k and d (1 β€ n β€ 100, 1 β€ k β€ 100, 1 β€ d β€ n). The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show that is broadcasted on the i-th day.
It is guaranteed that the sum of the values ββof n for all test cases in the input does not exceed 100.
Output
Print t integers β the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for d consecutive days. Please note that it is permissible that you will be able to watch more than d days in a row.
Example
Input
4
5 2 2
1 2 1 2 1
9 3 3
3 3 3 2 2 2 1 1 1
4 10 4
10 8 6 4
16 9 8
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Output
2
1
4
5
Note
In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show 1 and on show 2. So the answer is two.
In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show.
In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows.
In the fourth test case, you can buy subscriptions to shows 3,5,7,8,9, and you will be able to watch shows for the last eight days.
Submitted Solution:
```
mini = []
q = int(input())
for i in range(q):
mini.append(1000000)
for i in range(q):
n, k, d = map(int, input().split())
numbers = [int(num) for num in input().split()]
kol = []
h = 0
for j in range(k):
kol.append(0)
for j in range(n - d+1):
for u in range(j, j + d):
kol[numbers[u] - 1] += 1
for u in range(len(kol)):
if kol[u] > 0:
h += 1
kol[u] = 0
if mini[i] > h:
mini[i] = h
h = 0
for i in range(q):
print(mini[i])
``` | instruction | 0 | 27,505 | 10 | 55,010 |
Yes | output | 1 | 27,505 | 10 | 55,011 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is constraints.
The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show, the episode of which will be shown in i-th day.
The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately.
How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows d (1 β€ d β€ n) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of d consecutive days in which all episodes belong to the purchased shows.
Input
The first line contains an integer t (1 β€ t β€ 100) β the number of test cases in the input. Then t test case descriptions follow.
The first line of each test case contains three integers n, k and d (1 β€ n β€ 100, 1 β€ k β€ 100, 1 β€ d β€ n). The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show that is broadcasted on the i-th day.
It is guaranteed that the sum of the values ββof n for all test cases in the input does not exceed 100.
Output
Print t integers β the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for d consecutive days. Please note that it is permissible that you will be able to watch more than d days in a row.
Example
Input
4
5 2 2
1 2 1 2 1
9 3 3
3 3 3 2 2 2 1 1 1
4 10 4
10 8 6 4
16 9 8
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Output
2
1
4
5
Note
In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show 1 and on show 2. So the answer is two.
In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show.
In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows.
In the fourth test case, you can buy subscriptions to shows 3,5,7,8,9, and you will be able to watch shows for the last eight days.
Submitted Solution:
```
a=int(input())
for i in range(a):
n,k,d=[int(s) for s in input().split()]
array=[int(s) for s in input().split()]
best=-1
new=-1
for j in range(n):
t=0
c=set()
for j1 in range(j,n):
c.add(array[j1])
t+=1
if t>d:
break
if t==d:
new=len(c)
if new<best or best==-1:
best=new
new=-1
else:
new=-1
break
print(best)
``` | instruction | 0 | 27,506 | 10 | 55,012 |
Yes | output | 1 | 27,506 | 10 | 55,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is constraints.
The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show, the episode of which will be shown in i-th day.
The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately.
How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows d (1 β€ d β€ n) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of d consecutive days in which all episodes belong to the purchased shows.
Input
The first line contains an integer t (1 β€ t β€ 100) β the number of test cases in the input. Then t test case descriptions follow.
The first line of each test case contains three integers n, k and d (1 β€ n β€ 100, 1 β€ k β€ 100, 1 β€ d β€ n). The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show that is broadcasted on the i-th day.
It is guaranteed that the sum of the values ββof n for all test cases in the input does not exceed 100.
Output
Print t integers β the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for d consecutive days. Please note that it is permissible that you will be able to watch more than d days in a row.
Example
Input
4
5 2 2
1 2 1 2 1
9 3 3
3 3 3 2 2 2 1 1 1
4 10 4
10 8 6 4
16 9 8
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Output
2
1
4
5
Note
In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show 1 and on show 2. So the answer is two.
In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show.
In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows.
In the fourth test case, you can buy subscriptions to shows 3,5,7,8,9, and you will be able to watch shows for the last eight days.
Submitted Solution:
```
#sys.stdout=open("output.txt", 'w')
#sys.stdout.write("Yes" + '\n')
#from sys import stdin
#input=stdin.readline
#a = sorted([(n, i) for i, n in enumerate(map(int, input().split()))])
# from collections import Counter
# import sys
#s="abcdefghijklmnopqrstuvwxyz"
#n=int(input())
#n,k=map(int,input().split())
#arr=list(map(int,input().split()))
#arr=list(map(int,input().split
'''n=int(input())
abc=[]
for i in range(n):
abc.append(list(map(int,input().split())))
dp=[[-1,-1,-1] for i in range(n)]
for i in range(n):
if i==0:
for j in range(3):
dp[i][j]=abc[i][j]
else:
dp[i][0]=max(dp[i-1][1]+abc[i][0],dp[i-1][2]+abc[i][0])
dp[i][1]=max(dp[i-1][2]+abc[i][1],dp[i-1][0]+abc[i][1])
dp[i][2]=max(dp[i-1][0]+abc[i][2],dp[i-1][1]+abc[i][2])
print(max(dp[n-1]))'''
from collections import Counter
for _ in range(int(input())):
n,k,d= map(int, input().split())
arr=list(map(int,input().split()))
ans=len(set(arr[:d]))
#print(ans)
q=Counter(arr[:d])
#print(q)
cnt=0
m=d
for i in range(d,n):
if arr[i] not in q:
q[arr[i]]=1
ans+=1
elif q[arr[i]]>0:
q[arr[i]]+=1
elif q[arr[i]]==0:
q[arr[i]]=1
ans+=1
q[arr[cnt]]-=1
if q[arr[cnt]]==0:
ans-=1
#print("ans",ans)
cnt+=1
m=min(m,ans)
print(min(m,ans))
``` | instruction | 0 | 27,507 | 10 | 55,014 |
No | output | 1 | 27,507 | 10 | 55,015 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is constraints.
The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show, the episode of which will be shown in i-th day.
The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately.
How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows d (1 β€ d β€ n) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of d consecutive days in which all episodes belong to the purchased shows.
Input
The first line contains an integer t (1 β€ t β€ 100) β the number of test cases in the input. Then t test case descriptions follow.
The first line of each test case contains three integers n, k and d (1 β€ n β€ 100, 1 β€ k β€ 100, 1 β€ d β€ n). The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show that is broadcasted on the i-th day.
It is guaranteed that the sum of the values ββof n for all test cases in the input does not exceed 100.
Output
Print t integers β the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for d consecutive days. Please note that it is permissible that you will be able to watch more than d days in a row.
Example
Input
4
5 2 2
1 2 1 2 1
9 3 3
3 3 3 2 2 2 1 1 1
4 10 4
10 8 6 4
16 9 8
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Output
2
1
4
5
Note
In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show 1 and on show 2. So the answer is two.
In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show.
In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows.
In the fourth test case, you can buy subscriptions to shows 3,5,7,8,9, and you will be able to watch shows for the last eight days.
Submitted Solution:
```
t=int(input())
for i in range(t):
n,k,d=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
minn=len(a[:d])
for i in range(1,n-d+1):
print(a[i:i+d],len(set(a[i:i+d])))
s=len(set(a[i:i+d]))
if s<minn:
minn=s
print(minn)
``` | instruction | 0 | 27,508 | 10 | 55,016 |
No | output | 1 | 27,508 | 10 | 55,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is constraints.
The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show, the episode of which will be shown in i-th day.
The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately.
How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows d (1 β€ d β€ n) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of d consecutive days in which all episodes belong to the purchased shows.
Input
The first line contains an integer t (1 β€ t β€ 100) β the number of test cases in the input. Then t test case descriptions follow.
The first line of each test case contains three integers n, k and d (1 β€ n β€ 100, 1 β€ k β€ 100, 1 β€ d β€ n). The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show that is broadcasted on the i-th day.
It is guaranteed that the sum of the values ββof n for all test cases in the input does not exceed 100.
Output
Print t integers β the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for d consecutive days. Please note that it is permissible that you will be able to watch more than d days in a row.
Example
Input
4
5 2 2
1 2 1 2 1
9 3 3
3 3 3 2 2 2 1 1 1
4 10 4
10 8 6 4
16 9 8
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Output
2
1
4
5
Note
In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show 1 and on show 2. So the answer is two.
In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show.
In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows.
In the fourth test case, you can buy subscriptions to shows 3,5,7,8,9, and you will be able to watch shows for the last eight days.
Submitted Solution:
```
t = int(input())
for i in range(t):
n, k, d = map(int, input().split())
a = list(map(int, input().split()))
serials = [0] * k
ans = float('inf')
for i in range(len(a)):
serials[a[i] - 1] += 1
if i >= d - 1:
ans = min(ans, sum([1 if x > 0 else 0 for x in serials]))
if i > d - 1:
serials[a[i - d] - 1] -= 1
ans = min(ans, sum([1 if x > 0 else 0 for x in serials]))
print(ans)
``` | instruction | 0 | 27,509 | 10 | 55,018 |
No | output | 1 | 27,509 | 10 | 55,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is constraints.
The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show, the episode of which will be shown in i-th day.
The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately.
How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows d (1 β€ d β€ n) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of d consecutive days in which all episodes belong to the purchased shows.
Input
The first line contains an integer t (1 β€ t β€ 100) β the number of test cases in the input. Then t test case descriptions follow.
The first line of each test case contains three integers n, k and d (1 β€ n β€ 100, 1 β€ k β€ 100, 1 β€ d β€ n). The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show that is broadcasted on the i-th day.
It is guaranteed that the sum of the values ββof n for all test cases in the input does not exceed 100.
Output
Print t integers β the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for d consecutive days. Please note that it is permissible that you will be able to watch more than d days in a row.
Example
Input
4
5 2 2
1 2 1 2 1
9 3 3
3 3 3 2 2 2 1 1 1
4 10 4
10 8 6 4
16 9 8
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Output
2
1
4
5
Note
In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show 1 and on show 2. So the answer is two.
In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show.
In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows.
In the fourth test case, you can buy subscriptions to shows 3,5,7,8,9, and you will be able to watch shows for the last eight days.
Submitted Solution:
```
def uniq(a):
b = []
for i in range(len(a)):
if b.count(a[i]) == 0:
b.append(a[i])
return b
t = int(input())
for i in range(t):
n, k, d = [int(x) for x in input().split()]
seriali = [int(x) for x in input().split()]
nadokupit = seriali[0:d]
for j in range(d + 1):
if len(uniq(seriali[j:j+d])) < len(nadokupit):
nadokupit = uniq(seriali[j:j+d])
print(len(nadokupit))
``` | instruction | 0 | 27,510 | 10 | 55,020 |
No | output | 1 | 27,510 | 10 | 55,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles. | instruction | 0 | 27,681 | 10 | 55,362 |
Tags: binary search, greedy, implementation, sortings, two pointers
Correct Solution:
```
import sys
import math
import collections
import heapq
import decimal
input=sys.stdin.readline
n=int(input())
l=[]
for i in range(n):
a,b=(int(i) for i in input().split())
l.append((b,a))
l.sort()
s=0
for i in range(n):
s+=l[i][1]
c=0
for i in range(n-1,-1,-1):
k=s-l[i][0]
c+=max(min(k,l[i][1]),0)
s-=max(min(k,l[i][1]),0)
print(s*2+c)
``` | output | 1 | 27,681 | 10 | 55,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles. | instruction | 0 | 27,682 | 10 | 55,364 |
Tags: binary search, greedy, implementation, sortings, two pointers
Correct Solution:
```
t=int(input())
a=[]
for T in range(t):
b=list(map(int,input().split()))
a.append(b)
h=sorted(a,key=lambda x: (x[1]))
s=0
j=0
i=len(h)-1
cost=0
while(j<=i):
k=h[i][0]
if(k+s<h[j][1]):
s=s+k
i=i-1
cost=cost+2*k
elif(k+s == h[j][1]):
s=s+k
i=i-1
cost=cost+2*k
while(s >= h[j][1] and j<i):
cost=cost+h[j][0]
s=s+h[j][0]
j=j+1
if(j>i):
break
else:
if(j!=i):
his=h[j][1]-s
cost=cost+his*2
s=s+his
h[i][0]=k-his
while(s>=h[j][1] and j<i):
cost=cost+h[j][0]
s=s+h[j][0]
j=j+1
if(j>i):
break
else:
if(s+k>h[i][1]):
hi=s+k-h[i][1]
if(hi>=k):
cost=cost+k
else:
cost=cost+hi
cost=cost+(k-hi)*2
j=j+1
i=i-1
print(cost)
``` | output | 1 | 27,682 | 10 | 55,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles. | instruction | 0 | 27,683 | 10 | 55,366 |
Tags: binary search, greedy, implementation, sortings, two pointers
Correct Solution:
```
N = int(input())
A = [0] * N
for i in range(N):
A[i] = list(map(int, input().split()))
A.sort(key=lambda a: a[1])
low = 0
high = N - 1
bought = 0
cost = 0
while low <= high:
if bought >= A[low][1]: # buy from min Bi to get full discount
bought += A[low][0]
cost += A[low][0]
# print(l, 1, A[l])
A[low][0] = 0
low += 1
elif A[low][1] - bought >= A[high][0]:
bought += A[high][0]
cost += 2 * A[high][0]
# print(h, 2, A[h])
A[high][0] = 0
high -= 1
else:
need = A[low][1] - bought
bought += need
cost += 2 * need
# print(h, 2, need)
A[high][0] -= need
assert all([A[i][0] == 0 for i in range(N)])
print(cost)
``` | output | 1 | 27,683 | 10 | 55,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles. | instruction | 0 | 27,684 | 10 | 55,368 |
Tags: binary search, greedy, implementation, sortings, two pointers
Correct Solution:
```
n=int(input())
l=[]
for i in range(n):
x,y=map(int,input().split())
l.append([x,y])
l.sort(key=lambda l:l[1])
i=0;j=n-1
ans=0
x=0
while i<=j:
if x>=l[i][1]:
ans += l[i][0]
x += l[i][0]
i += 1
else:
if x+l[j][0]<=l[i][1]:
ans+=2*(l[j][0])
x+=l[j][0]
j-=1
else:
l[j][0] -= l[i][1] - x
ans+=2*(l[i][1]-x)
x+=(l[i][1]-x)
print(ans)
``` | output | 1 | 27,684 | 10 | 55,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles. | instruction | 0 | 27,685 | 10 | 55,370 |
Tags: binary search, greedy, implementation, sortings, two pointers
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#######################################
from collections import *
from collections import deque
from operator import itemgetter , attrgetter
from decimal import *
import bisect
import math
import heapq as hq
#import sympy
MOD=10**9 +7
def is_prime(n):
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
r = int(n**0.5)
# since all primes > 3 are of the form 6n Β± 1
# start with f=5 (which is prime)
# and test f, f+2 for being prime
# then loop by 6.
f = 5
while f <= r:
if n % f == 0: return False
if n % (f+2) == 0: return False
f += 6
return True
def pow(a,b,m):
ans=1
while b:
if b&1:
ans=(ans*a)%m
b//=2
a=(a*a)%m
return ans
#vis=[]
#graph=[]
def ispalindrome(s):
if s[:]==s[::-1]:
return 1
return 0
dp=[]
limit=[]
v=[]
def dpdfs(u,t=-1):
dp[0][u]=0
dp[1][u]=0
for i in v[u]:
if i==t:
continue
if dp[1][i]==-1:
dpdfs(i,u)
dp[0][u]+=max(abs(limit[0][u]-limit[1][i])+dp[1][i],abs(limit[0][u]-limit[0][i])+dp[0][i])
dp[1][u] += max(abs(limit[1][u] - limit[1][i]) + dp[1][i], abs(limit[1][u] - limit[0][i]) + dp[0][i])
vis=[]
def dfs(v):
if vis[v]==0:
return
vis[v]=0
for vv in g[v]:
dfs(vv)
n = int(input())
l = []
for _ in range(n):
a, b = map(int, input().split())
l.append([b, a])
l.sort()
ans = 0
count = 0
i=0
j=len(l)-1
while i<=j:
if count >= l[i][0]:
count += l[i][1]
ans += l[i][1]
i+=1
continue
if count + l[j][1] <= l[i][0]:
count += l[j][1]
ans += 2 * l[j][1]
j-=1
continue
ans += 2 * (l[i][0] - count)
l[j][1] = l[j][1] - (l[i][0] - count)
count = l[i][0]
print(ans)
``` | output | 1 | 27,685 | 10 | 55,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles. | instruction | 0 | 27,686 | 10 | 55,372 |
Tags: binary search, greedy, implementation, sortings, two pointers
Correct Solution:
```
a=sorted([*map(int,s.split())][::-1]for s in[*open(0)][1:])
i=c=r=0
j=len(a)-1
while i<=j:
x=_,y=a[j];d=min(a[i][0]-c,y)
if d>0:x[1]-=d;r+=d;j-=d==y
else:d=a[i][1];i+=1
c+=d
print(c+r)
``` | output | 1 | 27,686 | 10 | 55,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles. | instruction | 0 | 27,687 | 10 | 55,374 |
Tags: binary search, greedy, implementation, sortings, two pointers
Correct Solution:
```
# Author Name: Ajay Meena
# Codeforce : https://codeforces.com/profile/majay1638
import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout
# -------------- INPUT FUNCTIONS ------------------
def get_ints_in_variables(): return map(
int, sys.stdin.readline().strip().split())
def get_int(): return int(sys.stdin.readline())
def get_ints_in_list(): return list(
map(int, sys.stdin.readline().strip().split()))
def get_list_of_list(n): return [list(
map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def get_string(): return sys.stdin.readline().strip()
# -------- SOME CUSTOMIZED FUNCTIONS-----------
def myceil(x, y): return (x + y - 1) // y
# -------------- SOLUTION FUNCTION ------------------
def Solution(arr, n):
# Write Your Code Here
arr = sorted(arr, key=lambda x: x[1])
l = 0
r = n-1
ans = 0
itmBought = 0
while l <= r:
if arr[l][1] > itmBought:
need = min(arr[l][1]-itmBought, arr[r][0])
arr[r][0] -= need
itmBought += need
ans += (2*need)
if arr[r][0] == 0:
r -= 1
else:
itmBought += arr[l][0]
ans += arr[l][0]
l += 1
print(ans)
def main():
# Take input Here and Call solution function
n = get_int()
arr = [get_ints_in_list() for _ in range(n)]
Solution(arr, n)
# calling main Function
if __name__ == '__main__':
main()
``` | output | 1 | 27,687 | 10 | 55,375 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles. | instruction | 0 | 27,688 | 10 | 55,376 |
Tags: binary search, greedy, implementation, sortings, two pointers
Correct Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
products = []
for _ in range(n):
products.append([int(x) for x in stdin.readline().split()])
products.sort(key = lambda x:x[1], reverse = True)
answer = 0
volume = 0
front_pointer = 0
back_pointer = n-1
while front_pointer < back_pointer:
if volume + products[front_pointer][0] >= products[back_pointer][1]:
gap = max(0,(products[back_pointer][1] - volume))
products[front_pointer][0] -= gap
answer += gap*2 + products[back_pointer][0]
volume += gap + products[back_pointer][0]
back_pointer -= 1
else:
answer += products[front_pointer][0] * 2
volume += products[front_pointer][0]
front_pointer += 1
if volume + products[front_pointer][0] >= products[back_pointer][1]:
gap = max(0,(products[back_pointer][1] - volume))
answer += gap*2 + products[front_pointer][0] - gap
else:
answer += products[front_pointer][0] * 2
stdout.write(str(answer)+'\n')
``` | output | 1 | 27,688 | 10 | 55,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
Submitted Solution:
```
import sys
n=int(input())
items=[]
total_items=0
for _ in range(n):
c,l=list(map(int,sys.stdin.readline().strip().split()))
total_items+=c
items.append((c,l))
items.sort(key= lambda x:x[1],reverse=True)
total_cost=2*total_items
pos=0
while pos<total_items:
count,start=items.pop()
if start<total_items:
if start>pos:
pos=start
pos+=count
total_cost-=count
if pos>total_items:
total_cost+=(pos-(total_items))
else:
break
print(total_cost)
``` | instruction | 0 | 27,689 | 10 | 55,378 |
Yes | output | 1 | 27,689 | 10 | 55,379 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
Submitted Solution:
```
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import defaultdict as dd
# sys.setrecursionlimit(100000000)
flush = lambda: stdout.flush()
stdstr = lambda: stdin.readline()
stdint = lambda: int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdmap = lambda: map(int, stdstr().split())
stdarr = lambda: list(map(int, stdstr().split()))
mod = 1000000007
n = stdint()
pairs = []
for _ in range(n):
pairs.append(list(stdmap()))
pairs.sort(key = lambda x: (x[1], x[0]))
bought = 0
cost = 0
i,j = 0, n-1
while(i <= j):
if(bought >= pairs[i][1]):
bought += pairs[i][0]
cost += pairs[i][0]
pairs[i][0] = 0
i += 1
else:
needed = pairs[i][1]-bought
cost += min(needed, pairs[j][0])*2
bought += min(needed, pairs[j][0])
pairs[j][0] -= min(needed, pairs[j][0])
if(pairs[j][0] == 0):
j -= 1
print(cost)
``` | instruction | 0 | 27,690 | 10 | 55,380 |
Yes | output | 1 | 27,690 | 10 | 55,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
N = int(input())
A = [0] * N
B = [0] * N
for i in range(N):
A[i], B[i] = map(int, input().split())
indices = list(range(N))
indices.sort(key=lambda i: B[i])
low = 0
high = N - 1
bought = 0
cost = 0
while low <= high:
l = indices[low]
h = indices[high]
if bought >= B[l]: # buy from min Bi to get full discount
bought += A[l]
cost += A[l]
A[l] = 0
low += 1
else:
buy = min(B[l] - bought, A[h])
bought += buy
A[h] -= buy
cost += 2 * buy
if A[h] == 0:
high -= 1
assert sum(A) == 0
print(cost)
``` | instruction | 0 | 27,691 | 10 | 55,382 |
Yes | output | 1 | 27,691 | 10 | 55,383 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
Submitted Solution:
```
import os , io , sys,time, collections , math , pprint , itertools as it , operator as op , bisect as bs ,functools as fn
maxx , localsys , mod = float('inf'), 0 , int(1e9 + 7)
nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r)
ceil = lambda n , x: (n+x -1 )//x
osi, oso = '/home/priyanshu/Documents/cp/input.txt','/home/priyanshu/Documents/cp/output.txt'
if os.path.exists(osi):
sys.stdin = open(osi, 'r') ; sys.stdout = open(oso, 'w')
input = sys.stdin.readline
def maps():return map(int , input().split())
"""for __ in range(int(input())):
n , k = maps() ; k-=1
collisions = k // (n//2)
print((k + (n%2)*collisions)%n +1)
"""
"""for _ in range(int(input())):
q, d = maps() ; a = list(maps()) ; mx = 10*d ; sd = str(d)
for i in a:
if sd in str(i) or i >= mx:
print('YES') ; continue
while i>=0 and sd not in str(i):
i-=d
print('YES' if i >=0 else 'NO')
"""
"""
n ,q , k = maps() ; a= list(maps())
for _ in range(q):
l ,r = maps()
print(2*((a[r-1] - a[l-1] + 1) - (r - l + 1)) + k - a[r-1] + a[l-1]-1)
# 2 * ((a[r] - a[l] + 1) - (r -l + 1)) ---> since , every element != a[i] , will have to choices to seat
# itself in the array , thus multiplying by two and the first element can chose any number except itself
# and the last element can k - a[r] elements since all of them will be greater than the previous element
"""
"""
n = int(input()) ; k = n//2
print(2*(k + 1)*(k+2)) if n % 2 else print((k+1)**2)
For these types of problems you need to draw the whole thing and observe from there
*be patient*
"""
"""
for _ in range(int(input())):
n ,r = maps() ; a = sorted((j , i+1) for i , j in enumerate(maps())) ; rem ,ans = r , []
for i in range(n-1 ,-1 ,-1):
if rem >= a[i][0]:rem-=a[i][0] ; ans.append(a[i][1])
print(len(ans) , '\n' , *ans) if (r-rem) >= math.ceil(r/2) and (r-rem) <= r else print(-1)
"""
"""
def func(arr ,n):
ans =1 ; prev = arr[0]
for i in range(1, n):
if arr[i] - 1 != prev:
ans+=1
prev = arr[i]
return ans
for _ in range(int(input())):
n = int(input()) ; s = input().rstrip('\n') ; A,B=[],[]
for i in range(n):
if i % 2 and s[i] != '0':A.append(i)
if i % 2 and s[i] != '1':B.append(i)
if not i % 2 and s[i] != '1':A.append(i)
if not i % 2 and s[i] != '0':B.append(i)
if not A or not B:print(0);continue
print(min(func(A,len(A)) , func(B, len(B)) ))
"""
"""
for _ in range(int(input())):
A ,B = maps() ; a = input().strip('\n') ; ans , cnt = 0 , maxx
for i in a:
if i == '0':
cnt+=1
else:
ans += min(A , cnt*B) ; cnt=0
print(ans)
"""
"""
for _ in range(int(input())):
n = int(input()) ; a = list(maps()) ; b = [i+1 for i in range(n) if a[i] != a[0]]
if not b:print('NO');continue
[print(i+1 ,1 if a[i] != a[0] else b[0]) for i in range(n)]
"""
"""for _ in range(int(input())):
s = input().rstrip('\n') ; st, i ,n,t = [] , 0 , len(s),0
while i < n:
if s[i] =='B' and st and st[-1] =='A':st.pop()
else:st.append(s[i])
i+=1
if st:
prev = st[0]
for i in st:
if i == prev and i == 'B':t+=1
prev = i
print(len(st) - (t//2)*2 )
"""
n = int(input()) ; s,a =0 ,[]
for __ in range(n):
u ,v = maps() ; s+=u
a.append((u,v))
a.sort(key= lambda x :x[1] , reverse=True) ;ans = 2*s
for i,j in a:
if j >= s:continue
ans -= min(i, s-j) ;s -= min(i, s-j)
print(ans)
"""
starting from the biggest bj , if it is greater than needed then continue (nothing can be done since we can't get a discount),
but if bj < need , then ai(items needed to buy this specific product) and need - bi (items needed to buy after getting a discount)
"""
``` | instruction | 0 | 27,692 | 10 | 55,384 |
Yes | output | 1 | 27,692 | 10 | 55,385 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
Submitted Solution:
```
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import defaultdict as dd
# sys.setrecursionlimit(100000000)
flush = lambda: stdout.flush()
stdstr = lambda: stdin.readline()
stdint = lambda: int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdmap = lambda: map(int, stdstr().split())
stdarr = lambda: list(map(int, stdstr().split()))
mod = 1000000007
n = stdint()
pairs = []
for _ in range(n):
pairs.append(list(stdmap()))
pairs.sort(key = lambda x: (x[1], -1*x[0]))
bought = 0
cost = 0
i,j = 0, n-1
while(i <= j):
if(i == j):
for2 = min(max(pairs[i][1]-bought, 0), pairs[i][0])
for1 = pairs[i][0]-for2
cost += for2*2 + for1
i += 1
else:
l = pairs[i]
r = pairs[j]
needed = max(l[1]-bought, 0)
if(needed == 0):
bought += l[0]
cost += l[0]
pairs[i][0] = 0
i += 1
continue
fromJ = min(needed, r[0])
atJFor2 = max(r[1]-bought, 0)
if(fromJ <= atJFor2):
bought += fromJ
cost += fromJ*2
pairs[j][0] -= fromJ
if(bought >= needed):
cost += pairs[i][0]
bought += pairs[i][0]
pairs[i][0] = 0
i += 1
if(pairs[j][0] == 0):
j -= 1
else:
bought += atJFor2
cost += 2*atJFor2
rem = r[0]-atJFor2
cost += rem
pairs[j][0] = 0
if(bought >= needed):
bought += l[0]
cost += l[0]
i += 1
j -= 1
else:
j -= 1
print(cost)
``` | instruction | 0 | 27,693 | 10 | 55,386 |
No | output | 1 | 27,693 | 10 | 55,387 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 22 10:47:41 2021
@author: hienpham
"""
import os
import math
import sys
parse_input = lambda: sys.stdin.readline().rstrip("\r\n")
def func(n_cases, shop_list, total_items):
total_pay = 0
n_items = 0
shop_list = sorted(shop_list)
i = 0
j = n_cases - 1
while n_items < total_items:
n_buy = shop_list[j][1]
req_disc = shop_list[i][0]
buy_more = req_disc - n_items
if n_items >= req_disc:
total_pay += 1*shop_list[i][1]
n_items += shop_list[i][1]
i += 1
else:
if buy_more > 0 and buy_more < n_buy:
total_pay += 2*buy_more
n_items += buy_more
n_item_disc = n_buy - buy_more
total_pay += 1*n_item_disc
n_items += n_item_disc
j -= 1
else:
total_pay += 2*n_buy
n_items += n_buy
j -= 1
return total_pay
#n_cases = 3
#shop_list = [(7, 2), (8, 2), (2, 1), (4, 2), (8, 1)]
#total_items = 8
#ans = func(n_cases, shop_list, total_items)
def main():
n_cases = int(parse_input())
shop_list = []
total_items = 0
for i in range(n_cases):
a, b = [int(i) for i in parse_input().split()]
shop_list.append((b, a))
total_items += a
print(func(n_cases, shop_list, total_items))
if __name__ == "__main__":
main()
``` | instruction | 0 | 27,694 | 10 | 55,388 |
No | output | 1 | 27,694 | 10 | 55,389 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
Submitted Solution:
```
try:
n = int(input())
arr = []
total = 0
for _ in range(n):
tuples = list(map(int, input().split()))
total += tuples[0]
arr.append(tuples)
arr = list(reversed(sorted(arr, key=lambda item: (item[1], item[0]))))
arr.append([0, 0])
goods_with_original_prize = 0
for i in range(n):
if goods_with_original_prize < arr[i][1] and arr[i][0] < arr[i][1]:
goods_with_original_prize += arr[i][0]
if goods_with_original_prize > arr[i + 1][1]:
goods_with_original_prize -= goods_with_original_prize - arr[i + 1][1]
break
else:
goods_with_original_prize += arr[i][1]
goods_with_discount = total - goods_with_original_prize
print(goods_with_discount * 1 + goods_with_original_prize * 2)
except e:
pass
``` | instruction | 0 | 27,695 | 10 | 55,390 |
No | output | 1 | 27,695 | 10 | 55,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* For every product i there is a discount for experienced buyers: if you buy b_i items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50\% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least a_i items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the number of products.
Each of next n lines contains a product description. Each description consists of two integers a_i and b_i (1 β€ a_i β€ 10^{14}, 1 β€ b_i β€ 10^{14}) β the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.
The sum of all a_i does not exceed 10^{14}.
Output
Output the minimum sum that Lena needs to make all purchases.
Examples
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
Note
In the first example, Lena can purchase the products in the following way:
1. one item of product 3 for 2 rubles,
2. one item of product 1 for 2 rubles,
3. one item of product 1 for 2 rubles,
4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
1. one item of product 1 for 2 rubles,
2. two items of product 2 for 2 rubles for each,
3. one item of product 5 for 2 rubles,
4. one item of product 3 for 1 ruble,
5. two items of product 4 for 1 ruble for each,
6. one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
Submitted Solution:
```
price = 2 # rubles each product
discounted_price = 1 # ruble
class sortable:
def __init__(self, necessary, qfdp) -> None:
self.necessary = necessary
self.quantity_for_discounted_price = qfdp
def __lt__(self, other):
return self.quantity_for_discounted_price < other.quantity_for_discounted_price
def buy(products, quantity, rightmost):
while quantity > 0:
if quantity > products[rightmost].necessary:
quantity -= products[rightmost].necessary
products[rightmost].necessary = 0
rightmost -= 1
products[rightmost].necessary -= quantity
quantity = 0
assert rightmost >= 0
return rightmost
def solve():
n = int(input())
products = [0] * n
for i in range(n):
necessary_quantity, discounted_quantity = [int(x) for x in input().split()]
products[i] = sortable(necessary_quantity, discounted_quantity)
products.sort()
all_products = 0
for p in products:
all_products += p.necessary
if all_products <= products[0].necessary:
return price * all_products
bought, paid, r = 0, 0, n - 1
for i in range(n):
# print(f'{bought} already bought. Discount {products[i].quantity_for_discounted_price}')
if products[i].necessary == 0:
continue
if bought < products[i].quantity_for_discounted_price:
r = buy(products, products[i].quantity_for_discounted_price - bought, r)
paid += price * (products[i].quantity_for_discounted_price - bought)
bought = products[i].quantity_for_discounted_price
# print(f'Paid {paid} before current item')
paid += discounted_price * products[i].necessary
bought += products[i].necessary
# print(f'PAid {discounted_price * products[i].necessary} for current item')
products[i].necessary = 0
return paid
if __name__=='__main__':
print(solve())
``` | instruction | 0 | 27,696 | 10 | 55,392 |
No | output | 1 | 27,696 | 10 | 55,393 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA | instruction | 0 | 27,745 | 10 | 55,490 |
Tags: greedy, math
Correct Solution:
```
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n=int(input())
a,g=0,0
sol=""
for i in range (n):
prva, druga = map(int, input().split())
if a+prva-g<=500:
a+=prva
print("A")
else:
g+=druga
print("G")
``` | output | 1 | 27,745 | 10 | 55,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA | instruction | 0 | 27,746 | 10 | 55,492 |
Tags: greedy, math
Correct Solution:
```
from math import ceil,radians,cos,sin,atan,degrees
from sys import stdin,stdout
def main():
s1,s2 = 0,0
n = int(stdin.readline().strip())
r = ''
for i in range(n):
a,b = [int(i) for i in stdin.readline().split()]
if abs(s1-b-s2) <= 500:
s2+=b;
r+='G'
else:
s1+=a;
r+='A'
stdout.write( "%s" % (r if abs(s1-s2) <= 500 else "-1"))
if __name__ == "__main__":
##sys.stdin = open("in.txt",'r')
##sys.stdout = open("out.txt",'w')
main()
##sys.stdin.close()
##sys.stdout.close()
``` | output | 1 | 27,746 | 10 | 55,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA | instruction | 0 | 27,747 | 10 | 55,494 |
Tags: greedy, math
Correct Solution:
```
sum = 0
s = ''
n = int(input())
for _ in range(n):
a,g = map(int,input().split())
if sum+a <= 500:
sum+=a
s+='A'
else:
sum-=g
s += 'G'
print(s)
``` | output | 1 | 27,747 | 10 | 55,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA | instruction | 0 | 27,748 | 10 | 55,496 |
Tags: greedy, math
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
import time
start_time = time.time()
import collections as col
import math, string
from functools import reduce
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
"""
Greedily try to keep the difference <= 500 at all times?
Suggest A leads G by D, and the current prices are x, 1000-x
If D+x > 500, what does that tell us about D-(1000-x)? That it is > -500
So it's always possible.
If D+x <= 500, just keep rolling with that
"""
def solve():
N = getInt()
ans = []
diff = 0 # A-G
for n in range(N):
a, g = getInts()
if diff:
if diff+a <= 500:
ans.append('A')
diff += a
else:
ans.append('G')
diff -= g
else:
if diff-g >= -500:
ans.append('G')
diff -= g
else:
ans.append('A')
diff += a
return ''.join(ans)
#for _ in range(getInt()):
print(solve())
``` | output | 1 | 27,748 | 10 | 55,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA | instruction | 0 | 27,749 | 10 | 55,498 |
Tags: greedy, math
Correct Solution:
```
import sys
n=int(sys.stdin.readline())
A=[]
B=[]
diff=0
Ans=""
for i in range(n):
x,y=map(int,sys.stdin.readline().split())
if(diff+x<=500):
diff+=x
Ans+="A"
else:
diff-=y
Ans+="G"
if(abs(diff)<=500):
sys.stdout.write(Ans)
else:
print(-1)
``` | output | 1 | 27,749 | 10 | 55,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA | instruction | 0 | 27,750 | 10 | 55,500 |
Tags: greedy, math
Correct Solution:
```
n = int(input())
numa = 0
for i in range(n):
a, b = [int(x) for x in input().split()]
numa += a
if numa < 500:
li = ['A'] * n
print(''.join(li))
else:
i = 0
while numa >= 500:
numa -= 1000
i += 1
li = ['G'] * i + ['A'] * (n - i)
print(''.join(li))
``` | output | 1 | 27,750 | 10 | 55,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA | instruction | 0 | 27,751 | 10 | 55,502 |
Tags: greedy, math
Correct Solution:
```
n = int(input())
k = (sum(int(input().split()[0]) for i in range(n))+499)//1000
print('G'*k+'A'*(n-k))
``` | output | 1 | 27,751 | 10 | 55,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA | instruction | 0 | 27,752 | 10 | 55,504 |
Tags: greedy, math
Correct Solution:
```
from sys import stdin, stdout
def main():
n = int(stdin.readline())
a = []
ind = []
total = 0
for i in range(n):
x, y = map(int, stdin.readline().split())
a.append(x)
total += x
ind.append(i)
ind.sort(key=lambda k: a[k], reverse=True)
ans = ['G'] * n
f = 0
s = n * 1000 - total
ok = False
if abs(f - s) <= 500:
ok = True
else:
for i in ind:
f += a[i]
s -= (1000 - a[i])
ans[i] = 'A'
if abs(s - f) <= 500:
ok = True
break
if ok:
for c in ans:
stdout.write(c)
else:
stdout.write('-1')
stdout.write('\n')
if __name__ == "__main__":
main()
``` | output | 1 | 27,752 | 10 | 55,505 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA
Submitted Solution:
```
n=int(input())
a=0
g=0
k = []
while(n>0):
r1, r2=map(int, input().split(" "))
if(g+r1-a<=500):
g+=r1
k.append("A")
else:
a+=r2
k.append("G")
n-=1
print(''.join(k))
``` | instruction | 0 | 27,753 | 10 | 55,506 |
Yes | output | 1 | 27,753 | 10 | 55,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA
Submitted Solution:
```
n = int(input())
a=0
g=0
ans=[]
for i in range(n):
c,d=map(int,input().split())
if abs(c+a-g)<=500:
ans.append("A")
a+=c
else:
ans.append("G")
g+=d
if abs(a-g)>500:
print("-1")
else:
print(''.join(ans))
``` | instruction | 0 | 27,754 | 10 | 55,508 |
Yes | output | 1 | 27,754 | 10 | 55,509 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA
Submitted Solution:
```
from sys import stdin,stdout
ans=''
x=0
for i in range(int(stdin.readline())):
a=int(stdin.readline().split()[0])
if abs(x-a)>500:
x+=1000-a;ans+='G'
else:
x-=a;ans+='A'
stdout.write(ans)
``` | instruction | 0 | 27,755 | 10 | 55,510 |
Yes | output | 1 | 27,755 | 10 | 55,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA
Submitted Solution:
```
t=int(input())
ta=0
tg=0
order=[]
for _ in range(t):
a,g = map(int,input().split())
if ta>tg:
if (ta+a-tg)<=500:
ta+=a
order.append("A")
else:
tg+=g
order.append("G")
elif ta==tg:
if a>=g:
tg+=g
order.append("G")
else:
ta+=a
order.append("A")
else:
if (tg+g-ta)<=500:
tg+=g
order.append("G")
else:
ta+=a
order.append("A")
if abs(ta-tg)<=500:
print("".join(order))
else:
print(-1)
``` | instruction | 0 | 27,756 | 10 | 55,512 |
Yes | output | 1 | 27,756 | 10 | 55,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA
Submitted Solution:
```
import sys
import math
import collections
import bisect
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip()
for t in range(1):
n=int(input())
cost_a,cost_g=0,0
ans=""
for i in range(n):
a,g=get_ints()
if cost_a>=cost_g:
cost_g+=g
ans+="G"
else:
cost_a+=a
ans+="A"
print(ans)
``` | instruction | 0 | 27,757 | 10 | 55,514 |
No | output | 1 | 27,757 | 10 | 55,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA
Submitted Solution:
```
n=int(input())
for i in range(n):
a,g=map(int,input().split())
s="AGAGAGAGAGAGAGAGAGAGAGAG"
print(s[n::n])
``` | instruction | 0 | 27,758 | 10 | 55,516 |
No | output | 1 | 27,758 | 10 | 55,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA
Submitted Solution:
```
'''
def main():
from sys import stdin,stdout
if __name__=='__main__':
main()
'''
#Journey to moon
'''
def main():
from sys import stdin,stdout
import collections
N,I =map(int,stdin.readline().split())
visited=list(0 for x in range(N))
G=collections.defaultdict(list)
groups=[0]
for _ in range(I):
a,b=map(int,stdin.readline().split())
G[a].append(b)
G[b].append(a)
q=collections.deque()
flag=0
for i in range(N):
if not visited[i]:
q.append(i)
visited[i]=flag+1
groups[flag]+=1
while len(q):
top=q.popleft()
for j in G[top]:
if visited[j]!=visited[top]:
visited[j]=flag+1
groups[flag]+=1
q.append(j)
flag+=1
groups.append(0)
counter=0
for i in range(len(groups)-1):
for j in range(i+1,len(groups)):
counter+=groups[i]*groups[j]
stdout.write(str(counter))
if __name__=='__main__':
main()
'''
#Djikstra's
'''
import collections
class Graph:
def __init__(self):
self.nodes=set()
self.edges=collections.defaultdict(list)
self.distances = {}
def add_node(self, value):
self.nodes.add(value)
def add_edge(self, from_node, to_node, distance):
self.edges[from_node].append(to_node)
self.edges[to_node].append(from_node)
self.distances[(from_node, to_node)] = distance
self.distances[(to_node, from_node)] = distance
def dijsktra(graph, initial):
visited = {initial: 0}
path = {}
nodes = set(graph.nodes)
while nodes:
min_node = None
for node in nodes:
if node in visited:
if min_node is None:
min_node = node
elif visited[node] < visited[min_node]:
min_node = node
if min_node is None:
break
nodes.remove(min_node)
current_weight = visited[min_node]
for edge in graph.edges[min_node]:
weight = current_weight + graph.distances[(min_node, edge)]
if edge not in visited or weight < visited[edge]:
visited[edge] = weight
path[edge] = min_node
return visited, path
def main():
from sys import stdin,stdout
for _ in range(int(stdin.readline())):
n,m=map(int,stdin.readline().split())
G=Graph()
for i in range(n):
G.add_node(i+1)
for i in range(m):
a,b,c=map(int,stdin.readline().split())
G.add_edge(a,b,c)
initial=int(stdin.readline())
v,p=dijsktra(G, initial)
#print(v)
#print(p)
for i in range(1,n+1):
if i!=initial:
k=v.get(i,-1)
stdout.write(str(k)+' ')
stdout.write('\n')
if __name__=='__main__':
main()
'''
#Larget pallindrome in String
'''
def main():
from sys import stdin,stdout
string=stdin.readline().strip()
l=len(string)
#Triangle logic
arrlen=(l*(l-1))//2
arr=list(0 for x in range(arrlen))
f=0
c=l-1
for i in range(l-1):
for j in range(i+1,l):
if string[i]==string[j]:
arr[f+j-i-1]=1
f+=c
c-=1
#print(arr)
if any(arr):
else:
if l & 1:
stdout.write('First')
else:
stdout.write('Second')
#2-d Array Logic
arr=list(list(0 for i in range(l)) for j in range(l))
for i in range(l):
for j in range(l):
if string[i]==string[j]:
arr[i][j]=1
maxim=0
for i in range(0,l*(l-1)-2,l+1):
a,b=i+1,i+2
#print(a,b)
acount=0
x=a//5
y=a%5
acount=arr[x][y]
x-=1
y-=1
while x>=0 and y>=0:
acount+=arr[x][y]
x-=1
y-=1
x=b//5
y=b%5
bcount=arr[x][y]
x-=1
y-=1
while x>=0 and y>=0:
bcount+=arr[x][y]
x-=1
y-=1
maxim=max((acount,bcount,maxim))
maxim=max(maxim,arr[l-2][l-1])
maxim=(maxim<<1)^1
delta=l-maxim
if delta & 1:
stdout.write('Second')
else:
stdout.write('First')
if __name__=='__main__':
main()
'''
#276B
'''
def main():
from sys import stdin,stdout
import collections
s=stdin.readline().strip()
count=collections.Counter(s)
l=list(filter(lambda x: count[x] & 1,list(x for x in count)))
removed=sum(list(count[x] for x in l))-max(list(count[x] for x in l)+[0])
if removed & 1:
stdout.write('Second')
else:
stdout.write('First')
if __name__=='__main__':
main()
'''
#362B
'''
def main():
from sys import stdin,stdout
n,m=map(int,stdin.readline().split())
if m:
dirty=sorted(map(int,stdin.readline().split()))
if dirty[0]==1 or dirty[-1]==n:
stdout.write('NO')
else:
flag=True
for i in range(m-2):
if dirty[i+1]==dirty[i]+1 and dirty[i+2]==dirty[i]+2:
flag=False
break
if flag:
stdout.write('YES')
else:
stdout.write('NO')
else:
stdout.write('YES')
if __name__=='__main__':
main()
'''
#279B SUM OF SUB-ARRAY
'''
def main():
from sys import stdin,stdout
n,t=map(int,stdin.readline().split())
arr=list(map(int,stdin.readline().split()))
maxim=0
curr_sum=arr[0]
i=0
j=1
if curr_sum <=t:
count=1
else:
curr_sum=0
count=0
i=1
j=2
while j<n:
if curr_sum+arr[j]<=t:
count+=1
curr_sum+=arr[j]
j+=1
else:
maxim=max(count,maxim)
if curr_sum:
curr_sum-=arr[i]
count-=1
else:
j+=1
i+=1
maxim=max(count,maxim)
stdout.write(str(maxim))
if __name__=='__main__':
main()
'''
#469B
'''
def main():
from sys import stdin,stdout
p,q,l,r=map(int,stdin.readline().split())
a=[]
b=[]
visited=list(0 for x in range(r-l+1))
#print(visited)
for i in range(p):
x,y=map(int,stdin.readline().split())
a.append(x)
b.append(y)
for i in range(q):
x,y=map(int,stdin.readline().split())
x+=l
y+=l
for j in range(p):
#print('x=',x,'y=',y)
lower=max(0,a[j]-y)
upper=min(b[j]-x,r)+1
if upper > lower:
delta=upper-lower
#print('upper=',upper,'lower=',lower)
visited[lower:upper]=list(1 for x in range(delta))
#print('visited:\n',visited)
# print(visited)
stdout.write(str(visited[:r-l+1].count(1)))
if __name__=='__main__':
main()
'''
'''
def main():
from sys import stdin,stdout
#import numpy as np
n,k=map(int,stdin.readline().split())
a=tuple(map(int,stdin.readline().split()))
minim=min(a)
maxim=max(a)
arr=list(a)
for i in range(n):
arr[i]-=minim
if max(arr) > k:
stdout.write('NO')
else:
stdout.write('YES\n')
for i in a:
stdout.write('1 '*minim)
for j in range(i-minim):
stdout.write(str(j%k+1)+' ')
stdout.write('\n')
if __name__=='__main__':
main()
'''
'''
def main():
from sys import stdin,stdout
n,p=[],[]
for _ in range(int(stdin.readline())):
last=int(stdin.readline())
if last<0:
n.append(-1*last)
else:
p.append(last)
if sum(p)>sum(n):
stdout.write('first')
elif sum(n)>sum(p):
stdout.write('second')
else:
maxim=max(n,p)
#print(maxim)
if maxim==p:
if maxim==n:
if last<0:
stdout.write('second')
else:
stdout.write('first')
else:
stdout.write('first')
else:
stdout.write('second')
if __name__=='__main__':
main()
'''
#286C
'''
def main():
from sys import stdin,stdout
m,n=map(int,stdin.readline().split())
minim=min(m,n)
stdout.write(str(minim+1)+'\n')
if n==minim:
for i in range(minim+1):
stdout.write(str(m)+' '+str(i)+'\n')
m-=1
else:
for i in range(minim+1):
stdout.write(str(i)+' '+str(n)+'\n')
n-=1
if __name__=='__main__':
main()
'''
#387B
'''
def main():
from sys import stdin,stdout
n,m=map(int,stdin.readline().split())
a=tuple(map(int,stdin.readline().split()))
b=tuple(map(int,stdin.readline().split()))
i=0
j=0
while True:
#print(i,j)
if i>=n or j>=m:
break
if b[j]>=a[i]:
i+=1
j+=1
else:
j+=1
stdout.write(str(n-i))
if __name__=='__main__':
main()
'''
#365B
'''
def main():
from sys import stdin,stdout
n=int(stdin.readline())
a=tuple(map(int,stdin.readline().split()))
maxim=2
count=2
i=2
while True:
if i>=n:
break
if a[i]==a[i-1]+a[i-2]:
count+=1
maxim=max(count,maxim)
else:
count=2
i+=1
stdout.write(str(min(maxim,n)))
if __name__=='__main__':
main()
''' #474D
'''
def main():
from sys import stdin,stdout
MOD=int(1e9)+7
T,k=map(int,stdin.readline().split())
fib=[x for x in range(1,k+1)]
for i in range(k,100001):
fib.append((fib[i-1]+fib[i-k]+1)%MOD)
for _ in range(T):
a,b=map(int,stdin.readline().split())
stdout.write(str((fib[b]-fib[a-1])%MOD)+'\n')
if __name__=='__main__':
main()
'''
#330B
#not working
'''
def main():
from sys import stdin,stdout
import collections
road_not=collections.defaultdict(set)
n,m=map(int,stdin.readline().split())
for _ in range(m):
a,b=map(int,stdin.readline().split())
road_not[a].add(b)
road_not[b].add(a)
counter=0
road=collections.defaultdict(set)
visited=[0 for x in range(n)]
visited[0]=True
for index in range(1,n+1):
for i in range(1,n+1):
if not visited[i-1]:
if i not in road_not[index] and i!=index:
counter+=1
road[index].add(i)
visited[i-1]=True
stdout.write(str(counter)+'\n')
for i in road:
for j in road[i]:
stdout.write(str(i)+' '+str(j)+'\n')
if __name__=='__main__':
main()
'''
#208D
'''
def main():
from sys import stdin,stdout
import bisect
n=int(stdin.readline())
p=tuple(map(int,stdin.readline().split()))
P=tuple(map(int,stdin.readline().split()))
record=[0 for x in range(5)]
points=0
for i in p:
points+=i
while points>=P[0]:
index=bisect.bisect_right(P,points)
if index:
index-=1
number=points//P[index]
record[index]+=number
points-=P[index]*number
for i in record:
stdout.write(str(i)+' ')
stdout.write('\n'+str(points))
if __name__=='__main__':
main()
'''
#339D Using Al.Cash's Segment Trees
#Segment Tree
#not-working
'''
powers=1
t=[0 for x in range(3*int(1e5))]
def build(n):
global t,powers
flag=False
for i in range(n-1,0,-1):
if i==powers-1:
flag=not flag
powers>>=1
if flag:
t[i]=t[i<<1]^t[i<<1|1]
else:
t[i]=t[i<<1]|t[i<<1|1]
def modify(i,v,n):
global t
flag=False
if t[i+n-1]==v or v|t[(i+n-1)^1]==t[(i+n-1)>>1]:
#print('skipped')
#print('t[i+n-1]=',t[i+n-1],'v=',v)
#print('v|t[(i+n-1)^1]=',v|t[(i+n-1)^1],'t[(i+n-1)>>1]',t[(i+n-1)>>1])
t[i+n-1]=v
return
t[i+n-1]=v
p=i+n-1
while p>1:
if flag:
if t[p>>1]==t[p]^t[p^1]:
break
t[p>>1]=t[p]^t[p^1]
flag=not flag
else:
if t[p>>1]==t[p]|t[p^1]:
break
t[p>>1]=t[p]|t[p^1]
flag=not flag
p>>=1
def main():
from sys import stdin,stdout
global t,powers
n,m=map(int,stdin.readline().split())
p=tuple(map(int,stdin.readline().split()))
powers=1<<(n-1)
n=1<<(n)
for i in range(n):
t[i+n]=p[i]
build(n)
#print(t[:2*n])
for _ in range(m):
a,b=map(int,stdin.readline().split())
modify(a,b,n)
#print(t[:2*n])
stdout.write(str(t[1])+'\n')
if __name__=='__main__':
main()
'''
#330B
'''
def main():
from sys import stdin,stdout
n,m=map(int,stdin.readline().split())
start_not=set()
for _ in range(m):
a,b=map(int,stdin.readline().split())
start_not.add(a-1)
start_not.add(b-1)
visited=[False for _ in range(n)]
for i in range(n):
if i not in start_not:
center=i
break
stdout.write(str(n-1)+'\n')
for i in range(n):
if i != center:
stdout.write(str(center+1)+' '+str(i+1)+'\n')
if __name__=='__main__':
main()
'''
#116B
'''
def main():
from sys import stdin,stdout
n,m=map(int,stdin.readline().split())
arr=[]
for _ in range(n):
arr.append(stdin.readline().strip())
pigs=set()
count=0
for i in range(n):
for j in range(m):
if arr[i][j]=='W':
flag=0
if i>0:
if arr[i-1][j]=='P':
pigs.add((i-1,j))
flag=1
if i<n-1:
if arr[i+1][j]=='P':
pigs.add((i+1,j))
flag=1
if j>0:
if arr[i][j-1]=='P':
pigs.add((i,j-1))
flag=1
if j<m-1:
if arr[i][j+1]=='P':
pigs.add((i,j+1))
flag=1
if flag:
count+=1
stdout.write(str(min(len(pigs),count)))
if __name__=='__main__':
main()
'''
#339D using Al.Cash's Segment Tree Implementation
'''
def main():
from sys import stdin,stdout
answers=()
n,m=map(int,stdin.readline().split())
p=tuple(map(int,stdin.readline().split()))
powers=1<<(n-1)
n=powers<<1
t=[0 for _ in range(n<<1)]
for i in range(n):
t[n+i]=p[i]
flag=False
for i in range(n-1,0,-1):
if i==powers-1:
flag=not flag
powers>>=1
if flag:
t[i]=t[i<<1]^t[i<<1|1]
else:
t[i]=t[i<<1]|t[i<<1|1]
for _ in range(m):
a,b=map(int,stdin.readline().split())
flag=False
if t[a+n-1]==b or b|t[(a+n-1)^1]==t[(a+n-1)>>1]:
t[a+n-1]=b
else:
t[a+n-1]=b
p=a+n-1
while p > 1:
if flag:
if t[p>>1]==t[p]^t[p^1]:
break
t[p>>1]=t[p]^t[p^1]
flag=not flag
else:
if t[p>>1]==t[p]|t[p^1]:
break
t[p>>1]=t[p]|t[p^1]
flag= not flag
p>>=1
stdout.write(str(t[1])+'\n')
if __name__=='__main__':
main()
'''
#515C
'''
def main():
from sys import stdin,stdout
import collections
nc=[0 for x in range(10)]
n=int(stdin.readline())
num=stdin.readline().strip()
for i in num:
k=int(i)
if k==9:
nc[7]+=1
nc[3]+=2
nc[2]+=1
elif k==8:
nc[7]+=1
nc[2]+=3
elif k==7:
nc[7]+=1
elif k==6:
nc[5]+=1
nc[3]+=1
elif k==5:
nc[5]+=1
elif k==4:
nc[3]+=1
nc[2]+=2
elif k==3:
nc[3]+=1
elif k==2:
nc[2]+=1
ans=''
for i in range(10):
ans+=str(9-i)*nc[9-i]
stdout.write(ans)
if __name__=='__main__':
main()
'''
#313B
'''
def main():
from sys import stdin,stdout
s=stdin.readline().strip()
flag=s[0]
if flag=='.':
anti='#'
else:
anti='.'
n=len(s)
l=[0 for x in range(n)]
for i in range(1,n):
if s[i]==flag:
l[i]=l[i-1]+1
else:
flag,anti=anti,flag
l[i]=l[i-1]
#print(l)
for _ in range(int(stdin.readline())):
a,b=map(int,stdin.readline().split())
stdout.write(str(l[b-1]-l[a-1])+'\n')
if __name__=='__main__':
main()
'''
#431C
'''
def main():
from sys import stdin,stdout
MOD=int(1e9)+7
n,k,d=map(int,stdin.readline().split())
d-=1
klist=[(1<<i)%MOD for i in range(k)]
klist=[1]+klist
for i in range(k+1,n+1):
klist.append((klist[i-1]*2-klist[i-1-k])%MOD)
if d:
dlist=[(1<<i)%MOD for i in range(d)]
dlist=[1]+dlist
for i in range(d+1,n+1):
dlist.append((dlist[i-1]*2-dlist[i-1-d])%MOD)
#print(klist)
#print(dlist)
ans=klist[n]-dlist[n]
else:
ans=klist[n]
stdout.write(str(ans%MOD))
if __name__=='__main__':
main()
'''
#441C
'''
def main():
from sys import stdin,stdout
n,m,k=map(int,stdin.readline().split())
if n>=m:
i=-1
j=-1
num=n*m
while k:
ans=num//k
stdout.write(str(ans)+' ')
counter=0
while counter<ans:
if j==-1:
flag=True
i+=1
j+=1
if j==m:
flag=False
i+=1
j-=1
stdout.write(str(i+1)+' '+str(j+1)+' ')
if flag:
j+=1
else:
j-=1
counter+=1
num-=ans
k-=1
stdout.write('\n')
else:
i=-1
j=-1
num=n*m
while k:
ans=num//k
stdout.write(str(ans)+' ')
counter=0
while counter<ans:
if i==-1:
flag=True
j+=1
i+=1
if i==n:
flag=False
j+=1
i-=1
stdout.write(str(i+1)+' '+str(j+1)+' ')
if flag:
i+=1
else:
i-=1
counter+=1
num-=ans
k-=1
stdout.write('\n')
if __name__=='__main__':
main()
'''
#538B
'''
def main():
from sys import stdin,stdout
n=int(stdin.readline())
ans=[]
while n:
t=''
num=str(n)
for i in num:
if int(i):
t+='1'
else:
t+='0'
ans.append(t)
n-=int(t)
stdout.write(str(len(ans))+'\n')
for i in ans:
stdout.write(i+' ')
if __name__=='__main__':
main()
'''
#486C
#not working
'''
def main():
from sys import stdin,stdout
n,o=map(int,stdin.readline().split())
string=stdin.readline().strip()
if n & 1:
s1=string[:n//2]
s2=string[n//2+1:]
rev=s2[::-1]
minim=0
indexlist=[]
for i in range(n//2):
k=ord(s[i])-ord(rev[i])
minim=min(k,26-k)
indexlist.append(i)
if o>n//2:
else:
o-=1
else:
s1=string[:n//2+1]
s2=string[n//2+1:]
rev=s2[::-1]
minim=0
indexlist=[]
for i in range(n//2):
k=ord(s[i])-ord(rev[i])
minim=min(k,26-k)
indexlist.append(i)
if o > n//2:
o=o-n//2-1
else:
o-=1
if len(indexlist):
if len(indexlist)>1:
minim+=min(abs(o-indexlist[0]),abs(o-indexlist[-1]))+(indexlist[-1]-indexlist[0])
else:
minim+=indexlist[0]-o
stdout.write(str(minim))
if __name__=='__main__':
main()
'''
#4B
'''
def main():
from sys import stdin,stdout
n,sumtime=map(int,stdin.readline().split())
a,b=[],[]
for i in range(n):
x,y=map(int,stdin.readline().split())
a.append(x)
b.append(y)
if sum(b)<sumtime or sum(a)>sumtime:
stdout.write('NO')
else:
stdout.write('YES\n')
diff=sumtime-sum(a)
for i in range(n):
if diff:
k=b[i]-a[i]
minim=min(k,diff)
a[i]+=minim
diff-=minim
for i in a:
stdout.write(str(i)+' ')
if __name__=='__main__':
main()
'''
#282B
def main():
from sys import stdin,stdout
n=int(stdin.readline())
a,b=[],[]
for i in range(n):
x,y=map(int,stdin.readline().split())
a.append(x)
b.append(y)
if a[0]>b[0]:
total=-1*b[0]
t='G'
else:
total=a[0]
t='A'
for i in range(1,n):
if total>=0:
total-=b[i]
t+='G'
else:
total+=a[i]
t+='A'
if total<-500 or total >500:
stdout.write('-1')
else:
stdout.write(t)
if __name__=='__main__':
main()
``` | instruction | 0 | 27,759 | 10 | 55,518 |
No | output | 1 | 27,759 | 10 | 55,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of eggs.
Next n lines contain two integers ai and gi each (0 β€ ai, gi β€ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| β€ 500.
If there are several solutions, you are allowed to print any of them.
Examples
Input
2
1 999
999 1
Output
AG
Input
3
400 600
400 600
400 600
Output
AGA
Submitted Solution:
```
n = int(input())
at= 0
gt=0
s =[]
for i in range(n):
a,b=map(int,input().split())
if(at>gt):
gt+=b
s.append("G")
else:
at+=a
s.append("A")
if(abs(gt-at)>500):
print(-1)
else:
print("".join(s))
``` | instruction | 0 | 27,760 | 10 | 55,520 |
No | output | 1 | 27,760 | 10 | 55,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 β€ N β€ 3Β·105), the number of days.
Following this is a line with exactly N integers p1, p2, ..., pN (1 β€ pi β€ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20. | instruction | 0 | 27,975 | 10 | 55,950 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
import heapq as hq
n = int(input())
a = list(map(int,input().split()))
ans = 0
q = []
for i in range(n):
if q:
x = hq.heappop(q)
if x < a[i]:
ans += a[i]-x
hq.heappush(q,a[i])
else:
hq.heappush(q,x)
hq.heappush(q,a[i])
print(ans)
``` | output | 1 | 27,975 | 10 | 55,951 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 β€ N β€ 3Β·105), the number of days.
Following this is a line with exactly N integers p1, p2, ..., pN (1 β€ pi β€ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20. | instruction | 0 | 27,976 | 10 | 55,952 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
import math
from heapq import *
def maxProfit(prices, days):
payoff = 0
maxPrice, minPrice = max(prices), min(prices)
maxIndex, minIndex = prices.index(maxPrice), prices.index(minPrice)
iterator = iter(prices)
h = [] # heap
if days == 1:
print(0)
return
for i in range(days):
p = next(iterator)
if not i:
heappush(h, p)
continue
if h[0] < p:
payoff += p - h[0]
heappop(h)
heappush(h, p)
heappush(h, p)
print(payoff)
if __name__ == "__main__":
n = int(input())
prices = list(map(int, input().split()))
maxProfit(prices, n)
``` | output | 1 | 27,976 | 10 | 55,953 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 β€ N β€ 3Β·105), the number of days.
Following this is a line with exactly N integers p1, p2, ..., pN (1 β€ pi β€ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20. | instruction | 0 | 27,977 | 10 | 55,954 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
from heapq import *
N = int(input())
price = [int(i) for i in input().split()]
total = 0
inf = (10**6) + 1
h = [inf]
#Assume we bought and sold optimally for the first k prices.
#We adjust our answer for the (k+1)th price that comes up.
for p in price:
if p > h[0]:
total += (p - heappop(h))
#We push p onto heap in case we should have bought at this price instead
#of selling.
heappush(h, p)
heappush(h, p)
print(total)
``` | output | 1 | 27,977 | 10 | 55,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 β€ N β€ 3Β·105), the number of days.
Following this is a line with exactly N integers p1, p2, ..., pN (1 β€ pi β€ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20. | instruction | 0 | 27,978 | 10 | 55,956 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
from heapq import heappush,heappop
n=int(input())
ar=list(map(int,input().split()))
unsed=[]
selled=[]
sm=0
for x in ar:
U,S=len(unsed),len(selled)
u=heappop(unsed) if U else float('inf')
s=heappop(selled) if S else float('inf')
y=min(u,s)
if(y<x):
if(u==y):
sm+=x-y
if(S):heappush(selled,s)
heappush(selled,x)
else:
sm+=x-y
heappush(unsed,y)
if(U):heappush(unsed,u)
heappush(selled,x)
else:
if(S):heappush(selled,s)
if(U):heappush(unsed,u)
heappush(unsed,x)
print(sm)
``` | output | 1 | 27,978 | 10 | 55,957 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 β€ N β€ 3Β·105), the number of days.
Following this is a line with exactly N integers p1, p2, ..., pN (1 β€ pi β€ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20.
Submitted Solution:
```
import math
from heapq import *
def maxProfit(prices, days):
payoff = 0
maxPrice, minPrice = max(prices), min(prices)
maxIndex, minIndex = prices.index(maxPrice), prices.index(minPrice)
iterator = iter(prices)
h = [] # heap
if days == 1:
return
for i in range(days):
p = next(iterator)
if not i:
heappush(h, p)
if h[0] < p:
payoff += p - h[0]
heappop(h)
heappush(h, p)
heappush(h, p)
print(payoff)
if __name__ == "__main__":
n = int(input())
prices = list(map(int, input().split()))
maxProfit(prices, n)
``` | instruction | 0 | 27,979 | 10 | 55,958 |
No | output | 1 | 27,979 | 10 | 55,959 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 β€ N β€ 3Β·105), the number of days.
Following this is a line with exactly N integers p1, p2, ..., pN (1 β€ pi β€ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20.
Submitted Solution:
```
from heapq import *
N = int(input())
price = [int(i) for i in input().split()]
total = 0
inf = 3 * (10**5) + 1
h = [inf]
#Assume we bought and sold optimally for the first k prices.
#We adjust our answer for the (k+1)th price that comes up.
for p in price:
if p > h[0]:
total += (p - heappop(h))
#We push p onto heap in case we should have bought at this price instead
#of selling.
heappush(h, p)
heappush(h, p)
print(total)
``` | instruction | 0 | 27,980 | 10 | 55,960 |
No | output | 1 | 27,980 | 10 | 55,961 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 β€ N β€ 3Β·105), the number of days.
Following this is a line with exactly N integers p1, p2, ..., pN (1 β€ pi β€ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20.
Submitted Solution:
```
n = input()
a = list(map(int, input().split()))
NotFinished = True
profit = 0
while NotFinished:
# get firstmin, firstminIndex, from a
if a:
mini = min(a)
mini_ix = a.index(mini)
maxi = max(a[mini_ix:])
maxi_ix = len(a) - len(a[mini_ix:]) + a[mini_ix:].index(maxi)
else:
break
print(mini, maxi, mini_ix, maxi_ix, a)
if (mini_ix == maxi_ix):
del a[mini_ix]
else:
profit += maxi - mini
del a[mini_ix]
del a[maxi_ix-1]
for i in range(0, len(a)-1):
if (a[i] < a[i+1] or len(a) == 1):
NotFinished = True
break
else:
NotFinished = False
print(profit)
# get firstMaxes, firstminimums etc..
``` | instruction | 0 | 27,981 | 10 | 55,962 |
No | output | 1 | 27,981 | 10 | 55,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 β€ N β€ 3Β·105), the number of days.
Following this is a line with exactly N integers p1, p2, ..., pN (1 β€ pi β€ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20.
Submitted Solution:
```
N = int(input())
p = [int(i) for i in input().split()]
def solve(p, i, num_stocks, memo):
if i == len(p):
return 0
elif (i, num_stocks) in memo:
return memo[(i, num_stocks)]
elif len(p) - i == num_stocks:
memo[(i, num_stocks)] = p[i] + solve(p, i + 1, num_stocks - 1, memo)
return memo[(i, num_stocks)]
else:
take = -p[i] + solve(p, i + 1, num_stocks + 1, memo)
skip = solve(p, i + 1, num_stocks, memo)
if num_stocks == 0:
sell = 0
else:
sell = p[i] + solve(p, i + 1, num_stocks - 1, memo)
memo[(i, num_stocks)] = max(take, skip, sell)
return memo[(i, num_stocks)]
memo = dict()
try:
print(solve(p, 0, 0, memo))
except Exception:
print(Exception)
``` | instruction | 0 | 27,982 | 10 | 55,964 |
No | output | 1 | 27,982 | 10 | 55,965 |
Provide a correct Python 3 solution for this coding contest problem.
To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.
He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:
* A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.
In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.
* Buy stock: Pay A_i yen and receive one stock.
* Sell stock: Sell one stock for A_i yen.
What is the maximum possible amount of money that M-kun can have in the end by trading optimally?
Constraints
* 2 \leq N \leq 80
* 100 \leq A_i \leq 200
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_N
Output
Print the maximum possible amount of money that M-kun can have in the end, as an integer.
Examples
Input
7
100 130 130 130 115 115 150
Output
1685
Input
6
200 180 160 140 120 100
Output
1000
Input
2
157 193
Output
1216 | instruction | 0 | 28,016 | 10 | 56,032 |
"Correct Solution:
```
N=int(input())
A=list(map(int, input().split()))
mon=1000
for i in range(1,N):
if A[i]>A[i-1]:
sho,mod=divmod(mon,A[i-1])
mon=sho*A[i]+mod
print(mon)
``` | output | 1 | 28,016 | 10 | 56,033 |
Provide a correct Python 3 solution for this coding contest problem.
To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.
He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:
* A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.
In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.
* Buy stock: Pay A_i yen and receive one stock.
* Sell stock: Sell one stock for A_i yen.
What is the maximum possible amount of money that M-kun can have in the end by trading optimally?
Constraints
* 2 \leq N \leq 80
* 100 \leq A_i \leq 200
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_N
Output
Print the maximum possible amount of money that M-kun can have in the end, as an integer.
Examples
Input
7
100 130 130 130 115 115 150
Output
1685
Input
6
200 180 160 140 120 100
Output
1000
Input
2
157 193
Output
1216 | instruction | 0 | 28,017 | 10 | 56,034 |
"Correct Solution:
```
N=int(input())
A=list(map(int, input().split()))
mon=1000
for i in range(1,N):
if A[i]>A[i-1]:
mon=mon//A[i-1]*A[i]+mon%A[i-1]
print(mon)
``` | output | 1 | 28,017 | 10 | 56,035 |
Provide a correct Python 3 solution for this coding contest problem.
To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.
He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:
* A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.
In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.
* Buy stock: Pay A_i yen and receive one stock.
* Sell stock: Sell one stock for A_i yen.
What is the maximum possible amount of money that M-kun can have in the end by trading optimally?
Constraints
* 2 \leq N \leq 80
* 100 \leq A_i \leq 200
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_N
Output
Print the maximum possible amount of money that M-kun can have in the end, as an integer.
Examples
Input
7
100 130 130 130 115 115 150
Output
1685
Input
6
200 180 160 140 120 100
Output
1000
Input
2
157 193
Output
1216 | instruction | 0 | 28,018 | 10 | 56,036 |
"Correct Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
assets = 1000
for i in range(n - 1):
if a[i] < a[i + 1]:
assets = assets//a[i] * a[i + 1] + assets%a[i]
print(assets)
``` | output | 1 | 28,018 | 10 | 56,037 |
Provide a correct Python 3 solution for this coding contest problem.
To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.
He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:
* A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.
In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.
* Buy stock: Pay A_i yen and receive one stock.
* Sell stock: Sell one stock for A_i yen.
What is the maximum possible amount of money that M-kun can have in the end by trading optimally?
Constraints
* 2 \leq N \leq 80
* 100 \leq A_i \leq 200
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_N
Output
Print the maximum possible amount of money that M-kun can have in the end, as an integer.
Examples
Input
7
100 130 130 130 115 115 150
Output
1685
Input
6
200 180 160 140 120 100
Output
1000
Input
2
157 193
Output
1216 | instruction | 0 | 28,019 | 10 | 56,038 |
"Correct Solution:
```
N = int(input())
A = list(map(int,input().split()))
m = 1000
stock = 0
for i in range(N-1):
if A[i] < A[i+1]:
stock = m//A[i]
m = m%A[i] + stock*A[i+1]
print(m)
``` | output | 1 | 28,019 | 10 | 56,039 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.