message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 6,764 | 19 | 13,528 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
import sys
import math
from collections import defaultdict
import heapq
def getnum(num):
cnt=0
ans=0
while((1<<cnt)<=num):
ans=cnt
cnt+=1
if num==0:
return 0
return ans+1
t=int(sys.stdin.readline())
for _ in range(t):
n=int(sys.stdin.readline())
arr=list(map(int,sys.stdin.readline().split()))
mp=[[] for x in range(31)]
last=[]
for i in range(n):
x=getnum(arr[i])
mp[x].append(arr[i])
last.append(x)
last.sort()
rem=n
z=True
for i in range(30,0,-1):
if len(mp[i])!=0:
y=len(mp[i])
rem=n-y
if y==1:
z=False
print('WIN')
break
if y%2!=0:
if rem%2==0:
first=(y+1)//2
second=y//2
if first%2!=0:
print('WIN')
else:
print('LOSE')
z=False
break
if rem%2!=0:
print('WIN')
z=False
break
else:
for j in range(y):
mp[i][j]%=(1<<(i-1))
x=getnum(mp[i][j])
mp[x].append(mp[i][j])
else:
continue
if z:
print('DRAW')
``` | output | 1 | 6,764 | 19 | 13,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 6,765 | 19 | 13,530 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
from bisect import bisect_left as lower_bound, bisect_right as upper_bound
def so(): return int(input())
def st(): return input()
def mj(): return map(int,input().strip().split(" "))
def msj(): return map(str,input().strip().split(" "))
def le(): return list(map(int,input().split()))
def lebe():return list(map(int, input()))
def dmain():
sys.setrecursionlimit(1000000)
threading.stack_size(1024000)
thread = threading.Thread(target=main)
thread.start()
def joro(L):
return(''.join(map(str, L)))
def decimalToBinary(n): return bin(n).replace("0b","")
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def read():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def tr(n):
return n*(n+1)//2
def iu():
m=so()
L=le()
i=30
while(i>=0):
c=0
for j in range(m):
c=c+((L[j]//(2**i))&1)
if(c%4==1):
print("WIN")
return
elif(m%2!=0 and c%2!=0):
print("LOSE")
return
elif(1==c%2):
print("WIN")
return
i=i-1
print("DRAW")
return
def main():
for i in range(so()):
iu()
# region fastio
# template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
main()
#dmain()
# Comment Read()
``` | output | 1 | 6,765 | 19 | 13,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 6,766 | 19 | 13,532 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
import sys
from heapq import heappush,heappop,heapify
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
# mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('a')
file=1
def solve():
for _ in range(ii()):
n=ii()
a=li()
x=0
for i in a:
x^=i
if(x==0):
print("DRAW")
continue
for i in range(30,-1,-1):
if x>>i&1:
one=0
zero=0
for j in a:
if j>>i&1:
one+=1
else:
zero+=1
# if ith bit of even number element are not set
# then her best friend forced Koa to chose (x*2 + 2)[x=one//4] no of elements
# whose ith bit is set then Koa score ith bit will not set but her best
# friend select (x*2 + 1) no of elements so her bestfriend score ith bit
# will set. So,koa will lose.
if(zero%2==0 and one%4==3):
print('LOSE')
else:
print('WIN')
break
if __name__ =="__main__":
if(file):
if path.exists('input.txt'):
sys.stdin=open('input.txt', 'r')
sys.stdout=open('output.txt','w')
else:
input=sys.stdin.readline
solve()
``` | output | 1 | 6,766 | 19 | 13,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 6,767 | 19 | 13,534 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ones = [0]*40
for i in range(40):
for ai in a:
ones[i] += (ai>>i)&1
for i in range(39, -1, -1):
if ones[i]%2==0:
continue
else:
if ones[i]%4==3 and (n-ones[i])%2==0:
print('LOSE')
else:
print('WIN')
break
else:
print('DRAW')
``` | output | 1 | 6,767 | 19 | 13,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 6,768 | 19 | 13,536 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
a=[int(o) for o in input().split()]
ones=[0]*35
zeros= [0]*35
for i in a:
ba=bin(i)[2:][::-1]
j=-1
for k in ba:
if k=='1':
ones[j]+=1
else:
zeros[j]+=1
j-=1
res="DRAW"
# print(ones)
for i in range(35):
if ones[i]%2!=0:
if ones[i]%4==3 and (n-ones[i])%2==0:
res="LOSE"
else:
res="WIN"
break
print(res)
``` | output | 1 | 6,768 | 19 | 13,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 6,769 | 19 | 13,538 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
def solve():
n = int(input())
lst = list(map(int,input().split()))
k = 1
while k < 10**9:
k *= 2
num = 0
while k and num % 2 == 0:
num = 0
for i in lst:
if i % (k * 2) // k == 1:
num += 1
k //= 2
if k == 0 and num % 2 == 0:
print("DRAW")
return 0
if (num % 4 == 1) or (n % 2 == 0):
print("WIN")
else:
print("LOSE")
for i in range(int(input())):
solve()
``` | output | 1 | 6,769 | 19 | 13,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
d = {1:'WIN', 0:'LOSE', -1:'DRAW'}
t=int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
f = [0] * 30
for x in a:
for b in range(30):
if (x >> b) & 1:
f[b] += 1
ans = -1
for b in reversed(range(30)):
if f[b] % 2 == 1:
ans = 0 if f[b] % 4 == 3 and (n - f[b]) % 2 == 0 else 1
break
print(d[ans])
``` | instruction | 0 | 6,770 | 19 | 13,540 |
Yes | output | 1 | 6,770 | 19 | 13,541 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
import sys,os,io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range (int(input())):
n = int(input())
a = [int(i) for i in input().split()]
cnt = [0]*35
for i in a:
bi = bin(i)[2:][::-1]
for j in range (len(bi)):
if bi[j]=='1':
cnt[j]+=1
ans = "DRAW"
for i in range (34,-1,-1):
if cnt[i]%4==1 or (cnt[i]%4==3 and not n%2):
ans = "WIN"
break
if cnt[i]%4==3:
ans = "LOSE"
break
print(ans)
``` | instruction | 0 | 6,771 | 19 | 13,542 |
Yes | output | 1 | 6,771 | 19 | 13,543 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
from bisect import *
from collections import *
from math import gcd,ceil,sqrt,floor,inf
from heapq import *
from itertools import *
from operator import add,mul,sub,xor,truediv,floordiv
from functools import *
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
#------------------------------------------------------------------------
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
farr=[1]
ifa=[]
def fact(x,mod=0):
if mod:
while x>=len(farr):
farr.append(farr[-1]*len(farr)%mod)
else:
while x>=len(farr):
farr.append(farr[-1]*len(farr))
return farr[x]
def ifact(x,mod):
global ifa
ifa.append(pow(farr[-1],mod-2,mod))
for i in range(x,0,-1):
ifa.append(ifa[-1]*i%mod)
ifa=ifa[::-1]
def per(i,j,mod=0):
if i<j: return 0
if not mod:
return fact(i)//fact(i-j)
return farr[i]*ifa[i-j]%mod
def com(i,j,mod=0):
if i<j: return 0
if not mod:
return per(i,j)//fact(j)
return per(i,j,mod)*ifa[j]%mod
def catalan(n):
return com(2*n,n)//(n+1)
def linc(f,t,l,r):
while l<r:
mid=(l+r)//2
if t>f(mid):
l=mid+1
else:
r=mid
return l
def rinc(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t<f(mid):
r=mid-1
else:
l=mid
return l
def ldec(f,t,l,r):
while l<r:
mid=(l+r)//2
if t<f(mid):
l=mid+1
else:
r=mid
return l
def rdec(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t>f(mid):
r=mid-1
else:
l=mid
return l
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def binfun(x):
c=0
for w in arr:
c+=ceil(w/x)
return c
def lowbit(n):
return n&-n
def inverse(a,m):
a%=m
if a<=1: return a
return ((1-inverse(m,a)*m)//a)%m
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
'''
class SMT:
def __init__(self,arr):
self.n=len(arr)-1
self.arr=[0]*(self.n<<2)
self.lazy=[0]*(self.n<<2)
def Build(l,r,rt):
if l==r:
self.arr[rt]=arr[l]
return
m=(l+r)>>1
Build(l,m,rt<<1)
Build(m+1,r,rt<<1|1)
self.pushup(rt)
Build(1,self.n,1)
def pushup(self,rt):
self.arr[rt]=self.arr[rt<<1]+self.arr[rt<<1|1]
def pushdown(self,rt,ln,rn):#lr,rn表区间数字数
if self.lazy[rt]:
self.lazy[rt<<1]+=self.lazy[rt]
self.lazy[rt<<1|1]=self.lazy[rt]
self.arr[rt<<1]+=self.lazy[rt]*ln
self.arr[rt<<1|1]+=self.lazy[rt]*rn
self.lazy[rt]=0
def update(self,L,R,c,l=1,r=None,rt=1):#L,R表示操作区间
if r==None: r=self.n
if L<=l and r<=R:
self.arr[rt]+=c*(r-l+1)
self.lazy[rt]+=c
return
m=(l+r)>>1
self.pushdown(rt,m-l+1,r-m)
if L<=m: self.update(L,R,c,l,m,rt<<1)
if R>m: self.update(L,R,c,m+1,r,rt<<1|1)
self.pushup(rt)
def query(self,L,R,l=1,r=None,rt=1):
if r==None: r=self.n
#print(L,R,l,r,rt)
if L<=l and R>=r:
return self.arr[rt]
m=(l+r)>>1
self.pushdown(rt,m-l+1,r-m)
ans=0
if L<=m: ans+=self.query(L,R,l,m,rt<<1)
if R>m: ans+=self.query(L,R,m+1,r,rt<<1|1)
return ans
'''
class DSU:#容量+路径压缩
def __init__(self,n):
self.c=[-1]*n
def same(self,x,y):
return self.find(x)==self.find(y)
def find(self,x):
if self.c[x]<0:
return x
self.c[x]=self.find(self.c[x])
return self.c[x]
def union(self,u,v):
u,v=self.find(u),self.find(v)
if u==v:
return False
if self.c[u]<self.c[v]:
u,v=v,u
self.c[u]+=self.c[v]
self.c[v]=u
return True
def size(self,x): return -self.c[self.find(x)]
class UFS:#秩+路径
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return prime
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
for v in graph[u]:
if v not in d or d[v]>d[u]+graph[u][v]:
d[v]=d[u]+graph[u][v]
heappush(heap,(d[v],v))
return d
def GP(it): return [(ch,len(list(g))) for ch,g in groupby(it)]
class DLN:
def __init__(self,val):
self.val=val
self.pre=None
self.next=None
t=N()
for i in range(t):
n=N()
a=RLL()
res=0
for x in a:
res^=x
if res==0:
ans='DRAW'
else:
k=0
while res:
res>>=1
k+=1
k-=1
c=0
for x in a:
if x&(1<<k):
c+=1
c//=2
#print(c)
if c&1:
if n&1:
ans='LOSE'
else:
ans="WIN"
else:
ans="WIN"
print(ans)
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thread(target=main)
t.start()
t.join()
'''
``` | instruction | 0 | 6,772 | 19 | 13,544 |
Yes | output | 1 | 6,772 | 19 | 13,545 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
d = { 1: 'WIN', 0: 'LOSE', -1: 'DRAW' }
t = int(input())
for _ in range(t):
n = int(input())
a = map(int, input().split())
f = [0] * 30
for x in a:
for b in range(30):
if x >> b & 1:
f[b] += 1
ans = -1
for x in reversed(range(30)):
if f[x] % 2 == 1:
ans = 0 if f[x] % 4 == 3 and (n - f[x]) % 2 == 0 else 1
break
print(d[ans])
``` | instruction | 0 | 6,773 | 19 | 13,546 |
Yes | output | 1 | 6,773 | 19 | 13,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
def run(n, a):
for i in range(30, -1, -1):
count = 0
for j in range(n):
count += (a[j] >> i) & 1
if count == 1:
return 'WIN'
elif count % 2 == 1 and n % 2 == 1:
return 'LOSE'
elif count % 2 == 0:
return 'WIN'
return 'DRAW'
def main():
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = run(n, a)
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 6,774 | 19 | 13,548 |
No | output | 1 | 6,774 | 19 | 13,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
for i in range(30,-1,-1):
cnt=0
for j in l:
if j&(1<<i):
cnt+=1
if cnt%4==1 or (n-cnt)&1:
print("WIN")
quit()
elif cnt%4==3:
print("LOSE")
quit()
print("DRAW")
``` | instruction | 0 | 6,775 | 19 | 13,550 |
No | output | 1 | 6,775 | 19 | 13,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
for _ in range(int(input())):
n=input()
a=[int(o) for o in input().split()]
ones=[0]*31
zeros= [0]*31
for i in a:
ba=bin(i)[2:][::-1]
j=-1
for k in ba:
if k=='1':
ones[j]+=1
else:
zeros[j]+=1
j-=1
res="DEAW"
for i in range(31):
if ones[i]%2!=0:
if ones[i]%4==3 and zeros[i]%2==0:
res="LOSE"
else:
res="WIN"
break
print(res)
``` | instruction | 0 | 6,776 | 19 | 13,552 |
No | output | 1 | 6,776 | 19 | 13,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
def run(n, a):
for i in range(30, -1, -1):
count = 0
for j in range(n):
count += (a[j] >> i) & 1
if count == 1:
return 'WIN'
elif count % 2 == 1 and n % 2 == 1:
return 'LOSE'
elif count % 2 == 1:
return 'WIN'
return 'DRAW'
def main():
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = run(n, a)
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 6,777 | 19 | 13,554 |
No | output | 1 | 6,777 | 19 | 13,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games. | instruction | 0 | 6,794 | 19 | 13,588 |
Tags: greedy, implementation, sortings
Correct Solution:
```
for t in range(int(input())):
n,k=map(int,input().split())
s=input()
if k>=n:
print(n*2-1)
continue
l=0
inter=[]
count=0
out=0
for i in s:
if i=='L':
l+=1
count+=1
else:
if count!=0:
inter.append(count)
out+=1
else:
out+=2
count=0
if s[0]=='W':
out-=1
elif inter:
inter.pop(0)
if l<=k:
print(n*2-1)
elif l==n and k!=0:
print(k*2-1)
else:
r=n-l
inter.sort()
for i in inter:
if k>=i:
out+=i*2+1
k-=i
else:
out+=k*2
k=0
break
out+=k*2
print(out)
``` | output | 1 | 6,794 | 19 | 13,589 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games. | instruction | 0 | 6,795 | 19 | 13,590 |
Tags: greedy, implementation, sortings
Correct Solution:
```
for _ in " "*int(input()):
n,k=map(int,input().split())
s=list(input())
if "W" not in s:
print(max((min(k,n)*2)-1,0))
elif k >= s.count("L"):
print((n*2)-1)
else:
cnt,sm,ind=list(),s.count("W"),s.index("W")
for i in range(ind+1,n):
if s[i] == "W":
cnt.append(i-ind-1)
ind=i
cnt.sort()
for i in cnt:
if k >= i:
sm+=(2*i)+1
k-=i
else:
break;
if k>0:
sm+=(2*k)
print(sm)
``` | output | 1 | 6,795 | 19 | 13,591 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games. | instruction | 0 | 6,796 | 19 | 13,592 |
Tags: greedy, implementation, sortings
Correct Solution:
```
for _ in range(int(input())):
n, k = map(int, input().split())
x = 1
X = []
ans = 0
y = 0
for s in input():
if s == 'W':
y = 1
if x:
X += [x]
ans += 1
x = 0
else:
ans += 2
else:
x += 1
if y == 0:
print(max(min(k, n) * 2 - 1, 0))
continue
if x:
X += [x + 10 ** 8]
X[0] += 99999999
X.sort()
X.reverse()
while k > 0 and X:
x = X.pop()
if x >= 10 ** 7:
x -= 10 ** 8
ans += 2 * min(x, k)
k -= min(x, k)
elif x > k:
ans += 2 * k
break
else:
ans += 2 * x + 1
k -= x
print(ans)
``` | output | 1 | 6,796 | 19 | 13,593 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games. | instruction | 0 | 6,797 | 19 | 13,594 |
Tags: greedy, implementation, sortings
Correct Solution:
```
nums = int(input().strip())
for _ in range(nums):
n,k = map(int,input().strip().split())
s = input().strip()
lw,rw = s.find("W"),s.rfind("W")
res = cur_num = 0
if lw==rw:
if lw==-1:
res = 2*k-1
else:
res = 2*k+1
res = min(2*len(s)-1,res)
else:
part = []
for i in range(lw,rw+1):
if s[i]=="W":
if i>lw and s[i-1]=="L":
part.append(cur_num)
cur_num = 0
if i>lw and s[i-1]=="W":
res+=2
else:
res+=1
else:
cur_num+=1
if k>=(sum(part)+lw+len(s)-rw-1):
res = 2*len(s)-1
else:
part.sort()
for i in range(len(part)):
if k>=part[i]:
res+=2*part[i]+1
k-=part[i]
else:
break
res+=2*k
print(max(res,0))
``` | output | 1 | 6,797 | 19 | 13,595 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games. | instruction | 0 | 6,798 | 19 | 13,596 |
Tags: greedy, implementation, sortings
Correct Solution:
```
for _ in range(int(input())):
n,k=map(int,input().split())
s=input()
s=list(s)
cw=0
w=[]
idx=-1
cl=0
fw=-1
lw=-1
ans = 0
for i in range(n):
if(s[i]=='W'):
if(i>0 and s[i-1]=='W'):
ans+=2
else:
ans+=1
if(fw==-1):
fw=i
lw=i
cw+=1
if(idx!=-1):
if(i-idx-1):
w.append(i-idx-1)
idx=i
else:
cl+=1
w.sort()
for i in w:
if(k==0):
break
if(i<=k):
k-=i
ans+=2*(i-1)+3
else:
ans+=2*(k)
k -= k
if(k>0):
if(k>=cl):
ans=1+(n-1)*2
else:
if(cw==0):
if(k>=n):
ans = 1 + (n - 1) * 2
k=0
else:
ans=1+(k-1)*2
k=0
else:
for i in range(lw+1,n):
if(k==0):
break
ans+=2
k-=1
if(k>0):
for i in range(fw-1,-1,-1):
if(k==0):
break
ans+=2
k-=1
print(ans)
``` | output | 1 | 6,798 | 19 | 13,597 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games. | instruction | 0 | 6,799 | 19 | 13,598 |
Tags: greedy, implementation, sortings
Correct Solution:
```
I=input
for _ in[0]*int(I()):
n,k=map(int,I().split());s=I();c=s.count('W');n=min(n,c+k);a=sorted(map(len,filter(None,s.strip('L').split('W'))))
while a and c+a[0]<=n:c+=a.pop(0)
print((2*n-len(a)or 1)-1)
``` | output | 1 | 6,799 | 19 | 13,599 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games. | instruction | 0 | 6,800 | 19 | 13,600 |
Tags: greedy, implementation, sortings
Correct Solution:
```
def score(a,n):
score = 0 if a[0]=='L' else 1
for i in range(1,n):
if a[i]==a[i-1] =='W':
score+=2
elif a[i]=='W':
score+=1
return score
t = int(input())
for _ in range(t):
n,k = map(int,input().split())
s = input()
mylist = []
x = 0
while(x<n and s[x]=='L'):
x+=1
count = 0
while(x<n):
if(s[x]=='W'):
if count!=0:
mylist.append(count)
count=0
else:
count+=1
x+=1
mylist.sort()
ans = 0
for i in mylist:
k-=i
if k==0:
ans+= 2*i + 1
break
elif k>0:
ans+=2*i+1
else:
ans+=2*i
break
counter = 0
while(counter<n and s[counter]=='L'):
counter+=1
scounter = 0
while( scounter<n and s[n-1-scounter]=='L' ):
scounter+=1
ans+=score(s,n)
if ans==0 and k>0:
ans-=1
if k<=(scounter+counter):
ans+=2*k
else:
ans+=2*(scounter+counter)
# print("ans",ans)
print(ans)
``` | output | 1 | 6,800 | 19 | 13,601 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games. | instruction | 0 | 6,801 | 19 | 13,602 |
Tags: greedy, implementation, sortings
Correct Solution:
```
t = int(input())
for it in range(0, t):
n, k = tuple(list(map(int, input().split(' '))))
results = [char for char in input()]
initial_score = 0
loss_amount = 0
for i in range(0, len(results)):
if i > 0 and results[i] == 'W' and results[i - 1] == 'W':
initial_score += 1
if results[i] == 'L':
loss_amount += 1
k = min(k, loss_amount)
initial_score += (n - loss_amount)
streak_increase_added_score = 2 * k
if loss_amount == n and streak_increase_added_score > 0:
streak_increase_added_score -= 1
streak_diffs = []
current_streak_diff = 0
streak_found = 0
for i in range(0, n):
if results[i] == 'W':
streak_found = True
if current_streak_diff != 0:
streak_diffs.append(current_streak_diff)
current_streak_diff = 0
if results[i] == 'L' and streak_found:
current_streak_diff += 1
sorted_streak_diffs = sorted(streak_diffs)
disjoint_streak_decrease_added_score = 0
for i in range(0, len(sorted_streak_diffs)):
if k >= sorted_streak_diffs[i]:
disjoint_streak_decrease_added_score += 1
k -= sorted_streak_diffs[i]
else:
break
final_score = (initial_score + streak_increase_added_score + disjoint_streak_decrease_added_score)
print(final_score)
``` | output | 1 | 6,801 | 19 | 13,603 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games.
Submitted Solution:
```
import sys
input=sys.stdin.readline
t=int(input())
for i in range(t):
n,k=map(int,input().split())
state=input()
state=[i for i in state]
ans,prev=0,0
store=[]
if state[0]=='W':
rang=[-1]
else:
rang=[]
for i in range(len(state)):
if state[i]=='W':
if len(rang)==1:
if rang[0]==i-1:
rang=[i]
else:
store.append((i-rang[0]-1,rang[0]+1,i))
rang=[i]
else:
rang=[i]
last=(rang[0] if rang else 0)
store.sort()
if k>0:
for i in store:
for j in range(i[1],i[2]):
state[j]='W'
k-=1
if k==0:
break
if k==0:
break
if k>0:
for i in range(last+1,len(state)):
if state[i]=='L' :
state[i]='W'
k-=1
if k==0:
break
if k>0:
for i in range(len(state)-1,-1,-1):
if state[i]=='L' :
state[i]='W'
k-=1
if k==0:
break
if state[0]=='W':
ans=1
else:
ans=0
for i in range(1,len(state)):
if state[i]=='W':
if state[i-1]=='W':
ans+=2
else:
ans+=1
sys.stdout.write(str(ans)+'\n')
``` | instruction | 0 | 6,802 | 19 | 13,604 |
Yes | output | 1 | 6,802 | 19 | 13,605 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games.
Submitted Solution:
```
def solve():
n, k = map(int, input().split())
A = input()
segs = []
s, t = 0, 0
while s < n and A[s] == 'L':
s += 1
head = (0, s)
nn = n
while nn >= 1 and A[nn - 1] == 'L':
nn -= 1
tail = (nn, n)
while s < nn:
if A[s] == 'W':
s += 1
continue
t = s
while t < nn and A[t] == 'L':
t += 1
segs.append((s, t))
s = t
segs.sort(key=lambda x: x[1] - x[0])
B = list(A)
for (s, t) in segs:
if k <= 0:
break
w = min(t - s, k)
B[s:s+w] = 'W' * w
k -= w
if k > 0 and tail[0] != n:
s, t = tail
w = min(t - s, k)
B[s:s+w] = 'W' * w
k -= w
if k > 0 and head[1] > 0:
s, t = head
w = min(t - s, k)
B[t - w: t] = 'W' * w
k -= w
score = 0
for i in range(n):
if i >= 1 and B[i - 1] == 'W' and B[i] == 'W':
score += 2
continue
if B[i] == 'W':
score += 1
continue
return score
TC = int(input())
for _ in range(TC):
print(solve())
``` | instruction | 0 | 6,803 | 19 | 13,606 |
Yes | output | 1 | 6,803 | 19 | 13,607 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games.
Submitted Solution:
```
for i in range(int(input())):
n, k = map(int, input().split())
s = input()
wins = s.count('W') + k
if wins >= n:
print(2 * n - 1)
else:
streaks = int(s[0] == 'W') + s.count('LW') or int(wins > 0)
gaps = s.strip('L').replace('W', ' ').strip().split()
for g in sorted(map(len, gaps)):
if g > k:
break
k -= g
streaks -= 1
print(wins * 2 - streaks)
``` | instruction | 0 | 6,804 | 19 | 13,608 |
Yes | output | 1 | 6,804 | 19 | 13,609 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games.
Submitted Solution:
```
from itertools import groupby
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
if k >= s.count('L'):
print(n * 2 - 1)
else:
s = s.strip("L")
group = []
for i, g in groupby(s):
if i == 'L':
group.append(len(list(g)))
group.sort()
i, m = 0, len(group)
r = k
while i < m and r >= group[i]:
r -= group[i]
i += 1
ans = (s.count('W') + k) * 2 - (m + 1 - i)
print(max(0, ans))
``` | instruction | 0 | 6,805 | 19 | 13,610 |
Yes | output | 1 | 6,805 | 19 | 13,611 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
from itertools import permutations
from decimal import *
getcontext().prec = 25
MOD = pow(10, 9) + 7
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# n, k = map(int, input().split(" "))
# l = list(map(int, input().split(" ")))
for _ in range(int(input())):
n, k = map(int, input().split(" "))
l = input()
if n == k:
print(2 * k - 1)
else:
t = 0
z = []
w = False
le = pre = 0
for i in range(n):
if l[i] == "L":
pre += 1
else:
break
start = max(1, pre)
if l[0]=="W":
t+=1
for i in range(start, n):
if l[i] == "W":
w=True
if le:
z.append(le)
le = 0
if l[i - 1] == "W":
t += 2
else:
t += 1
if l[i] == "L":
le += 1
extra = le
z.sort()
if k == 0:
print(t)
elif not extra and not z:
if not w:
print(2*k -1)
else:
print(2*k+1)
elif not z:
t += 2 * (min(pre, k))
k = max(0, k - pre)
t += 2 * (min(k, extra))
print(t)
elif z:
for i in range(len(z)):
if z[i] <= k:
k -= z[i]
t += 2 * z[i] + 1
else:
t += 2 * k
k = 0
break
if k:
t += 2 * (min(pre, k))
k = max(0, k - pre)
t += 2 * (min(k, extra))
print(t)
``` | instruction | 0 | 6,806 | 19 | 13,612 |
No | output | 1 | 6,806 | 19 | 13,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games.
Submitted Solution:
```
test_cases = int(input())
for i in range(0, test_cases):
stats = [int(x) for x in input().split()]
record = str(input())
g = stats[0]
c = stats[1]
score = 0
des = [] #irability
indices = {} # of desirable locations
indices[1] = []
indices[2] = []
indices[3] = []
for i in range(0, g):
if record[i] == "W":
des.append(0)
score += 1
if i != 0 and record[i - 1] == "W":
score += 1
else:
d = 1
if i != 0 and record[i - 1] == "W":
d += 1
if i != g - 1 and record[i + 1] == "W":
d += 1
des.append(d)
indices[d].append(i)
for choice in range(0, c):
# arbitrarily pick first top desirable index
d = 3
while d > 0:
if bool(indices[d]):
break
d -= 1
#des == 0: perfect game lol
if d == 0:
break
ind = indices[d][0]
# flip it!
score += d
des[ind] = 0
indices[d].remove(ind)
if ind != 0 and des[ind - 1] > 0:
indices[des[ind - 1] + 1].append(ind - 1)
indices[des[ind - 1]].remove(ind - 1)
des[ind - 1] += 1
if ind != g - 1 and des[ind + 1] > 0:
indices[des[ind + 1] + 1].append(ind + 1)
indices[des[ind + 1]].remove(ind + 1)
des[ind + 1] += 1
print(score)
``` | instruction | 0 | 6,807 | 19 | 13,614 |
No | output | 1 | 6,807 | 19 | 13,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
from functools import cmp_to_key
def main():
for _ in range(int(input())):
n,k=map(int,input().split(" "))
a=list(input())
start=-1
end=-1
temp=[]
for x in range(n):
if a[x]=='L':
end+=1
else:
if start!=end:
temp.append([end-start,[start+1,end]])
start=x
end=x
if start!=end:
temp.append([end-start,[start+1,end]])
#print(temp)
def boss(a,b):
if a[0]>b[0]:
return 1
elif a[0]<b[0]:
return -1
else:
if a[1][0]==0 or a[1][1]==n-1:
return 1
else:
return -1
temp.sort(key=cmp_to_key(boss))
for x in temp:
if k<=0:
break
for z in range(x[1][0],x[1][1]+1):
a[z]='W'
k-=1
if k<=0:
break
if k<=0:
break
ans=0
chk=-1
#print(a,temp)
for x in range(n):
if a[x]=='W':
if chk==-1:
ans+=1
chk=0
else:
ans+=2
else:
chk=-1
print(ans)
#-----------------------------BOSS-------------------------------------!
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 6,808 | 19 | 13,616 |
No | output | 1 | 6,808 | 19 | 13,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You like playing chess tournaments online.
In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, otherwise you get 1 point. If you win the very first game of the tournament you get 1 point (since there is not a "previous game").
The outcomes of the n games are represented by a string s of length n: the i-th character of s is W if you have won the i-th game, while it is L if you have lost the i-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most k of your games (meaning that at most k times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
Input
Each test contains multiple test cases. The first line contains an integer t (1≤ t ≤ 20,000) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers n, k (1≤ n≤ 100,000, 0≤ k≤ n) – the number of games played and the number of outcomes that you can change.
The second line contains a string s of length n containing only the characters W and L. If you have won the i-th game then s_i= W, if you have lost the i-th game then s_i= L.
It is guaranteed that the sum of n over all testcases does not exceed 200,000.
Output
For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.
Example
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
Note
Explanation of the first testcase. Before changing any outcome, the score is 2. Indeed, you won the first game, so you got 1 point, and you won also the third, so you got another 1 point (and not 2 because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is 7=1+2+2+2: 1 point for the first game and 2 points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is 3. Indeed, you won the fourth game, so you got 1 point, and you won also the fifth game, so you got 2 more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is 11 = 1+2+2+2+2+2: 1 point for the first game and 2 points for all the other games.
Submitted Solution:
```
#include <CodeforcesSolutions.h>
#include <ONLINE_JUDGE <solution.cf(contestID = "1427",questionID = "A",method = "GET")>.h>
"""
Author : thekushalghosh
Team : CodeDiggers
I prefer Python language over the C++ language :p :D
Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time,collections
start_time = time.time()
##########################################################################
################# ---- THE ACTUAL CODE STARTS BELOW ---- #################
def solve():
n,k = invr()
s = list(insr())
if "W" not in s:
if k == 0:
c = 0
else:
c = (2 * min(k,len(s))) - 1
else:
i = s.index("W")
while i < len(s):
if k > 0 and s[i] == "L":
s[i] = "W"
k = k - 1
i = i + 1
i = s.index("W")
while i >= 0:
if k > 0 and s[i] == "L":
s[i] = "W"
k = k - 1
i = i - 1
c = 0
for i in range(len(s)):
if s[i] == "W":
if i != 0 and s[i - 1] == "W":
c = c + 2
else:
c = c + 1
print(c)
################## ---- THE ACTUAL CODE ENDS ABOVE ---- ##################
##########################################################################
def main():
global tt
if not ONLINE_JUDGE:
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
t = 1
t = inp()
for tt in range(1,t + 1):
solve()
if not ONLINE_JUDGE:
print("Time Elapsed :",time.time() - start_time,"seconds")
sys.stdout.close()
#---------------------- USER DEFINED INPUT FUNCTIONS ----------------------#
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
return(input().strip())
def invr():
return(map(int,input().split()))
#------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------#
def counter(a):
q = [0] * max(a)
for i in range(len(a)):
q[a[i] - 1] = q[a[i] - 1] + 1
return(q)
def counter_elements(a):
q = dict()
for i in range(len(a)):
if a[i] not in q:
q[a[i]] = 0
q[a[i]] = q[a[i]] + 1
return(q)
def string_counter(a):
q = [0] * 26
for i in range(len(a)):
q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
return(q)
def factorial(n,m = 1000000007):
q = 1
for i in range(n):
q = (q * (i + 1)) % m
return(q)
def factors(n):
q = []
for i in range(1,int(n ** 0.5) + 1):
if n % i == 0: q.append(i); q.append(n // i)
return(list(sorted(list(set(q)))))
def prime_factors(n):
q = []
while n % 2 == 0: q.append(2); n = n // 2
for i in range(3,int(n ** 0.5) + 1,2):
while n % i == 0: q.append(i); n = n // i
if n > 2: q.append(n)
return(list(sorted(q)))
def transpose(a):
n,m = len(a),len(a[0])
b = [[0] * n for i in range(m)]
for i in range(m):
for j in range(n):
b[i][j] = a[j][i]
return(b)
def power_two(x):
return (x and (not(x & (x - 1))))
def ceil(a, b):
return -(-a // b)
def seive(n):
a = [1]
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p ** 2,n + 1, p):
prime[i] = False
p = p + 1
for p in range(2,n + 1):
if prime[p]:
a.append(p)
return(a)
#-----------------------------------------------------------------------#
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
#import io,os
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
input = sys.stdin.readline
main()
``` | instruction | 0 | 6,809 | 19 | 13,618 |
No | output | 1 | 6,809 | 19 | 13,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | instruction | 0 | 6,977 | 19 | 13,954 |
Tags: games, greedy, sortings
Correct Solution:
```
from sys import stdin
from collections import deque
import heapq
n = int(stdin.readline())
piles = []
for x in range(n):
a = [int(x) for x in stdin.readline().split()][1:]
piles.append(a)
cielTotal = 0
jiroTotal = 0
mids = []
for x in piles:
cielTotal += sum(x[:len(x)//2])
jiroTotal += sum(x[len(x)//2+len(x)%2:])
#print(x)
#print(cielTotal,jiroTotal)
if len(x)%2 == 1:
mids.append(x[len(x)//2])
mids.sort(reverse=True)
turn = True
for x in mids:
if turn:
cielTotal += x
else:
jiroTotal += x
turn = not turn
print(cielTotal,jiroTotal)
``` | output | 1 | 6,977 | 19 | 13,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | instruction | 0 | 6,978 | 19 | 13,956 |
Tags: games, greedy, sortings
Correct Solution:
```
n = int(input())
a,b = 0,0
l = []
for _ in range(n):
inpt = list(map(int,input().split()))[1:]
li = len(inpt)
if li%2:
l.append(inpt[li//2])
a += sum((inpt[:li//2]))
b += sum((inpt[(li + 1)//2:]))
l.sort(reverse=True)
a += sum(l[::2])
b += sum(l[1::2])
print(a, b)
``` | output | 1 | 6,978 | 19 | 13,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | instruction | 0 | 6,979 | 19 | 13,958 |
Tags: games, greedy, sortings
Correct Solution:
```
#!/usr/bin/env python3
odd, even = [], []
player1_turn = True
player1 = player2 = 0
pile_number = int(input())
for _ in range(pile_number):
n, *pile = tuple(map(int, input().split()))
if n % 2 == 0:
even.append(pile)
else:
odd.append(pile)
for pile in even:
n = len(pile)
player1 += sum(pile[:n//2])
player2 += sum(pile[n//2:])
for pile in sorted(odd, reverse=True, key=lambda x: x[len(x)//2]):
n = len(pile)
top, middle, bottom = pile[:n//2], pile[n//2], pile[n//2+1:]
player1 += sum(top)
player2 += sum(bottom)
if player1_turn:
player1 += middle
player1_turn = not player1_turn
else:
player2 += middle
player1_turn = not player1_turn
print(player1, player2)
``` | output | 1 | 6,979 | 19 | 13,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | instruction | 0 | 6,980 | 19 | 13,960 |
Tags: games, greedy, sortings
Correct Solution:
```
from functools import reduce
n = int(input())
cards = [list(map(int, input().split()[1:])) for i in range(n)]
mid = sorted((c[len(c) >> 1] for c in cards if len(c) & 1 == 1), reverse=True)
add = lambda x=0, y=0: x + y
a, b = reduce(add, mid[::2] or [0]), reduce(add, mid[1::2] or [0])
for c in cards:
m = len(c) >> 1
a += reduce(add, c[:m] or [0])
b += reduce(add, c[m + (len(c) & 1):] or [0])
print(a, b)
``` | output | 1 | 6,980 | 19 | 13,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | instruction | 0 | 6,981 | 19 | 13,962 |
Tags: games, greedy, sortings
Correct Solution:
```
n = int(input())
S = [0] * n
ciel, giro = 0, 0
odd = []
for i in range(n):
L = list(map(int, input().split()))
k = L[0]
L = L[1:]
S[i] = sum(L)
if k % 2:
odd.append(L[k // 2])
ciel += sum(L[:k // 2])
giro += sum(L[(k + 1) // 2:])
odd.sort(reverse=True)
for i, x in enumerate(odd):
if i % 2:
giro += x
else:
ciel += x
print(ciel, giro)
``` | output | 1 | 6,981 | 19 | 13,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | instruction | 0 | 6,982 | 19 | 13,964 |
Tags: games, greedy, sortings
Correct Solution:
```
n = int(input())
c = [list(map(int, input().split())) for _ in range(n)]
a, b = 0, 0
d = []
for i in range(n):
if len(c[i]) % 2:
a += sum(c[i][1:c[i][0]//2+1])
b += sum(c[i][c[i][0]//2+1:])
else:
a += sum(c[i][1:c[i][0]//2+1])
b += sum(c[i][c[i][0]//2+2:])
d.append(c[i][c[i][0]//2+1])
d.sort(reverse=True)
print(a+sum(d[0::2]), b+sum(d[1::2]))
``` | output | 1 | 6,982 | 19 | 13,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | instruction | 0 | 6,983 | 19 | 13,966 |
Tags: games, greedy, sortings
Correct Solution:
```
n = int(input())
a = b = 0
s = []
for _ in range(n):
l = [*map(int, input().split())][1:]
m = len(l)
if m & 1:
s.append(l[m//2])
a += sum((l[:m//2]))
b += sum((l[(m + 1)//2:]))
s.sort(reverse = True)
a += sum(s[::2])
b += sum(s[1::2])
print(a, b)
``` | output | 1 | 6,983 | 19 | 13,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | instruction | 0 | 6,984 | 19 | 13,968 |
Tags: games, greedy, sortings
Correct Solution:
```
p, n = [], int(input())
a = b = 0
for i in range(n):
t = list(map(int, input().split()))
k = t[0] // 2 + 1
a += sum(t[1: k])
if t[0] & 1:
p.append(t[k])
b += sum(t[k + 1: ])
else: b += sum(t[k: ])
p.sort(reverse = True)
print(a + sum(p[0 :: 2]), b + sum(p[1 :: 2]))
``` | output | 1 | 6,984 | 19 | 13,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
Submitted Solution:
```
n = int(input())
lista = []
aux = []
somaA = 0
somaB = 0
for i in range(n):
a = [int(i) for i in input().split()][1:]
if len(a) > 1:
somaA += sum(a[0:len(a)//2])
somaB += sum(a[-(len(a)//2):])
if len(a) % 2 == 1:
aux.append(a[len(a)//2])
aux.sort(reverse=True)
for i in range(0, len(aux), 2):
somaA += aux[i]
for i in range(1, len(aux), 2):
somaB += aux[i]
print(somaA, somaB)
``` | instruction | 0 | 6,985 | 19 | 13,970 |
Yes | output | 1 | 6,985 | 19 | 13,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
Submitted Solution:
```
N = int(input())
one = two = 0
middles = []
for i in range(N):
array = list(map(int, input().split()))[1:]
size = len(array)-1
middle = size//2
for i in range(middle):
one += array[i]
for i in range(middle+1, len(array)):
two += array[i]
if len(array)%2==1:
middles.append(array[middle])
else:
one += array[middle]
middles = sorted(middles)
ONE = True
for i in range(len(middles)-1, -1, -1):
if ONE:
one += middles[i]
ONE = False
else:
two += middles[i]
ONE = True
print(one, two)
``` | instruction | 0 | 6,986 | 19 | 13,972 |
Yes | output | 1 | 6,986 | 19 | 13,973 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
Submitted Solution:
```
from functools import reduce
n = int(input())
cards = [list(map(int, input().split()[1:])) for i in range(n)]
mid = [c[len(c) >> 1] for c in cards if len(c) & 1 == 1]
a, b = 0, 0
add = lambda x=0, y=0: x + y
for c in cards:
m = len(c) >> 1
a += reduce(add, c[:m] or [0])
b += reduce(add, c[m + (len(c) & 1):] or [0])
mid.sort(reverse=True)
a += reduce(add, mid[::2] or [0])
b += reduce(add, mid[1::2] or [0])
print(a, b)
``` | instruction | 0 | 6,987 | 19 | 13,974 |
Yes | output | 1 | 6,987 | 19 | 13,975 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
Submitted Solution:
```
n=int(input())
s1,s2=0,0
tab = []
for i in range(n):
c = list(map(int,input().split()))
for j in range(1,c[0]+1):
if(j*2<=c[0]): s1+=c[j]
else: s2+=c[j]
if(c[0] & 1):
s2-=c[(c[0]+1)//2]
tab.append(c[(c[0]+1)//2])
if(len(tab)):
tab.sort()
tab.reverse()
for i in range(len(tab)):
if(i & 1): s2+=tab[i]
else: s1+=tab[i]
print(s1,s2)
# Made By Mostafa_Khaled
``` | instruction | 0 | 6,988 | 19 | 13,976 |
Yes | output | 1 | 6,988 | 19 | 13,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
Submitted Solution:
```
n = int(input())
S = [0] * n
ciel, giro = [], []
a, b = 0, 0
for i in range(n):
L = list(map(int, input().split()))
k = L[0]
L = L[1:]
S[i] = sum(L)
if k % 2:
ciel.append((sum(L[:k // 2 + 1]), i))
giro.append((sum(L[k // 2:], i), i))
else:
a += sum(L[:k // 2])
b += sum(L[k // 2:])
ciel.sort(reverse=True)
giro.sort(reverse=True)
vis = [False] * n
k = len(ciel)
i, j = 0, 0
finished = False
while not finished:
finished = True
while i < k and vis[ciel[i][1]]:
i += 1
if i < k:
finished = False
vis[ciel[i][1]] = True
a += ciel[i][0]
b += S[ciel[i][1]] - ciel[i][0]
while j < k and vis[giro[j][1]]:
j += 1
if j < k:
finished = False
vis[giro[j][1]] = True
b += giro[j][0]
a += S[giro[j][1]] - giro[j][0]
print(a, b)
``` | instruction | 0 | 6,989 | 19 | 13,978 |
No | output | 1 | 6,989 | 19 | 13,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
Submitted Solution:
```
from sys import stdin
from collections import deque
import heapq
n = int(stdin.readline())
piles = []
for x in range(n):
a = [int(x) for x in stdin.readline().split()][1:]
piles.append(a)
cielTotal = 0
jiroTotal = 0
mids = []
for x in piles:
cielTotal += sum(x[:len(x)//2])
jiroTotal += sum(x[len(x)//2+len(x)%2:])
if len(x)%2 == 1:
mids.append(x[len(x)//2])
mids.sort()
turn = True
for x in mids:
if turn:
cielTotal += x
else:
jiroTotal += x
print(cielTotal,jiroTotal)
``` | instruction | 0 | 6,990 | 19 | 13,980 |
No | output | 1 | 6,990 | 19 | 13,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
Submitted Solution:
```
from sys import stdin
from collections import deque
import heapq
n = int(stdin.readline())
piles = []
for x in range(n):
a = deque([int(x) for x in stdin.readline().split()][1:])
piles.append(a)
ciel = [(-x[0],i) for i,x in enumerate(piles)]
jiro = [(-x[-1],i) for i,x in enumerate(piles)]
heapq.heapify(ciel)
heapq.heapify(jiro)
empty = set([-1])
cielTotal = 0
jiroTotal = 0
turn = True
while True:
if turn:
ind = -1
while ind in empty and ciel:
nxt,ind = heapq.heappop(ciel)
nxt = -nxt
if ind in empty:
break
piles[ind].popleft()
cielTotal += nxt
if not piles[ind]:
empty.add(ind)
else:
heapq.heappush(ciel, (-piles[ind][0], ind))
else:
ind = -1
while ind in empty and jiro:
nxt,ind = heapq.heappop(jiro)
nxt = -nxt
if ind in empty:
break
piles[ind].pop()
jiroTotal += nxt
if not piles[ind]:
empty.add(ind)
else:
heapq.heappush(jiro, (-piles[ind][-1], ind))
turn = not turn
print(cielTotal,jiroTotal)
``` | instruction | 0 | 6,991 | 19 | 13,982 |
No | output | 1 | 6,991 | 19 | 13,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
Input
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Output
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
Examples
Input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
Submitted Solution:
```
#!/usr/bin/env python3
player1 = player2 = 0
player1_turn = True
pile_number = int(input())
piles = []
for _ in range(pile_number):
n, *pile = tuple(map(int, input().split()))
piles.append(pile)
for pile in sorted(piles, reverse=True):
n = len(pile)
if n % 2 == 0:
player1 += sum(pile[:n//2])
player2 += sum(pile[n//2:])
else:
top, middle, bottom = pile[:n//2], pile[n//2], pile[n//2+1:]
player1 += sum(top)
player2 += sum(bottom)
if player1_turn:
player1 += middle
player1_turn = not player1_turn
else:
player2 += middle
player1_turn = not player1_turn
print(player1, player2)
``` | instruction | 0 | 6,992 | 19 | 13,984 |
No | output | 1 | 6,992 | 19 | 13,985 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image>
Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.
The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by k2, and the loser's score is multiplied by k. In the beginning of the game, both Slastyona and Pushok have scores equal to one.
Unfortunately, Slastyona had lost her notepad where the history of all n games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not.
Input
In the first string, the number of games n (1 ≤ n ≤ 350000) is given.
Each game is represented by a pair of scores a, b (1 ≤ a, b ≤ 109) – the results of Slastyona and Pushok, correspondingly.
Output
For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise.
You can output each letter in arbitrary case (upper or lower).
Example
Input
6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000
Output
Yes
Yes
Yes
No
No
Yes
Note
First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.
The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3. | instruction | 0 | 7,206 | 19 | 14,412 |
Tags: math, number theory
Correct Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
avl=AvlTree()
#-----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default='z', func=lambda a, b: min(a ,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: a + b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left)/ 2)
# Check if middle element is
# less than or equal to key
if (arr[mid]<=key):
count = mid+1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def countGreater( arr,n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
for i in range(int(input())):
a,b=map(int,input().split())
c=a*b
l=int(c**(1./3)+0.5)
if l**3==a*b and a%l==0 and b%l==0:
print("YES")
else:
print("NO")
``` | output | 1 | 7,206 | 19 | 14,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image>
Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.
The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by k2, and the loser's score is multiplied by k. In the beginning of the game, both Slastyona and Pushok have scores equal to one.
Unfortunately, Slastyona had lost her notepad where the history of all n games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not.
Input
In the first string, the number of games n (1 ≤ n ≤ 350000) is given.
Each game is represented by a pair of scores a, b (1 ≤ a, b ≤ 109) – the results of Slastyona and Pushok, correspondingly.
Output
For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise.
You can output each letter in arbitrary case (upper or lower).
Example
Input
6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000
Output
Yes
Yes
Yes
No
No
Yes
Note
First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.
The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3. | instruction | 0 | 7,207 | 19 | 14,414 |
Tags: math, number theory
Correct Solution:
```
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left , bisect_right
import math
def ii():
return int(input())
def li():
return list(map(int,input().split()))
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
no = "No"
yes = "Yes"
def solve():
a,b = li()
x = (pow(a*b,1/3))
x=round(x)
if x*x*x==a*b and a%x==b%x==0:
print(yes)
else:
print(no)
t = 1
t = int(input())
for _ in range(t):
solve()
``` | output | 1 | 7,207 | 19 | 14,415 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image>
Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.
The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by k2, and the loser's score is multiplied by k. In the beginning of the game, both Slastyona and Pushok have scores equal to one.
Unfortunately, Slastyona had lost her notepad where the history of all n games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not.
Input
In the first string, the number of games n (1 ≤ n ≤ 350000) is given.
Each game is represented by a pair of scores a, b (1 ≤ a, b ≤ 109) – the results of Slastyona and Pushok, correspondingly.
Output
For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise.
You can output each letter in arbitrary case (upper or lower).
Example
Input
6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000
Output
Yes
Yes
Yes
No
No
Yes
Note
First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.
The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3. | instruction | 0 | 7,208 | 19 | 14,416 |
Tags: math, number theory
Correct Solution:
```
import sys
input = sys.stdin.readline
print = sys.stdout.write
cbrt = {i**3:i for i in range(1001)}
n = int(input())
all_res = []
for _ in range(n):
a, b = map(int, input().split())
if a == b:
all_res.append('Yes' if a in cbrt else 'No')
continue
if a > b:
a, b = b, a
r = cbrt.get(a * a // b, 0)
if r == 0 or a % (r * r) > 0:
all_res.append('No')
continue
y = a //(r * r)
if r * r * y == a and r * y * y == b:
all_res.append('Yes')
else:
all_res.append('No')
print('\n'.join(all_res))
``` | output | 1 | 7,208 | 19 | 14,417 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image>
Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.
The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by k2, and the loser's score is multiplied by k. In the beginning of the game, both Slastyona and Pushok have scores equal to one.
Unfortunately, Slastyona had lost her notepad where the history of all n games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not.
Input
In the first string, the number of games n (1 ≤ n ≤ 350000) is given.
Each game is represented by a pair of scores a, b (1 ≤ a, b ≤ 109) – the results of Slastyona and Pushok, correspondingly.
Output
For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise.
You can output each letter in arbitrary case (upper or lower).
Example
Input
6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000
Output
Yes
Yes
Yes
No
No
Yes
Note
First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.
The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3. | instruction | 0 | 7,209 | 19 | 14,418 |
Tags: math, number theory
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
for t in range (int(input())):
a,b=map(int,input().split())
p=a*b
#print(p)
c=int(round(p**(1./3)))
#print (c)
if c**3==p and a%c==0 and b%c==0:
print("Yes")
else:
print("No")
``` | output | 1 | 7,209 | 19 | 14,419 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.