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.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives. | instruction | 0 | 55,108 | 10 | 110,216 |
Tags: greedy
Correct Solution:
```
def f(n):
def toTri(x):
if x <= 2:
return [x]
l = toTri(x // 3)
l.append(x % 3)
return l
digits = toTri(n)
for i in range(len(digits) - 1, -1, -1):
if digits[i] != 0:
break
coins = 1
mul = 1
for j in range(i - 1, -1, -1):
coins += mul * digits[j]
mul *= 3
return coins
n = int(input())
print(f(n))
``` | output | 1 | 55,108 | 10 | 110,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives. | instruction | 0 | 55,109 | 10 | 110,218 |
Tags: greedy
Correct Solution:
```
n,a=int(input()),1
while n%a==0:a*=3
print(n//a+1)
``` | output | 1 | 55,109 | 10 | 110,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives. | instruction | 0 | 55,110 | 10 | 110,220 |
Tags: greedy
Correct Solution:
```
n = int(input())
p = 1
while n % p == 0: p *= 3
print(n//p + 1)
``` | output | 1 | 55,110 | 10 | 110,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
Submitted Solution:
```
k = 1
n = int(input())
while n%k==0:
k*=3
print(n//k+1)
``` | instruction | 0 | 55,111 | 10 | 110,222 |
Yes | output | 1 | 55,111 | 10 | 110,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
Submitted Solution:
```
import sys
from math import *
from fractions import gcd
readints=lambda:map(int, input().strip('\n').split())
n=int(input())
b=1
ans=1
while b<n:
if n%b != 0:
ans = max(ans, n//b + 1)
b *= 3
print(ans)
``` | instruction | 0 | 55,112 | 10 | 110,224 |
Yes | output | 1 | 55,112 | 10 | 110,225 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
Submitted Solution:
```
def readln(): return tuple(map(int, input().split()))
n, = readln()
p3 = []
t = n
while t:
p3.append(t % 3)
t //= 3
for i in range(len(p3)):
if p3[i]:
p3[i] -= 1
tmp = 1 + sum([v * 3 ** j for j, v in enumerate(p3[i + 1:])])
while p3[i] and 3**i * (p3[i] - 1) + 3**(i + 1) * tmp > n:
p3[i] -= 1
print(max(1, p3[i] + tmp))
break
``` | instruction | 0 | 55,113 | 10 | 110,226 |
Yes | output | 1 | 55,113 | 10 | 110,227 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
Submitted Solution:
```
n = int(input())
res = 1
x = 1
while x <= n:
if n%x != 0:
res = max(res, (n+x-1)//x)
x *= 3
print(res)
``` | instruction | 0 | 55,114 | 10 | 110,228 |
Yes | output | 1 | 55,114 | 10 | 110,229 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
Submitted Solution:
```
from math import ceil
n = int(input())
a = 1
while (n % a == 0):
a *= 3
print(ceil(n / a))
``` | instruction | 0 | 55,115 | 10 | 110,230 |
No | output | 1 | 55,115 | 10 | 110,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
Submitted Solution:
```
from math import ceil, floor
n = int(input())
a = 1
degree = 0
while (n % a == 0):
a *= 3
degree += 1
print(floor(n / degree))
``` | instruction | 0 | 55,116 | 10 | 110,232 |
No | output | 1 | 55,116 | 10 | 110,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
Submitted Solution:
```
n = int(input())
while n % 3 == 0:
n /= 3
print(n // 3 + 1)
``` | instruction | 0 | 55,117 | 10 | 110,234 |
No | output | 1 | 55,117 | 10 | 110,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.
One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
Input
The single line contains a single integer n (1 ≤ n ≤ 1017).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
Examples
Input
1
Output
1
Input
4
Output
2
Note
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
Submitted Solution:
```
#-------------------------------------------------------------------------------
# Name: Codeforces
# Author: Gogol
#-------------------------------------------------------------------------------
import sys
from math import *
def solve():
n = int(input())
x = int(floor(log(n,3)))
p = 3**x
ans = 0
while(n >= 3):
if (n >= p):
n-=p
ans += 1
else:
p //= 3
if (n % 3):
ans += 1
print(ans)
solve()
``` | instruction | 0 | 55,118 | 10 | 110,236 |
No | output | 1 | 55,118 | 10 | 110,237 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909 | instruction | 0 | 55,269 | 10 | 110,538 |
Tags: binary search
Correct Solution:
```
def energy(acc, level, perc_loss):
surplus = sum([max(x - level, 0) for x in acc])
en_cost = sum([(abs(min(0, x - level)) * 100) / (100 - perc_loss) for x in acc])
return surplus >= en_cost
class CodeforcesTask68BSolution:
def __init__(self):
self.result = ''
self.n_k = []
self.accumulators = []
def read_input(self):
self.n_k = [int(x) for x in input().split(" ")]
self.accumulators = [int(x) for x in input().split(" ")]
def process_task(self):
l = 0
r = sum(self.accumulators) / self.n_k[0]
while r - l >= 0.000_0001:
mid = l + (r - l) / 2
if energy(self.accumulators, mid, self.n_k[1]):
l = mid
else:
r = mid
self.result = str(r)
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask68BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
``` | output | 1 | 55,269 | 10 | 110,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by <image> units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
Input
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
Examples
Input
3 50
4 2 1
Output
2.000000000
Input
2 90
1 11
Output
1.909090909 | instruction | 0 | 55,273 | 10 | 110,546 |
Tags: binary search
Correct Solution:
```
nk = list(map(int,input("").split()))
n, k = nk
aList = list(map(int,input("").split()))
aList.sort()
def isPossible(val, aList, k):
rightSum = 0
leftSum = 0
for item in aList:
if item > val:
rightSum += item - val
else:
leftSum += val - item
return rightSum - rightSum * k / 100.0 >= leftSum
left = aList[0]
right = aList[-1]
while right - left > 10**-6:
mid = (left + right) / 2
if isPossible(mid, aList, k):
left = mid
else:
right = mid
print("%.9f"%(left))
``` | output | 1 | 55,273 | 10 | 110,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan. | instruction | 0 | 56,131 | 10 | 112,262 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n, m = map(int, input().split())
mn = float('inf')
for _ in range(n):
a, b = map(int, input().split())
mn = min(mn, m*a/b)
print(mn)
``` | output | 1 | 56,131 | 10 | 112,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan. | instruction | 0 | 56,132 | 10 | 112,264 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n,m=map(int,input().split())
low=0
for i in range(n):
a,b=map(int,input().split())
if low==0:
low=a/b
if low>a/b:
low=a/b
print('%.8f' % (m*low))
``` | output | 1 | 56,132 | 10 | 112,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan. | instruction | 0 | 56,133 | 10 | 112,266 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n, m = map(int, input().split())
a, b = map(int, input().split())
mini = a / b
for i in range(1, n):
a, b = map(int, input().split())
mini = min(mini, a / b)
print(m * mini)
``` | output | 1 | 56,133 | 10 | 112,267 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan. | instruction | 0 | 56,134 | 10 | 112,268 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n, m = map(int, input().split())
asd = 10 ** 9
for i in range(n):
a, b = map(int, input().split())
if a / b < asd:
asd = a / b
print(asd * m)
``` | output | 1 | 56,134 | 10 | 112,269 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan. | instruction | 0 | 56,135 | 10 | 112,270 |
Tags: brute force, greedy, implementation
Correct Solution:
```
k, v = map(int, input().split())
result = []
for i in range(k):
u, m = map(int, input().split())
result.append(u / m)
minPrice = min(result)
print(minPrice * v)
``` | output | 1 | 56,135 | 10 | 112,271 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan. | instruction | 0 | 56,136 | 10 | 112,272 |
Tags: brute force, greedy, implementation
Correct Solution:
```
lst = []
n ,m = [int(i) for i in input().split()]
for i in range(n):
a, b = [int(i) for i in input().split()]
lst.append(a/b)
print(min(lst)*m)
``` | output | 1 | 56,136 | 10 | 112,273 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan. | instruction | 0 | 56,137 | 10 | 112,274 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n, m = map(int, input().split())
costs = []
for _ in range(n):
a, b = map(int, input().split())
costs.append((a * m) / b)
print(min(costs))
``` | output | 1 | 56,137 | 10 | 112,275 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan. | instruction | 0 | 56,138 | 10 | 112,276 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n,m= input().split(); n,m= int(n), int(m)
li=[]
for i in range(n):
a,b= input().split(); a,b= int(a), int(b)
out= (a*m)/b
li.append(out)
print (round((min(li)),8))
``` | output | 1 | 56,138 | 10 | 112,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan.
Submitted Solution:
```
n, m = map(int, input().split())
x = float('inf')
for _ in range(n):
a, b = map(int, input().split())
x = min(a / b, x)
print(m * x)
``` | instruction | 0 | 56,139 | 10 | 112,278 |
Yes | output | 1 | 56,139 | 10 | 112,279 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan.
Submitted Solution:
```
n , m = map(int,input().split())
compare_list = []
for i in range(n):
a , b = map(int,input().split())
compare_list.append(a / b)
ans = min(compare_list) * m
print(ans)
``` | instruction | 0 | 56,140 | 10 | 112,280 |
Yes | output | 1 | 56,140 | 10 | 112,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan.
Submitted Solution:
```
from math import*
sum=int()
mi=1000
a,b=map(int,input().split())
for i in range(1,a+1,1):
x,y=map(int,input().split())
sum=x/y
if(sum<mi):
mi=sum
print("%.8f" % (mi*b))
``` | instruction | 0 | 56,141 | 10 | 112,282 |
Yes | output | 1 | 56,141 | 10 | 112,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan.
Submitted Solution:
```
n, m = map(int, input().split())
k = []
for i in range(n):
a, b = map(int, input().split())
k.append(m * a / b)
k.sort()
print(k[0])
``` | instruction | 0 | 56,142 | 10 | 112,284 |
Yes | output | 1 | 56,142 | 10 | 112,285 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan.
Submitted Solution:
```
n,m=map(int,input().split())
minn=10**100
for i in range(n):
a,b=map(int,input().split())
if a/b<minn:
minn=a/b
print(m*a/b)
``` | instruction | 0 | 56,143 | 10 | 112,286 |
No | output | 1 | 56,143 | 10 | 112,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan.
Submitted Solution:
```
a = list(map(int,input().split()))
c = []
for i in range(a[0]):
b = list(map(int,input().split()))
if a[1] > b[1]:
c.append(a[1]/b[1]*b[0])
else:
c.append(b[0]/b[1])
print(min(c))
``` | instruction | 0 | 56,144 | 10 | 112,288 |
No | output | 1 | 56,144 | 10 | 112,289 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan.
Submitted Solution:
```
import sys
read = input().split(" ")
n = int(read[0])
m = int(read[1])
mn = -sys.maxsize
for i in range(n):
read = input().split(" ")
a = int(read[0])
b = int(read[1])
if(a/b < mn):
mn = a/b
print(a/b * m)
``` | instruction | 0 | 56,145 | 10 | 112,290 |
No | output | 1 | 56,145 | 10 | 112,291 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don't need to care about what "yuan" is), the same as a/b yuan for a kilo.
Now imagine you'd like to buy m kilos of apples. You've asked n supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets.
Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.
The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.
Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6}.
Formally, let your answer be x, and the jury's answer be y. Your answer is considered correct if \frac{|x - y|}{max{(1, |y|)}} ≤ 10^{-6}.
Examples
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5/3 yuan.
In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98/99 yuan.
Submitted Solution:
```
n,m=map(int,input().split())
a,b=map(int,input().split())
mini=round(b/a,8)
for i in range(n-1):
a,b=map(int,input().split())
if mini>a/b:
mini=a/b
print(round(m*mini,8))
``` | instruction | 0 | 56,146 | 10 | 112,292 |
No | output | 1 | 56,146 | 10 | 112,293 |
Provide a correct Python 3 solution for this coding contest problem.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942 | instruction | 0 | 56,211 | 10 | 112,422 |
"Correct Solution:
```
I=lambda:map(int,input().split());n,m=I();d=[0]+[9**9]*2**n
while m:
m-=1;a,_=I();s=sum(1<<c-1for c in I())
for j in range(2**n):d[s|j]=min(d[s|j],d[j]+a)
print(d[-2]%9**9or-1)
``` | output | 1 | 56,211 | 10 | 112,423 |
Provide a correct Python 3 solution for this coding contest problem.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942 | instruction | 0 | 56,212 | 10 | 112,424 |
"Correct Solution:
```
n, m = map(int, input().split())
dp = [float("inf") for i in range(2 ** n)]
dp[0] = 0
for i in range(m):
a, b = map(int, input().split())
c = list(map(int, input().split()))
num = 0
for j in range(b):
num += 2 ** (c[j] - 1)
for k in range(2 ** n):
dp[k | num] = min(dp[k | num], dp[k] + a)
if dp[2 ** n - 1] == float("inf"):
print(-1)
else:
print(dp[2 ** n - 1])
``` | output | 1 | 56,212 | 10 | 112,425 |
Provide a correct Python 3 solution for this coding contest problem.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942 | instruction | 0 | 56,213 | 10 | 112,426 |
"Correct Solution:
```
INF = float("inf")
N, M = map(int, input().split())
dp = [INF] * (2 ** N)
dp[0] = 0
for _ in range(M):
a, b = map(int, input().split())
C = [int(i) for i in input().split()]
mask = sum(1 << (c - 1) for c in C)
for i, dpi in enumerate(dp):
if dp[i | mask] > dpi + a:
dp[i | mask] = dpi + a
ans = dp[-1]
print(-1 if ans == INF else ans)
``` | output | 1 | 56,213 | 10 | 112,427 |
Provide a correct Python 3 solution for this coding contest problem.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942 | instruction | 0 | 56,214 | 10 | 112,428 |
"Correct Solution:
```
n,m=map(int,input().split())
keys=[None]*m
INF = 10**10
dp=[INF]*(2**n)
dp[0]=0
for i in range(m):
a,b=map(int,input().split())
c=list(map(int,input().split()))
cond=0
for j in c:
cond+=2**(j-1)
keys[i]=[a,cond]
for key in keys:
for sta in range(2**n):
dp[sta|key[1]]=min(dp[sta|key[1]],dp[sta]+key[0])
print(dp[-1] if dp[-1] != INF else -1)
``` | output | 1 | 56,214 | 10 | 112,429 |
Provide a correct Python 3 solution for this coding contest problem.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942 | instruction | 0 | 56,215 | 10 | 112,430 |
"Correct Solution:
```
n,m = map(int,input().split())
INF = 10 ** 9
cur = [INF] * (2**n)
cur[0] = 0
for i in range(m):
a,b = map(int,input().split())
c = 0
for j in map(int,input().split()):
c += 2**(j-1)
pre,cur = cur,[INF]*(2**n)
for j in range(2**n):
cur[j] = min(pre[j],cur[j])
jc = j|c
cur[jc] = min(pre[j]+a,cur[jc])
if cur[-1] == INF:
print(-1)
else:
print(cur[-1])
``` | output | 1 | 56,215 | 10 | 112,431 |
Provide a correct Python 3 solution for this coding contest problem.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942 | instruction | 0 | 56,216 | 10 | 112,432 |
"Correct Solution:
```
N,M=map(int,input().split())
INF=10**18
dp=[INF]*(1<<N)
dp[0]=0
cost=[None]*M
target=[0]*M
for i in range(M):
cost[i],b=map(int,input().split())
c=[int(x) for x in input().split()]
for j in c:
target[i]=target[i]|(1<<(j-1))
for s in range(1<<N):
for i in range(M):
ns=s|target[i]
dp[ns]=min(dp[ns],dp[s]+cost[i])
if dp[-1]==INF:
print(-1)
else :
print(dp[-1])
``` | output | 1 | 56,216 | 10 | 112,433 |
Provide a correct Python 3 solution for this coding contest problem.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942 | instruction | 0 | 56,217 | 10 | 112,434 |
"Correct Solution:
```
n,m=map(int,input().split())
cost=[0]*m
keys=[0]*m
for i in range(m):
a,b=map(int,input().split())
cost[i]=a
k=list(map(int,input().split()))
for j in range(b):
keys[i]|=(1<<(k[j]-1))
INF=10**9+1
dp=[INF]*(2**n)
dp[0]=0
for i in range(len(keys)):
for j in range(len(dp)-1,-1,-1):
if dp[j]!=INF:
dp[j|keys[i]]=min(dp[j|keys[i]],dp[j]+cost[i])
if dp[-1]!=INF:
print(dp[-1])
else:
print(-1)
``` | output | 1 | 56,217 | 10 | 112,435 |
Provide a correct Python 3 solution for this coding contest problem.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942 | instruction | 0 | 56,218 | 10 | 112,436 |
"Correct Solution:
```
n,m = map(int,input().split())
x = 2**n
dp = [1000000000 for _ in range(x)]
dp[0] = 0
for i in range(m):
ai,bi = map(int,input().split())
c = list(map(int,input().split()))
bit = 0
for j in range(bi):
bit += 2**(c[j]-1)
for k in range(x):
dp[k | bit] = min(dp[k | bit],dp[k] + ai)
if dp[x-1] == 1000000000:
print(-1)
else:
print(dp[x-1])
``` | output | 1 | 56,218 | 10 | 112,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942
Submitted Solution:
```
n, m = [int(item) for item in input().split()]
INF = 10 ** 10
dp = [INF] * (2 ** n)
dp[0] = 0
for i in range(m):
a, b = [int(item) for item in input().split()]
c = [int(item) - 1 for item in input().split()]
bit = 0
for item in c:
bit |= 1 << item
for j in range(2 ** n):
dp[j | bit] = min(dp[j | bit], dp[j] + a)
if dp[-1] == INF:
print(-1)
else:
print(dp[-1])
``` | instruction | 0 | 56,219 | 10 | 112,438 |
Yes | output | 1 | 56,219 | 10 | 112,439 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942
Submitted Solution:
```
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
INF = 1<<30
dp = [INF]*(1<<N)
dp[0] = 0
K = []
for _ in [0]*M:
a, b = map(int, input().split())
*C, = map(int, input().split())
h = 0
for c in C:
h ^= 1 << (c-1)
for i in range(1<<N):
nxt = i|h
dp[nxt] = min(dp[nxt], dp[i]+a)
ans = dp[(1<<N)-1]
print(-1 if ans == INF else ans)
``` | instruction | 0 | 56,220 | 10 | 112,440 |
Yes | output | 1 | 56,220 | 10 | 112,441 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942
Submitted Solution:
```
n,m=map(int,input().split())
arr=[]
for _ in range(m):
a,b=map(int,input().split())
tl=list(map(int,input().split()))
tmp=0
for val in tl:
tmp+=2**(val-1)
arr.append((a,tmp))
dp=[10**18]*(2**n)
dp[0]=0
for val,flag in arr:
for i in range(2**n-1,-1,-1):
if dp[i]==10**18:
continue
else:
dp[i|flag]=min(dp[i|flag],dp[i]+val)
if dp[-1]==10**18:
print(-1)
else:
print(dp[-1])
``` | instruction | 0 | 56,221 | 10 | 112,442 |
Yes | output | 1 | 56,221 | 10 | 112,443 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942
Submitted Solution:
```
def min(a,b):
return a if a<=b else b
def main():
n,m=map(int,input().split())
inf=10**9
cost=[0]*m
dp=[inf]*(1<<n)
dp[0]=0
for i in range(m):
a,b=map(int,input().split())
t=sum([1<<(int(x)-1) for x in input().split()])
for s in range(1<<n):
dp[t|s]=min(dp[t|s], dp[s]+a)
print(-1 if dp[-1]==inf else dp[-1])
if __name__=='__main__':
main()
``` | instruction | 0 | 56,222 | 10 | 112,444 |
Yes | output | 1 | 56,222 | 10 | 112,445 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942
Submitted Solution:
```
N, M = [int(i) for i in input().split()]
keys = {}
for i in range(M):
keys[i] = []
for j in range(2):
if j == 0:
n, m = [int(i) for i in input().split()]
keys[i].append(n)
else:
op = set([int(i) for i in input().split()])
keys[i].append(op)
memo = {}
def rec(i, opened):
if len(opened) == N:
return 0
elif i == M:
return float("inf")
if i in memo and tuple(opened) in memo[i]:
return memo[i][tuple(opened)]
val1 = keys[i][0] + rec(i+1, opened | keys[i][1])
val2 = rec(i+1, opened)
val = min(val1, val2)
if i not in memo:
memo[i] = {}
memo[i][tuple(opened)] = val
return val
val = rec(0, set())
if val == float("inf"):
print(-1)
else:
print(val)
``` | instruction | 0 | 56,223 | 10 | 112,446 |
No | output | 1 | 56,223 | 10 | 112,447 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942
Submitted Solution:
```
import sys
from collections import defaultdict
from itertools import product, permutations, combinations
from time import time
sys.setrecursionlimit(10**7)
def input():
return sys.stdin.readline()[:-1]
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
q = list(range(N))
c = defaultdict(int)
t = [0]*N
r = 0
a = time()
while q:
b = time()
if b - a > 0.9:
print(N*(N-1)//2)
exit()
r += 1
nq = []
for i in q:
j = A[i][t[i]] - 1
k = i
if k < j:
j, k = k, j
c[(j, k)] += 1
if c[(j, k)] == 2:
t[j] += 1
t[k] += 1
if t[j] < N-1:
nq.append(j)
if t[k] < N-1:
nq.append(k)
q = nq
for tt in t:
if tt != N-1:
print(-1)
exit()
print(r)
``` | instruction | 0 | 56,224 | 10 | 112,448 |
No | output | 1 | 56,224 | 10 | 112,449 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942
Submitted Solution:
```
import sys
import heapq
def solve():
input = sys.stdin.readline
N, M = map(int, input().split())
can_open = dict()
key_count = dict()
for i in range(N):
key_count[i] = 0
q = []
heapq.heapify(q)
cost = 0
for i in range(M):
a, b = map(int, input().split())
cost += a
key = [int(k) - 1 for k in input().split()]
qk = [-a, b]
for k in key:
qk.append(k)
key_count[k] += 1
heapq.heappush(q, qk)
for c in key_count:
if key_count[c] == 0:
print(-1)
break
else:
while q:
now_key = heapq.heappop(q)
can_delete = False
for k in now_key[2:]:
if key_count[k] == 1: break
else:
cost += now_key[0]
for k in now_key[2:]: key_count[k] -= 1
print(cost)
return 0
if __name__ == "__main__":
solve()
``` | instruction | 0 | 56,225 | 10 | 112,450 |
No | output | 1 | 56,225 | 10 | 112,451 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Constraints
* All values in input are integers.
* 1 \leq N \leq 12
* 1 \leq M \leq 10^3
* 1 \leq a_i \leq 10^5
* 1 \leq b_i \leq N
* 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.
Examples
Input
2 3
10 1
1
15 1
2
30 2
1 2
Output
25
Input
12 1
100000 1
2
Output
-1
Input
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Output
69942
Submitted Solution:
```
n, m = map(int, input().split())
can_open = []
cost = []
for _ in range(m):
a, b = map(int, input().split())
cost.append(a)
it = map(int, input().split())
x = 0
for c in it:
x += 1 << (c-1)
can_open.append(x)
n_bit = 1 << n
INF = 10**18
dp = [[INF]*n_bit for _ in range(m+1)]
dp[0][0] = 0
# 配る
for i in range(m):
for j in range(n_bit):
dp[i+1][j] = min(dp[i+1][j], dp[i][j])
if (j | can_open[i]) <= n_bit:
dp[i+1][j | can_open[i]
] = min(dp[i+1][j | can_open[i]], dp[i+1][j]+cost[i])
print(dp[-1][-1] if dp[-1][-1] != INF else -1)
``` | instruction | 0 | 56,226 | 10 | 112,452 |
No | output | 1 | 56,226 | 10 | 112,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people in this world, conveniently numbered 1 through n. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let d(a,b) denote the debt of a towards b, or 0 if there is no such debt.
Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money.
When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done:
1. Let d(a,b) > 0 and d(c,d) > 0 such that a ≠ c or b ≠ d. We can decrease the d(a,b) and d(c,d) by z and increase d(c,b) and d(a,d) by z, where 0 < z ≤ min(d(a,b),d(c,d)).
2. Let d(a,a) > 0. We can set d(a,a) to 0.
The total debt is defined as the sum of all debts:
$$$\Sigma_d = ∑_{a,b} d(a,b)$$$
Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt.
Input
The first line contains two space separated integers n (1 ≤ n ≤ 10^5) and m (0 ≤ m ≤ 3⋅ 10^5), representing the number of people and the number of debts, respectively.
m lines follow, each of which contains three space separated integers u_i, v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), d_i (1 ≤ d_i ≤ 10^9), meaning that the person u_i borrowed d_i burles from person v_i.
Output
On the first line print an integer m' (0 ≤ m' ≤ 3⋅ 10^5), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint.
After that print m' lines, i-th of which contains three space separated integers u_i, v_i, d_i, meaning that the person u_i owes the person v_i exactly d_i burles. The output must satisfy 1 ≤ u_i, v_i ≤ n, u_i ≠ v_i and 0 < d_i ≤ 10^{18}.
For each pair i ≠ j, it should hold that u_i ≠ u_j or v_i ≠ v_j. In other words, each pair of people can be included at most once in the output.
Examples
Input
3 2
1 2 10
2 3 5
Output
2
1 2 5
1 3 5
Input
3 3
1 2 10
2 3 15
3 1 10
Output
1
2 3 5
Input
4 2
1 2 12
3 4 8
Output
2
1 2 12
3 4 8
Input
3 4
2 3 1
2 3 2
2 3 4
2 3 8
Output
1
2 3 15
Note
In the first example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 2, d = 3 and z = 5. The resulting debts are: d(1, 2) = 5, d(2, 2) = 5, d(1, 3) = 5, all other debts are 0;
2. Perform an operation of the second type with a = 2. The resulting debts are: d(1, 2) = 5, d(1, 3) = 5, all other debts are 0.
In the second example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 3, d = 1 and z = 10. The resulting debts are: d(3, 2) = 10, d(2, 3) = 15, d(1, 1) = 10, all other debts are 0;
2. Perform an operation of the first type with a = 2, b = 3, c = 3, d = 2 and z = 10. The resulting debts are: d(2, 2) = 10, d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
3. Perform an operation of the second type with a = 2. The resulting debts are: d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
4. Perform an operation of the second type with a = 3. The resulting debts are: d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
5. Perform an operation of the second type with a = 1. The resulting debts are: d(2, 3) = 5, all other debts are 0. | instruction | 0 | 56,504 | 10 | 113,008 |
Tags: constructive algorithms, data structures, graphs, greedy, implementation, math, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
D=[0]*(n+1)
for i in range(m):
x,y,z=map(int,input().split())
D[x]+=z
D[y]-=z
PLUS=[]
MINUS=[]
for i in range(n+1):
if D[i]>0:
PLUS.append([D[i],i])
elif D[i]<0:
MINUS.append([-D[i],i])
Pind=0
Mind=0
PMAX=len(PLUS)
MMAX=len(MINUS)
ANS=[]
while Pind<PMAX and Mind<MMAX:
if PLUS[Pind][0]>MINUS[Mind][0]:
PLUS[Pind][0]-=MINUS[Mind][0]
ANS.append((PLUS[Pind][1],MINUS[Mind][1],MINUS[Mind][0]))
Mind+=1
elif PLUS[Pind][0]==MINUS[Mind][0]:
ANS.append((PLUS[Pind][1],MINUS[Mind][1],MINUS[Mind][0]))
Mind+=1
Pind+=1
else:
MINUS[Mind][0]-=PLUS[Pind][0]
ANS.append((PLUS[Pind][1],MINUS[Mind][1],PLUS[Pind][0]))
Pind+=1
print(len(ANS))
for x,y,z in ANS:
print(x,y,z)
``` | output | 1 | 56,504 | 10 | 113,009 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people in this world, conveniently numbered 1 through n. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let d(a,b) denote the debt of a towards b, or 0 if there is no such debt.
Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money.
When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done:
1. Let d(a,b) > 0 and d(c,d) > 0 such that a ≠ c or b ≠ d. We can decrease the d(a,b) and d(c,d) by z and increase d(c,b) and d(a,d) by z, where 0 < z ≤ min(d(a,b),d(c,d)).
2. Let d(a,a) > 0. We can set d(a,a) to 0.
The total debt is defined as the sum of all debts:
$$$\Sigma_d = ∑_{a,b} d(a,b)$$$
Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt.
Input
The first line contains two space separated integers n (1 ≤ n ≤ 10^5) and m (0 ≤ m ≤ 3⋅ 10^5), representing the number of people and the number of debts, respectively.
m lines follow, each of which contains three space separated integers u_i, v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), d_i (1 ≤ d_i ≤ 10^9), meaning that the person u_i borrowed d_i burles from person v_i.
Output
On the first line print an integer m' (0 ≤ m' ≤ 3⋅ 10^5), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint.
After that print m' lines, i-th of which contains three space separated integers u_i, v_i, d_i, meaning that the person u_i owes the person v_i exactly d_i burles. The output must satisfy 1 ≤ u_i, v_i ≤ n, u_i ≠ v_i and 0 < d_i ≤ 10^{18}.
For each pair i ≠ j, it should hold that u_i ≠ u_j or v_i ≠ v_j. In other words, each pair of people can be included at most once in the output.
Examples
Input
3 2
1 2 10
2 3 5
Output
2
1 2 5
1 3 5
Input
3 3
1 2 10
2 3 15
3 1 10
Output
1
2 3 5
Input
4 2
1 2 12
3 4 8
Output
2
1 2 12
3 4 8
Input
3 4
2 3 1
2 3 2
2 3 4
2 3 8
Output
1
2 3 15
Note
In the first example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 2, d = 3 and z = 5. The resulting debts are: d(1, 2) = 5, d(2, 2) = 5, d(1, 3) = 5, all other debts are 0;
2. Perform an operation of the second type with a = 2. The resulting debts are: d(1, 2) = 5, d(1, 3) = 5, all other debts are 0.
In the second example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 3, d = 1 and z = 10. The resulting debts are: d(3, 2) = 10, d(2, 3) = 15, d(1, 1) = 10, all other debts are 0;
2. Perform an operation of the first type with a = 2, b = 3, c = 3, d = 2 and z = 10. The resulting debts are: d(2, 2) = 10, d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
3. Perform an operation of the second type with a = 2. The resulting debts are: d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
4. Perform an operation of the second type with a = 3. The resulting debts are: d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
5. Perform an operation of the second type with a = 1. The resulting debts are: d(2, 3) = 5, all other debts are 0. | instruction | 0 | 56,505 | 10 | 113,010 |
Tags: constructive algorithms, data structures, graphs, greedy, implementation, math, two pointers
Correct Solution:
```
from collections import deque
import sys
input = sys.stdin.buffer.readline
# input = sys.stdin.readline
# x = list ( map(int,input().split()))
n, m = map(int,input().split())
balance = [ 0 for i in range(n)]
for _ in range(m):
# 1 2 10: 1 bayad 10 ta be 2 bede
u, v ,d = map(int,input().split())
balance[u-1] -=d
balance[v-1] +=d
# print(balance)
bgive = [[x,-balance[x]] for x in range(n) if balance[x]<0 ]#giver
btake = [[x,balance[x]] for x in range(n) if balance[x]>0 ]#taker
# print(bgive)
# print(btake)
tdbt =0
for x in balance:
if x>0:
tdbt+=x
else:
tdbt-=x
# print(tdbt)
# print("_______-")
# print(f'bgive: {bgive}')
# print(f'btake: {btake}')
i = 0
j = 0
total = 0
printall =[]
printall.append
while(tdbt>0):
# print(i,j)
if(bgive[i][1]>btake[j][1]):
printall.append([bgive[i][0]+1,btake[j][0]+1,btake[j][1]])
bgive[i][1]-=btake[j][1]
tdbt-=(2*btake[j][1])
j+=1
elif bgive[i][1]<btake[j][1]:
printall.append([bgive[i][0]+1,btake[j][0]+1,bgive[i][1]])
btake[j][1]-=bgive[i][1]
tdbt-=(2*bgive[i][1])
i+=1
else:
printall.append([bgive[i][0]+1,btake[j][0]+1,btake[j][1]])
tdbt-=(2*btake[j][1])
j+=1
i+=1
total+=1
print(total)
for x in printall:
print(" ".join(map(str,x)))
``` | output | 1 | 56,505 | 10 | 113,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people in this world, conveniently numbered 1 through n. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let d(a,b) denote the debt of a towards b, or 0 if there is no such debt.
Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money.
When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done:
1. Let d(a,b) > 0 and d(c,d) > 0 such that a ≠ c or b ≠ d. We can decrease the d(a,b) and d(c,d) by z and increase d(c,b) and d(a,d) by z, where 0 < z ≤ min(d(a,b),d(c,d)).
2. Let d(a,a) > 0. We can set d(a,a) to 0.
The total debt is defined as the sum of all debts:
$$$\Sigma_d = ∑_{a,b} d(a,b)$$$
Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt.
Input
The first line contains two space separated integers n (1 ≤ n ≤ 10^5) and m (0 ≤ m ≤ 3⋅ 10^5), representing the number of people and the number of debts, respectively.
m lines follow, each of which contains three space separated integers u_i, v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), d_i (1 ≤ d_i ≤ 10^9), meaning that the person u_i borrowed d_i burles from person v_i.
Output
On the first line print an integer m' (0 ≤ m' ≤ 3⋅ 10^5), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint.
After that print m' lines, i-th of which contains three space separated integers u_i, v_i, d_i, meaning that the person u_i owes the person v_i exactly d_i burles. The output must satisfy 1 ≤ u_i, v_i ≤ n, u_i ≠ v_i and 0 < d_i ≤ 10^{18}.
For each pair i ≠ j, it should hold that u_i ≠ u_j or v_i ≠ v_j. In other words, each pair of people can be included at most once in the output.
Examples
Input
3 2
1 2 10
2 3 5
Output
2
1 2 5
1 3 5
Input
3 3
1 2 10
2 3 15
3 1 10
Output
1
2 3 5
Input
4 2
1 2 12
3 4 8
Output
2
1 2 12
3 4 8
Input
3 4
2 3 1
2 3 2
2 3 4
2 3 8
Output
1
2 3 15
Note
In the first example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 2, d = 3 and z = 5. The resulting debts are: d(1, 2) = 5, d(2, 2) = 5, d(1, 3) = 5, all other debts are 0;
2. Perform an operation of the second type with a = 2. The resulting debts are: d(1, 2) = 5, d(1, 3) = 5, all other debts are 0.
In the second example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 3, d = 1 and z = 10. The resulting debts are: d(3, 2) = 10, d(2, 3) = 15, d(1, 1) = 10, all other debts are 0;
2. Perform an operation of the first type with a = 2, b = 3, c = 3, d = 2 and z = 10. The resulting debts are: d(2, 2) = 10, d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
3. Perform an operation of the second type with a = 2. The resulting debts are: d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
4. Perform an operation of the second type with a = 3. The resulting debts are: d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
5. Perform an operation of the second type with a = 1. The resulting debts are: d(2, 3) = 5, all other debts are 0. | instruction | 0 | 56,506 | 10 | 113,012 |
Tags: constructive algorithms, data structures, graphs, greedy, implementation, math, two pointers
Correct Solution:
```
n,m=map(int,input().split())
d={}
for i in range(m):
x,y,a=map(int,input().split())
d[x]=d.get(x,0)-a
d[y]=d.get(y,0)+a
pos=[]
neg=[]
for i in d:
if d[i]<0:
neg.append([d[i],i])
elif d[i]>0:
pos.append([d[i],i])
ans=[]
i=0
j=0
while(i<len(neg) and j<len(pos)):
z=min(-neg[i][0],pos[j][0])
ans.append([neg[i][1],pos[j][1],z])
neg[i][0]+=z
pos[j][0]-=z
if pos[j][0]==0:
j+=1
if neg[i][0]==0:
i+=1
print(len(ans))
for i in ans:
print(*i)
``` | output | 1 | 56,506 | 10 | 113,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people in this world, conveniently numbered 1 through n. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let d(a,b) denote the debt of a towards b, or 0 if there is no such debt.
Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money.
When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done:
1. Let d(a,b) > 0 and d(c,d) > 0 such that a ≠ c or b ≠ d. We can decrease the d(a,b) and d(c,d) by z and increase d(c,b) and d(a,d) by z, where 0 < z ≤ min(d(a,b),d(c,d)).
2. Let d(a,a) > 0. We can set d(a,a) to 0.
The total debt is defined as the sum of all debts:
$$$\Sigma_d = ∑_{a,b} d(a,b)$$$
Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt.
Input
The first line contains two space separated integers n (1 ≤ n ≤ 10^5) and m (0 ≤ m ≤ 3⋅ 10^5), representing the number of people and the number of debts, respectively.
m lines follow, each of which contains three space separated integers u_i, v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), d_i (1 ≤ d_i ≤ 10^9), meaning that the person u_i borrowed d_i burles from person v_i.
Output
On the first line print an integer m' (0 ≤ m' ≤ 3⋅ 10^5), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint.
After that print m' lines, i-th of which contains three space separated integers u_i, v_i, d_i, meaning that the person u_i owes the person v_i exactly d_i burles. The output must satisfy 1 ≤ u_i, v_i ≤ n, u_i ≠ v_i and 0 < d_i ≤ 10^{18}.
For each pair i ≠ j, it should hold that u_i ≠ u_j or v_i ≠ v_j. In other words, each pair of people can be included at most once in the output.
Examples
Input
3 2
1 2 10
2 3 5
Output
2
1 2 5
1 3 5
Input
3 3
1 2 10
2 3 15
3 1 10
Output
1
2 3 5
Input
4 2
1 2 12
3 4 8
Output
2
1 2 12
3 4 8
Input
3 4
2 3 1
2 3 2
2 3 4
2 3 8
Output
1
2 3 15
Note
In the first example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 2, d = 3 and z = 5. The resulting debts are: d(1, 2) = 5, d(2, 2) = 5, d(1, 3) = 5, all other debts are 0;
2. Perform an operation of the second type with a = 2. The resulting debts are: d(1, 2) = 5, d(1, 3) = 5, all other debts are 0.
In the second example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 3, d = 1 and z = 10. The resulting debts are: d(3, 2) = 10, d(2, 3) = 15, d(1, 1) = 10, all other debts are 0;
2. Perform an operation of the first type with a = 2, b = 3, c = 3, d = 2 and z = 10. The resulting debts are: d(2, 2) = 10, d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
3. Perform an operation of the second type with a = 2. The resulting debts are: d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
4. Perform an operation of the second type with a = 3. The resulting debts are: d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
5. Perform an operation of the second type with a = 1. The resulting debts are: d(2, 3) = 5, all other debts are 0. | instruction | 0 | 56,507 | 10 | 113,014 |
Tags: constructive algorithms, data structures, graphs, greedy, implementation, math, two pointers
Correct Solution:
```
n,m=map(int,input().split())
d={}
for i in range(m):
x,y,a=map(int,input().split())
d[x]=d.get(x,0)-a
d[y]=d.get(y,0)+a
pos=[]
neg=[]
for i in d:
if d[i]<0:
neg.append([d[i],i])
elif d[i]>0:
pos.append([d[i],i])
ans=[]
i=0
j=0
while(i<len(neg)):
z=min(-neg[i][0],pos[j][0])
ans.append([neg[i][1],pos[j][1],z])
neg[i][0]+=z
pos[j][0]-=z
if pos[j][0]==0:
j+=1
if neg[i][0]==0:
i+=1
print(len(ans))
for i in ans:
print(*i)
``` | output | 1 | 56,507 | 10 | 113,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people in this world, conveniently numbered 1 through n. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let d(a,b) denote the debt of a towards b, or 0 if there is no such debt.
Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money.
When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done:
1. Let d(a,b) > 0 and d(c,d) > 0 such that a ≠ c or b ≠ d. We can decrease the d(a,b) and d(c,d) by z and increase d(c,b) and d(a,d) by z, where 0 < z ≤ min(d(a,b),d(c,d)).
2. Let d(a,a) > 0. We can set d(a,a) to 0.
The total debt is defined as the sum of all debts:
$$$\Sigma_d = ∑_{a,b} d(a,b)$$$
Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt.
Input
The first line contains two space separated integers n (1 ≤ n ≤ 10^5) and m (0 ≤ m ≤ 3⋅ 10^5), representing the number of people and the number of debts, respectively.
m lines follow, each of which contains three space separated integers u_i, v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), d_i (1 ≤ d_i ≤ 10^9), meaning that the person u_i borrowed d_i burles from person v_i.
Output
On the first line print an integer m' (0 ≤ m' ≤ 3⋅ 10^5), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint.
After that print m' lines, i-th of which contains three space separated integers u_i, v_i, d_i, meaning that the person u_i owes the person v_i exactly d_i burles. The output must satisfy 1 ≤ u_i, v_i ≤ n, u_i ≠ v_i and 0 < d_i ≤ 10^{18}.
For each pair i ≠ j, it should hold that u_i ≠ u_j or v_i ≠ v_j. In other words, each pair of people can be included at most once in the output.
Examples
Input
3 2
1 2 10
2 3 5
Output
2
1 2 5
1 3 5
Input
3 3
1 2 10
2 3 15
3 1 10
Output
1
2 3 5
Input
4 2
1 2 12
3 4 8
Output
2
1 2 12
3 4 8
Input
3 4
2 3 1
2 3 2
2 3 4
2 3 8
Output
1
2 3 15
Note
In the first example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 2, d = 3 and z = 5. The resulting debts are: d(1, 2) = 5, d(2, 2) = 5, d(1, 3) = 5, all other debts are 0;
2. Perform an operation of the second type with a = 2. The resulting debts are: d(1, 2) = 5, d(1, 3) = 5, all other debts are 0.
In the second example the optimal sequence of operations can be the following:
1. Perform an operation of the first type with a = 1, b = 2, c = 3, d = 1 and z = 10. The resulting debts are: d(3, 2) = 10, d(2, 3) = 15, d(1, 1) = 10, all other debts are 0;
2. Perform an operation of the first type with a = 2, b = 3, c = 3, d = 2 and z = 10. The resulting debts are: d(2, 2) = 10, d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
3. Perform an operation of the second type with a = 2. The resulting debts are: d(3, 3) = 10, d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
4. Perform an operation of the second type with a = 3. The resulting debts are: d(2, 3) = 5, d(1, 1) = 10, all other debts are 0;
5. Perform an operation of the second type with a = 1. The resulting debts are: d(2, 3) = 5, all other debts are 0. | instruction | 0 | 56,508 | 10 | 113,016 |
Tags: constructive algorithms, data structures, graphs, greedy, implementation, math, two pointers
Correct Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
pass
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ----
n, k = map(int, input().split())
debts = [0]*(n+1)
for _ in range(k):
a, b, amt = map(int, input().split())
debts[a] += amt
debts[b] -= amt
ameer_log = {}
gareeb_log = {}
for i in range(1, n+1):
if debts[i] < 0:
ameer_log[i] = -1*debts[i]
elif debts[i] > 0:
gareeb_log[i] = debts[i]
rich = sorted(ameer_log, key=ameer_log.get, reverse=True)
poor = sorted(gareeb_log, key=gareeb_log.get, reverse=True)
#print(rich, poor)
#print(len(poor[1]))
i, j = 0, 0
ans = []
while i < len(rich) and j < len(poor):
#print(i, j, rich, poor)
rn, rd, pn, pd = rich[i], ameer_log[rich[i]], poor[j], gareeb_log[poor[j]]
pay = min(rd, pd)
if rd == pd:
ans.append([pn, rn, rd])
i += 1
j += 1
elif pay == pd:
ans.append([pn, rn, pay])
ameer_log[rich[i]] -= pd
j += 1
else:
ans.append([pn, rn, pay])
gareeb_log[poor[j]] -= rd
i += 1
print(len(ans))
for x in ans:
print(x[0], x[1], x[2])
``` | output | 1 | 56,508 | 10 | 113,017 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.