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 a correct Python 3 solution for this coding contest problem.
We will host a rock-paper-scissors tournament with N people. The participants are called Person 1, Person 2, \ldots, Person N. For any two participants, the result of the match between them is determined in advance. This information is represented by positive integers A_{i,j} ( 1 \leq j < i \leq N ) as follows:
* If A_{i,j} = 0, Person j defeats Person i.
* If A_{i,j} = 1, Person i defeats Person j.
The tournament proceeds as follows:
* We will arrange the N participants in a row, in the order Person 1, Person 2, \ldots, Person N from left to right.
* We will randomly choose two consecutive persons in the row. They will play a match against each other, and we will remove the loser from the row. We will repeat this process N-1 times, and the last person remaining will be declared the champion.
Find the number of persons with the possibility of becoming the champion.
Constraints
* 1 \leq N \leq 2000
* A_{i,j} is 0 or 1.
Input
Input is given from Standard Input in the following format:
N
A_{2,1}
A_{3,1}A_{3,2}
:
A_{N,1}\ldotsA_{N,N-1}
Output
Print the number of persons with the possibility of becoming the champion.
Examples
Input
3
0
10
Output
2
Input
6
0
11
111
1111
11001
Output
3 | instruction | 0 | 64,633 | 19 | 129,266 |
"Correct Solution:
```
import sys
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline()[:-1]
def pbit(aa,L):
for a in aa:print(format(a,"b").zfill(L))
print()
def main():
n=II()
aa=[[int(c) for c in SI()] for _ in range(n-1)]
#p2D(aa)
# 人iが勝てる相手をbitで管理する
win=[0]*n
for i,row in enumerate(aa,1):
for j,a in enumerate(row):
if a:win[i]|=1<<j
else:win[j]|=1<<i
#pbit(win,n)
# dpl[i]...[j,i]の範囲で優勝できるjをbitで
# dpr[i]...[i,j]の範囲で優勝できるjをbitで
dpl=[1<<i for i in range(n)]
dpr=[1<<i for i in range(n)]
for d in range(1,n):
for i in range(n-d):
j=i+d
if dpl[j] & dpr[i+1] & win[i]:dpl[j]|=1<<i
for i in range(d,n):
j=i-d
if dpl[i-1] & dpr[j] & win[i]:dpr[j]|=1<<i
#pbit(dpl,n)
#pbit(dpr,n)
#print(format(dpl[n-1]&dpr[0],"b").zfill(n))
print(bin(dpl[n-1]&dpr[0]).count("1"))
main()
``` | output | 1 | 64,633 | 19 | 129,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We will host a rock-paper-scissors tournament with N people. The participants are called Person 1, Person 2, \ldots, Person N. For any two participants, the result of the match between them is determined in advance. This information is represented by positive integers A_{i,j} ( 1 \leq j < i \leq N ) as follows:
* If A_{i,j} = 0, Person j defeats Person i.
* If A_{i,j} = 1, Person i defeats Person j.
The tournament proceeds as follows:
* We will arrange the N participants in a row, in the order Person 1, Person 2, \ldots, Person N from left to right.
* We will randomly choose two consecutive persons in the row. They will play a match against each other, and we will remove the loser from the row. We will repeat this process N-1 times, and the last person remaining will be declared the champion.
Find the number of persons with the possibility of becoming the champion.
Constraints
* 1 \leq N \leq 2000
* A_{i,j} is 0 or 1.
Input
Input is given from Standard Input in the following format:
N
A_{2,1}
A_{3,1}A_{3,2}
:
A_{N,1}\ldotsA_{N,N-1}
Output
Print the number of persons with the possibility of becoming the champion.
Examples
Input
3
0
10
Output
2
Input
6
0
11
111
1111
11001
Output
3
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**7)
n = int(input())
lose = [set() for _ in range(n)]
for i in range(1, n):
A = map(int, list(input()))
for j, a in enumerate(A):
if a:
lose[j].add(i)
else:
lose[i].add(j)
def dfs(i, left, right):
l_bool, r_bool = False, False
if left == i:
l_bool = True
if right == i:
r_bool = True
for j in range(left, i):
if i in lose[j]:
l_bool |= dfs(j, left, i-1)
for j in range(i+1, right+1):
if i in lose[j]:
r_bool |= dfs(j, i+1, right)
return l_bool & r_bool
ans = 0
for i in range(n):
if dfs(i, 0, n-1):
ans += 1
print(ans)
``` | instruction | 0 | 64,638 | 19 | 129,276 |
No | output | 1 | 64,638 | 19 | 129,277 |
Provide tags and a correct Python 3 solution for this coding contest problem.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | instruction | 0 | 65,039 | 19 | 130,078 |
Tags: brute force, constructive algorithms, games, greedy
Correct Solution:
```
for _ in range (int(input())):
n = int(input())
a = [int(i) for i in input().split()]
if n==1:
print('T')
elif n==2:
if a[0]==a[1]:
print('HL')
else:
print('T')
else:
if max(a)>sum(a)-max(a):
print('T')
else:
num = sum(a)-n
if num%2==0 and n%2==0:
print('HL')
elif num%2==0 and n%2==1:
print('T')
elif num%2==1 and n%2==0:
print('T')
elif num%2==1 and n%2==1:
print('HL')
``` | output | 1 | 65,039 | 19 | 130,079 |
Provide tags and a correct Python 3 solution for this coding contest problem.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | instruction | 0 | 65,040 | 19 | 130,080 |
Tags: brute force, constructive algorithms, games, greedy
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
m = 0
s = 0
for i in a:
m = max(m, i)
s += i
if m > s//2:
print("T")
elif s % 2 == 0:
print("HL")
else:
print("T")
``` | output | 1 | 65,040 | 19 | 130,081 |
Provide tags and a correct Python 3 solution for this coding contest problem.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | instruction | 0 | 65,041 | 19 | 130,082 |
Tags: brute force, constructive algorithms, games, greedy
Correct Solution:
```
import heapq as hq
for _ in range(int(input())):
n = int(input())
l= list(map(lambda x: -int(x),input().split()))
if n==1:
print('T')
continue
f=0
hq.heapify(l)
while True:
x = hq.heappop(l)
if x>=0:
break
else:
f=1
x+=1
y = hq.heappop(l)
if y>=0:
break
else:
f=0
y+=1
hq.heappush(l,x)
hq.heappush(l,y)
if f==0:
print('HL')
else:
print('T')
``` | output | 1 | 65,041 | 19 | 130,083 |
Provide tags and a correct Python 3 solution for this coding contest problem.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | instruction | 0 | 65,042 | 19 | 130,084 |
Tags: brute force, constructive algorithms, games, greedy
Correct Solution:
```
def checkMax(pilesArr,n):
pilesArr.sort()
pilesArrSum = sum(pilesArr)
if(pilesArr[-1]>(pilesArrSum-pilesArr[-1])):
return "T"
else:
if(pilesArrSum%2==0):
return "HL"
return "T"
if __name__ == "__main__":
testCases = input()
for i in range(int(testCases)):
numOfPiles = int(input())
pilesArr = list(map(int,input().split()))
ans = checkMax(pilesArr,numOfPiles)
print(ans)
``` | output | 1 | 65,042 | 19 | 130,085 |
Provide tags and a correct Python 3 solution for this coding contest problem.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | instruction | 0 | 65,043 | 19 | 130,086 |
Tags: brute force, constructive algorithms, games, greedy
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
if(s%2 != 0):
print("T")
else:
k = s//2
t = 0
for i in range(n):
if(a[i] > k):
t = 1
break
if(t == 1):
print("T")
else:
print("HL")
``` | output | 1 | 65,043 | 19 | 130,087 |
Provide tags and a correct Python 3 solution for this coding contest problem.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | instruction | 0 | 65,044 | 19 | 130,088 |
Tags: brute force, constructive algorithms, games, greedy
Correct Solution:
```
t = int(input())
while t:
t -= 1
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
maxi = max(a)
if maxi > total - maxi:
print("T")
continue
if total % 2 == 0:
print("HL")
else:
print("T")
``` | output | 1 | 65,044 | 19 | 130,089 |
Provide tags and a correct Python 3 solution for this coding contest problem.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | instruction | 0 | 65,045 | 19 | 130,090 |
Tags: brute force, constructive algorithms, games, greedy
Correct Solution:
```
from collections import defaultdict
from math import inf
for _ in range(int(input())):
n=int(input())
arr=list(map(int,input().split()))
mi=min(arr)
ans=0
su=0
res="T"
for i in arr:
ans+=i-mi
su+=i
if max(arr)>su-max(arr):
print(res)
else:
if ans%2==1:
res="HL"
if n%2==1:
if mi%2==0:
if res == "T":
print("HL")
else:
print("T")
else:
print(res)
else:
if res=="T":
print("HL")
else:
print("T")
``` | output | 1 | 65,045 | 19 | 130,091 |
Provide tags and a correct Python 3 solution for this coding contest problem.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | instruction | 0 | 65,046 | 19 | 130,092 |
Tags: brute force, constructive algorithms, games, greedy
Correct Solution:
```
import sys
import math,bisect
sys.setrecursionlimit(10 ** 5)
from collections import defaultdict
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,OrderedDict
def I(): return int(sys.stdin.readline())
def neo(): return map(int, sys.stdin.readline().split())
def Neo(): return list(map(int, sys.stdin.readline().split()))
for _ in range(int(input())):
n = I()
A = Neo()
A.sort()
if sum(A[:-1]) < A[-1]:
print('T')
else:
print(['HL','T'][sum(A)%2])
``` | output | 1 | 65,046 | 19 | 130,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
# n, m = map(int, input().split())
l = list(map(int, input().split()))
# l = [list(map(int, input().split())) for i in range(n)]
# s = list(str(input()))
# l.sort()
m = max(l)
if sum(l) - m < m:
print('T')
elif sum(l) % 2 == 1:
print("T")
else:
print("HL")
``` | instruction | 0 | 65,047 | 19 | 130,094 |
Yes | output | 1 | 65,047 | 19 | 130,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
s = sum(a)
flag = False
for x in a:
if x > s//2:
flag = True
break
if flag:
print("T")
continue
print("HL"if s%2==0 else "T")
``` | instruction | 0 | 65,048 | 19 | 130,096 |
Yes | output | 1 | 65,048 | 19 | 130,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
Submitted Solution:
```
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
s=sum(l)
end=False
for i in l:
if i>s-i:
end=True
break
if end: print('T')
else: print('T' if s%2==1 else 'HL')
``` | instruction | 0 | 65,049 | 19 | 130,098 |
Yes | output | 1 | 65,049 | 19 | 130,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
Submitted Solution:
```
import math,sys,bisect,heapq
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
#sys.setrecursionlimit(200000000)
int1 = lambda x: int(x) - 1
#input = iter(sys.stdin.buffer.read().decode().splitlines())._next_
ilele = lambda: map(int,input().split())
alele = lambda: list(map(int, input().split()))
ilelec = lambda: map(int1,input().split())
alelec = lambda: list(map(int1, input().split()))
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)]
MOD = 1000000000 + 7
def Y(c): print(["NO","YES"][c])
def y(c): print(["no","yes"][c])
def Yy(c): print(["No","Yes"][c])
for _ in range(int(input())):
N = int(input())
A = alele()
H = []
heapq.heapify(H)
for i in A:
heapq.heappush(H,(-i,-1))
#print(H)
win = 0
turn =1
while H:
x,y = heapq.heappop(H)
if y == turn -1:
if H:
x1,y1 = heapq.heappop(H)
heapq.heappush(H,(x,y))
x1 = x1+1
if x1 !=0:
heapq.heappush(H,(x1,turn))
turn += 1
else:
break
else:
x = x + 1
if x != 0:
heapq.heappush(H,(x,turn))
turn+=1
print(["HL","T"][not(turn%2)])
``` | instruction | 0 | 65,050 | 19 | 130,100 |
Yes | output | 1 | 65,050 | 19 | 130,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
piles = list(map(int,input().split()))
if n==1:
print('T'); continue
piles.sort(reverse=True)
t=0;hl=1
T=piles[t]
HL=0
while t<len(piles) and hl<len(piles):
while hl<len(piles) and HL<=T:
HL+=piles[hl]
hl+=1
HL,T = HL-T,max(0,T-HL)
t=hl
while t<len(piles) and T<=HL:
T+=piles[t]
t+=1
T,HL = T-HL, max(0,HL-T)
hl=t
if T<=HL: print('HL')
else: print('T')
``` | instruction | 0 | 65,051 | 19 | 130,102 |
No | output | 1 | 65,051 | 19 | 130,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
Submitted Solution:
```
from sys import stdin
# Input data
#stdin = open("input", "r")
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
arr.sort(reverse=True)
if n == 1:
print('T')
continue
count = 1
while(count < n + 1):
if arr[0] == 0:
print('HT')
break
elif arr[1] == 0:
print('T')
break
arr[0] -= arr[1]
arr[1] = 0
arr.sort(reverse=True)
count += 1
``` | instruction | 0 | 65,052 | 19 | 130,104 |
No | output | 1 | 65,052 | 19 | 130,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
Submitted Solution:
```
t=int(input())
while t:
t=t-1
n=int(input())
#n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
T=0
HL=0
for i in range(n):
if i%2==0:
T+=a[i]
else:
HL+=a[i]
if T>HL:
print('T')
else:
print("HL")
``` | instruction | 0 | 65,053 | 19 | 130,106 |
No | output | 1 | 65,053 | 19 | 130,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
T is playing a game with his friend, HL.
There are n piles of stones, the i-th pile initially has a_i stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.
Assuming both players play optimally, given the starting configuration of t games, determine the winner of each game.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of games. The description of the games follows. Each description contains two lines:
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of piles.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100).
Output
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)
Example
Input
2
1
2
2
1 1
Output
T
HL
Note
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains 1 stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
Submitted Solution:
```
######### ## ## ## #### ##### ## # ## # ##
# # # # # # # # # # # # # # # # # # #
# # # # ### # # # # # # # # # # # #
# ##### # # # # ### # # # # # # # # #####
# # # # # # # # # # # # # # # # # #
######### # # # # ##### # ##### # ## # ## # #
"""
PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE
PPPPPPPP RRRRRRRR OOOOOO VV VV EE
PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE
PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE
PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE
PP RRRR OOOOOOOO VV VV EEEEEE
PP RR RR OOOOOOOO VV VV EE
PP RR RR OOOOOO VV VV EE
PP RR RR OOOO VVVV EEEEEEEEEE
"""
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
import sys
input = sys.stdin.readline
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // gcd(a, b)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0, hi = None):
if hi == None:
hi = len(a);
while lo < hi:
mid = (lo+hi)//2;
if a[mid] < x:
lo = mid+1;
else:
hi = mid;
return lo;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
from bisect import bisect_left
def solve():
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print("T")
return
a.sort(reverse = True)
# print(*a)
sL = a[0]
sHL = 0
for x in a[1:]:
# print(sL, sHL, x)
if sL > sHL:
sHL += x
else:
sL += x
# print(sL, sHL)
print({False: "T", True:"HL"}[sHL >= sL])
if __name__ == '__main__':
for _ in range(int(input())):
solve()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
``` | instruction | 0 | 65,054 | 19 | 130,108 |
No | output | 1 | 65,054 | 19 | 130,109 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image> | instruction | 0 | 65,125 | 19 | 130,250 |
Tags: dp, math, probabilities
Correct Solution:
```
n = eval(input())
read = input()
p = []
for x in read.split():
p.append((float)(x))
v = 0.0
l = 0.0
for u in p:
v = v * (1 - u) + u * (v + 2 * l + 1)
l = (l + 1) * u
print(v)
# Made By Mostafa_Khaled
``` | output | 1 | 65,125 | 19 | 130,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image> | instruction | 0 | 65,126 | 19 | 130,252 |
Tags: dp, math, probabilities
Correct Solution:
```
n = eval(input())
read = input()
p = []
for x in read.split():
p.append((float)(x))
v = 0.0
l = 0.0
for item in p:
v = v*(1-item) + item*(v + 2*l + 1)
l = (l + 1)*item
print(v)
``` | output | 1 | 65,126 | 19 | 130,253 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image> | instruction | 0 | 65,127 | 19 | 130,254 |
Tags: dp, math, probabilities
Correct Solution:
```
# encoding: utf-8
"""
长度为n的字符串,第i个位置上为O的概率为pi,否则为x
字符串的得分为连续O的数量的平方和
如"OOXXOOOXOO"的得分为4+9+4=17
问得分期望
Di为到i位置连续O的期望,Ei为到i位置的得分期望
Di = (D[i-1]+1)*pi
Ei = E[i-1] + Pi * (2D[i-1] + 1)
"""
from math import sqrt
from queue import Queue
import sys
sys.setrecursionlimit(10**6)
if __name__ == '__main__':
# sys.stdin = open('1.txt', 'r')
n = int(input())
p = [float(i) for i in input().split()]
d = [0.0 for i in range(n)]
e = [0.0 for i in range(n)]
d[0] = e[0] = p[0]
for i in range(1, n):
d[i] = p[i] * (d[i-1] + 1.0)
e[i] = e[i-1] + p[i] * (2.0 * d[i-1] + 1)
print(e[n-1])
``` | output | 1 | 65,127 | 19 | 130,255 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image> | instruction | 0 | 65,128 | 19 | 130,256 |
Tags: dp, math, probabilities
Correct Solution:
```
n = eval(input())
read = input()
p = []
for x in read.split():
p.append((float)(x))
v = 0.0
l = 0.0
for u in p:
v = v * (1 - u) + u * (v + 2 * l + 1)
l = (l + 1) * u
print(v)
``` | output | 1 | 65,128 | 19 | 130,257 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image> | instruction | 0 | 65,129 | 19 | 130,258 |
Tags: dp, math, probabilities
Correct Solution:
```
n=int(input().strip())
p=[0]+list(map(float,input().split()))
a=[0]*(n+1)
b=[0]*(n+1)
for i in range(1,n+1):
b[i]=(b[i-1]+1)*p[i]
a[i]=(a[i-1]+2*b[i-1]+1)*p[i]+a[i-1]*(1-p[i])
print(a[-1])
``` | output | 1 | 65,129 | 19 | 130,259 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image> | instruction | 0 | 65,130 | 19 | 130,260 |
Tags: dp, math, probabilities
Correct Solution:
```
n=input()
lst = list(map(float, input().split()))
ds = []
ds.append(0)
for i, elem in enumerate(lst[1:]):
ds.append(ds[i] * elem + lst[i]*elem)
ans = 2 * sum(ds) + sum(lst)
print(ans)
``` | output | 1 | 65,130 | 19 | 130,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image> | instruction | 0 | 65,131 | 19 | 130,262 |
Tags: dp, math, probabilities
Correct Solution:
```
n = int(input())
p = list(map(float, input().split()))
p.append(0)
s=[p[0]]*3
s[2]=0
ans = (2*s[2]+s[1])*(1-p[1])
for i in range(1,n):
s[0] += 1 - p[i-1]
for j in range(2,0,-1):
s[j] += s[j-1]
for j in range(3):
s[j] *= p[i]
ans += (2*s[2]+s[1])*(1-p[i+1])
print(ans)
``` | output | 1 | 65,131 | 19 | 130,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image> | instruction | 0 | 65,132 | 19 | 130,264 |
Tags: dp, math, probabilities
Correct Solution:
```
n=int(input())
p=list(map(float,input().split()))
val=[0.0]
for j in range(n):
val.append((val[-1]+1.0)*p[j])
dp=[0 for i in range(n+1)]
for i in range(1,n+1):
dp[i]=p[i-1]*(1.0+dp[i-1]+2*val[i-1])+(1-p[i-1])*(dp[i-1])
print(dp[-1])
``` | output | 1 | 65,132 | 19 | 130,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image>
Submitted Solution:
```
n,a,b=int(input()),0,0
for i in map(float,input().split()):a,b=a+i*(1+b*2),i*(b+1)
print(a)
``` | instruction | 0 | 65,133 | 19 | 130,266 |
Yes | output | 1 | 65,133 | 19 | 130,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image>
Submitted Solution:
```
t1=t2=0.0
A=[0.0]*100005
n=int(input())
A=list(map(float,input().split()))
for i in range(n):
t2+=(t1*2+1)*A[i]
t1=(t1+1)*A[i]
print("{:.15f}".format(t2))
``` | instruction | 0 | 65,134 | 19 | 130,268 |
Yes | output | 1 | 65,134 | 19 | 130,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image>
Submitted Solution:
```
count = 0
total = 0
nums=0
new=0
while True :
inp = input ()
INP = input()
for char in inp :
if char == 'O':
count = count+1
else :
continue
new = count**2
total= new + total
nums = nums +1
print (nums , total ,new )
count = 0
if not 'O' in inp :
break
final = total/nums
print (final)
``` | instruction | 0 | 65,135 | 19 | 130,270 |
No | output | 1 | 65,135 | 19 | 130,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image>
Submitted Solution:
```
t1=t2=0.0
A=[0.0]*100005
n=int(input())
A=list(map(float,input().split()))
for i in range(n):
t2+=(t1*2+1)*A[i-1]
t1=(t1+1)*A[i-1]
print("{:.15f}".format(t2))
``` | instruction | 0 | 65,136 | 19 | 130,272 |
No | output | 1 | 65,136 | 19 | 130,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Examples
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
* "OOO" → 32 = 9;
* "OOX" → 22 = 4;
* "OXO" → 12 + 12 = 2;
* "OXX" → 12 = 1;
* "XOO" → 22 = 4;
* "XOX" → 12 = 1;
* "XXO" → 12 = 1;
* "XXX" → 0.
So the expected score is <image>
Submitted Solution:
```
n=input()
lst = list(map(float, input().split()))
ds = []
ds.append(0)
for i, elem in enumerate(lst[1:]):
ds.append(ds[i] * elem + lst[i-1]*elem)
ans = 2 * sum(ds) + sum(lst)
print(ans)
``` | instruction | 0 | 65,137 | 19 | 130,274 |
No | output | 1 | 65,137 | 19 | 130,275 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum. | instruction | 0 | 65,223 | 19 | 130,446 |
Tags: binary search, brute force, dp, greedy, sortings, two pointers
Correct Solution:
```
f = lambda: map(int, input().split())
g = lambda: m - l * p[l - 1] + s[l]
n, A, x, y, m = f()
t = sorted((q, i) for i, q in enumerate(f()))
p = [q for q, i in t]
s = [0] * (n + 1)
for j in range(n): s[j + 1] = p[j] + s[j]
l = r = n
F = L = R = B = -1
while 1:
if p:
while l > r or g() < 0: l -= 1
b = min(p[l - 1] + g() // l, A)
else: b, l = A, 0
f = x * (n - r) + y * b
if F < f: F, L, R, B = f, l, r, b
if not p: break
m += p.pop() - A
r -= 1
if m < 0: break
print(F)
p = [(i, B if j < L else q if j < R else A) for j, (q, i) in enumerate(t)]
for i, q in sorted(p): print(q)
``` | output | 1 | 65,223 | 19 | 130,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum. | instruction | 0 | 65,224 | 19 | 130,448 |
Tags: binary search, brute force, dp, greedy, sortings, two pointers
Correct Solution:
```
def main():
from bisect import bisect
n, A, cf, cm, m = map(int, input().split())
skills = list(map(int, input().split()))
xlat = sorted(range(n), key=skills.__getitem__)
sorted_skills = [skills[_] for _ in xlat]
bottom_lift, a, c = [], 0, 0
for i, b in enumerate(sorted_skills):
c += i * (b - a)
bottom_lift.append(c)
a = b
root_lift, a = [0], 0
for b in reversed(sorted_skills):
a += A - b
root_lift.append(a)
max_level = -1
for A_width, a in enumerate(root_lift):
if m < a:
break
money_left = m - a
floor_width = bisect(bottom_lift, money_left)
if floor_width > n - A_width:
floor_width = n - A_width
money_left -= bottom_lift[floor_width - 1]
if floor_width > 0:
floor = sorted_skills[floor_width - 1] + money_left // floor_width
if floor > A:
floor = A
else:
floor = A
level = cf * A_width + cm * floor
if max_level < level:
max_level, save = level, (A_width, floor, floor_width)
A_width, floor, floor_width = save
for id in xlat[:floor_width]:
skills[id] = floor
for id in xlat[n - A_width:]:
skills[id] = A
print(max_level)
print(' '.join(map(str, skills)))
if __name__ == '__main__':
main()
# Made By Mostafa_Khaled
``` | output | 1 | 65,224 | 19 | 130,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum. | instruction | 0 | 65,225 | 19 | 130,450 |
Tags: binary search, brute force, dp, greedy, sortings, two pointers
Correct Solution:
```
import bisect as bs
import heapq as hq
def force(cf, cm, f, m):
return f*cf + m*cm
# def perfect(sa, amax, m):
# p = 0
# while sa[-p-1] == amax:
# p += 1
# while sa[p] + m >= amax:
# for _ in range(amax-sa[-p-1]):
# yield p
# m -= amax-sa[-p-1]
# p += 1
# for _ in range(m+1):
# yield p
#
#
# def improve(sa, amax, m):
# am = sa[0]
# i = 1
# while i < len(a) and sa[i] == am:
# i += 1
# while i <= m:
# for _ in range(i):
# yield am
# am += 1
# m -= i
# while i < len(a) and sa[i] == am:
# i += 1
# for _ in range(m+1):
# yield am
def mtable(sa):
mt = [0]*len(sa)
for i in range(1, len(sa)):
mt[i] = mt[i-1] + i*(sa[i]-sa[i-1])
return mt
def maxm(sa, mt, f, k):
i = bs.bisect_right(mt, k, hi=len(sa)-f)
return sa[i-1] + (k-mt[i-1])//i
def optimize(a, amax, cf, cm, k):
if sum(a) + k >= len(a)*amax:
return len(a)*cf + amax*cm, len(a), amax
sa = sorted(a)
f = 0
while sa[-f-1] == amax:
f += 1
mt = mtable(sa)
of = f
om = maxm(sa, mt, f, k)
o = force(cf, cm, of, om)
while k >= amax - sa[-f-1]:
k -= amax - sa[-f-1]
f += 1
m = maxm(sa, mt, f, k)
t = force(cf, cm, f, m)
if t > o:
of, om, o = f, m, t
return o, of, om
# sa = sorted(a)
# fs = list(perfect(sa, amax, m))
# ms = list(improve(sa, amax, m))
# of, om = max(zip(fs, reversed(ms)), key=lambda fm: force(fm[0], fm[1]))
# return force(of, om), of, om
def apply(a, amax, of, om):
# Ensure all values are at least om
a_ = [max(om, ai) for ai in a]
# Increase top p values to amax
h = [(-a[i], i) for i in range(len(a))]
hq.heapify(h)
for _ in range(of):
_, i = hq.heappop(h)
a_[i] = amax
return a_
def best_force(a, amax, cf, cm, m):
t, of, om = optimize(a, amax, cf, cm, m)
if of == len(a):
return t, [amax]*len(a)
else:
return t, apply(a, amax, of, om)
if __name__ == '__main__':
n, amax, cf, cm, k = map(int, input().split())
a = list(map(int, input().split()))
assert len(a) == n
t, o = best_force(a, amax, cf, cm, k)
print(t)
print(' '.join(map(str, o)))
``` | output | 1 | 65,225 | 19 | 130,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum. | instruction | 0 | 65,226 | 19 | 130,452 |
Tags: binary search, brute force, dp, greedy, sortings, two pointers
Correct Solution:
```
import itertools
import bisect
n, A, cf, cm, m = [int(x) for x in input().split()]
skills = [int(x) for x in input().split()]
sorted_skills = list(sorted((k, i) for i, k in enumerate(skills)))
bottom_lift = [0 for i in range(n)]
for i in range(1, n):
bottom_lift[i] = bottom_lift[i-1] + i * (sorted_skills[i][0] - sorted_skills[i-1][0])
root_lift = [0 for i in range(n+1)]
for i in range(1, n+1):
root_lift[i] = root_lift[i-1] + A - sorted_skills[n-i][0]
max_level = -1
for i in range(n+1):
money_left = m - root_lift[i]
if money_left < 0: break
k = min(bisect.bisect(bottom_lift, money_left), n-i)
money_left -= bottom_lift[k-1]
min_level = min(A, sorted_skills[k-1][0] + money_left//k) if k > 0 else A
level = cf*i + cm*min_level
if max_level < level:
max_level = level
argmax = i
argmax_min_level = min_level
argmax_k = k
ans = [0 for i in range(n)]
for i, skill in enumerate(sorted_skills):
if i < argmax_k:
ans[skill[1]] = argmax_min_level
elif i >= n - argmax:
ans[skill[1]] = A
else:
ans[skill[1]] = skill[0]
print(max_level)
for a in ans:
print(a, end = ' ')
``` | output | 1 | 65,226 | 19 | 130,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum. | instruction | 0 | 65,227 | 19 | 130,454 |
Tags: binary search, brute force, dp, greedy, sortings, two pointers
Correct Solution:
```
n,A,cf,cm,mN = map(int,input().split())
a = list(map(int,input().split()))
aCOPY = []
for elem in a:
aCOPY.append(elem)
a.sort()
aPartialSum = [0]
for elem in a:
aPartialSum.append(aPartialSum[-1] + elem)
maxScore = 0
ansMAXIBound = 0
ansMAXI = 0
ansMIN = 0
for MAXI in range(n + 1):
currentScore = cf * MAXI
if MAXI >= 1:
mN -= (A - a[-MAXI])
if mN < 0:
break
if MAXI == n:
maxScore = currentScore + A * cm
ansMAXIBound = 0
ansMAXI = 10 ** 10
ansMIN = 0
# Find the maximum of minimum
l = a[0]
r = A - 1
while l < r:
m = (l + r + 1) // 2
lA = 0
rA = n - MAXI - 1
while lA < rA:
mA = (lA + rA) // 2
if a[mA] > m:
rA = mA - 1
if a[mA] < m:
lA = mA + 1
if a[mA] == m:
lA = mA
rA = mA
break
lA = min(lA,n - MAXI - 1)
if a[lA] > m:
lA -= 1
expenditure = (lA + 1) * m - aPartialSum[lA + 1]
if expenditure > mN:
r = m - 1
else:
l = m
currentScore += l * cm
if currentScore > maxScore:
maxScore = currentScore
ansMAXIBound = a[-MAXI]
ansMAXI = MAXI
ansMIN = l
print(maxScore)
inclCount = 0
for i in range(n):
if aCOPY[i] > ansMAXIBound and inclCount < ansMAXI:
aCOPY[i] = A
inclCount += 1
for i in range(n):
if aCOPY[i] == ansMAXIBound and inclCount < ansMAXI:
aCOPY[i] = A
inclCount += 1
if aCOPY[i] < ansMIN:
aCOPY[i] = ansMIN
print(" ".join(map(str,aCOPY)))
``` | output | 1 | 65,227 | 19 | 130,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum. | instruction | 0 | 65,228 | 19 | 130,456 |
Tags: binary search, brute force, dp, greedy, sortings, two pointers
Correct Solution:
```
def main():
from bisect import bisect
n, A, cf, cm, m = map(int, input().split())
skills = list(map(int, input().split()))
xlat = sorted(range(n), key=skills.__getitem__)
sorted_skills = [skills[_] for _ in xlat]
bottom_lift, a, c = [], 0, 0
for i, b in enumerate(sorted_skills):
c += i * (b - a)
bottom_lift.append(c)
a = b
root_lift, a = [0], 0
for b in reversed(sorted_skills):
a += A - b
root_lift.append(a)
max_level = -1
for A_width, a in enumerate(root_lift):
if m < a:
break
money_left = m - a
floor_width = bisect(bottom_lift, money_left)
if floor_width > n - A_width:
floor_width = n - A_width
money_left -= bottom_lift[floor_width - 1]
if floor_width > 0:
floor = sorted_skills[floor_width - 1] + money_left // floor_width
if floor > A:
floor = A
else:
floor = A
level = cf * A_width + cm * floor
if max_level < level:
max_level, save = level, (A_width, floor, floor_width)
A_width, floor, floor_width = save
for id in xlat[:floor_width]:
skills[id] = floor
for id in xlat[n - A_width:]:
skills[id] = A
print(max_level)
print(' '.join(map(str, skills)))
if __name__ == '__main__':
main()
``` | output | 1 | 65,228 | 19 | 130,457 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum.
Submitted Solution:
```
import sys
__author__ = "runekri3"
n, max_level, cf, cm, money = 100000, 1000000000, 0, 0, 10000000000
e_levels = list(enumerate(map(int, input().split())))
# n, max_level, cf, cm, money = list(map(int, input().split()))
# e_levels = list(enumerate(map(int, input().split())))
if n * max_level - sum(cur_level[1] for cur_level in e_levels) <= money:
force = n * cf + max_level * cm
print(force)
print(" ".join([str(max_level)] * n))
sys.exit()
if cf == 0 and cm == 0:
print(0)
print(" ".join(str(e_level[1]) for e_level in e_levels))
sys.exit()
e_levels.sort(key=lambda x: x[1])
amax_i = len(e_levels) - 1
while e_levels[amax_i][1] == max_level:
amax_i -= 1
min_level = e_levels[0][1]
last_min_i = 0
try:
while e_levels[last_min_i + 1][1] == min_level:
last_min_i += 1
except IndexError:
pass
start_force = cf * (len(e_levels) - amax_i - 1) + cm * min_level
force = 0
stack = [(money, force, min_level, last_min_i, amax_i)]
best_stack_item = stack[0]
while stack:
money, force, min_level, last_min_i, amax_i = stack.pop()
incr_min_price = last_min_i + 1
incr_max_price = max_level - e_levels[amax_i][1]
if incr_min_price <= money:
n_last_min_i = last_min_i
try:
while e_levels[n_last_min_i + 1][1] == min_level + 1:
n_last_min_i += 1
except IndexError:
pass
stack.append((money - incr_min_price, force + cm, min_level + 1, n_last_min_i, amax_i))
if incr_max_price <= money:
stack.append((money - incr_max_price, force + cf, min_level, last_min_i, amax_i - 1))
elif incr_max_price <= money:
stack.append((money - incr_max_price, force + cf, min_level, last_min_i, amax_i - 1))
else:
if force > best_stack_item[1]:
best_stack_item = (money, force, min_level, last_min_i, amax_i)
money, force, min_level, last_min_i, amax_i = best_stack_item
final_levels = [""] * n
for i, level in e_levels:
final_levels[i] = str(level)
for i in range(last_min_i + 1):
final_levels[e_levels[i][0]] = str(min_level)
for i in range(amax_i + 1, len(e_levels)):
final_levels[e_levels[i][0]] = str(max_level)
print(start_force + force)
print(" ".join(final_levels))
``` | instruction | 0 | 65,229 | 19 | 130,458 |
No | output | 1 | 65,229 | 19 | 130,459 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum.
Submitted Solution:
```
__author__ = 'abdujabbor'
def calculate_strength(aa, a, cf, cm):
c = 0
for i in aa:
if i == a:
c += 1
return c * cf + min(aa) * cm
n, a, cf, cm, m = [int(x) for x in input().split()]
aa = sorted([int(x) for x in input().split()], key=None, reverse=True)
while m >= 0:
updated = False
for i in range(len(aa)):
if aa[i] < a and m > 0:
aa[i] += 1
m -= 1
updated = True
if updated is False:
break
m -= 1
print(calculate_strength(aa, a, cf, cm))
for i in range(len(aa)):
print(aa[i], end=' ')
``` | instruction | 0 | 65,230 | 19 | 130,460 |
No | output | 1 | 65,230 | 19 | 130,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum.
Submitted Solution:
```
#! /usr/bin/env python3
import sys
a = []
suf_sum = []
def need(idx, n, min_val):
if (a[n - 1][0] >= min_val):
return True
l, r = idx, n - 1
while l < r:
mid = (l + r) >> 1
if a[mid][0] >= min_val:
l = mid + 1
else:
r = mid
count = n - l
return min_val * count - suf_sum[l]
def get_min(idx, n, max_val, m):
if idx >= n:
return max_val
l, r = 0, max_val
while l < r:
mid = (l + r + 1) >> 1
if need(idx, n, mid) > m:
r = mid - 1
else:
l = mid
return l
class IO:
def __init__(self):
self.input_buffer = ''.join(sys.stdin.readlines())
self.idx = 0
self.output_buffer = ''
def read_int(self):
last_char = ' '
while self.input_buffer[self.idx] < '0' or self.input_buffer[self.idx] > '9':
last_char = self.input_buffer[self.idx]
self.idx += 1
x = 0
while self.input_buffer[self.idx] >= '0' and self.input_buffer[self.idx] <= '9':
x = x * 10 + int(self.input_buffer[self.idx])
self.idx += 1
if last_char == '-':
x *= -1
return x
def print(self, x, c = ' '):
self.output_buffer += str(x) + c
def flush(self):
print(self.output_buffer)
io = IO()
def print_vals(idx, n, max_val, m):
b = [0] * n
for i in range(n):
b[a[i][1]] = a[i][0]
for i in range(idx + 1):
b[a[i][1]] = max_val
min_val = get_min(idx + 1, n, max_val, m)
for i in range(n - 1, -1, -1):
if b[i] <= min_val:
b[i] = min_val
if n > 1000:
return
for i in range(n):
io.print(b[i])
io.print('\n', '')
def main():
global a, suf_sum
n = io.read_int()
max_val = io.read_int()
cf = io.read_int()
cm = io.read_int()
m = io.read_int()
if n > 1000:
return
a = [(0, 0)] * n
for i in range(n):
a[i] = (io.read_int(), i)
a.sort(reverse = True)
suf_sum = [0] * len(a)
suf_sum[n - 1] = a[n - 1][0]
for i in range(n - 2, -1, -1):
suf_sum[i] = suf_sum[i + 1] + a[i][0]
ans = cm * get_min(0, n, max_val, m)
best_idx, rem_money = -1, m
for i in range(n):
m -= (max_val - a[i][0])
if m >= 0:
cur = cf * (i + 1) + cm * get_min(i + 1, n, max_val, m)
if ans < cur:
ans = cur
best_idx = i
rem_money = m
io.print(ans, '\n')
print_vals(best_idx, n, max_val, rem_money)
io.flush()
if __name__ == '__main__':
main()
``` | instruction | 0 | 65,231 | 19 | 130,462 |
No | output | 1 | 65,231 | 19 | 130,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
* The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
* The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
Input
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
Output
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
Examples
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5
Note
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum.
Submitted Solution:
```
#! /usr/bin/env python3
import sys
a = []
suf_sum = []
def need(idx, n, min_val):
if (a[n - 1][0] >= min_val):
return True
l, r = idx, n - 1
while l < r:
mid = (l + r) >> 1
if a[mid][0] >= min_val:
l = mid + 1
else:
r = mid
count = n - l
return min_val * count - suf_sum[l]
def get_min(idx, n, max_val, m):
if idx >= n:
return max_val
l, r = 0, max_val
while l < r:
mid = (l + r + 1) >> 1
if need(idx, n, mid) > m:
r = mid - 1
else:
l = mid
return l
class IO:
def __init__(self):
self.input_buffer = ''.join(sys.stdin.readlines())
self.idx = 0
self.output_buffer = ''
def read_int(self):
last_char = ' '
while self.input_buffer[self.idx] < '0' or self.input_buffer[self.idx] > '9':
last_char = self.input_buffer[self.idx]
self.idx += 1
x = 0
while self.input_buffer[self.idx] >= '0' and self.input_buffer[self.idx] <= '9':
x = x * 10 + int(self.input_buffer[self.idx])
self.idx += 1
if last_char == '-':
x *= -1
return x
def print(self, x, c = ' '):
self.output_buffer += str(x) + c
def flush(self):
print(self.output_buffer)
io = IO()
def print_vals(idx, n, max_val, m):
b = [0] * n
for i in range(n):
b[a[i][1]] = a[i][0]
for i in range(idx + 1):
b[a[i][1]] = max_val
min_val = get_min(idx + 1, n, max_val, m)
for i in range(n - 1, -1, -1):
if b[i] <= min_val:
b[i] = min_val
if n > 1000:
return
for i in range(n):
io.print(b[i])
io.print('\n', '')
def main():
global a, suf_sum
n = io.read_int()
max_val = io.read_int()
cf = io.read_int()
cm = io.read_int()
m = io.read_int()
a = [(0, 0)] * n
for i in range(n):
a[i] = (io.read_int(), i)
a.sort(reverse = True)
if n > 1000:
return
suf_sum = [0] * len(a)
suf_sum[n - 1] = a[n - 1][0]
for i in range(n - 2, -1, -1):
suf_sum[i] = suf_sum[i + 1] + a[i][0]
ans = cm * get_min(0, n, max_val, m)
best_idx, rem_money = -1, m
for i in range(n):
m -= (max_val - a[i][0])
if m >= 0:
cur = cf * (i + 1) + cm * get_min(i + 1, n, max_val, m)
if ans < cur:
ans = cur
best_idx = i
rem_money = m
io.print(ans, '\n')
print_vals(best_idx, n, max_val, rem_money)
io.flush()
if __name__ == '__main__':
main()
``` | instruction | 0 | 65,232 | 19 | 130,464 |
No | output | 1 | 65,232 | 19 | 130,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow.
A positive integer a is initially on the screen. The player can put a coin into the machine and then add 1 to or subtract 1 from any two adjacent digits. All digits must remain from 0 to 9 after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add 1 to 9, to subtract 1 from 0 and to subtract 1 from the leading 1. Once the number on the screen becomes equal to b, the player wins the jackpot. a and b have the same number of digits.
Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) standing for the length of numbers a and b.
The next two lines contain numbers a and b, each one on a separate line (10^{n-1} ≤ a, b < 10^n).
Output
If it is impossible to win the jackpot, print a single integer -1.
Otherwise, the first line must contain the minimal possible number c of coins the player has to spend.
min(c, 10^5) lines should follow, i-th of them containing two integers d_i and s_i (1≤ d_i≤ n - 1, s_i = ± 1) denoting that on the i-th step the player should add s_i to the d_i-th and (d_i + 1)-st digits from the left (e. g. d_i = 1 means that two leading digits change while d_i = n - 1 means that there are two trailing digits which change).
Please notice that the answer may be very big and in case c > 10^5 you should print only the first 10^5 moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.
Examples
Input
3
223
322
Output
2
1 1
2 -1
Input
2
20
42
Output
2
1 1
1 1
Input
2
35
44
Output
-1
Note
In the first example, we can make a +1 operation on the two first digits, transforming number 223 into 333, and then make a -1 operation on the last two digits, transforming 333 into 322.
It's also possible to do these operations in reverse order, which makes another correct answer.
In the last example, one can show that it's impossible to transform 35 into 44. | instruction | 0 | 65,669 | 19 | 131,338 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
def main():
n = int(input())
a = list(map(int, (x for x in input())))
b = list(map(int, (x for x in input())))
x = [0] * (n - 1)
x[0] = b[0] - a[0]
for i in range(1, n - 1):
x[i] = b[i] - a[i] - x[i - 1]
if a[n - 1] + x[n - 2] != b[n - 1]:
print(-1)
return
cnt = sum(map(abs, x)) # prevbug: ftl
print(cnt)
cnt = min(cnt, 10 ** 5)
index = 0
def handle_zero_nine(cur_zero):
nonlocal cnt
nxt = index + 1
# cur_zero = True prevbug: preserved this line
while True:
if cur_zero and a[nxt + 1] != 9:
break
if not cur_zero and a[nxt + 1] != 0:
break
nxt += 1
cur_zero = not cur_zero
while nxt > index:
if cnt == 0:
break
if cur_zero:
print(nxt + 1, 1)
a[nxt] += 1
a[nxt + 1] += 1
else:
print(nxt + 1, -1)
a[nxt] -= 1
a[nxt + 1] -= 1
nxt -= 1
cnt -= 1
# print(a)
cur_zero = not cur_zero
while cnt > 0:
if a[index] == b[index]:
index += 1
continue
elif a[index] > b[index] and a[index + 1] == 0:
handle_zero_nine(True)
elif a[index] < b[index] and a[index + 1] == 9:
handle_zero_nine(False)
elif a[index] > b[index]:
print(index + 1, -1)
a[index] -= 1
a[index + 1] -= 1
cnt -= 1
# print(a)
elif a[index] < b[index]:
print(index + 1, 1)
a[index] += 1
a[index + 1] += 1
cnt -= 1
# print(a)
if __name__ == '__main__':
main()
``` | output | 1 | 65,669 | 19 | 131,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow.
A positive integer a is initially on the screen. The player can put a coin into the machine and then add 1 to or subtract 1 from any two adjacent digits. All digits must remain from 0 to 9 after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add 1 to 9, to subtract 1 from 0 and to subtract 1 from the leading 1. Once the number on the screen becomes equal to b, the player wins the jackpot. a and b have the same number of digits.
Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) standing for the length of numbers a and b.
The next two lines contain numbers a and b, each one on a separate line (10^{n-1} ≤ a, b < 10^n).
Output
If it is impossible to win the jackpot, print a single integer -1.
Otherwise, the first line must contain the minimal possible number c of coins the player has to spend.
min(c, 10^5) lines should follow, i-th of them containing two integers d_i and s_i (1≤ d_i≤ n - 1, s_i = ± 1) denoting that on the i-th step the player should add s_i to the d_i-th and (d_i + 1)-st digits from the left (e. g. d_i = 1 means that two leading digits change while d_i = n - 1 means that there are two trailing digits which change).
Please notice that the answer may be very big and in case c > 10^5 you should print only the first 10^5 moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.
Examples
Input
3
223
322
Output
2
1 1
2 -1
Input
2
20
42
Output
2
1 1
1 1
Input
2
35
44
Output
-1
Note
In the first example, we can make a +1 operation on the two first digits, transforming number 223 into 333, and then make a -1 operation on the last two digits, transforming 333 into 322.
It's also possible to do these operations in reverse order, which makes another correct answer.
In the last example, one can show that it's impossible to transform 35 into 44.
Submitted Solution:
```
def parse():
input()
a = list(map(int, list(input())))
b = list(map(int, list(input())))
return a, b
def main():
a, b = parse()
steps = []
for i in range(len(a) - 1):
diff = abs(a[i] - b[i])
if a[i] > b[i]:
a[i] -= diff
a[i + 1] -= diff
if a[i + 1] <= 0:
print(-1)
return
steps.append([diff, i + 1, -1])
else:
a[i] += diff
a[i + 1] += diff
if a[i + 1] >= 9:
print(-1)
return
steps.append([diff, i + 1, 1])
if a[-1] == b[-1]:
print(len(steps))
for i, s in enumerate(steps):
if i >= 10e5:
break
for _ in range(s[0]):
print(s[1], s[2])
else:
print(-1)
if __name__ == "__main__":
main()
``` | instruction | 0 | 65,670 | 19 | 131,340 |
No | output | 1 | 65,670 | 19 | 131,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow.
A positive integer a is initially on the screen. The player can put a coin into the machine and then add 1 to or subtract 1 from any two adjacent digits. All digits must remain from 0 to 9 after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add 1 to 9, to subtract 1 from 0 and to subtract 1 from the leading 1. Once the number on the screen becomes equal to b, the player wins the jackpot. a and b have the same number of digits.
Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) standing for the length of numbers a and b.
The next two lines contain numbers a and b, each one on a separate line (10^{n-1} ≤ a, b < 10^n).
Output
If it is impossible to win the jackpot, print a single integer -1.
Otherwise, the first line must contain the minimal possible number c of coins the player has to spend.
min(c, 10^5) lines should follow, i-th of them containing two integers d_i and s_i (1≤ d_i≤ n - 1, s_i = ± 1) denoting that on the i-th step the player should add s_i to the d_i-th and (d_i + 1)-st digits from the left (e. g. d_i = 1 means that two leading digits change while d_i = n - 1 means that there are two trailing digits which change).
Please notice that the answer may be very big and in case c > 10^5 you should print only the first 10^5 moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.
Examples
Input
3
223
322
Output
2
1 1
2 -1
Input
2
20
42
Output
2
1 1
1 1
Input
2
35
44
Output
-1
Note
In the first example, we can make a +1 operation on the two first digits, transforming number 223 into 333, and then make a -1 operation on the last two digits, transforming 333 into 322.
It's also possible to do these operations in reverse order, which makes another correct answer.
In the last example, one can show that it's impossible to transform 35 into 44.
Submitted Solution:
```
def parse():
input()
a = list(map(int, list(input())))
b = list(map(int, list(input())))
return a, b
def main():
a, b = parse()
steps = []
n_steps = 0
for i in range(len(a) - 1):
diff = abs(a[i] - b[i])
if diff == 0:
continue
if a[i] > b[i]:
a[i] -= diff
a[i + 1] -= diff
if a[i + 1] < 0:
print(-1)
return
steps.append([diff, i + 1, -1])
n_steps += diff
else:
a[i] += diff
a[i + 1] += diff
if a[i + 1] > 9:
print(-1)
return
steps.append([diff, i + 1, 1])
n_steps += diff
if a[-1] == b[-1]:
print(n_steps)
for i, s in enumerate(steps):
if i >= 10e5:
break
for _ in range(s[0]):
print(s[1], s[2])
else:
print(-1)
if __name__ == "__main__":
main()
``` | instruction | 0 | 65,671 | 19 | 131,342 |
No | output | 1 | 65,671 | 19 | 131,343 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow.
A positive integer a is initially on the screen. The player can put a coin into the machine and then add 1 to or subtract 1 from any two adjacent digits. All digits must remain from 0 to 9 after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add 1 to 9, to subtract 1 from 0 and to subtract 1 from the leading 1. Once the number on the screen becomes equal to b, the player wins the jackpot. a and b have the same number of digits.
Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) standing for the length of numbers a and b.
The next two lines contain numbers a and b, each one on a separate line (10^{n-1} ≤ a, b < 10^n).
Output
If it is impossible to win the jackpot, print a single integer -1.
Otherwise, the first line must contain the minimal possible number c of coins the player has to spend.
min(c, 10^5) lines should follow, i-th of them containing two integers d_i and s_i (1≤ d_i≤ n - 1, s_i = ± 1) denoting that on the i-th step the player should add s_i to the d_i-th and (d_i + 1)-st digits from the left (e. g. d_i = 1 means that two leading digits change while d_i = n - 1 means that there are two trailing digits which change).
Please notice that the answer may be very big and in case c > 10^5 you should print only the first 10^5 moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.
Examples
Input
3
223
322
Output
2
1 1
2 -1
Input
2
20
42
Output
2
1 1
1 1
Input
2
35
44
Output
-1
Note
In the first example, we can make a +1 operation on the two first digits, transforming number 223 into 333, and then make a -1 operation on the last two digits, transforming 333 into 322.
It's also possible to do these operations in reverse order, which makes another correct answer.
In the last example, one can show that it's impossible to transform 35 into 44.
Submitted Solution:
```
n = int(input())
a = int(input())
b = int(input())
if a == b:
print(0)
exit(0)
if (a - b) % 11:
print(-1)
exit(0)
L = 10**(n-1)-b
R = 10**n - b - 1
if R < 0 or L > 0:
print(-1)
exit(0)
t = -1
d = (a - b) // 11
if d < 0:
d = -d
t = -t
s = str(d)
res = []
l = len(s)
for i in range(l):
xx = ord(s[i]) - ord('0')
if xx <= 11 - xx:
for j in range(0, xx):
res.append([i + 1, t])
else:
res.append([i + 1, t])
for j in range(0, 10 - xx):
res.append([i + 2, -t])
sz = len(res)
print(sz)
sz = min(sz, 100000);
for i in range(0, sz):
for x in res[i]:
print(x, end=' ')
print("")
``` | instruction | 0 | 65,672 | 19 | 131,344 |
No | output | 1 | 65,672 | 19 | 131,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow.
A positive integer a is initially on the screen. The player can put a coin into the machine and then add 1 to or subtract 1 from any two adjacent digits. All digits must remain from 0 to 9 after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add 1 to 9, to subtract 1 from 0 and to subtract 1 from the leading 1. Once the number on the screen becomes equal to b, the player wins the jackpot. a and b have the same number of digits.
Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) standing for the length of numbers a and b.
The next two lines contain numbers a and b, each one on a separate line (10^{n-1} ≤ a, b < 10^n).
Output
If it is impossible to win the jackpot, print a single integer -1.
Otherwise, the first line must contain the minimal possible number c of coins the player has to spend.
min(c, 10^5) lines should follow, i-th of them containing two integers d_i and s_i (1≤ d_i≤ n - 1, s_i = ± 1) denoting that on the i-th step the player should add s_i to the d_i-th and (d_i + 1)-st digits from the left (e. g. d_i = 1 means that two leading digits change while d_i = n - 1 means that there are two trailing digits which change).
Please notice that the answer may be very big and in case c > 10^5 you should print only the first 10^5 moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.
Examples
Input
3
223
322
Output
2
1 1
2 -1
Input
2
20
42
Output
2
1 1
1 1
Input
2
35
44
Output
-1
Note
In the first example, we can make a +1 operation on the two first digits, transforming number 223 into 333, and then make a -1 operation on the last two digits, transforming 333 into 322.
It's also possible to do these operations in reverse order, which makes another correct answer.
In the last example, one can show that it's impossible to transform 35 into 44.
Submitted Solution:
```
def main():
n = int(input())
a = list(map(int, (x for x in input())))
b = list(map(int, (x for x in input())))
x = [0] * (n - 1)
x[0] = b[0] - a[0]
for i in range(1, n - 1):
x[i] = b[i] - a[i] - x[i - 1]
if a[n - 1] + x[n - 2] != b[n - 1]:
print(-1)
return
cnt = sum(map(abs, x))
cnt = min(cnt, 10 ** 5)
index = 0
def handle_zero_nine(cur_zero):
nonlocal cnt
nxt = index + 1
cur_zero = True
while True:
if cur_zero and a[nxt + 1] != 9:
break
if not cur_zero and a[nxt + 1] != 0:
break
nxt += 1
cur_zero = not cur_zero
while nxt > index:
if cnt == 0:
break
if cur_zero:
print(nxt + 1, 1)
a[nxt] += 1
a[nxt + 1] += 1
else:
print(nxt + 1, -1)
a[nxt] -= 1
a[nxt + 1] -= 1
nxt -= 1
cnt -= 1
cur_zero = not cur_zero
while cnt > 0:
if a[index] == b[index]:
index += 1
continue
elif a[index] > b[index] and a[index + 1] == 0:
handle_zero_nine(True)
elif a[index] < b[index] and a[index + 1] == 9:
handle_zero_nine(False)
elif a[index] > b[index]:
print(index + 1, -1)
a[index] -= 1
a[index + 1] -= 1
cnt -= 1
elif a[index] < b[index]:
print(index + 1, 1)
a[index] += 1
a[index + 1] += 1
cnt -= 1
if __name__ == '__main__':
main()
``` | instruction | 0 | 65,673 | 19 | 131,346 |
No | output | 1 | 65,673 | 19 | 131,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"You must lift the dam. With a lever. I will give it to you.
You must block the canal. With a rock. I will not give the rock to you."
Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them.
Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task.
You are given a positive integer n, and an array a of positive integers. The task is to calculate the number of such pairs (i,j) that i<j and a_i \& a_j ≥ a_i ⊕ a_j, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), and ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Danik has solved this task. But can you solve it?
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1 ≤ t ≤ 10) denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer n (1 ≤ n ≤ 10^5) — length of the array.
The second line contains n positive integers a_i (1 ≤ a_i ≤ 10^9) — elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For every test case print one non-negative integer — the answer to the problem.
Example
Input
5
5
1 4 3 7 10
3
1 1 1
4
6 2 5 3
2
2 4
1
1
Output
1
3
2
0
0
Note
In the first test case there is only one pair: (4,7): for it 4 \& 7 = 4, and 4 ⊕ 7 = 3.
In the second test case all pairs are good.
In the third test case there are two pairs: (6,5) and (2,3).
In the fourth test case there are no good pairs.
Submitted Solution:
```
from collections import Counter
t=int(input())
for i in range(t):
n=int(input())
arr=list(map(int,input().split()))
arr1=[]
for i in range(len(arr)):
bnr = bin(arr[i]).replace('0b','')
if bnr[0]=="1":
arr1.append(len(bnr)-1)
p=Counter(arr1)
count=0
for i in p.values():
if i>=2:
count+=((i-1)*((i-1)+1))//2
print(count)
``` | instruction | 0 | 65,778 | 19 | 131,556 |
Yes | output | 1 | 65,778 | 19 | 131,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"You must lift the dam. With a lever. I will give it to you.
You must block the canal. With a rock. I will not give the rock to you."
Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them.
Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task.
You are given a positive integer n, and an array a of positive integers. The task is to calculate the number of such pairs (i,j) that i<j and a_i \& a_j ≥ a_i ⊕ a_j, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), and ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Danik has solved this task. But can you solve it?
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1 ≤ t ≤ 10) denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer n (1 ≤ n ≤ 10^5) — length of the array.
The second line contains n positive integers a_i (1 ≤ a_i ≤ 10^9) — elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For every test case print one non-negative integer — the answer to the problem.
Example
Input
5
5
1 4 3 7 10
3
1 1 1
4
6 2 5 3
2
2 4
1
1
Output
1
3
2
0
0
Note
In the first test case there is only one pair: (4,7): for it 4 \& 7 = 4, and 4 ⊕ 7 = 3.
In the second test case all pairs are good.
In the third test case there are two pairs: (6,5) and (2,3).
In the fourth test case there are no good pairs.
Submitted Solution:
```
for T in range(int(input())):
n=int(input())
a=[ int(x) for x in input().split()]
cnt=[0 for i in range(50)]
ans=0
for i in range(n):
now=0
x=a[i]
while(x>0):
x//=2
now+=1
cnt[now]+=1
ans=0
for i in range(33):
ans+=cnt[i]*(cnt[i]-1)//2
print(ans)
``` | instruction | 0 | 65,779 | 19 | 131,558 |
Yes | output | 1 | 65,779 | 19 | 131,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"You must lift the dam. With a lever. I will give it to you.
You must block the canal. With a rock. I will not give the rock to you."
Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them.
Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task.
You are given a positive integer n, and an array a of positive integers. The task is to calculate the number of such pairs (i,j) that i<j and a_i \& a_j ≥ a_i ⊕ a_j, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), and ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Danik has solved this task. But can you solve it?
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1 ≤ t ≤ 10) denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer n (1 ≤ n ≤ 10^5) — length of the array.
The second line contains n positive integers a_i (1 ≤ a_i ≤ 10^9) — elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For every test case print one non-negative integer — the answer to the problem.
Example
Input
5
5
1 4 3 7 10
3
1 1 1
4
6 2 5 3
2
2 4
1
1
Output
1
3
2
0
0
Note
In the first test case there is only one pair: (4,7): for it 4 \& 7 = 4, and 4 ⊕ 7 = 3.
In the second test case all pairs are good.
In the third test case there are two pairs: (6,5) and (2,3).
In the fourth test case there are no good pairs.
Submitted Solution:
```
import math
def nc2(n):
return int((n*(n-1))/2)
t = int(input())
for h in range(t):
k = int(input())
n = list(map(int, input().split()))
n.sort()
# l = max(n)
# ma = int(math.log(l,2))
j = 0
app = 0
ans = 0
for i in range(k):
if j == int(math.log(n[i],2)):
app += 1
elif j < int(math.log(n[i],2)):
ans += nc2(app)
app = 1
j = int(math.log(n[i],2))
ans += nc2(app)
print(ans)
``` | instruction | 0 | 65,780 | 19 | 131,560 |
Yes | output | 1 | 65,780 | 19 | 131,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"You must lift the dam. With a lever. I will give it to you.
You must block the canal. With a rock. I will not give the rock to you."
Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them.
Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task.
You are given a positive integer n, and an array a of positive integers. The task is to calculate the number of such pairs (i,j) that i<j and a_i \& a_j ≥ a_i ⊕ a_j, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), and ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Danik has solved this task. But can you solve it?
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1 ≤ t ≤ 10) denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer n (1 ≤ n ≤ 10^5) — length of the array.
The second line contains n positive integers a_i (1 ≤ a_i ≤ 10^9) — elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For every test case print one non-negative integer — the answer to the problem.
Example
Input
5
5
1 4 3 7 10
3
1 1 1
4
6 2 5 3
2
2 4
1
1
Output
1
3
2
0
0
Note
In the first test case there is only one pair: (4,7): for it 4 \& 7 = 4, and 4 ⊕ 7 = 3.
In the second test case all pairs are good.
In the third test case there are two pairs: (6,5) and (2,3).
In the fourth test case there are no good pairs.
Submitted Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
t=int(input())
for i in range(t):
n=int(input())
arr=list(map(int,input().split()))
if n==1:
print(0)
else:
num=[0]*31
for j in range(n):
num[len(bin(arr[j])[2:])-1]+=1
ans=0
for j in range(31):
if num[j]>1:
a=num[j]
b=(a*(a-1))//2
ans+=(b)
print(ans)
``` | instruction | 0 | 65,781 | 19 | 131,562 |
Yes | output | 1 | 65,781 | 19 | 131,563 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.