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 |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total. | instruction | 0 | 100,909 | 10 | 201,818 |
Tags: dp, greedy
Correct Solution:
```
z=input;c,n,k,a,b=0,int(z()),int(z()),int(z()),int(z())
if k>1:
while(((n//k)*a*(k-1))>b):c+=(n%k)*a +b;n=n//k
c+=(n-1)*a;print(c)
``` | output | 1 | 100,909 | 10 | 201,819 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
Submitted Solution:
```
n = int(input())
k = int(input())
A = int(input())
B = int(input())
cost = 0
if k == 1:
print(A*(n-1))
else:
while n != 1:
if n%k != 0:
shift = (n%k) if n>k else n-1
tcost = shift * A
n -= shift
else:
tcost = min(B, A*(n-n//k))
n //= k
cost += tcost
print(cost)
``` | instruction | 0 | 100,910 | 10 | 201,820 |
Yes | output | 1 | 100,910 | 10 | 201,821 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
Submitted Solution:
```
n = int(input())
k = int(input())
a = int(input())
b = int(input())
if n==1:
print(0)
exit()
if(k>n or k==1):
print(a*(n-1))
exit()
mn = a*(n-1)
ans =0
while n//k>=1:
x = n%k
y = n//k
ans+= min(a*(n-y),x*a+b)
n = n//k
if(n):
ans+=(n-1)*a
print(min(ans,mn))
``` | instruction | 0 | 100,911 | 10 | 201,822 |
Yes | output | 1 | 100,911 | 10 | 201,823 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
Submitted Solution:
```
# ===================================
# (c) MidAndFeed aka ASilentVoice
# ===================================
# import math
# import collections
# ===================================
n = int(input())
k = int(input())
a = int(input())
b = int(input())
ans = 0
if k == 1:
ans = (n-1)*a
else:
while(n != 1):
if n%k==0:
t = n//k
ta = min(b, (n-t)*a)
ans += ta
n = t
else:
t = n%k if n//k else n-1
ans += t*a
n -= t
print(ans)
``` | instruction | 0 | 100,912 | 10 | 201,824 |
Yes | output | 1 | 100,912 | 10 | 201,825 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
Submitted Solution:
```
n=int(input())
k=int(input())
a=int(input())
b=int(input())
ans=0
if k==1:
ans=(n-1)*a
n=1
while n!=1:
if n<k:
ans+=(n-1)*a
n=1
elif n>k and n%k!=0:
ans+=(n%k)*a
n=n-(n%k)
else:
ans+=min(b,(n-(n//k))*a)
n=n//k
#print(n)
print(ans)
``` | instruction | 0 | 100,913 | 10 | 201,826 |
Yes | output | 1 | 100,913 | 10 | 201,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
Submitted Solution:
```
n = int(input())
k = int(input())
A = int(input())
B = int(input())
answer = 0
rest = 666
x = n
while x != 1:
if (k == 1) or (x < k):
answer += A * (x - 1)
x = 1
else:
temp = answer
res1 = answer + A * (x - 1)
res2 = answer + A * (x % k)
x -= (x % k)
i = 1
t = k
while x % t == 0:
t *= k
i += 1
res2 += B * (i-1)
x //= (t // k)
if (res1 < res2):
x = 1
answer = res1
else:
rest = res1 if (res1 < rest) else rest
answer = res2
print(answer if answer < rest else rest)
``` | instruction | 0 | 100,914 | 10 | 201,828 |
No | output | 1 | 100,914 | 10 | 201,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
Submitted Solution:
```
# http://codeforces.com/contest/940/problem/B
import sys
n = int(sys.stdin.readline().strip())
k = int(sys.stdin.readline().strip())
A = int(sys.stdin.readline().strip())
B = int(sys.stdin.readline().strip())
optimalPrice = 0
while n > 1:
if n % k == 0:
optimalPrice += min(B, A * (n - n/k))
n /= k
else:
if n < k:
optimalPrice += A * (n - 1)
n = 1
else:
optimalPrice += A * (n % k)
n -= n % k
print(optimalPrice)
``` | instruction | 0 | 100,915 | 10 | 201,830 |
No | output | 1 | 100,915 | 10 | 201,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
Submitted Solution:
```
o = lambda: int(input())
n= o()
k = o()
A = o()
B = o()
coins = 0
while n!= 1:
if (n%k !=0):
if (n<k):
n-=n-1
coins+=A*(n-1)
else:
coins += A*(n%k)
n-=int(n%k)
else:
if ( (n-n/k)*A < B ):
coins += int(A*(n-n/k))
n//=k
else:
n//=k
coins += B
print(coins)
``` | instruction | 0 | 100,916 | 10 | 201,832 |
No | output | 1 | 100,916 | 10 | 201,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input
The first line contains a single integer n (1 ≤ n ≤ 2·109).
The second line contains a single integer k (1 ≤ k ≤ 2·109).
The third line contains a single integer A (1 ≤ A ≤ 2·109).
The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
Note
In the first testcase, the optimal strategy is as follows:
* Subtract 1 from x (9 → 8) paying 3 coins.
* Divide x by 2 (8 → 4) paying 1 coin.
* Divide x by 2 (4 → 2) paying 1 coin.
* Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
Submitted Solution:
```
n=int(input())
k=int(input())
a=int(input())
b=int(input())
res=0
kk=a*(n-1)
while n>=1:
if n%k==0:
n=n//k
res+=b
else:
p=n%k
n=n-p
res+=a
print(min(res,kk))
``` | instruction | 0 | 100,917 | 10 | 201,834 |
No | output | 1 | 100,917 | 10 | 201,835 |
Provide a correct Python 3 solution for this coding contest problem.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA | instruction | 0 | 101,095 | 10 | 202,190 |
"Correct Solution:
```
while True:
n = input()
if n == '0': break
q1, b, c1, c2, q2 = map(int, n.split())
for i in range(q2, 0, -1):
c = i * c1
x = (b-c)//c2
if i+x < q1:
continue
elif x >= 0:
print(i, x)
break
else:
print('NA')
``` | output | 1 | 101,095 | 10 | 202,191 |
Provide a correct Python 3 solution for this coding contest problem.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA | instruction | 0 | 101,096 | 10 | 202,192 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0186
"""
import sys
from sys import stdin
input = stdin.readline
def Cond(q1, b, mid, c1, c2):
b -= (c1 * mid)
if b < 0:
return False
b -= (c2 * (q1 - mid))
if b < 0:
return False
return True
def solve(q1, b, c1, c2, q2):
ub = q2
lb = 1
aizu_chicken = 0
while (ub - lb) > 1:
mid = (ub + lb) // 2
if Cond(q1, b, mid, c1, c2):
lb = mid
aizu_chicken = max(aizu_chicken, mid)
else:
ub = mid
if Cond(q1, b, lb, c1, c2):
aizu_chicken = max(aizu_chicken, lb)
if Cond(q1, b, ub, c1, c2):
aizu_chicken = max(aizu_chicken, ub)
b -= (aizu_chicken * c1)
normal_chicken = b // c2
if normal_chicken < 0 or aizu_chicken == 0:
return False, 0, 0
else:
return True, aizu_chicken, normal_chicken
def main(args):
while True:
txt = input().strip()
if txt[0] == '0':
break
q1, b, c1, c2, q2 = map(int, txt.split())
result, aizu, normal = solve(q1, b, c1, c2, q2)
if result is False:
print('NA')
else:
print(aizu, normal)
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 101,096 | 10 | 202,193 |
Provide a correct Python 3 solution for this coding contest problem.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA | instruction | 0 | 101,097 | 10 | 202,194 |
"Correct Solution:
```
while True:
N = input()
if N == "0":
break
q1,b,c1,c2,q2 = [int(i) for i in N.split()]
if c1 <= c2:
x = min(q2, b // c1)
else:
x = min((b-c2*q1) // (c1-c2), q2, b // c1)
y = (b-c1*x) // c2
if x <= 0 or x + y < q1:
print("NA")
else:
print(x,y)
``` | output | 1 | 101,097 | 10 | 202,195 |
Provide a correct Python 3 solution for this coding contest problem.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA | instruction | 0 | 101,098 | 10 | 202,196 |
"Correct Solution:
```
def binary_search(quantity, budget, aizu_chicken_price, chicken_price, aizu_chicken_limit):
if budget < (quantity - 1) * chicken_price + aizu_chicken_price:
return None
if aizu_chicken_price * quantity <= budget:
aizu_chicken_count = budget // aizu_chicken_price
if aizu_chicken_limit < aizu_chicken_count:
aizu_chicken_count = aizu_chicken_limit
rest = budget - aizu_chicken_price * aizu_chicken_count
chicken_count = rest // chicken_price
return aizu_chicken_count, chicken_count
if aizu_chicken_price * aizu_chicken_limit + chicken_price * (quantity - aizu_chicken_limit) < budget:
rest = budget - aizu_chicken_price * aizu_chicken_limit
chicken_count = rest // chicken_price
return aizu_chicken_limit, chicken_count
# 二分探索
aizu_chicken_count = aizu_chicken_limit // 2
chicken_count = quantity - aizu_chicken_count
update_count = aizu_chicken_count // 2 + 1
max_pair = (0, quantity)
while 0 < update_count:
if aizu_chicken_count * aizu_chicken_price + chicken_count * chicken_price <= budget:
max_pair = (aizu_chicken_count, chicken_count)
aizu_chicken_count += update_count + 1
chicken_count -= (update_count + 1)
else:
aizu_chicken_count -= (update_count + 1)
chicken_count += update_count + 1
update_count //= 2
return max_pair
while True:
input_data = input()
if input_data == "0":
break
quantity, budget, aizu_chicken_price, chicken_price, aizu_chicken_limit = [int(item) for item in
input_data.split(" ")]
result = binary_search(quantity, budget, aizu_chicken_price, chicken_price, aizu_chicken_limit)
if result is not None:
print(result[0], result[1])
else:
print("NA")
``` | output | 1 | 101,098 | 10 | 202,197 |
Provide a correct Python 3 solution for this coding contest problem.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA | instruction | 0 | 101,099 | 10 | 202,198 |
"Correct Solution:
```
# Aizu Problem 0186: Aizu Chicken
#
import sys, math, os, bisect
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
def aizu_chicken(q1, b, c1, c2, q2):
for i in range(q2, 0, -1):
cj = i * c1
normal = int((b - cj) // c2)
if i + normal < q1:
continue
elif normal >= 0:
print(i, normal)
break
else:
print("NA")
while True:
inp = [int(_) for _ in input().split()]
if len(inp) == 1:
break
q1, b, c1, c2, q2 = inp[:]
aizu_chicken(q1, b, c1, c2, q2)
``` | output | 1 | 101,099 | 10 | 202,199 |
Provide a correct Python 3 solution for this coding contest problem.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA | instruction | 0 | 101,100 | 10 | 202,200 |
"Correct Solution:
```
def check(mid, b, c1, c2, q1):
if mid + (b - mid * c1) // c2 < q1:
return False
return True
while True:
s = input()
if s == "0":
break
q1, b, c1, c2, q2 = map(int, s.split())
max_aizu = min(b // c1, q2)
if max_aizu <= 0:
print("NA")
continue
if c2 >= c1:
max_normal = (b - max_aizu * c1) // c2
if max_aizu + max_normal < q1:
print("NA")
else:
print(max_aizu, max_normal)
continue
if (b - c1) // c2 + 1 < q1:
print("NA")
continue
left = 0
right = max_aizu + 1
while right - left > 1:
mid = (left + right) // 2
if check(mid, b, c1, c2, q1):
left = mid
else:
right = mid
print(left, (b - left * c1) // c2)
``` | output | 1 | 101,100 | 10 | 202,201 |
Provide a correct Python 3 solution for this coding contest problem.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA | instruction | 0 | 101,101 | 10 | 202,202 |
"Correct Solution:
```
class Solve:
def __init__(self):
tmp = [int(i) for i in input().split()]
if len(tmp) == 1:
self.cal = False
else:
self.cal = True
q1, b, c1, c2, q2 = tmp
self.B = b
self.C = (c1, c2)
self.Q = (q1, q2)
self.a = 0 # >= 0; True;
self.b = min(self.Q[1]+1, self.B//self.C[0]+1) # <= 10**6+1; False
def check(self, A):
b = self.B
b -= self.C[0] * A # self.b(??????)?????????????´\?¶????100A[g]????????\??¶?????????????????????
b -= self.C[1] * max(0, self.Q[0]-A)
if b >= 0:
return True
else:
return False
def solve(self):
while self.b - self.a > 1:
m = (self.a + self.b) // 2
if self.check(m):
self.a = m
else:
self.b = m
return self.buy(self.a)
def buy(self, A):
if A == 0:
return ['NA']
else:
return [str(A), str((self.B-self.C[0]*A)//self.C[1])]
while True:
s = Solve()
if not s.cal:
break
print(" ".join(s.solve()))
``` | output | 1 | 101,101 | 10 | 202,203 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0186
"""
import sys
from sys import stdin
input = stdin.readline
def Cond(q1, b, mid, c1, c2):
b -= (c1 * mid)
if b < 0:
return False
b -= (c2 * (q1 - mid))
if b < 0:
return False
return True
def solve(q1, b, c1, c2, q2):
ub = q2
lb = 1
aizu_chicken = 0
while (ub - lb) > 1:
mid = (ub + lb) // 2
if Cond(q1, b, mid, c1, c2):
lb = mid
aizu_chicken = max(aizu_chicken, mid)
else:
ub = mid
if Cond(q1, b, ub, c1, c2):
aizu_chicken = max(aizu_chicken, ub)
b -= (aizu_chicken * c1)
normal_chicken = b // c2
if normal_chicken < 0 or aizu_chicken == 0:
return False, 0, 0
else:
return True, aizu_chicken, normal_chicken
def main(args):
while True:
txt = input().strip()
if txt[0] == '0':
break
q1, b, c1, c2, q2 = map(int, txt.split())
result, aizu, normal = solve(q1, b, c1, c2, q2)
if result is False:
print('NA')
else:
print(aizu, normal)
if __name__ == '__main__':
main(sys.argv[1:])
``` | instruction | 0 | 101,102 | 10 | 202,204 |
No | output | 1 | 101,102 | 10 | 202,205 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA
Submitted Solution:
```
# Aizu Problem 0186: Aizu Chicken
#
import sys, math, os, bisect
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
def aizu_chicken(q1, b, c1, c2, q2):
for i in range(q2, 0, -1):
cj = i * c1
normal = int((b - cj) / c2)
if i + normal < q1:
continue
elif normal >= 0:
print(i, normal)
break
else:
print("NA")
while True:
inp = [int(_) for _ in input().split()]
if len(inp) == 1:
break
q1, b, c1, c2, q2 = inp[:]
aizu_chicken(q1, b, c1, c2, q2)
``` | instruction | 0 | 101,103 | 10 | 202,206 |
No | output | 1 | 101,103 | 10 | 202,207 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA
Submitted Solution:
```
def check(mid, b, c1, c2, q1):
if mid + (b - mid * c1) // c2 < q1:
return False
return True
while True:
s = input()
if s == "0":
break
q1, b, c1, c2, q2 = map(int, s.split())
max_aizu = min(b // c1, q2)
if max_aizu <= 0:
print("NA")
continue
if c2 >= c1:
if max_aizu >= q1:
print(max_aizu, (b - max_aizu * c1) // c2)
else:
print("NA")
continue
left = 0
right = max_aizu + 1
while right - left > 1:
mid = (left + right) // 2
if check(mid, b, c1, c2, q1):
left = mid
else:
right = mid
if (b - left * c1) // c2 + left < q1:
print("NA")
else:
print(left, (b - left * c1) // c2)
``` | instruction | 0 | 101,104 | 10 | 202,208 |
No | output | 1 | 101,104 | 10 | 202,209 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high.
<image>
Relatives come to visit Ichiro's house from afar today. Mom decided to cook chicken and entertain her relatives. A nearby butcher shop sells two types of chicken, Aizu chicken and regular chicken. The mother gave Ichiro the following instructions and asked the butcher to go and buy chicken.
* If you run out of chicken, you will be in trouble, so buy more chicken than you have decided.
* Buy as many Aizu local chickens as your budget allows (must buy Aizu local chickens).
* Buy as much regular chicken as possible with the rest of the Aizu chicken (don't buy if you don't have enough budget).
When Ichiro went to the butcher shop, the amount of Aizu chicken that could be bought by one person was limited due to lack of stock. When Ichiro follows all the instructions given by his mother when shopping, how many grams of Aizu chicken and regular chicken will Ichiro buy?
Enter the lower limit of the amount of chicken to buy q1, the budget b, the price of 100 grams of Aizu chicken at this butcher c1, the price of 100 grams of regular chicken c2, and the upper limit of the amount of Aizu chicken that one person can buy q2. Create a program that outputs the amount of Aizu chicken and ordinary chicken that Ichiro buys in units of 100 grams. However, if you cannot buy at this butcher's shop as instructed by your mother, please output "NA".
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
q1 b c1 c2 q2
The data q1 and q2, which represent the amount of chicken, are specified in units of 100 grams. Also, b, c1, c2, q1, and q2 are integers between 1 and 1,000,000.
The number of datasets does not exceed 50.
Output
For each data set, the amount of Aizu chicken purchased by Ichiro and the amount of normal chicken (separated by half-width spaces) or "NA" is output on one line.
Example
Input
48 9297 240 126 32
20 3010 157 141 7
30 117002 5680 962 15
8 1673 1712 190 22
64 8478 87 54 307
23 5477 117 92 12
50 7558 1396 187 17
279 88677 4522 514 14
0
Output
28 20
7 13
15 33
NA
97 0
12 44
NA
NA
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0186
"""
import sys
from sys import stdin
input = stdin.readline
def Cond(q1, b, mid, c1, c2):
b -= (c1 * mid)
if b < 0:
return False
b -= (c2 * (q1 - mid))
if b < 0:
return False
return True
def solve(q1, b, c1, c2, q2):
if c2 * q1 > b:
return False, 0, 0
ub = q2
lb = 1
max_aizu = 0
while (ub - lb) > 1:
mid = (ub + lb) // 2
if Cond(q1, b, mid, c1, c2):
lb = mid
max_aizu = mid
else:
ub = mid
if Cond(q1, b, ub, c1, c2):
max_aizu = ub
b -= (max_aizu * c1)
normal_chicken = b // c2
if normal_chicken < 0 or max_aizu == 0:
return False, 0, 0
else:
return True, max_aizu, normal_chicken
def main(args):
while True:
txt = input().strip()
if txt[0] == '0':
break
q1, b, c1, c2, q2 = map(int, txt.split())
result, aizu, normal = solve(q1, b, c1, c2, q2)
if result is False:
print('NA')
else:
print(aizu, normal)
if __name__ == '__main__':
main(sys.argv[1:])
``` | instruction | 0 | 101,105 | 10 | 202,210 |
No | output | 1 | 101,105 | 10 | 202,211 |
Provide a correct Python 3 solution for this coding contest problem.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10 | instruction | 0 | 101,148 | 10 | 202,296 |
"Correct Solution:
```
I=input
n,d,x=map(int,I().split())
w=[list(map(int,I().split()))for i in range(d)];d-=1
p=[[w[i+1][j]-w[i][j]for j in range(n)]for i in range(d)]
b=x
for i in range(d):
dp = [False for j in range(b+1)]
dp[0]=0
for j in range(n):
for k in range(b):
if w[i][j]+k<b+1 and id(dp[k])!=False:
dp[k+w[i][j]]=max(dp[k+w[i][j]],dp[k]+p[i][j])
b+=max(dp[:b+1])
print(b)
``` | output | 1 | 101,148 | 10 | 202,297 |
Provide a correct Python 3 solution for this coding contest problem.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10 | instruction | 0 | 101,149 | 10 | 202,298 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
rr = []
while True:
n,d,x = LI()
a = [LI() for _ in range(d)]
for i in range(d-1):
t = [0] * (x+1)
t[x] = x
for j in range(n):
if a[i][j] >= a[i+1][j]:
continue
aj = a[i][j]
ak = a[i+1][j] - aj
for k in range(x,aj-1,-1):
if t[k-aj] < t[k] + ak:
t[k-aj] = t[k] + ak
x = max(t)
rr.append(x)
break
return '\n'.join(map(str,rr))
print(main())
``` | output | 1 | 101,149 | 10 | 202,299 |
Provide a correct Python 3 solution for this coding contest problem.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10 | instruction | 0 | 101,150 | 10 | 202,300 |
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
n,d,x = map(int,input().split())
weights = []
for i in range(d):
weights.append(list(map(int,input().split())))
prices = []
for i in range(d-1):
prices.append([weights[i+1][j] - weights[i][j] for j in range(n)])
bag = x
for i in range(d-1):
dp = [False for i in range(bag+1)]
dp[0] = 0
for j in range(n):
for k in range(bag):
if weights[i][j] + k < bag+1 and dp[k] is not False:
dp[k+weights[i][j]] = max(dp[k+weights[i][j]],dp[k] + prices[i][j])
bag += max(dp[:bag+1])
print(bag)
``` | output | 1 | 101,150 | 10 | 202,301 |
Provide a correct Python 3 solution for this coding contest problem.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10 | instruction | 0 | 101,151 | 10 | 202,302 |
"Correct Solution:
```
N, D, X = map(int, input().split())
INF = 10 ** 5
dp = [0] * (INF + 1)
P = [tuple(map(int, input().split())) for _ in range(D)]
for i in range(X + 1):
dp[i] = i
ans = X
for d in range(D - 1):
for i in range(ans + 1):
dp[i] = i
tmp = ans
for i in range(N):
for j in range(P[d][i], ans + 1):
dp[j] = max(dp[j], dp[j - P[d][i]] + P[d + 1][i])
tmp = max(tmp, dp[j])
ans = tmp
print(ans)
``` | output | 1 | 101,151 | 10 | 202,303 |
Provide a correct Python 3 solution for this coding contest problem.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10 | instruction | 0 | 101,152 | 10 | 202,304 |
"Correct Solution:
```
def get_pro(w, v):
"""
dp[a][b] ... a番目、金額bまでで作れる最高の利益
dp[a][b] = max(dp[a - 1][b], dp[a - 1][b - w * k] + v * k)
"""
dp = [0] * (x + 1)
for a in range(n):
wa = wlst[a]
va = vlst[a]
for b in range(wa, x + 1):
dp[b] = max(dp[b], dp[b - wa] + va)
return dp[x]
n, d, x = map(int, input().split())
plst = [list(map(int, input().split())) for _ in range(d)]
for i in range(d - 1):
vlst = [plst[i + 1][j] - plst[i][j] for j in range(n)]
wlst = [plst[i][j] for j in range(n)]
x += get_pro(wlst, vlst)
print(x)
``` | output | 1 | 101,152 | 10 | 202,305 |
Provide a correct Python 3 solution for this coding contest problem.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10 | instruction | 0 | 101,153 | 10 | 202,306 |
"Correct Solution:
```
import sys
def calc(x, n, p1, p2):
dp = []
for i in range(n):
dp.append( [0] * (x + 1) )
pocket = x
for i in range(n):
for j in range(x + 1):
dp[i][j] = dp[i - 1][j]
if j - p1[i] >= 0:
dp[i][j] = max(dp[i][j], dp[i][j - p1[i]] + p2[i])
pocket = max(pocket, (x - j) + dp[i][j])
return pocket
def main():
n,d,x = map(int, sys.stdin.readline().split())
pp = []
for i in range(d):
pp.append( list(map(int,sys.stdin.readline().split())) )
curr_x = x
for i in range(d - 1):
curr_x = calc(curr_x, n, pp[i], pp[i + 1])
print(curr_x)
if __name__ == '__main__':
main()
``` | output | 1 | 101,153 | 10 | 202,307 |
Provide a correct Python 3 solution for this coding contest problem.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10 | instruction | 0 | 101,154 | 10 | 202,308 |
"Correct Solution:
```
import sys
def main():
n,d,x = map(int, sys.stdin.readline().split())
pp = []
for i in range(d):
pp.append( list(map(int,sys.stdin.readline().split())) )
dp = [0] * 100001
curr_x = x
for k in range(d - 1):
next_x = curr_x
for i in range(curr_x + 1):
dp[i] = 0
for i in range(n):
for j in range(curr_x + 1):
if j - pp[k][i] >= 0:
dp[j] = max(dp[j], dp[j - pp[k][i]] + pp[k + 1][i])
next_x = max(next_x, (curr_x - j) + dp[j])
curr_x = next_x
print(curr_x)
if __name__ == '__main__':
main()
``` | output | 1 | 101,154 | 10 | 202,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10
Submitted Solution:
```
N, D, X = map(int, input().split())
INF = 10 ** 5
dp = [0] * (INF + 1)
P = [tuple(map(int, input().split())) for _ in range(D)]
for i in range(X + 1):
dp[i] = i
ans = X
for d in range(D - 1):
for i in range(N):
for j in reversed(range(P[d][i], ans + 1)):
if 0 <= j + P[d + 1][i] - P[d][i] <= INF:
dp[j + P[d + 1][i] - P[d][i]] = j + P[d + 1][i] - P[d][i]
ans = max(ans, j + P[d + 1][i] - P[d][i])
print(ans)
``` | instruction | 0 | 101,155 | 10 | 202,310 |
No | output | 1 | 101,155 | 10 | 202,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10
Submitted Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
n,d,x = map(int,input().split())
weights = []
for i in range(d):
weights.append(list(map(int,input().split())))
prices = []
for i in range(d-1):
prices.append([weights[i+1][j] - weights[i][j] for j in range(n)])
dp = [False for i in range(10**5+1)]
dp[0] = 0
bag = x
for i in range(d-1):
for j in range(n):
for k in range(10**5):
if weights[i][j] + k < 10**5+1 and dp[k] is not False:
dp[k+weights[i][j]] = max(dp[k+weights[i][j]],dp[k] + prices[i][j])
bag = max(dp[:bag]) + bag
print(bag)
``` | instruction | 0 | 101,156 | 10 | 202,312 |
No | output | 1 | 101,156 | 10 | 202,313 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks.
Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers.
* Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j.
* Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen.
(While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.)
Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day.
Input
The input is given in the following format.
n d x
p1,1 ... p1, n
...
pd, 1 ... pd, n
* n: Number of stock types
* d: days
* x: Money on the first day
* pi, j: Stock price of stock j on day i (today is the first day)
Constraints
Each variable being input is an integer that satisfies the following constraints.
* 1 ≤ n ≤ 10
* 1 ≤ d ≤ 10
* 1 ≤ x, pi, j ≤ 105
* It is guaranteed that the amount of money you have on the last day will be 105 or less.
Output
Output the last day's money in one line when you invest optimally.
Examples
Input
2 2 5
3 2
5 4
Output
9
Input
1 2 5
6
10000
Output
5
Input
2 3 5
4 5
6 3
8 5
Output
11
Input
3 3 10
10 9 6
8 7 3
7 5 1
Output
10
Submitted Solution:
```
n, d, x = map(int, input().split())
plst = [list(map(int, input().split())) for _ in range(d)]
for i in range(d - 1):
max_pro = 0
for j in range(n):
pro = (plst[i + 1][j] - plst[i][j]) * (x // plst[i][j])
max_pro= max(pro, max_pro)
x += max_pro
print(x)
``` | instruction | 0 | 101,157 | 10 | 202,314 |
No | output | 1 | 101,157 | 10 | 202,315 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A thief made his way to a shop.
As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.
The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).
Find all the possible total costs of products the thief can nick into his knapsack.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.
The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.
Output
Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.
Examples
Input
3 2
1 2 3
Output
2 3 4 5 6
Input
5 5
1 1 1 1 1
Output
5
Input
3 3
3 5 11
Output
9 11 13 15 17 19 21 25 27 33
Submitted Solution:
```
z=input().split()
nums=input().split()
parts=[]
possible=[]
length=[]
n=int(z[1])-1
for i in range(int(z[1])-1):
for j in range(int(z[0])):
length.append(n)
parts.append(int(nums[j])*n)
n=n-1
l=len(parts)
for i in range(len(parts)):
l1=len(parts)-1
while(l1!=i):
if length[i]+length[l1]==int(z[1]):
possible.append(parts[i]+parts[l1])
elif length[i]+length[l1] < int(z[1]):
parts.append(parts[i]+parts[l1])
length.append(length[i]+length[l1])
l1=l1-1
possible=set(possible)
possible=list(possible)
possible.sort()
string=""
for i in range(len(possible)):
string=string+str(possible[i])+" "
string=string[0:len(string)-1]
print(string)
``` | instruction | 0 | 101,645 | 10 | 203,290 |
No | output | 1 | 101,645 | 10 | 203,291 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A thief made his way to a shop.
As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.
The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).
Find all the possible total costs of products the thief can nick into his knapsack.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.
The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.
Output
Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.
Examples
Input
3 2
1 2 3
Output
2 3 4 5 6
Input
5 5
1 1 1 1 1
Output
5
Input
3 3
3 5 11
Output
9 11 13 15 17 19 21 25 27 33
Submitted Solution:
```
z=input().split()
nums=input().split()
parts=[]
possible=[]
length=[]
n=int(z[1])-1
for i in range(int(z[1])-1):
for j in range(int(z[0])):
length.append(n)
parts.append(int(nums[j])*n)
n=n-1
for i in range(len(parts)):
n=len(parts)-1
while(length[i]+length[n]<=int(z[1]) and n>=i):
if (length[i]+length[n]==int(z[1])):
possible.append(parts[i]+parts[n])
n=n-1
possible=set(possible)
possible=list(possible)
possible.sort()
for i in range(len(possible)):
print(str(possible[i])+" ",end='')
``` | instruction | 0 | 101,646 | 10 | 203,292 |
No | output | 1 | 101,646 | 10 | 203,293 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A thief made his way to a shop.
As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.
The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).
Find all the possible total costs of products the thief can nick into his knapsack.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.
The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.
Output
Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.
Examples
Input
3 2
1 2 3
Output
2 3 4 5 6
Input
5 5
1 1 1 1 1
Output
5
Input
3 3
3 5 11
Output
9 11 13 15 17 19 21 25 27 33
Submitted Solution:
```
z=input().split()
nums=input().split()
parts=[]
possible=[]
length=[]
n=int(z[1])-1
for i in range(int(z[1])-1):
for j in range(int(z[0])):
length.append(n)
parts.append(int(nums[j])*n)
n=n-1
for i in range(len(parts)):
n=len(parts)-1
while(length[i]+length[n]<=int(z[1]) and n>=i):
if (length[i]+length[n]==int(z[1])):
possible.append(parts[i]+parts[n])
n=n-1
possible=set(possible)
possible=list(possible)
possible.sort()
string=""
for i in range(len(possible)):
string=string+str(possible[i])+" "
string=string[0:len(string)-1]
print(string)
``` | instruction | 0 | 101,647 | 10 | 203,294 |
No | output | 1 | 101,647 | 10 | 203,295 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A thief made his way to a shop.
As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.
The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).
Find all the possible total costs of products the thief can nick into his knapsack.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.
The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.
Output
Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.
Examples
Input
3 2
1 2 3
Output
2 3 4 5 6
Input
5 5
1 1 1 1 1
Output
5
Input
3 3
3 5 11
Output
9 11 13 15 17 19 21 25 27 33
Submitted Solution:
```
z=input().split()
n=int(z[0])
k=int(z[1])
nums=input().split()
parts=[int(nums[i]) for i in range(int(z[0]))]
parts1=[]
ini=parts.copy()
possible=[]
f=n
for i in range(n):
for j in range(i,n):
parts1.append(ini[i]+parts[j])
c=2
parts=parts1.copy()
parts1.clear()
while(c!=k):
pos=0
e=len(parts)
for i in range(3):
for j in range(pos,e):
parts1.append(ini[i]+parts[j])
pos=pos+f-i
parts=parts1.copy()
parts1.clear()
f=e
c=c+1
possible=parts.copy()
possible=set(possible)
possible=list(possible)
possible.sort()
string=""
for i in range(len(possible)):
string=string+str(possible[i])+" "
string=string[0:len(string)-1]
print(string)
``` | instruction | 0 | 101,648 | 10 | 203,296 |
No | output | 1 | 101,648 | 10 | 203,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000 | instruction | 0 | 101,688 | 10 | 203,376 |
Tags: data structures, implementation
Correct Solution:
```
n = int(input())
p = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
m =input()
c = map(int, input().split())
t = sorted(zip(p, a, b), key=lambda x: x[0])
t = [list(x) for x in t]
indxs = [None, 0, 0, 0]
r = []
for ci in c:
i = indxs[ci]
while i < n and ci not in (t[i][1], t[i][2]):
i += 1
if i == n:
r.append('-1')
else:
r.append(str(t[i][0]))
t[i][1], t[i][2] = 0, 0
i += 1
indxs[ci] = i
print(*r)
``` | output | 1 | 101,688 | 10 | 203,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000 | instruction | 0 | 101,689 | 10 | 203,378 |
Tags: data structures, implementation
Correct Solution:
```
n=int(input())
p=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
f=[0]*n
m=int(input())
c=list(map(int,input().split()))
l=[[],[],[],[]]
for i in range(n):
l[a[i]]+=[[p[i],i]]
if b[i]!=a[i]:
l[b[i]]+=[[p[i],i]]
for i in range(1,4):
l[i]=sorted(l[i])
i=[0,0,0,0]
ans=[]
for x in c:
while i[x]<len(l[x]) and f[l[x][i[x]][1]]:
i[x]+=1
if i[x]>=len(l[x]): ans+=[-1]
else: ans+=[l[x][i[x]][0]]; f[l[x][i[x]][1]]=1
print(*ans)
``` | output | 1 | 101,689 | 10 | 203,379 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000 | instruction | 0 | 101,690 | 10 | 203,380 |
Tags: data structures, implementation
Correct Solution:
```
import math as mt
import sys,string,bisect
input=sys.stdin.readline
import random
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : map(int,input().split())
I=lambda :int(input())
t=I()
p=L()
a=L()
b=L()
m=I()
c=L()
one=[]
two=[]
three=[]
for i in range(t):
if(a[i]==1 or b[i]==1):
one.append((p[i],i))
if(a[i]==2 or b[i]==2):
two.append((p[i],i))
if(a[i]==3 or b[i]==3):
three.append((p[i],i))
if(len(one)>0):
one.sort(key=lambda x:x[0])
if(len(two)>0):
two.sort(key=lambda x:x[0])
if(len(three)>0):
three.sort(key=lambda x:x[0])
i=0
j=0
k=0
v=[0]*t
for _ in c:
if(_==1):
while(i<len(one) and v[one[i][1]]):
i+=1
if(i<len(one)):
v[one[i][1]]=1
print(one[i][0],end=" ")
i+=1
else:
print(-1,end=" ")
elif(_==2):
while(j<len(two) and v[two[j][1]]):
j+=1
if(j<len(two)):
v[two[j][1]]=1
print(two[j][0],end=" ")
j+=1
else:
print(-1,end=" ")
else:
while(k<len(three) and v[three[k][1]]):
k+=1
if(k<len(three)):
v[three[k][1]]=1
print(three[k][0],end=" ")
k+=1
else:
print(-1,end=" ")
``` | output | 1 | 101,690 | 10 | 203,381 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000 | instruction | 0 | 101,691 | 10 | 203,382 |
Tags: data structures, implementation
Correct Solution:
```
import collections
def mp(): return map(int,input().split())
def lt(): return list(map(int,input().split()))
def pt(x): print(x)
def ip(): return input()
def it(): return int(input())
def sl(x): return [t for t in x]
def spl(x): return x.split()
def aj(liste, item): liste.append(item)
def bin(x): return "{0:b}".format(x)
def listring(l): return ' '.join([str(x) for x in l])
def ptlist(l): print(' '.join([str(x) for x in l]))
n = it()
p = lt()
a = lt()
b = lt()
m = it()
c = lt()
shirt = list(zip(p,a,b))
shirt.sort()
pointer = [-1,0,0,0]
l = []
for i in range(m):
cl = c[i]
while pointer[cl] < n and (shirt[pointer[cl]] == None or cl not in shirt[pointer[cl]][1:]):
pointer[cl] += 1
if pointer[cl] == n:
l.append(-1)
else:
l.append(shirt[pointer[cl]][0])
shirt[pointer[cl]] = None
ptlist(l)
``` | output | 1 | 101,691 | 10 | 203,383 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000 | instruction | 0 | 101,692 | 10 | 203,384 |
Tags: data structures, implementation
Correct Solution:
```
from collections import deque
class CodeforcesTask799BSolution:
def __init__(self):
self.result = ''
self.n = 0
self.prices = []
self.front_colors = []
self.back_colors = []
self.m = 0
self.customers = []
def read_input(self):
self.n = int(input())
self.prices = [int(x) for x in input().split(" ")]
self.front_colors = [int(x) for x in input().split(" ")]
self.back_colors = [int(x) for x in input().split(" ")]
self.m = int(input())
self.customers = [int(x) for x in input().split(" ")]
def process_task(self):
shop = [[[] for _ in range(3)] for __ in range(3)]
for t in range(self.n):
shop[self.front_colors[t] - 1][self.back_colors[t] - 1].append(self.prices[t])
for x in range(3):
for y in range(3):
shop[x][y].sort()
shop[x][y] = deque(shop[x][y])
res = []
for customer in self.customers:
mn_p = 10**10
selected = None
for back in range(3):
if shop[customer - 1][back]:
if shop[customer - 1][back][0] < mn_p:
selected = (customer - 1, back)
mn_p = shop[customer - 1][back][0]
for front in range(3):
if shop[front][customer - 1]:
if shop[front][customer - 1][0] < mn_p:
selected = (front, customer - 1)
mn_p = shop[front][customer - 1][0]
if selected:
res.append(shop[selected[0]][selected[1]].popleft())
else:
res.append(-1)
self.result = " ".join([str(x) for x in res])
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask799BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
``` | output | 1 | 101,692 | 10 | 203,385 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000 | instruction | 0 | 101,693 | 10 | 203,386 |
Tags: data structures, implementation
Correct Solution:
```
n = int(input())
p = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
P = []
for i in range(n):
P.append([p[i],a[i],b[i],0])
P = sorted(P)
last = [0,0,0,0]
m = int(input())
c = list(map(int,input().split()))
R = []
for i in c:
if last[i] == -1:
R.append(-1)
else:
for j in range(last[i],n):
if P[j][3] == 0:
if P[j][1] == i or P[j][2] == i:
R.append(P[j][0])
P[j][3] = 1
if j+1 < n:
last[i] = j+1
else:
last[i] = -1
break
else:
R.append(-1)
last[i] = -1
print(' '.join(map(str,R)))
``` | output | 1 | 101,693 | 10 | 203,387 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000 | instruction | 0 | 101,694 | 10 | 203,388 |
Tags: data structures, implementation
Correct Solution:
```
import sys
# 1. create color to tshirt map
# 2. sort tshirts by orice for each color
# 3. save the current tshirt for each color ready for the next buyer
# 4. simulate sale
def main(in_stream=sys.stdin):
max_col = 3
in_stream.readline()
plist = [int(tok) for tok in in_stream.readline().split()]
alist = [int(tok) for tok in in_stream.readline().split()]
blist = [int(tok) for tok in in_stream.readline().split()]
in_stream.readline()
clist = [int(tok) for tok in in_stream.readline().split()]
color_to_tshirts = {i: [] for i in range(1, max_col + 1)}
color_to_avail_index = {}
for triple in zip(plist, alist, blist):
tshirt = list(triple + (False, ))
color_to_tshirts[tshirt[1]].append(tshirt)
color_to_tshirts[tshirt[2]].append(tshirt)
# sort by pricest and assign cheapest
for c, tlist in color_to_tshirts.items():
tlist.sort(key=lambda ts: ts[0])
color_to_avail_index[c] = 0
# simulate the sale
result = []
for ci in clist:
the_list = color_to_tshirts[ci]
avail_index = color_to_avail_index[ci]
while avail_index < len(the_list) and the_list[avail_index][-1]:
avail_index += 1
color_to_avail_index[ci] = avail_index
if len(the_list) == avail_index:
result.append(-1)
continue
tsel = the_list[avail_index]
tsel[-1] = True
result.append(tsel[0])
return result
if __name__ == '__main__':
print(*main())
``` | output | 1 | 101,694 | 10 | 203,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000 | instruction | 0 | 101,695 | 10 | 203,390 |
Tags: data structures, implementation
Correct Solution:
```
"""
Code of Ayush Tiwari
Codeforces: servermonk
Codechef: ayush572000
"""
import sys
input = sys.stdin.buffer.readline
def solution():
n=int(input())
p=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
f=[0]*n
m=int(input())
c=list(map(int,input().split()))
l=[[],[],[],[]]
for i in range(n):
l[a[i]]+=[[p[i],i]]
if b[i]!=a[i]:
l[b[i]]+=[[p[i],i]]
for i in range(1,4):
l[i]=sorted(l[i])
i=[0,0,0,0]
ans=[]
for x in c:
while i[x]<len(l[x]) and f[l[x][i[x]][1]]:
i[x]+=1
if i[x]>=len(l[x]): ans+=[-1]
else: ans+=[l[x][i[x]][0]]; f[l[x][i[x]][1]]=1
print(*ans)
solution()
``` | output | 1 | 101,695 | 10 | 203,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000
Submitted Solution:
```
n = int(input())
p = list(map(int, input().split()))
b = list(map(int, input().split()))
a = list(map(int, input().split()))
m = int(input())
c = list(map(int, input().split()))
tp = {(i,j): [] for i in range(1,4) for j in range(i,4)}
for i in range(n):
k = [a[i], b[i]]
k.sort()
tp[tuple(k)].append(p[i])
for k,v in tp.items():
v.sort(reverse=True)
ans = []
for i in range(m):
mm = -1
for k,v in tp.items():
if c[i] in k:
if len(v) != 0:
if mm == -1 or tp[mm][-1] > v[-1]:
mm = k
if mm == -1:
ans.append('-1')
else:
z = tp[mm].pop()
ans.append(str(z))
print(' '.join(ans))
``` | instruction | 0 | 101,696 | 10 | 203,392 |
Yes | output | 1 | 101,696 | 10 | 203,393 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 30 07:23:34 2019
@author: avina
"""
n = int(input())
price = list(map(int, input().split()))
fron = list(map(int, input().split()))
back = list(map(int, input().split()))
e = [0,0,0]
group = []
for i in range(n):
group.append([price[i],fron[i],back[i]])
group.sort(key = lambda x:x[0])
m = int(input())
l = list(map(int, input().split()))
for i in range(m):
a = l[i]
j = e[a - 1]
t = True
while j < n:
if group[j][1] == a or group[j][2] == a:
print(group[j][0],end = ' ')
group[j][1] = 0;group[j][2] = 0
t = False
break
j+=1
if t:
print(-1,end=' ')
e[a-1] = j+1
``` | instruction | 0 | 101,697 | 10 | 203,394 |
Yes | output | 1 | 101,697 | 10 | 203,395 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000
Submitted Solution:
```
import operator
n = int(input())
p = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
input()
c = map(int, input().split())
t = sorted(zip(p, a, b), key=operator.itemgetter(0))
p = [None, 0, 0, 0]
r = []
for ci in c:
i = p[ci]
while i < n and (t[i] is None or ci not in (t[i][1:])):
i += 1
if i == n:
r.append('-1')
else:
r.append(str(t[i][0]))
t[i] = None
i += 1
p[ci] = i
print(' '.join(r))
``` | instruction | 0 | 101,698 | 10 | 203,396 |
Yes | output | 1 | 101,698 | 10 | 203,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000
Submitted Solution:
```
n = int(input())
ps = [int(a) for a in input().split()]
fronts = [int(a) for a in input().split()]
backs = [int(a) for a in input().split()]
shirts = list(zip(ps,fronts,backs))
shirts.sort()
bought = [False] * n
ptrs = [0] * 4
def mvptrs():
for i in range(1,len(ptrs)):
while ptrs[i] < n and ((shirts[ptrs[i]][1] != i and shirts[ptrs[i]][2] != i) or bought[ptrs[i]]):
ptrs[i] += 1
mvptrs()
m = int(input())
colors = [int(a) for a in input().split()]
def serve(i):
if ptrs[colors[i]] >= n:
print(-1, end='')
else:
print(shirts[ptrs[colors[i]]][0], end='')
bought[ptrs[colors[i]]] = True
mvptrs()
serve(0)
for i in range(1,m):
print(' ', end='')
serve(i)
print()
``` | instruction | 0 | 101,699 | 10 | 203,398 |
Yes | output | 1 | 101,699 | 10 | 203,399 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000
Submitted Solution:
```
shirt_num = int(input())
price_shirt = input().split()
upper_shirt = input().split()
down_shirt = input().split()
shop_num = int(input())
love_color = input().split()
dict_color = {}
for i in range(shirt_num):
dict_color[price_shirt[i]] = upper_shirt[i], down_shirt[i]
price = 0
ans = ""
for i in range(shop_num):
for (key, value) in dict_color.items():
if love_color[i] in value and price == 0:
price = key
elif price != 0 and key<price:
price = key
if price == 0:
ans+="-1 "
break
del dict_color[price]
ans += str(price) + " "
price = 0
print(ans)
``` | instruction | 0 | 101,700 | 10 | 203,400 |
No | output | 1 | 101,700 | 10 | 203,401 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000
Submitted Solution:
```
import heapq
n = int(input())
color = [[] for x in range(4)]
p = list(map(int,input().split()))
f = list(map(int,input().split()))
b = list(map(int,input().split()))
cost = [0 for _ in range((10))]
buy = int(input())
ans = []
buyColor = list(map(int,input().split()))
done = []
heapq.heapify(done)
for i in range(1,4):
heapq.heapify(color[i])
for i in range(n):
front = f[i]
back = b[i]
if front!=back:
heapq.heappush(color[front],p[i])
heapq.heappush(color[back],p[i])
else:
heapq.heappush(color[front],p[i])
for need in buyColor:
while(1):
if len(color[need])>0:
pay = heapq.heappop(color[need])
if len(done)>0:
l=0
h = len(done)-1
while(l!=h and done[h]!=pay):
next = (l+h)//2
if done[next]<pay:
l = next+1
else:
h = next
else:
done.append(pay)
h = 0
ans.append(pay)
break
if done[h]!= pay:
ans.append(pay)
done.append(pay)
break
else:
ans.append(-1)
break
print(*ans)
``` | instruction | 0 | 101,701 | 10 | 203,402 |
No | output | 1 | 101,701 | 10 | 203,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000
Submitted Solution:
```
n = int(input())
Price = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
m = int(input())
Color = list(map(int, input().split()))
Res = []
for i in Color:
Buy = []
for a in range(len(Price)):
if A[a] == i:
Buy.append(a)
if B[a] == i:
if Buy == []:
Buy.append(a)
elif Buy[-1] != a:
Buy.append(a)
K = []
for b in Buy:
K.append(Price[b])
if K ==[]:
Res.append(-1)
else:
Res.append(min(K))
Price.remove(min(K))
print(" ".join(map(str, Res)))
``` | instruction | 0 | 101,702 | 10 | 203,404 |
No | output | 1 | 101,702 | 10 | 203,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.
Examples
Input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
Output
200 400 300 500 911 -1
Input
2
1000000000 1
1 1
1 2
2
2 1
Output
1 1000000000
Submitted Solution:
```
def preferences(p, a, b, c):
ans = []
for i in range(len(c)):
cost = -1
j_container = -1
for j in range(len(p)):
if cost == -1:
if (a[j] == c[i] or b[j] == c[i]) and p[j] != -1:
cost = p[j]
j_container = j
else:
if (a[j] == c[i] or b[j] == c[i]) and p[j] < cost and p[j] != -1:
cost = p[j]
j_container = j
ans.append(cost)
p[j_container] = -1
return ans
if __name__ == '__main__':
n = int(input())
p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
m = int(input())
c = [int(i) for i in input().split()]
out = preferences(p, a, b, c)
print(" ".join(str(x) for x in out))
``` | instruction | 0 | 101,703 | 10 | 203,406 |
No | output | 1 | 101,703 | 10 | 203,407 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.