message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn. | instruction | 0 | 97,021 | 19 | 194,042 |
Tags: binary search, math
Correct Solution:
```
for q in range(int(input())):
k,n,a,b=map(int,input().split(" "))
rem=k-n*b
if rem<=0:
print(-1)
else:
val=(rem-1)//(a-b)
if val>n:
print(n)
else:
print(val)
``` | output | 1 | 97,021 | 19 | 194,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn. | instruction | 0 | 97,022 | 19 | 194,044 |
Tags: binary search, math
Correct Solution:
```
def f(ans, k, b, n, a):
works = True
r = k
if ans > n:
works = False
else:
if r > a * ans:
r -= a * ans
if not (((r - 1) // b) >= n - ans and r > b * (n - ans)):
works = False
else:
works = False
return works
def bS(l, r, k, b, n, a):
while True:
mid = (l + r) // 2
if r - l == 1:
break
if f(mid, k, b, n, a):
l = mid
else:
r = mid
if f(r, k, b, n, a):
return r
return l
def main():
for _ in range(int(input())):
k, n, a, b = map(int, input().split())
ans = bS(0, n + 3, k, b, n, a)
if f(ans, k, b, n, a):
print(ans)
else:
print(-1)
main()
``` | output | 1 | 97,022 | 19 | 194,045 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn. | instruction | 0 | 97,023 | 19 | 194,046 |
Tags: binary search, math
Correct Solution:
```
#!/usr/bin/env python3
from sys import stdin
import math
def solve(tc):
k, n, a, b = map(int, stdin.readline().split())
if k<=b*n:
print(-1)
return
r = (k - (b * n)) // (a - b)
if (k-(b*n)) % (a-b) == 0:
r -= 1
print(min(r,n))
LOCAL_TEST = not __debug__
if LOCAL_TEST:
infile = __file__.split('.')[0] + "-test.in"
stdin = open(infile, 'r')
tcs = (int(stdin.readline().strip()))# if LOCAL_TEST else 1)
tc = 1
while tc <= tcs:
solve(tc)
tc += 1
``` | output | 1 | 97,023 | 19 | 194,047 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn. | instruction | 0 | 97,024 | 19 | 194,048 |
Tags: binary search, math
Correct Solution:
```
for q in range(int(input())):
k, n, a, b = map(int, input().split())
if k <= n * b:
print(-1)
else:
l = 0
r = 10 ** 9
while l + 1 < r:
x = (l + r) // 2
if (k - x * a - 1) // b + x >= n:
l = x
else:
r = x
print(min(n, l))
``` | output | 1 | 97,024 | 19 | 194,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn. | instruction | 0 | 97,025 | 19 | 194,050 |
Tags: binary search, math
Correct Solution:
```
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
k,n,a,b = list(map(int, input().split()))
s = b*n
if s >= k:
print(-1)
else:
print(min((k-s-1)//(a-b), n))
``` | output | 1 | 97,025 | 19 | 194,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn. | instruction | 0 | 97,026 | 19 | 194,052 |
Tags: binary search, math
Correct Solution:
```
t=int(input(''))
for _ in range(t):
n,k,a,b=map(int, input().split())
if (k*a)<n:
print(k)
continue
elif n<= k*b:
print(-1)
continue
else:
a-=b
l=n-(b*k)
if l%a==0:
print((l//a)-1)
else:
print(l//a)
``` | output | 1 | 97,026 | 19 | 194,053 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn. | instruction | 0 | 97,027 | 19 | 194,054 |
Tags: binary search, math
Correct Solution:
```
#Anuneet Anand
q=int(input())
while q:
k,n,a,b=map(int,input().split())
c=0
if k-n*b<=0:
c=-1
elif k-n*a>0:
c=n
else:
c=(k-n*b)/(a-b)
if int(c)==c:
c=int(c)-1
else:
c=int(c)
print(c)
q=q-1
``` | output | 1 | 97,027 | 19 | 194,055 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.
Submitted Solution:
```
'''
from sys import stdin,stdout
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
d = {}
for i in arr:
if i in d:
d[i] += 1
else:
d[i] = 1
cnt = list(d[i] for i in d)
cnt.sort(reverse = True)
ans = []
ans.append(cnt[0])
for i in range(1, len(cnt)):
if cnt[i-1] != 0:
while cnt[i]>0:
if cnt[i] not in ans:
ans.append(cnt[i])
break
else:
cnt[i] -= 1
else:
break
stdout.write('{}\n'.format(sum(ans)))
'''
'''
a = int(input())
while True:
temp = a; s=0
while temp != 0:
s += temp%10
temp //= 10
if s%4 == 0:
break
a += 1
print(a)
'''
'''
for _ in range(int(input())):
n, k = map(int, input().split())
arr = list(map(int, input().split()))
if min(arr)+k < max(arr)-k:
print(-1)
else:
print(min(arr)+k)
'''
for _ in range(int(input())):
k, n, a, b = map(int, input().split())
ans = (b * n - k + 1) // (b - a)
print(min(n, ans) if ans >= 0 else -1)
``` | instruction | 0 | 97,028 | 19 | 194,056 |
Yes | output | 1 | 97,028 | 19 | 194,057 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.
Submitted Solution:
```
def main():
def pos(x, a, b, k):
return ((a * x + b * (n - x)) < k)
for i in range(int(input())):
k, n, a, b = map(int, input().split())
left, right = -1, n + 1
while left + 1 < right:
mid = (left + right) // 2
if pos(mid, a, b, k):
left = mid
else:
right = mid
print(left)
main()
``` | instruction | 0 | 97,029 | 19 | 194,058 |
Yes | output | 1 | 97,029 | 19 | 194,059 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.
Submitted Solution:
```
for _ in range(int(input())):
k,n,a,b = map(int,input().split())
if n*b>=k:
print(-1)
else:
num = k-(b*n)-1
deno = a-b
print(min(num//deno,n))
``` | instruction | 0 | 97,030 | 19 | 194,060 |
Yes | output | 1 | 97,030 | 19 | 194,061 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.
Submitted Solution:
```
from math import *
p = int(input())
A = []
for i in range(p):
k, n, a, b = map(int, input().split())
x = (k - n*b)/(a - b)
x = ceil(x)-1
if x > n:
x = n
if x < 0:
x = -1
A.append(x)
for i in range(p):
print(A[i])
``` | instruction | 0 | 97,031 | 19 | 194,062 |
Yes | output | 1 | 97,031 | 19 | 194,063 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.
Submitted Solution:
```
def ispossible(charge, playcost, lowplaycost, playcnt, allcnt):
return charge > playcost*playcnt + lowplaycost*(allcnt - playcnt)
def solution():
k, n, a, b = map(int, input().split())
if k <= b*n:
return -1
l = 0
r = n + 1
while r - l > 1:
m = (r+l)//2
if ispossible(k, a, b, m, n) == True:
l = m
else:
r = m
return l
def main():
test = int(input())
for i in range(test):
print(i,solution())
return
if __name__ == "__main__":
main()
``` | instruction | 0 | 97,032 | 19 | 194,064 |
No | output | 1 | 97,032 | 19 | 194,065 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.
Submitted Solution:
```
t = int(input())
while t:
k, n, a, b = [int(a) for a in input().split(" ")]
p = (k - b * n) // (a - b)
diff = ((k - b * n) / (a - b)) - p
if p == 0 and diff == 0:
print('-1')
elif diff == 0:
print(max(min(p - 1, n), 0))
else:
print(max(min(p, n), 0))
t -= 1
``` | instruction | 0 | 97,033 | 19 | 194,066 |
No | output | 1 | 97,033 | 19 | 194,067 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.
Submitted Solution:
```
import math
t=int(input())
for _ in range(t):
k,n,a,b,=map(int,input().split())
val=min(a,b)
if k-n*val>0:
if val==a:
print(n)
elif k-a*n>0:
print(n)
else:
f=1
for i in range(n-1,0,-1):
if i*a+(n-i)*b<=k:
print(i)
f=0
break
if f==1:
print(0)
else:
print(-1)
``` | instruction | 0 | 97,034 | 19 | 194,068 |
No | output | 1 | 97,034 | 19 | 194,069 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.
During each turn Vova can choose what to do:
* If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
* if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;
* if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries. Each query is presented by a single line.
The only line of the query contains four integers k, n, a and b (1 ≤ k, n ≤ 10^9, 1 ≤ b < a ≤ 10^9) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.
Output
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.
Submitted Solution:
```
import math,bisect
from collections import Counter,defaultdict
I =lambda:int(input())
M =lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))
for _ in range(I()):
k,n,a,b=M()
if k//b<=n:
print(-1)
else:
ans=k//a;k-=ans*a
while ans+(k//b)<n:
ans-=1;k+=a
if ans==n and k%a==0:ans-=1
print(min(ans,n))
``` | instruction | 0 | 97,035 | 19 | 194,070 |
No | output | 1 | 97,035 | 19 | 194,071 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The programmers from the R2 company love playing 2048. One day, they decided to invent their own simplified version of this game — 2k on a stripe.
Imagine an infinite in one direction stripe, consisting of unit squares (the side of each square is equal to the height of the stripe). Each square can either be empty or contain some number.
Initially, all squares are empty. Then at infinity one of the unit squares number 2 or 4 appears. Then the player presses a button once, and the appeared number begins to move towards the beginning of the stripe. Let's assume that some number x moves to the beginning of the stripe, then it will stop if:
1. it either gets in the first square of the stripe;
2. or it is in the square that is preceded by a square with number y (y ≠ x). But if number x at some point of time gets to the square with the same number then both numbers add to each other and result in 2x. The new number 2x continues moving to the beginning of the stripe by the same rules.
After the final stop of the number moving process, the infinity gets a new number 2 or 4 and the process repeats. Read the notes to the test samples to better understand the moving strategy.
I guess you've understood that the game progress fully depends on the order in which numbers 2 and 4 appear. Let's look at some sequence of numbers 2 and 4 in the game. We assume that the sequence is winning if it results in at least one square getting the number greater or equal than 2k.
The goal of the game is to make up a winning sequence of n numbers. But not everything is so simple, some numbers in the sequence are identified beforehand. You are given a sequence consisting of numbers 0, 2, 4. Count how many ways there are to replace each 0 of the sequence with 2 or 4 to get a winning sequence.
Input
The first line contains two integers n and k (1 ≤ n ≤ 2000; 3 ≤ k ≤ 11). The next line contains sequence of n integers, each of them is either 0, or 2, or 4.
Output
Print a single integer — the number of ways to replace zeroes by numbers 2 or 4 to get a winning sequence. As this number can be rather large, print it modulo 1000000007 (109 + 7).
Examples
Input
7 4
2 2 4 2 2 2 2
Output
1
Input
1 3
0
Output
0
Input
2 3
0 4
Output
1
Input
5 4
2 0 0 4 4
Output
2
Note
Consider the first example. The beginning of the strip will look as follows:
2 → 4 → 8 → 8 2 → 8 4 → 8 4 2 → 16.
To better understand the game, you can see the original game on http://gabrielecirulli.github.io/2048/. Please note that the game that is described on the strip is slightly different from the original game (when the two numbers add up in the original game, they do not keep moving). Be careful, the game is addictive, there isn't much time for the contest!
Submitted Solution:
```
n, k = map(int, input().split())
seq = list(map(int, input().split()))
tmp = [0]
for x in seq:
if x == 2:
tmp[-1] += 1
elif x == 4:
tmp[-1] += 2
else:
tmp.append(0)
need = (1 << k - 1) - sum(tmp) - len(tmp) + 1
if need <= 0:
print((1 << len(tmp) - 1) % 1000000007)
elif need <= len(tmp) - 1:
print((1 << len(tmp) - 1 - need) % 1000000007)
else:
print(0)
``` | instruction | 0 | 97,327 | 19 | 194,654 |
No | output | 1 | 97,327 | 19 | 194,655 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The programmers from the R2 company love playing 2048. One day, they decided to invent their own simplified version of this game — 2k on a stripe.
Imagine an infinite in one direction stripe, consisting of unit squares (the side of each square is equal to the height of the stripe). Each square can either be empty or contain some number.
Initially, all squares are empty. Then at infinity one of the unit squares number 2 or 4 appears. Then the player presses a button once, and the appeared number begins to move towards the beginning of the stripe. Let's assume that some number x moves to the beginning of the stripe, then it will stop if:
1. it either gets in the first square of the stripe;
2. or it is in the square that is preceded by a square with number y (y ≠ x). But if number x at some point of time gets to the square with the same number then both numbers add to each other and result in 2x. The new number 2x continues moving to the beginning of the stripe by the same rules.
After the final stop of the number moving process, the infinity gets a new number 2 or 4 and the process repeats. Read the notes to the test samples to better understand the moving strategy.
I guess you've understood that the game progress fully depends on the order in which numbers 2 and 4 appear. Let's look at some sequence of numbers 2 and 4 in the game. We assume that the sequence is winning if it results in at least one square getting the number greater or equal than 2k.
The goal of the game is to make up a winning sequence of n numbers. But not everything is so simple, some numbers in the sequence are identified beforehand. You are given a sequence consisting of numbers 0, 2, 4. Count how many ways there are to replace each 0 of the sequence with 2 or 4 to get a winning sequence.
Input
The first line contains two integers n and k (1 ≤ n ≤ 2000; 3 ≤ k ≤ 11). The next line contains sequence of n integers, each of them is either 0, or 2, or 4.
Output
Print a single integer — the number of ways to replace zeroes by numbers 2 or 4 to get a winning sequence. As this number can be rather large, print it modulo 1000000007 (109 + 7).
Examples
Input
7 4
2 2 4 2 2 2 2
Output
1
Input
1 3
0
Output
0
Input
2 3
0 4
Output
1
Input
5 4
2 0 0 4 4
Output
2
Note
Consider the first example. The beginning of the strip will look as follows:
2 → 4 → 8 → 8 2 → 8 4 → 8 4 2 → 16.
To better understand the game, you can see the original game on http://gabrielecirulli.github.io/2048/. Please note that the game that is described on the strip is slightly different from the original game (when the two numbers add up in the original game, they do not keep moving). Be careful, the game is addictive, there isn't much time for the contest!
Submitted Solution:
```
s = input()
if s == 'FCF':
print('Yes')
elif 'C' in s and 'F' in s and s.index('C') < s.index('F'):
print("Yes")
else:
print("No")
``` | instruction | 0 | 97,328 | 19 | 194,656 |
No | output | 1 | 97,328 | 19 | 194,657 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 97,345 | 19 | 194,690 |
Tags: greedy, sortings
Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
from heapq import heappush, heappop, heapify
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
N = INT()
A = LIST()
A = [-a for a in A]
heapify(A)
ans = 0
while len(A) > 1:
a = heappop(A)
b = heappop(A)
ans += -(a+b)
heappush(A, a+b)
ans += -A[0]
print(ans)
``` | output | 1 | 97,345 | 19 | 194,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 97,346 | 19 | 194,692 |
Tags: greedy, sortings
Correct Solution:
```
N = int(input())
ints = list(map(int, input().split()))
ints = sorted(ints)
cum_sum = [0] * (N+1)
for i in range(N):
cum_sum[i+1] = cum_sum[i] + ints[i]
res = sum(ints)
for i in range(1,N):
res += ints[i-1] + (cum_sum[-1]-cum_sum[i])
print(res)
``` | output | 1 | 97,346 | 19 | 194,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 97,347 | 19 | 194,694 |
Tags: greedy, sortings
Correct Solution:
```
from collections import Counter
import string
import bisect
#import random
import math
import sys
# sys.setrecursionlimit(10**6)
from fractions import Fraction
def array_int():
return [int(i) for i in sys.stdin.readline().split()]
def vary(arrber_of_variables):
if arrber_of_variables==1:
return int(sys.stdin.readline())
if arrber_of_variables>=2:
return map(int,sys.stdin.readline().split())
def makedict(var):
return dict(Counter(var))
testcases=1
for _ in range(testcases):
n=vary(1)
num=array_int()
num.sort()
sumt=0
for i in range(n-1):
sumt+=num[i]*(i+2)
print(sumt+n*num[-1])
``` | output | 1 | 97,347 | 19 | 194,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 97,348 | 19 | 194,696 |
Tags: greedy, sortings
Correct Solution:
```
import itertools
input()
a = list(map(int, str.split(input())))
a.sort(reverse=True)
print(sum(itertools.accumulate(a)) + sum(a) - max(a))
``` | output | 1 | 97,348 | 19 | 194,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 97,349 | 19 | 194,698 |
Tags: greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = n*a[-1]
for i in range(n-1):
ans += (i+2)*a[i]
print(ans)
``` | output | 1 | 97,349 | 19 | 194,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 97,350 | 19 | 194,700 |
Tags: greedy, sortings
Correct Solution:
```
n = int(input())
num = list(map(int, input().split()))
num.sort(reverse=True)
psum = [0]*n
psum[0] = num[0]
for i in range(1, n):
psum[i] = psum[i-1]+num[i]
print(sum(psum[1:])+psum[n-1])
``` | output | 1 | 97,350 | 19 | 194,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 97,351 | 19 | 194,702 |
Tags: greedy, sortings
Correct Solution:
```
def main():
n = int(input())
seq = [int(c) for c in input().split()]
if n == 1:
print(seq[0])
return
seq.sort()
s = sum(seq)
ans = 2 * s
for i in range(n-2):
s -= seq[i]
ans += s
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 97,351 | 19 | 194,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 97,352 | 19 | 194,704 |
Tags: greedy, sortings
Correct Solution:
```
from heapq import *
def main():
n = int(input())
s = [int(a) for a in map(int, input().split())]
heapify(s)
res = sum(s)
ans = res
for i in range(n - 1):
x = heappop(s)
res -= x
ans += x + res
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 97,352 | 19 | 194,705 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Submitted Solution:
```
n = int(input())
nums = list( map(int,input().split(" ")))
nums.sort()
ans = 0
l = len(nums)
if l == 1:
print(nums[0])
else:
for i in range(l - 2):
ans += (i + 2) * nums[i]
ans += (nums[-1] + nums[-2]) * l
print(ans)
``` | instruction | 0 | 97,353 | 19 | 194,706 |
Yes | output | 1 | 97,353 | 19 | 194,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Submitted Solution:
```
#!/usr/bin/env python3
n = int(input())
a = sorted(int(x) for x in input().split())
count = 0
for i in range(n):
count += (i+2) * a[i]
print(count - a[-1])
``` | instruction | 0 | 97,354 | 19 | 194,708 |
Yes | output | 1 | 97,354 | 19 | 194,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Submitted Solution:
```
n=int(input())
lis=sorted(list(map(int,input().split())))
ans=0
for i in range(n):ans+=(i+2)*lis[i]
ans-=lis[-1]
print(ans)
``` | instruction | 0 | 97,355 | 19 | 194,710 |
Yes | output | 1 | 97,355 | 19 | 194,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Submitted Solution:
```
from heapq import heappush, heappop
""" でかい数を後に残した方がいいに決まってる """
n = int(input())
v = [int(i) for i in input().split()]
ans = sum(v)
s = ans
q = []
for i in range(n):
heappush(q, v[i])
v = None
while len(q) > 1:
t = heappop(q)
s -= t
ans += t
ans += s
print(ans)
``` | instruction | 0 | 97,356 | 19 | 194,712 |
Yes | output | 1 | 97,356 | 19 | 194,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Submitted Solution:
```
from collections import deque
# N = 3
# L = [3, 1, 5]
N = int(input())
L = list(map(int,input().split()))
ans = sum(L)
L.sort()
queue = deque([L[0], L[1:]])
while queue:
a = queue.popleft()
b = queue.popleft()
if a:
ans += a
if b:
ans += sum(b)
b.sort()
queue.append(b[0])
queue.append(b[1:])
print(ans)
``` | instruction | 0 | 97,357 | 19 | 194,714 |
No | output | 1 | 97,357 | 19 | 194,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Submitted Solution:
```
N = int(input())
ints = list(map(int, input().split()))
def T(large, small):
if len(large) == 0: return 0
s = min(large)
return sum(large) + sum(small) + T( [i for i in large if i != s], [s])
small = min(ints)
print(sum(ints) + T([i for i in ints if i != small], [small]))
``` | instruction | 0 | 97,358 | 19 | 194,716 |
No | output | 1 | 97,358 | 19 | 194,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Submitted Solution:
```
#!/snap/bin/pypy3
def main():
n = int(input())
numbers = set(map(int, input().split()))
ans = 0
while len(numbers) > 1:
print(numbers)
a = max(numbers)
numbers.remove(a)
b = max(numbers)
numbers.remove(b)
ans += a + b
numbers.add(a + b)
print(ans + sum(numbers))
if __name__ == '__main__':
main()
``` | instruction | 0 | 97,359 | 19 | 194,718 |
No | output | 1 | 97,359 | 19 | 194,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Submitted Solution:
```
def main():
n = int(input())
seq = [int(c) for c in input().split()]
seq.sort()
s = sum(seq)
ans = 2 * s
for i in range(n-2):
s -= seq[i]
ans += s
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 97,360 | 19 | 194,720 |
No | output | 1 | 97,360 | 19 | 194,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:
1. Each domino completely covers two squares.
2. No two dominoes overlap.
3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.
Find the maximum number of dominoes, which can be placed under these restrictions.
Input
In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).
Output
Output one number — the maximal number of dominoes, which can be placed.
Examples
Input
2 4
Output
4
Input
3 3
Output
4 | instruction | 0 | 97,377 | 19 | 194,754 |
Tags: greedy, math
Correct Solution:
```
m, n = list(map(int, input().split()))
print((m * n) // 2) # S_of_deck / S_of_domino
``` | output | 1 | 97,377 | 19 | 194,755 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:
1. Each domino completely covers two squares.
2. No two dominoes overlap.
3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.
Find the maximum number of dominoes, which can be placed under these restrictions.
Input
In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).
Output
Output one number — the maximal number of dominoes, which can be placed.
Examples
Input
2 4
Output
4
Input
3 3
Output
4 | instruction | 0 | 97,378 | 19 | 194,756 |
Tags: greedy, math
Correct Solution:
```
a,b=map(int,input().split())
c=(int(a*b))
d=2
maximum1=(int(c/d))
print(round(maximum1))
``` | output | 1 | 97,378 | 19 | 194,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:
1. Each domino completely covers two squares.
2. No two dominoes overlap.
3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.
Find the maximum number of dominoes, which can be placed under these restrictions.
Input
In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).
Output
Output one number — the maximal number of dominoes, which can be placed.
Examples
Input
2 4
Output
4
Input
3 3
Output
4 | instruction | 0 | 97,379 | 19 | 194,758 |
Tags: greedy, math
Correct Solution:
```
m,n=[int(x) for x in input().split()]
if(m%2==0 or n%2==0):
if(m%2==0):
ans=n*(int(m/2))
else:
ans=m*(int(n/2))
else:
ans=int((m*n)/2)
print(ans)
``` | output | 1 | 97,379 | 19 | 194,759 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:
1. Each domino completely covers two squares.
2. No two dominoes overlap.
3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.
Find the maximum number of dominoes, which can be placed under these restrictions.
Input
In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).
Output
Output one number — the maximal number of dominoes, which can be placed.
Examples
Input
2 4
Output
4
Input
3 3
Output
4 | instruction | 0 | 97,380 | 19 | 194,760 |
Tags: greedy, math
Correct Solution:
```
s= input().split(' ')
m = int(s[0])
n = int(s[1])
if m == n == 1:
print(0)
elif m == 1 or n == 1:
print(int(round(m*n/2-0.2)))
else:
if m%2 == 0 or n%2 == 0:
print(int(m*n/2))
else: #m,n le
if m>n:
m=m-1
print(int(m*n/2+ round(n/2-0.2)))
else:
n=n-1
print(int(m*n/2+ round(m/2-0.2)))
``` | output | 1 | 97,380 | 19 | 194,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:
1. Each domino completely covers two squares.
2. No two dominoes overlap.
3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.
Find the maximum number of dominoes, which can be placed under these restrictions.
Input
In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).
Output
Output one number — the maximal number of dominoes, which can be placed.
Examples
Input
2 4
Output
4
Input
3 3
Output
4 | instruction | 0 | 97,381 | 19 | 194,762 |
Tags: greedy, math
Correct Solution:
```
def domino():
m,n=map(int,input().split())
return m*n//2
print (domino())
``` | output | 1 | 97,381 | 19 | 194,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:
1. Each domino completely covers two squares.
2. No two dominoes overlap.
3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.
Find the maximum number of dominoes, which can be placed under these restrictions.
Input
In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).
Output
Output one number — the maximal number of dominoes, which can be placed.
Examples
Input
2 4
Output
4
Input
3 3
Output
4 | instruction | 0 | 97,382 | 19 | 194,764 |
Tags: greedy, math
Correct Solution:
```
a = map(int, input().split())
result = 1
for x in a:
result *= x
print(int(result/2))
``` | output | 1 | 97,382 | 19 | 194,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:
1. Each domino completely covers two squares.
2. No two dominoes overlap.
3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.
Find the maximum number of dominoes, which can be placed under these restrictions.
Input
In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).
Output
Output one number — the maximal number of dominoes, which can be placed.
Examples
Input
2 4
Output
4
Input
3 3
Output
4 | instruction | 0 | 97,383 | 19 | 194,766 |
Tags: greedy, math
Correct Solution:
```
m,n=map(int,input().split())
if n*m %2==0:
x=n*m/2
elif m*n%2==1:
x=((n*m)-1)/2
print(int(x))
``` | output | 1 | 97,383 | 19 | 194,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:
1. Each domino completely covers two squares.
2. No two dominoes overlap.
3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.
Find the maximum number of dominoes, which can be placed under these restrictions.
Input
In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).
Output
Output one number — the maximal number of dominoes, which can be placed.
Examples
Input
2 4
Output
4
Input
3 3
Output
4 | instruction | 0 | 97,384 | 19 | 194,768 |
Tags: greedy, math
Correct Solution:
```
m, n = [int(x) for x in input().strip().split(' ')]
print(m * n // 2)
``` | output | 1 | 97,384 | 19 | 194,769 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.
Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.
Input
The first line contains a single integer n (1 ≤ n ≤ 18) — the number of participants of the Sith Tournament.
Each of the next n lines contains n real numbers, which form a matrix pij (0 ≤ pij ≤ 1). Each its element pij is the probability that the i-th participant defeats the j-th in a duel.
The elements on the main diagonal pii are equal to zero. For all different i, j the equality pij + pji = 1 holds. All probabilities are given with no more than six decimal places.
Jedi Ivan is the number 1 in the list of the participants.
Output
Output a real number — the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10 - 6.
Examples
Input
3
0.0 0.5 0.8
0.5 0.0 0.4
0.2 0.6 0.0
Output
0.680000000000000 | instruction | 0 | 97,472 | 19 | 194,944 |
Tags: bitmasks, dp, math, probabilities
Correct Solution:
```
import sys
n = int(input())
prob = [list(map(float, input().split())) for _ in range(n)]
dp = [[0.0]*n for _ in range(1 << n)]
dp[1][0] = 1.0
for mask in range(3, 1 << n):
for i in range(n):
if not (mask & (1 << i)):
continue
for j in range(n):
if i != j and mask & (1 << j):
dp[mask][i] = max(
dp[mask][i],
dp[mask - (1 << j)][i] * prob[i][j]
+ dp[mask - (1 << i)][j] * prob[j][i]
)
print(max(dp[-1]))
``` | output | 1 | 97,472 | 19 | 194,945 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.
Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.
Input
The first line contains a single integer n (1 ≤ n ≤ 18) — the number of participants of the Sith Tournament.
Each of the next n lines contains n real numbers, which form a matrix pij (0 ≤ pij ≤ 1). Each its element pij is the probability that the i-th participant defeats the j-th in a duel.
The elements on the main diagonal pii are equal to zero. For all different i, j the equality pij + pji = 1 holds. All probabilities are given with no more than six decimal places.
Jedi Ivan is the number 1 in the list of the participants.
Output
Output a real number — the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10 - 6.
Examples
Input
3
0.0 0.5 0.8
0.5 0.0 0.4
0.2 0.6 0.0
Output
0.680000000000000 | instruction | 0 | 97,474 | 19 | 194,948 |
Tags: bitmasks, dp, math, probabilities
Correct Solution:
```
import sys
n = int(input())
prob = [list(map(float, input().split())) for _ in range(n)]
dp = [[0.0]*(1 << n) for _ in range(n)]
dp[0][1] = 1.0
for mask in range(3, 1 << n):
for i in range(n):
if not (mask & (1 << i)):
continue
for j in range(n):
if i != j and mask & (1 << j):
dp[i][mask] = max(
dp[i][mask],
dp[i][mask - (1 << j)] * prob[i][j]
+ dp[j][mask - (1 << i)] * prob[j][i]
)
print(max(dp[i][-1] for i in range(n)))
``` | output | 1 | 97,474 | 19 | 194,949 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.
Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.
Input
The first line contains a single integer n (1 ≤ n ≤ 18) — the number of participants of the Sith Tournament.
Each of the next n lines contains n real numbers, which form a matrix pij (0 ≤ pij ≤ 1). Each its element pij is the probability that the i-th participant defeats the j-th in a duel.
The elements on the main diagonal pii are equal to zero. For all different i, j the equality pij + pji = 1 holds. All probabilities are given with no more than six decimal places.
Jedi Ivan is the number 1 in the list of the participants.
Output
Output a real number — the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10 - 6.
Examples
Input
3
0.0 0.5 0.8
0.5 0.0 0.4
0.2 0.6 0.0
Output
0.680000000000000 | instruction | 0 | 97,475 | 19 | 194,950 |
Tags: bitmasks, dp, math, probabilities
Correct Solution:
```
import sys
import bisect
from bisect import bisect_left as lb
input_=lambda: sys.stdin.readline().strip("\r\n")
from math import log
from math import gcd
from math import atan2,acos
from random import randint
sa=lambda :input_()
sb=lambda:int(input_())
sc=lambda:input_().split()
sd=lambda:list(map(int,input_().split()))
sflo=lambda:list(map(float,input_().split()))
se=lambda:float(input_())
sf=lambda:list(input_())
flsh=lambda: sys.stdout.flush()
#sys.setrecursionlimit(10**6)
mod=10**9+7
gp=[]
cost=[]
dp=[]
mx=[]
ans1=[]
ans2=[]
special=[]
specnode=[]
a=0
kthpar=[]
def dfs(root,par):
if par!=-1:
dp[root]=dp[par]+1
for i in range(1,20):
if kthpar[root][i-1]!=-1:
kthpar[root][i]=kthpar[kthpar[root][i-1]][i-1]
for child in gp[root]:
if child==par:continue
kthpar[child][0]=root
dfs(child,root)
ans=0
def hnbhai(t):
n=sb()
p=[]
for i in range(n):
p.append(sflo())
#print(p)
dp=[0]*(1<<n)
dp[1]=1
for i in range(2,1<<n):
for j in range(1,n):
for k in range(0,j):
if (i>>j)&1 and (i>>k)&1:
dp[i]=max(dp[i],dp[i^(1<<j)]*p[k][j]+dp[i^(1<<k)]*p[j][k])
#print(dp)
print(dp[-1])
for _ in range(1):
hnbhai(_+1)
``` | output | 1 | 97,475 | 19 | 194,951 |
Provide a correct Python 3 solution for this coding contest problem.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2 | instruction | 0 | 97,623 | 19 | 195,246 |
"Correct Solution:
```
n,a,b=map(int,input().split())
if (b-a-1)%2==1:
print((b-a-1)//2+1)
else:
print(min(a-1,n-b)+1+(b-a-2)//2+1)
``` | output | 1 | 97,623 | 19 | 195,247 |
Provide a correct Python 3 solution for this coding contest problem.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2 | instruction | 0 | 97,624 | 19 | 195,248 |
"Correct Solution:
```
n,a,b=map(int,input().split())
if (b-a)%2==0:
print((b-a)//2)
else:
print(min(a-1+1+(b-a-1)//2,n-b+1+(n-(a+n-b+1))//2))
``` | output | 1 | 97,624 | 19 | 195,249 |
Provide a correct Python 3 solution for this coding contest problem.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2 | instruction | 0 | 97,625 | 19 | 195,250 |
"Correct Solution:
```
n,a,b = map(int,input().split())
if (b-a)%2==0:
print(int((b-a)//2))
else:
print(int(min(a-1,n-b)+1+(b-1-a)//2))
``` | output | 1 | 97,625 | 19 | 195,251 |
Provide a correct Python 3 solution for this coding contest problem.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2 | instruction | 0 | 97,626 | 19 | 195,252 |
"Correct Solution:
```
n, a, b = map(int, input().split())
dif = b-a
if dif%2 == 0:
print(dif//2)
else:
print(min(a-1, n-b)+1+(b-a-1)//2)
``` | output | 1 | 97,626 | 19 | 195,253 |
Provide a correct Python 3 solution for this coding contest problem.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2 | instruction | 0 | 97,627 | 19 | 195,254 |
"Correct Solution:
```
n, a, b = map(int, input().split())
d = b - a
print((d % 2 and min(a, n - b + 1)) + d // 2)
``` | output | 1 | 97,627 | 19 | 195,255 |
Provide a correct Python 3 solution for this coding contest problem.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2 | instruction | 0 | 97,628 | 19 | 195,256 |
"Correct Solution:
```
n,a,b=map(int,input().split())
print((b-a)//2 if (b-a)%2==0 else min(a-1,n-b)+(b-a)//2+1)
``` | output | 1 | 97,628 | 19 | 195,257 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.