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.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW | instruction | 0 | 19,938 | 19 | 39,876 |
Tags: dp, graphs
Correct Solution:
```
def ma():
s=input()
v=s.split(' ')
n=int(v[0])
k=int(v[1])
s=input()
dp=[[0,0] for _ in range(n+1)]
flag=True
mp={}
mp['W']=1
mp['D']=0
mp['L']=-1
for i in range (1,n):
c=s[i-1]
if c=='?':
dp[i][0]=min(dp[i-1][0]+1 ,k-1)
dp[i][1]=max(dp[i-1][1]-1 ,-k+1)
else:
dp[i][0]=min(dp[i-1][0]+mp[c],k-1)
dp[i][1]=max(dp[i-1][1]+mp[c],-k+1)
if dp[i][1]==k or dp[i][0]==-k:
flag=False
'''
elif c=='D':
dp[i][0]=dp[i-1][0]
dp[i][1]=dp[i-1][1]
elif c=='W':
dp[i][0]=min(dp[i-1][0]+1 ,k-1)
dp[i][1]=dp[i-1][1]+1
if dp[i][1]==k:
flag=False
elif c=='L':
dp[i][0]=dp[i-1][0]-1
dp[i][1]=max(dp[i-1][1]-1 ,-k+1)
if dp[i][0]==-k:
flag=False
'''
if not flag:
print('NO')
return
i=n
if s[i-1]=='?':
dp[i][0]=dp[i-1][0]+1
dp[i][1]=dp[i-1][1]-1
else :
dp[i][0]=dp[i-1][0]+mp[s[i-1]]
dp[i][1]=dp[i-1][1]+mp[s[i-1]]
'''
elif s[i-1]=='W':
dp[i][0]=dp[i-1][0]+1
dp[i][1]=dp[i-1][1]+1
elif s[i-1]=='L':
dp[i][0]=dp[i-1][0]-1
dp[i][1]=dp[i-1][1]-1
'''
res=['?']*n
if dp[i][0]==k or dp[i][1]==-k:
if dp[i][0]==k:
cur=k
else:
cur=-k
for i in range(n-1,-1,-1):
c=s[i]
if c=='?':
if cur>dp[i][0]:
res[i]='W'
elif dp[i][1]<=cur<=dp[i][0]:
res[i]='D'
elif cur<dp[i][1]:
res[i]='L'
else:
res[i]=c
cur=cur-mp[res[i]]
'''
elif c=='D':
cur=cur
res[i]=c
elif c=='W':
cur=cur-1
res[i]=c
elif c=='L':
cur=cur+1
res[i]=c
'''
for i in range(n):
print(res[i],end='')
else:
print('NO')
ma()
``` | output | 1 | 19,938 | 19 | 39,877 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW | instruction | 0 | 19,939 | 19 | 39,878 |
Tags: dp, graphs
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO,IOBase
def main():
n,k = map(int,input().split())
s = input().strip()
dp = [[0]*(2*k+5) for _ in range(n+1)]
# diff ; win/draw/lose
dp[0][0] = 1
prev = [[-1]*(2*k+5) for _ in range(n+1)]
for i in range(1,n):
for j in range(-k+1,k):
if (s[i-1] == '?' or s[i-1] == 'L') and dp[i-1][j+1]:
dp[i][j] = 1
prev[i][j] = 'L'
if (s[i-1] == '?' or s[i-1] == 'D') and dp[i-1][j]:
dp[i][j] = 1
prev[i][j] = 'D'
if (s[i-1] == '?' or s[i-1] == 'W') and dp[i-1][j-1]:
dp[i][j] = 1
prev[i][j] = 'W'
for j in range(-k,k+1):
if (s[n-1] == '?' or s[n-1] == 'L') and dp[n-1][j+1]:
dp[n][j] = 1
prev[n][j] = 'L'
if (s[n-1] == '?' or s[n-1] == 'D') and dp[n-1][j]:
dp[n][j] = 1
prev[n][j] = 'D'
if (s[n-1] == '?' or s[n-1] == 'W') and dp[n-1][j-1]:
dp[n][j] = 1
prev[n][j] = 'W'
if not dp[n][k] and not dp[n][-k]:
print('NO')
exit()
elif dp[n][k]:
st = k
else:
st = -k
ans = []
dct = {'L':1,'W':-1,'D':0}
for i in range(n,0,-1):
l = prev[i][st]
ans.append(l)
st += dct[l]
print(''.join(ans[::-1]))
# Fast IO Region
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")
if __name__ == "__main__":
main()
``` | output | 1 | 19,939 | 19 | 39,879 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW | instruction | 0 | 19,940 | 19 | 39,880 |
Tags: dp, graphs
Correct Solution:
```
# 803E
import collections
def do():
n, k = map(int, input().split(" "))
s = input()
dp = collections.defaultdict(set)
gap = {'W': 1, 'L': -1, 'D': 0}
dp[-1].add(0)
for i in range(n-1):
if s[i] == '?':
for pre in dp[i-1]:
for g in gap.values():
t = pre + g
if abs(t) != k:
dp[i].add(t)
else:
for pre in dp[i-1]:
t = pre + gap[s[i]]
if abs(t) != k:
dp[i].add(t)
if s[n-1] == '?':
for pre in dp[n-2]:
for g in gap.values():
dp[n-1].add(pre + g)
else:
for pre in dp[n-2]:
dp[n-1].add(pre + gap[s[n-1]])
# print(dp)
if k not in dp[n-1] and -k not in dp[n-1]:
return "NO"
res = [c for c in s]
cur = k if k in dp[n-1] else -k
for i in range(n-1, 0, -1):
if s[i] == '?':
for c in gap:
if abs(cur - gap[c]) != k and cur - gap[c] in dp[i-1]:
res[i] = c
cur -= gap[c]
break
else:
cur -= gap[s[i]]
if res[0] == '?':
if cur == 0:
res[0] = 'D'
elif cur == 1:
res[0] = 'W'
else:
res[0] = 'L'
return "".join(res)
print(do())
``` | output | 1 | 19,940 | 19 | 39,881 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW | instruction | 0 | 19,941 | 19 | 39,882 |
Tags: dp, graphs
Correct Solution:
```
N,k=list(map(int,input().strip().split(' ')))
S=input()
# num of W-num of L=j, j>k means k-j
dp=[[0 for j in range(2*k+1)]for i in range(N)]
#print(dp)
for i in range(len(S)):
if i==0:
if S[0]=='W':
dp[0][1]='W'
elif S[0]=='L':
dp[0][-1]='L'
elif S[0]=='D':
dp[0][0]='D'
else:
dp[0][1]='W'
dp[0][-1]='L'
dp[0][0]='D'
elif i!=len(S)-1:
if S[i]=='W':
for j in range(0,k):
if j==0:
if dp[i-1][-1]!=0:
dp[i][0]='W'
else:
if dp[i-1][j-1]!=0:
dp[i][j]='W'
for j in range(1,k):
if j!=k-1:
if dp[i-1][-j-1]!=0:
dp[i][-j]='W'
elif S[i]=='L':
for j in range(0,k):
if dp[i-1][j+1]!=0:
dp[i][j]='L'
for j in range(1,k):
if j==1:
if dp[i-1][0]!=0:
dp[i][-1]='L'
else:
if dp[i-1][-j+1]!=0:
dp[i][-j]='L'
elif S[i]=='D':
for j in range(0,2*k+1):
if dp[i-1][j]!=0:
dp[i][j]='D'
else:
for j in range(0,k):
if j==0:
if dp[i-1][-1]!=0:
dp[i][j]='W'
elif dp[i-1][1]!=0:
dp[i][j]='L'
elif dp[i-1][0]!=0:
dp[i][j]='D'
else:
if dp[i-1][j-1]!=0:
dp[i][j]='W'
elif dp[i-1][j+1]!=0:
dp[i][j]='L'
elif dp[i-1][j]!=0:
dp[i][j]='D'
for j in range(1,k):
if j==1:
if dp[i-1][0]!=0:
dp[i][-1]='L'
elif dp[i-1][-1]!=0:
dp[i][-1]='D'
elif dp[i-1][-2]!=0:
dp[i][-1]='W'
else:
if dp[i-1][-(j-1)]!=0:
dp[i][-j]='L'
elif dp[i-1][-j]!=0:
dp[i][-j]='D'
elif dp[i-1][-(j+1)]!=0:
dp[i][-j]='W'
else:
if S[i]=='W':
if dp[i-1][k-1]!=0:
dp[i][k]='W'
elif S[i]=='L':
if dp[i-1][-(k-1)]!=0:
dp[i][-k]='L'
elif S[i]=='D':
1
else:
if dp[i-1][k-1]!=0:
dp[i][k]='W'
elif dp[i-1][-(k-1)]!=0:
dp[i][-k]='L'
#print(dp)
if k>1 and N>=k:
if dp[len(S)-1][k]==0 and dp[len(S)-1][-k]==0:
print('NO')
else:
if dp[len(S)-1][k]!=0:
ans=''
cur=k
for j in range(1,len(S)+1):
temp=dp[len(S)-j][cur]
if temp=='W':
ans+=temp
cur-=1
elif temp=='D':
ans+=temp
elif temp=='L':
ans+=temp
cur+=1
elif dp[len(S)-1][-k]!=0:
ans=''
cur=-k
for j in range(1,len(S)+1):
temp=dp[len(S)-j][cur]
if temp=='W':
ans+=temp
cur-=1
elif temp=='D':
ans+=temp
elif temp=='L':
ans+=temp
cur+=1
ans=ans[::-1]
print(ans)
elif N<k:
print('NO')
elif k==1:
shit=0
for i in range(len(S)):
if i<len(S)-1:
if S[i]!='?':
shit=1
break
if shit==1:
print('NO')
else:
temp=''
for i in range(len(S)-1):
temp+='D'
if S[-1]=='D':
print('NO')
elif S[-1]=='L':
temp+='L'
print(temp)
else:
temp+='W'
print(temp)
``` | output | 1 | 19,941 | 19 | 39,883 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW | instruction | 0 | 19,942 | 19 | 39,884 |
Tags: dp, graphs
Correct Solution:
```
n, k = [int(i) for i in input().split()]
s = input()
dp = [[False] * 2010 for i in range(1001)]
dp[0][0] = True
for i in range(n):
l = -k + 1
r = k
if i == n - 1:
l -= 1
r += 1
for b in range(l, r):
if s[i] == 'L':
dp[i + 1][b] = dp[i][b + 1]
elif s[i] == 'W':
dp[i + 1][b] = dp[i][b - 1]
elif s[i] == 'D':
dp[i + 1][b] = dp[i][b]
else:
dp[i + 1][b] = dp[i][b + 1] or dp[i][b - 1] or dp[i][b]
ans = [0] * n
i = n
b = -1
if dp[i][k]:
b = k
elif dp[i][-k]:
b = -k
if b == -1:
print("NO")
else:
while i > 0:
if (s[i - 1] == 'L' or s[i - 1] == '?') and dp[i][b] == dp[i - 1][b + 1]:
ans[i - 1] = 'L'
b += 1
elif (s[i - 1] == 'W' or s[i - 1] == '?') and dp[i][b] == dp[i - 1][b - 1]:
ans[i - 1] = 'W'
b -= 1
else:
ans[i - 1] = 'D'
i -= 1
for j in ans:
print(j, end='')
``` | output | 1 | 19,942 | 19 | 39,885 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW | instruction | 0 | 19,943 | 19 | 39,886 |
Tags: dp, graphs
Correct Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
def main():
n,k=map(int,input().split())
s=list(input().rstrip())
dp,ans=[['0']*(2*k+2) for _ in range(n+1)],[]
dp[0][0]='.'
for i in range(1,n+1):
for j in range(k+1):
if j==k and i!=n:
continue
if s[i-1]=='W':
dp[i][j]=('W' if dp[i-1][j-1]!='0' else '0')
elif s[i-1]=='D':
dp[i][j]=('D' if dp[i-1][j]!='0' else '0')
elif s[i-1]=='L':
dp[i][j]=('L' if dp[i-1][j+1]!='0' else '0')
else:
if dp[i-1][j-1]!='0':
dp[i][j]='W'
elif dp[i-1][j]!='0':
dp[i][j]='D'
elif dp[i-1][j+1]!='0':
dp[i][j]='L'
for j in range(-1,-(k+1),-1):
if j==-k and i!=n:
continue
if s[i - 1] == 'W':
dp[i][j] = ('W' if dp[i - 1][j - 1] != '0' else '0')
elif s[i - 1] == 'D':
dp[i][j] = ('D' if dp[i - 1][j] != '0' else '0')
elif s[i - 1] == 'L':
dp[i][j] = ('L' if dp[i - 1][j + 1] != '0' else '0')
else:
if dp[i - 1][j - 1] != '0':
dp[i][j] = 'W'
elif dp[i - 1][j] != '0':
dp[i][j] = 'D'
elif dp[i - 1][j + 1] != '0':
dp[i][j] = 'L'
if dp[n][k]!='0' or dp[n][-k]!='0':
i,j=n,k
if dp[n][k]=='0':
j=-k
while len(ans)!=n:
ans.append(dp[i][j])
if dp[i][j]=='W':
i,j=i-1,j-1
elif dp[i][j]=='D':
i-=1
elif dp[i][j]=='L':
i,j=i-1,j+1
ans.reverse()
print("".join(ans))
else:
print('NO')
# 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")
if __name__ == "__main__":
main()
``` | output | 1 | 19,943 | 19 | 39,887 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW | instruction | 0 | 19,944 | 19 | 39,888 |
Tags: dp, graphs
Correct Solution:
```
n, k = [int(i) for i in input().split()]
s = input()
dp = [[False] * 2100 for i in range(1001)]
dp[0][0] = True
for i in range(n):
l = -k + 1
r = k
if i == n - 1:
l -= 1
r += 1
for b in range(l, r):
if s[i] == 'L':
dp[i + 1][b] = dp[i][b + 1]
elif s[i] == 'W':
dp[i + 1][b] = dp[i][b - 1]
elif s[i] == 'D':
dp[i + 1][b] = dp[i][b]
else:
dp[i + 1][b] = dp[i][b + 1] or dp[i][b - 1] or dp[i][b]
ans = []
i = n
b = -1
if dp[i][k]:
b = k
elif dp[i][-k]:
b = -k
if b == -1:
print("NO")
else:
while i > 0:
if (s[i - 1] == 'L' or s[i - 1] == '?') and dp[i][b] == dp[i - 1][b + 1]:
ans.append('L')
b += 1
elif (s[i - 1] == 'W' or s[i - 1] == '?') and dp[i][b] == dp[i - 1][b - 1]:
ans.append('W')
b -= 1
else:
ans.append('D')
i -= 1
for j in reversed(ans):
print(j, end='')
``` | output | 1 | 19,944 | 19 | 39,889 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW | instruction | 0 | 19,945 | 19 | 39,890 |
Tags: dp, graphs
Correct Solution:
```
import sys
n, k = map(int, input().split())
s = list(input())
if s[-1] == 'D':
print('NO')
exit()
size, zero = 2*k-1, k-1
dp = [[0]*size for _ in range(n)]
dp[0][zero] = 1
for i in range(n-1):
for j in range(size):
if j and (s[i] == 'W' or s[i] == '?'):
dp[i+1][j] |= dp[i][j-1]
if s[i] == 'D' or s[i] == '?':
dp[i+1][j] |= dp[i][j]
if j+1 < size and (s[i] == 'L' or s[i] == '?'):
dp[i+1][j] |= dp[i][j+1]
j = -1
if (s[-1] == 'W' or s[-1] == '?') and dp[-1][-1]:
j = size-1
s[-1] = 'W'
elif (s[-1] == 'L' or s[-1] == '?') and dp[-1][0]:
j = 0
s[-1] = 'L'
if j == -1:
print('NO')
exit()
for i in range(n-2, -1, -1):
if s[i] == 'W':
assert dp[i][j-1] == 1
j -= 1
elif s[i] == 'L':
assert dp[i][j+1] == 1
j += 1
elif s[i] == '?':
if dp[i][j]:
s[i] = 'D'
elif j > 0 and dp[i][j-1]:
s[i] = 'W'
j -= 1
else:
s[i] = 'L'
j += 1
assert j == zero
print(*s, sep='')
``` | output | 1 | 19,945 | 19 | 39,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW
Submitted Solution:
```
N, K = map( int, input().split() )
S = input()
offset = K + 1
dp = [ [ False for i in range( offset * 2 ) ] for j in range( N + 1 ) ]
pre = [ [ 0 for i in range( offset * 2 ) ] for j in range( N + 1 ) ] # previous state
dp[ 0 ][ offset ] = True
for i in range( N ):
for j in range( offset * 2 ):
if not dp[ i ][ j ]: continue
if ( S[ i ] == 'W' or S[ i ] == '?' ) and not ( i + 1 < N and j + 1 >= offset + K ):
if not dp[ i + 1 ][ j + 1 ]:
dp[ i + 1 ][ j + 1 ] = True
pre[ i + 1 ][ j + 1 ] = j
if ( S[ i ] == 'L' or S[ i ] == '?' ) and not ( i + 1 < N and j - 1 <= offset - K ):
if not dp[ i + 1 ][ j - 1 ]:
dp[ i + 1 ][ j - 1 ] = True
pre[ i + 1 ][ j - 1 ] = j
if S[ i ] == 'D' or S[ i ] == '?':
if not dp[ i + 1 ][ j ]:
dp[ i + 1 ][ j ] = True
pre[ i + 1 ][ j ] = j
if not dp[ N ][ offset + K ] and not dp[ N ][ offset - K ]:
print( "NO" )
else:
ans = ""
i, j = N, offset + K if dp[ N ][ offset + K ] else offset - K
while i:
pj = pre[ i ][ j ]
if S[ i - 1 ] == '?':
if pj + 1 == j:
ans += "W"
elif pj - 1 == j:
ans += "L"
else:
ans += "D"
else:
ans += S[ i - 1 ]
i, j = i - 1, pj
print( ans[ : : -1 ] )
``` | instruction | 0 | 19,946 | 19 | 39,892 |
Yes | output | 1 | 19,946 | 19 | 39,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW
Submitted Solution:
```
def ma():
s=input()
v=s.split(' ')
n=int(v[0])
k=int(v[1])
s=input()
dp=[[0,0] for _ in range(n+1)]
flag=True
for i in range (1,n):
c=s[i-1]
if c=='?':
dp[i][0]=min(dp[i-1][0]+1 ,k-1)
dp[i][1]=max(dp[i-1][1]-1 ,-k+1)
elif c=='D':
dp[i][0]=dp[i-1][0]
dp[i][1]=dp[i-1][1]
elif c=='W':
dp[i][0]=min(dp[i-1][0]+1 ,k-1)
dp[i][1]=dp[i-1][1]+1
if dp[i][1]==k:
flag=False
elif c=='L':
dp[i][0]=dp[i-1][0]-1
dp[i][1]=max(dp[i-1][1]-1 ,-k+1)
if dp[i][0]==-k:
flag=False
if not flag:
print('NO')
return
i=n
if s[i-1]=='?':
dp[i][0]=dp[i-1][0]+1
dp[i][1]=dp[i-1][1]-1
elif s[i-1]=='D':
dp[i][0]=dp[i-1][0]
dp[i][1]=dp[i-1][1]
elif s[i-1]=='W':
dp[i][0]=dp[i-1][0]+1
dp[i][1]=dp[i-1][1]+1
elif s[i-1]=='L':
dp[i][0]=dp[i-1][0]-1
dp[i][1]=dp[i-1][1]-1
res=['?']*n
if dp[i][0]==k or dp[i][1]==-k:
if dp[i][0]==k:
cur=k
else:
cur=-k
for i in range(n-1,-1,-1):
c=s[i]
if c=='?':
if cur>dp[i][0]:
cur=cur-1
res[i]='W'
elif dp[i][1]<=cur<=dp[i][0]:
cur=cur
res[i]='D'
elif cur<dp[i][1]:
cur=cur+1
res[i]='L'
elif c=='D':
cur=cur
res[i]=c
elif c=='W':
cur=cur-1
res[i]=c
elif c=='L':
cur=cur+1
res[i]=c
for i in range(n):
print(res[i],end='')
else:
print('NO')
ma()
``` | instruction | 0 | 19,947 | 19 | 39,894 |
Yes | output | 1 | 19,947 | 19 | 39,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW
Submitted Solution:
```
def ma():
s=input()
v=s.split(' ')
n=int(v[0])
k=int(v[1])
s=input()
dp=[[0,0] for _ in range(n+1)]
flag=True
mp={}
mp['W']=1
mp['D']=0
mp['L']=-1
for i in range (1,n):
c=s[i-1]
if c=='?':
dp[i][0]=min(dp[i-1][0]+1 ,k-1)
dp[i][1]=max(dp[i-1][1]-1 ,-k+1)
else:
dp[i][0]=min(dp[i-1][0]+mp[c],k-1)
dp[i][1]=max(dp[i-1][1]+mp[c],-k+1)
if dp[i][1]==k or dp[i][0]==-k:
flag=False
break
if not flag:
print('NO')
return
i=n
if s[i-1]=='?':
dp[i][0]=dp[i-1][0]+1
dp[i][1]=dp[i-1][1]-1
else :
dp[i][0]=dp[i-1][0]+mp[s[i-1]]
dp[i][1]=dp[i-1][1]+mp[s[i-1]]
res=['?']*n
if dp[i][0]==k or dp[i][1]==-k:
if dp[i][0]==k:
cur=k
else:
cur=-k
for i in range(n-1,-1,-1):
c=s[i]
if c=='?':
if cur>dp[i][0]:
res[i]='W'
elif dp[i][1]<=cur<=dp[i][0]:
res[i]='D'
elif cur<dp[i][1]:
res[i]='L'
else:
res[i]=c
cur=cur-mp[res[i]]
for i in range(n):
print(res[i],end='')
else:
print('NO')
ma()
``` | instruction | 0 | 19,948 | 19 | 39,896 |
Yes | output | 1 | 19,948 | 19 | 39,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW
Submitted Solution:
```
n, k = map(int, input().split())
*v, l = input()
sets = [(0, 0)]
d = {'W' : +1, 'D': 0, 'L': -1}
for c in v:
ms, mx = sets[-1]
ns = max(1 - k, ms + d.get(c, -1))
nx = min(k - 1, mx + d.get(c, +1))
if ns > nx:
print('NO')
exit(0)
sets.append((ns, nx))
ms, mx = sets[-1]
if mx == k - 1 and l in '?W':
cur = k - 1
ans = ['W']
elif ms == 1 - k and l in '?L':
cur = 1 - k
ans = ['L']
else:
print('NO')
exit(0)
ans += list(reversed(v))
for i, (c, (s, x)) in enumerate(zip(reversed(v), sets[-2::-1])):
if c == '?':
ans[i + 1] = next(p for p, q in d.items() if s <= cur - q and cur - q <= x)
cur -= d[ans[i + 1]]
print(''.join(reversed(ans)))
``` | instruction | 0 | 19,949 | 19 | 39,898 |
Yes | output | 1 | 19,949 | 19 | 39,899 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW
Submitted Solution:
```
#Bhargey Mehta (Sophomore)
#DA-IICT, Gandhinagar
import sys, math, queue
#sys.stdin = open("input.txt", "r")
MOD = 10**9+7
sys.setrecursionlimit(1000000)
def solve(n, k):
if abs(k) > K or (k == K and n < N):
return False
if dp[n][k] is not None:
return dp[n][k]
#print(s[n-1], n, k)
if s[n-1] == 'W':
dp[n][k] = solve(n-1, k-1)
elif s[n-1] == 'L':
dp[n][k] = solve(n-1, k+1)
elif s[n-1] == 'D':
dp[n][k] = solve(n-1, k)
else:
dp[n][k] = solve(n-1, k-1) or solve(n-1, k+1) or solve(n-1, k)
return dp[n][k]
def back(n, k):
if n == 0:
return
if s[n-1] == 'W':
back(n-1, k-1)
print('W', end="")
elif s[n-1] == 'L':
back(n-1, k+1)
print('L', end="")
elif s[n-1] == 'D':
back(n-1, k)
print('D', end="")
else:
if solve(n-1, k-1):
back(n-1, k-1)
print('W', end="")
elif solve(n-1, k+1):
back(n-1, k+1)
print('L', end="")
else:
back(n-1, k)
print('D', end="")
N, K = map(int, input().split())
s = input()
dp = [[None for i in range(2*K+1)] for j in range(N+1)]
for i in range(2*K+1):
dp[0][i] = False
dp[0][0] = True
for i in range(-K, K+1):
if solve(N, i):
ans = ""
back(N, i)
print(ans)
exit()
print("NO")
``` | instruction | 0 | 19,950 | 19 | 39,900 |
No | output | 1 | 19,950 | 19 | 39,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW
Submitted Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from collections import deque
def main():
n,k=map(int,input().split())
s=list(input().rstrip())
l,r,q=0,0,deque()
for i in range(n-1):
if s[i]=='W':
r+=1
elif s[i]=='L':
l+=1
elif s[i]=='?':
q.append(i)
if abs(r-l)==k:
if r>l and q:
s[q.popleft()]='L'
elif l>r and q:
s[q.popleft()]='W'
else:
print("NO")
return
l,r,q=s.count('L'),s.count('W'),s.count('?')
if abs(r-l)>k:
print("NO")
else:
z=k-abs(r-l)
c='W' if r>=l else 'L'
for i in range(n-1,-1,-1):
if s[i]=='?':
if z:
s[i]=c
z-=1
else:
s[i]='D'
if not z:
print("".join(s))
else:
print("NO")
# 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")
if __name__ == "__main__":
main()
``` | instruction | 0 | 19,951 | 19 | 39,902 |
No | output | 1 | 19,951 | 19 | 39,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW
Submitted Solution:
```
import sys
n, k = map(int, input().split())
s = list(input())
size, zero = 2*k-1, k-1
dp = [[0]*size for _ in range(n)]
dp[0][zero] = 1
for i in range(n-1):
for j in range(size):
if dp[i][j] == 0:
continue
if j+1 < size and (s[i] == 'W' or s[i] == '?'):
dp[i+1][j+1] = 1
if s[i] == 'D' or s[i] == '?':
dp[i+1][j] = 1
if j and (s[i] == 'L' or s[i] == '?'):
dp[i+1][j-1] = 1
j = -1
if (s[-1] == 'W' or s[-1] == '?') and dp[-1][-1]:
j = size-1
s[-1] = 'W'
elif (s[-1] == 'L' or s[-1] == '?') and dp[-1][0]:
j = 0
s[-1] = 'L'
if j == -1:
print('NO')
exit()
for i in range(n-2, -1, -1):
if s[i] == 'W':
j -= 1
elif s[i] == 'L':
j += 1
elif s[i] == '?':
if dp[i][j]:
s[i] = 'D'
elif j > 0 and dp[i][j-1]:
s[i] = 'W'
j -= 1
else:
s[i] = 'D'
j += 1
print(*s, sep='')
``` | instruction | 0 | 19,952 | 19 | 39,904 |
No | output | 1 | 19,952 | 19 | 39,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
* In the end the absolute difference between the number of wins and loses is equal to k;
* There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
Input
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
Output
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
Examples
Input
3 2
L??
Output
LDL
Input
3 1
W??
Output
NO
Input
20 5
?LLLLLWWWWW?????????
Output
WLLLLLWWWWWWWWLWLWDW
Submitted Solution:
```
from sys import exit
from copy import deepcopy
n, k = [int(i) for i in input().split()]
s = [i for i in input()]
p = [0] * (n + 1)
qind = -1
def check():
global s
b = 0
for i in range(len(s)):
if s[i] == 'L':
b -= 1
elif s[i] == 'W':
b += 1
if i != len(s) - 1 and abs(b) >= k:
return False
return abs(b) == k
def solved():
for i in s:
if i == '?':
print('D', end='')
else:
print(i, end='')
exit(0)
def no_ans():
print('NO')
exit(0)
for i in range(len(s)):
p[i + 1] = p[i]
if s[i] == 'L':
p[i + 1] -= 1
elif s[i] == 'W':
p[i + 1] += 1
if abs(p[i + 1]) >= k and i != len(s) - 1:
while qind < i and (qind == -1 or s[qind] != '?'):
qind += 1
if qind == i and s[i] != '?':
no_ans()
s[qind] = 'L' if p[i + 1] > 0 else 'W'
p[i + 1] = k - 1 if p[i + 1] > 0 else -k + 1
scpy = deepcopy(s)
rest = k - p[n]
j = n - 1
while j >= 0 and rest > 0:
if s[j] == '?':
s[j] = 'W'
rest -= 1
j -= 1
ok = (rest == 0) and check()
if ok:
solved()
s = deepcopy(scpy)
rest = (p[n] + k)
j = n - 1
while j >= 0 and rest > 0:
if s[j] == '?':
s[j] = 'L'
rest -= 1
j -= 1
ok = (rest == 0) and check()
if not ok:
no_ans()
solved()
``` | instruction | 0 | 19,953 | 19 | 39,906 |
No | output | 1 | 19,953 | 19 | 39,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N boxes arranged in a circle. The i-th box contains A_i stones.
Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:
* Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.
Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ A_i ≦ 10^9
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 … A_N
Output
If it is possible to remove all the stones from the boxes, print `YES`. Otherwise, print `NO`.
Examples
Input
5
4 5 1 2 3
Output
YES
Input
5
6 9 12 10 8
Output
YES
Input
4
1 2 3 1
Output
NO
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
'''
import random
N = 3
A = [0]*N
for i in range(1000):
n = random.randint(1,N)
for j in range(N):
A[(n-1+j)%N] += j+1
'''
M = N*(N+1)//2
sumA = sum(A)
if sumA % M != 0:
print('NO')
exit()
diff = [x-y for x,y in zip(A[1:] + [A[0]], A)]
all_cnt = 0
while min(diff) < 0:
cnt = 0
for i in range(N):
if diff[i] < 0:
t = -(diff[i]//(N-1))
cnt += t
diff[i] += t*N
diff = [d-cnt for d in diff]
all_cnt += cnt
N2 = sumA - all_cnt * M
if N2 % (M*N) == 0:
print('YES')
else:
print('NO')
``` | instruction | 0 | 20,161 | 19 | 40,322 |
No | output | 1 | 20,161 | 19 | 40,323 |
Provide a correct Python 3 solution for this coding contest problem.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60 | instruction | 0 | 20,994 | 19 | 41,988 |
"Correct Solution:
```
#!/usr/bin/env python3
from bisect import bisect
N, Q = map(int, input().split())
A = [0] * (N % 2) + list(map(int, input().split()))
HF = len(A) // 2
li = []
cumsum = [sum(A[HF:])]
for i in range(HF - 1):
left = A[2 * i + 1]
right = A[i + HF]
li += [(left + right) // 2 + 1]
cumsum += [cumsum[-1] + left - right]
for _ in range(Q):
X = int(input())
print(cumsum[bisect(li, X)])
``` | output | 1 | 20,994 | 19 | 41,989 |
Provide a correct Python 3 solution for this coding contest problem.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60 | instruction | 0 | 20,995 | 19 | 41,990 |
"Correct Solution:
```
# http://kmjp.hatenablog.jp/entry/2019/01/13/0930
import sys
input = sys.stdin.readline
from bisect import bisect_left
n, q = map(int, input().split())
a = list(map(int, input().split()))
xs = [int(input()) for i in range(q)]
s = [0]*(n+1)
se = [0]*(n+1)
for i in range(n):
s[i+1] = s[i] + a[i]
if i :
se[i+1] = se[i-1]
se[i+1] += a[i]
def check(x, k):
if k > n:
return False
Tk = (k+1)//2
i = bisect_left(a, x- (a[n-Tk]-x))
return i+k <= n
for x in xs:
left = 0
right = n+1
while right-left > 1:
mid = (left+right)//2
if check(x, mid):
left = mid
else:
right = mid
top = (left+1)//2
ans = 0
ans += s[n] - s[n-top]
if left%2:
left += 1
ans += se[n-left] if n-left>=0 else 0
print(ans)
``` | output | 1 | 20,995 | 19 | 41,991 |
Provide a correct Python 3 solution for this coding contest problem.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60 | instruction | 0 | 20,996 | 19 | 41,992 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
def calc(x):
l, r = 0, (N+1)//2
while r - l > 1:
m = (l+r) // 2
if A[m] + A[2*m] >= 2 * x:
l = m
else:
r = m
return l
N, Q = map(int, input().split())
A = [int(a) for a in input().split()][::-1]
B = [A[0]] + [0] * (N-1)
for i in range(1, N):
B[i] = B[i-1] + A[i]
C = [A[0]] + [0] * (N-1)
for i in range(2, N, 2):
C[i] = C[i-2] + A[i]
C[-1] += C[-2]
for _ in range(Q):
x = int(input())
c = calc(x)
print(B[c] + (C[-1] - C[2*c] if c*2<=N else 0))
``` | output | 1 | 20,996 | 19 | 41,993 |
Provide a correct Python 3 solution for this coding contest problem.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60 | instruction | 0 | 20,997 | 19 | 41,994 |
"Correct Solution:
```
from bisect import bisect_left
import sys
if sys.version_info[0:2] >= (3, 3):
from collections.abc import Sequence
else:
from collections import Sequence
class LazySequence(Sequence):
def __init__(self, f, n):
self.f = f
self.n = n
def __len__(self):
return self.n
def __getitem__(self, i):
if not (0 <= i < self.n):
raise IndexError
return self.f(i)
N, Q = map(int, input().split())
A = [int(s) for s in input().split()]
X = []
for _ in range(Q):
X.append(int(input()))
# A.sort()
s = [0] * (N + 1)
for i in range(1, N + 1):
s[i] = s[i - 1] + A[i - 1]
t = [0, A[0]] + [0] * (N - 1)
for i in range(2, N + 1):
t[i] = t[i - 2] + A[i - 1]
def index_left(x, i):
val = 2 * x - A[i]
return bisect_left(A, val)
def nankaime(x, i):
"""requires x <= A[i]"""
return i - index_left(x, i) + 1
def npi(x, i):
return nankaime(x, i) + i
def index_right(x, istart):
ls = LazySequence(lambda i: npi(x, i + istart), N - istart)
return istart + bisect_left(ls, N) - 1
def getans(x):
istart = bisect_left(A, x)
if istart == N:
return t[N]
j = index_right(x, istart)
turn = N - 1 - j
i = j - turn + 1
return s[N] - s[j + 1] + (t[i] if i > 0 else 0)
for i in range(Q):
print(getans(X[i]))
``` | output | 1 | 20,997 | 19 | 41,995 |
Provide a correct Python 3 solution for this coding contest problem.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60 | instruction | 0 | 20,998 | 19 | 41,996 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
N, Q = MAP()
A = LIST()
sm = sum([A[i] for i in range(N-1, -1, -2)])
borders = [(INF, sm)]
j = N-3
for i in range(N-2, N//2-1, -1):
border = (A[j] + A[i]) // 2
val = A[i] - A[j]
sm += val
borders.append((border, sm))
j -= 2
borders.sort()
for _ in range(Q):
x = INT()
idx = bisect_left(borders, (x, 0))
print(borders[idx][1])
``` | output | 1 | 20,998 | 19 | 41,997 |
Provide a correct Python 3 solution for this coding contest problem.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60 | instruction | 0 | 20,999 | 19 | 41,998 |
"Correct Solution:
```
from bisect import bisect_left
N, Q = map(int, input().split())
A = [int(i) for i in input().split()]
X = [int(input()) for _ in range(Q)]
i = (N & 1) ^ 1
j = N // 2
st = sum(A[j:])
ret_k = []
ret_v = []
while i < j :
ret_k.append((A[i] + A[j]) // 2)
ret_v.append(st)
st = st - A[j] + A[i]
i += 2
j += 1
ret_k.append(1e+12)
ret_v.append(st)
for x in X :
print(ret_v[bisect_left(ret_k, x)])
``` | output | 1 | 20,999 | 19 | 41,999 |
Provide a correct Python 3 solution for this coding contest problem.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60 | instruction | 0 | 21,000 | 19 | 42,000 |
"Correct Solution:
```
import bisect
n,q = map(int, input().split())
aList = list(map(int, input().split()))
sumList=[]
sep2SumList=[]
for i in range(n):
if i==0:
sumList.append(aList[i])
else:
sumList.append(sumList[-1]+aList[i])
if n%2==0:
if i%2==1:
if i==1:
sep2SumList.append(aList[i])
else:
sep2SumList.append(sep2SumList[-1]+aList[i])
else:
if i%2==0:
if i==0:
sep2SumList.append(aList[i])
else:
sep2SumList.append(sep2SumList[-1]+aList[i])
sakaime=[]
anskazu=(n+1)//2
for i in range(anskazu):
sakaime.append((aList[n-(i+1+1)]+aList[n-((i+1+1)*2-1)])//2)
sakaime.reverse()
sakaime=sakaime[1:]
def kotae(x):
bisect.bisect_left(sakaime,x)
num=len(sakaime)+1-bisect.bisect_left(sakaime,x)
if num==len(sakaime)+1:
ans=sumList[-1]-sumList[-1-num]
else:
ans=sumList[-1]-sumList[-1-num]+sep2SumList[(n-num*2+1)//2-1] #+1することで奇数を吸収
return ans
for i in range(q):
x=int(input())
print(kotae(x))
``` | output | 1 | 21,000 | 19 | 42,001 |
Provide a correct Python 3 solution for this coding contest problem.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60 | instruction | 0 | 21,001 | 19 | 42,002 |
"Correct Solution:
```
# coding: utf-8
# Your code here!
import bisect
n,q = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
if n%2 == 1:
n+=1
a.insert(0, 0)
sikiri = [(a[2*i+1] + a[i+n//2])//2 for i in range(n//2-1)]
s = sum(a[n//2:])
ans = [s]
for i in range(n//2-1):
s = s - a[i+n//2] + a[2*i+1]
ans.append(s)
#print(a)
#print(sikiri)
#print(ans)
for _ in range(q):
x = int(input())
i = bisect.bisect_left(sikiri,x)
print(ans[i])
``` | output | 1 | 21,001 | 19 | 42,003 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60
Submitted Solution:
```
import itertools
import sys
def input(): return sys.stdin.readline()
def inpl(): return [int(i) for i in input().split()]
def f(y):
return abs(x-A[y])
N, Q = inpl()
A = inpl()
SA = list(itertools.accumulate(A))
if N%2 :
B = [v for i, v in enumerate(A) if not i%2]
B = [0] + list(itertools.accumulate(B))
else:
B = [v for i, v in enumerate(A) if i%2]
B = [0] + list(itertools.accumulate(B))
for _ in range(Q):
x = int(input())
lo = 1
hi = -(-N//2) + 1
while hi - lo >1:
mid = (lo+hi)//2
if mid == 1:
continue
if max(f(N-mid-1), f(N-2*mid+1)) <= f(N-mid):
lo = mid
else:
hi = mid
print(SA[N-1] - SA[N-lo-1] + B[-((2*lo-N)//2)])
``` | instruction | 0 | 21,002 | 19 | 42,004 |
Yes | output | 1 | 21,002 | 19 | 42,005 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60
Submitted Solution:
```
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]
def inside(y, x, H, W):
return 0 <= y < H and 0 <= x < W
def ceil(a, b):
return (a + b - 1) // b
# aとbの最大公約数
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
# aとbの最小公倍数
def lcm(a, b):
g = gcd(a, b)
return a / g * b
def solve(A, Q):
N = len(A)
A = A[::-1]
s, es = [0] * (N + 1), [0] * (N + 1)
for i in range(N):
s[i + 1] = s[i] + A[i]
es[i + 1] = es[i] + (A[i] if i % 2 == 0 else 0)
x_list, sum_t_list = [], []
for n in range((N - 1) // 2):
x = (A[n + 1] + A[n * 2 + 2]) // 2 + 1
sum_t = s[n + 1] + (es[N] - es[n * 2 + 2])
x_list.append(x)
sum_t_list.append(sum_t)
x_list = x_list[::-1]
sum_t_list = sum_t_list[::-1]
for _ in range(Q):
x = int(input())
p = bisect_right(x_list, x)
if p == 0:
print(s[(N + 1) // 2])
else:
print(sum_t_list[p - 1])
def main():
N, Q = map(int, input().split())
A = list(map(int, input().split()))
solve(A, Q)
if __name__ == '__main__':
main()
``` | instruction | 0 | 21,003 | 19 | 42,006 |
Yes | output | 1 | 21,003 | 19 | 42,007 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60
Submitted Solution:
```
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10
#solve
def solve():
n, q = LI()
a = LI()
odd = [0] * n
even = [0] * n
def f(mid):
return abs(a[mid] - x) < abs(a[mid - (n - mid - 1)] - x)
for i in range(n):
if i & 1:
odd[i] += odd[i - 1] + a[i]
else:
odd[i] += odd[i - 1]
acc = list(itertools.accumulate(a))
for _ in range(q):
x = II()
ok = 0
ng = n
while abs(ng - ok) > 1:
mid = (ok + ng) // 2
if f(mid):
ok = mid
else:
ng = mid
ok = ng - (n - ng)
ng -= 1
ans = acc[-1] - acc[ng]
if ok < 0:
pass
elif n & 1:
ans += acc[ok]-odd[ok]
else:
ans += odd[ok]
print(ans)
return
#main
if __name__ == '__main__':
solve()
``` | instruction | 0 | 21,004 | 19 | 42,008 |
Yes | output | 1 | 21,004 | 19 | 42,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60
Submitted Solution:
```
#参考
#https://atcoder.jp/contests/aising2019/submissions/3995168
import bisect
N,Q=map(int,input().split())
A=list(map(int,input().split()))
Asum=[0]*N
Asum2=[0]*N
total=0
for i in range(N):
total+=A[i]
Asum[i]=total
if i>1:
Asum2[i]=A[i]+Asum2[i-2]
else:
Asum2[i]=A[i]
X=[]
for i in range(Q):
X.append((int(input()),i))
X.sort(key=lambda x :x[0])
#yで高橋くんと青木くんの取りたいカードが初めて衝突すると考える。
#このようなyは半分より小さい所にはないので初期値を半分のところとする。
#yがNまで来た時は最初から交互に取る
#Xを小さい順から計算することで、yの値が単調増加になる
y=N//2-1
ans=[0]*Q
for x,i in X:
#条件式はy<=N-1を最初に持ってこないとA[y]の参照でエラーの可能性
while y<=N-1 and x>A[y]:
y+=1
while y<=N-1:
takahashi=N-y
aoki=y+1-bisect.bisect_left(A,x-(A[y]-x))
if takahashi>aoki:
y+=1
else:
break
#右端からA[y]までは全て高橋くんが取る
ans[i]+=Asum[N-1]-Asum[y-1]
#青木くんはyからN-y個取っているので、その下のところから交互に取る
if y-(N-y)-1<0:
continue
ans[i]+=Asum2[y-(N-y)-1]
for i in range(Q):
print(ans[i])
``` | instruction | 0 | 21,005 | 19 | 42,010 |
Yes | output | 1 | 21,005 | 19 | 42,011 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60
Submitted Solution:
```
from bisect import bisect_left, bisect_right
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
cum1 = [0] * N
cum1[0], cum1[1] = A[0], A[1]
for i in range(2, N):
cum1[i] = cum1[i-2] + A[i]
cum2 = [0] * N
cum2[-1] = A[-1]
for i in range(N-1)[::-1]:
cum2[i] = cum2[i+1] + A[i]
for _ in range(Q):
x = int(input())
lb, ub = 0, 10**9
while ub - lb > 1:
m = (lb + ub) // 2
i = bisect_left(A, x - m)
j = bisect_right(A, x + m)
cnt = j - i
if cnt <= N - j:
lb = m
else:
ub = m
i = bisect_left(A, x - lb)
j = bisect_right(A, x + lb)
i -= (N - j) - (j - i)
ans = (cum1[i-1] if i > 0 else 0) + (cum2[j] if j < N else 0)
print(ans)
``` | instruction | 0 | 21,006 | 19 | 42,012 |
No | output | 1 | 21,006 | 19 | 42,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60
Submitted Solution:
```
from itertools import accumulate
import bisect
n,q = map(int,input().split())
if n%2:
a = [0]+list(map(int,input().split()))
n += 1
else:
a = list(map(int,input().split()))
fs = [a[i] if i%2 else 0 for i in range(n)]
accss = [0]*n
accff = list(accumulate(a))
accfs = list(accumulate(fs))
xls = []
xpt = []
for i in range(n//2):
xls.append((a[2*i+1]+a[i+n//2])/2)
if i == 0:
xpt.append(accff[n-1]-accff[n//2-1])
else:
xpt.append(accff[n-1]-accff[i+n//2-1]+accfs[2*i-1])
for _ in range(q):
x = bisect.bisect_left(xls,int(input()))
if x >= n-1:
print(xpt[-1])
else:
print(xpt[x])
``` | instruction | 0 | 21,007 | 19 | 42,014 |
No | output | 1 | 21,007 | 19 | 42,015 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60
Submitted Solution:
```
N, Q = map(int, input().split())
A = [int(a) for a in input().split()][::-1]
for _ in range(Q):
x = int(input())
l, r = 0, (N+1)//2
while r - l > 1:
m = (l+r) // 2
if A[m] + A[2*m] >= 2 * x:
l = m
else:
r = m
l += 1
print(sum(A[:l] + A[2*l::2]))
``` | instruction | 0 | 21,008 | 19 | 42,016 |
No | output | 1 | 21,008 | 19 | 42,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.
Using these cards, Takahashi and Aoki will play the following game:
* Aoki chooses an integer x.
* Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
* Takahashi should take the card with the largest integer among the remaining card.
* Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
* The game ends when there is no card remaining.
You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.
Constraints
* 2 \leq N \leq 100 000
* 1 \leq Q \leq 100 000
* 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
* 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.
Examples
Input
5 5
3 5 7 11 13
1
4
9
10
13
Output
31
31
27
23
23
Input
4 3
10 20 30 40
2
34
34
Output
70
60
60
Submitted Solution:
```
import bisect
import sys
input = sys.stdin.readline
INF = 10**12
n, q = [int(item) for item in input().split()]
a = [int(item) for item in input().split()]
# Calc cumrative sum
sum_even = [0] * (n + 1)
sum_odd = [0] * (n + 1)
sum_all = [0] * (n + 1)
for i, item in enumerate(a):
if i % 2 == 0:
sum_odd[i+1] = sum_odd[i] + a[i]
sum_even[i+1] = sum_even[i]
else:
sum_even[i+1] = sum_even[i] + a[i]
sum_odd[i+1] = sum_odd[i]
sum_all[i+1] = sum_all[i] + a[i]
# Iterate in query
a.append(INF)
ans = []
for i in range(q):
x = int(input())
# Get div of aoki - takahashi
# l: aoki takes, r: takahashi takes
l = 0; r = n - 1
while r - l > 1:
m = (r + l) // 2
rng = abs(x - a[m])
t_num = n - m
a_num = n - t_num - (bisect.bisect_left(a, x - rng))
if t_num <= a_num + 1:
r = m
else:
l = m
rng = abs(x - a[r])
t_num = n - r
rest = n - 2 * t_num
t_sum = sum_all[n] - sum_all[n - t_num]
a_sum = sum_all[r] - sum_all[r - t_num]
# Add stripe area
if n - t_num * 2 > 0:
if rest % 2 == 0:
t_sum += sum_even[rest]
a_sum += sum_odd[rest]
else:
t_sum += sum_odd[rest]
a_sum += sum_even[rest]
ans.append(t_sum)
print("\n".join([str(item) for item in ans]))
``` | instruction | 0 | 21,009 | 19 | 42,018 |
No | output | 1 | 21,009 | 19 | 42,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N boxes arranged in a row from left to right. The i-th box from the left contains a_i manju (buns stuffed with bean paste). Sugim and Sigma play a game using these boxes. They alternately perform the following operation. Sugim goes first, and the game ends when a total of N operations are performed.
* Choose a box that still does not contain a piece and is adjacent to the box chosen in the other player's last operation, then put a piece in that box. If there are multiple such boxes, any of them can be chosen.
* If there is no box that satisfies the condition above, or this is Sugim's first operation, choose any one box that still does not contain a piece, then put a piece in that box.
At the end of the game, each player can have the manju in the boxes in which he put his pieces. They love manju, and each of them is wise enough to perform the optimal moves in order to have the maximum number of manju at the end of the game.
Find the number of manju that each player will have at the end of the game.
Constraints
* 2 \leq N \leq 300 000
* 1 \leq a_i \leq 1000
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the numbers of Sugim's manju and Sigma's manju at the end of the game, in this order, with a space in between.
Examples
Input
5
20 100 10 1 10
Output
120 21
Input
6
4 5 1 1 4 5
Output
11 9
Input
5
1 10 100 10 1
Output
102 20
Submitted Solution:
```
from os import sys
sys.setrecursionlimit(1000000000)
n = int(input())
l = list(map(int, input().split()))
def completo(l):
for e in l:
if e != 0:
return False
return True
def solve(turno, a, b, l, index,n):
if completo(l):
print(a,b)
return
if turno == n:
print(a,b)
return
if index == -1:
m = max(l)
index = l.index(m)
elif l[index-1] == 0 and l[index+1] == 0:
solve(turno,a,b,l,-1,n)
elif l[index-1] > l[index+1]:
m = l[index-1]
index = index-1
else:
m = l[index+1]
index = index+1
l[index] = 0
if turno % 2 == 0:
if index == 0 or index == len(l)-1:
solve(turno+1,a+m,b,l,-1,n)
else:
solve(turno+1,a+m,b,l,index,n)
else:
if index == 0 or index == len(l)-1:
solve(turno+1,a,b+m,l,-1,n)
else:
solve(turno+1,a,b+m,l,index,n)
solve(0,0,0,l,-1,n)
``` | instruction | 0 | 21,010 | 19 | 42,020 |
No | output | 1 | 21,010 | 19 | 42,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N boxes arranged in a row from left to right. The i-th box from the left contains a_i manju (buns stuffed with bean paste). Sugim and Sigma play a game using these boxes. They alternately perform the following operation. Sugim goes first, and the game ends when a total of N operations are performed.
* Choose a box that still does not contain a piece and is adjacent to the box chosen in the other player's last operation, then put a piece in that box. If there are multiple such boxes, any of them can be chosen.
* If there is no box that satisfies the condition above, or this is Sugim's first operation, choose any one box that still does not contain a piece, then put a piece in that box.
At the end of the game, each player can have the manju in the boxes in which he put his pieces. They love manju, and each of them is wise enough to perform the optimal moves in order to have the maximum number of manju at the end of the game.
Find the number of manju that each player will have at the end of the game.
Constraints
* 2 \leq N \leq 300 000
* 1 \leq a_i \leq 1000
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the numbers of Sugim's manju and Sigma's manju at the end of the game, in this order, with a space in between.
Examples
Input
5
20 100 10 1 10
Output
120 21
Input
6
4 5 1 1 4 5
Output
11 9
Input
5
1 10 100 10 1
Output
102 20
Submitted Solution:
```
n = int(input())
l = list(map(int, input().split()))
def completo(l):
for e in l:
if e != 0:
return False
return True
def solve(turno, a, b, l, index,n):
if completo(l):
print(a,b)
return
if turno == n:
print(a,b)
return
if index == -1:
m = max(l)
index = l.index(m)
elif l[index-1] == 0 and l[index+1] == 0:
solve(turno,a,b,l,-1,n)
elif l[index-1] > l[index+1]:
m = l[index-1]
index = index-1
else:
m = l[index+1]
index = index+1
l[index] = 0
if turno % 2 == 0:
if index == 0 or index == len(l)-1:
solve(turno+1,a+m,b,l,-1,n)
else:
solve(turno+1,a+m,b,l,index,n)
else:
if index == 0 or index == len(l)-1:
solve(turno+1,a,b+m,l,-1,n)
else:
solve(turno+1,a,b+m,l,index,n)
solve(0,0,0,l,-1,n)
``` | instruction | 0 | 21,011 | 19 | 42,022 |
No | output | 1 | 21,011 | 19 | 42,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N boxes arranged in a row from left to right. The i-th box from the left contains a_i manju (buns stuffed with bean paste). Sugim and Sigma play a game using these boxes. They alternately perform the following operation. Sugim goes first, and the game ends when a total of N operations are performed.
* Choose a box that still does not contain a piece and is adjacent to the box chosen in the other player's last operation, then put a piece in that box. If there are multiple such boxes, any of them can be chosen.
* If there is no box that satisfies the condition above, or this is Sugim's first operation, choose any one box that still does not contain a piece, then put a piece in that box.
At the end of the game, each player can have the manju in the boxes in which he put his pieces. They love manju, and each of them is wise enough to perform the optimal moves in order to have the maximum number of manju at the end of the game.
Find the number of manju that each player will have at the end of the game.
Constraints
* 2 \leq N \leq 300 000
* 1 \leq a_i \leq 1000
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the numbers of Sugim's manju and Sigma's manju at the end of the game, in this order, with a space in between.
Examples
Input
5
20 100 10 1 10
Output
120 21
Input
6
4 5 1 1 4 5
Output
11 9
Input
5
1 10 100 10 1
Output
102 20
Submitted Solution:
```
import numpy as np
def main():
n = int(input())
a = list(map(int, input().split()))
a = np.array(a)
s1 = 0
s2 = 0
for i in range(n):
z = i % 2
if (i == 0):
index = np.argmax(a)
s1 = a[index]
a[index] = -1
continue
else:
if (z == 0):
[s1, a, index] = auto(s1, a, index)
else:
[s2, a, index] = auto(s2, a, index)
print('{} {}'.format(s1, s2))
def auto(s, a, index):
left = 0
right = 0
if (index == 0):
left = -1
right = a[index+1]
elif (index == len(a)-1):
left = a[index-1]
right = -1
else:
left = a[index - 1]
right = a[index + 1]
if (left == -1 and right == -1):
index = np.argmax(a)
s += a[index]
a[index] = -1
elif (left >= right):
s += left
index = index-1
a[index] = -1
elif (left < right):
s += right
index = index+1
a[index] = -1
return [s, a, index]
if __name__ == '__main__':
main()
``` | instruction | 0 | 21,012 | 19 | 42,024 |
No | output | 1 | 21,012 | 19 | 42,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N boxes arranged in a row from left to right. The i-th box from the left contains a_i manju (buns stuffed with bean paste). Sugim and Sigma play a game using these boxes. They alternately perform the following operation. Sugim goes first, and the game ends when a total of N operations are performed.
* Choose a box that still does not contain a piece and is adjacent to the box chosen in the other player's last operation, then put a piece in that box. If there are multiple such boxes, any of them can be chosen.
* If there is no box that satisfies the condition above, or this is Sugim's first operation, choose any one box that still does not contain a piece, then put a piece in that box.
At the end of the game, each player can have the manju in the boxes in which he put his pieces. They love manju, and each of them is wise enough to perform the optimal moves in order to have the maximum number of manju at the end of the game.
Find the number of manju that each player will have at the end of the game.
Constraints
* 2 \leq N \leq 300 000
* 1 \leq a_i \leq 1000
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the numbers of Sugim's manju and Sigma's manju at the end of the game, in this order, with a space in between.
Examples
Input
5
20 100 10 1 10
Output
120 21
Input
6
4 5 1 1 4 5
Output
11 9
Input
5
1 10 100 10 1
Output
102 20
Submitted Solution:
```
n = input()
slimes = map(int, input().split())
res = 0
last = -1
cnt = 0
for slime in slimes:
if slime != last:
res += cnt // 2
last = slime
cnt = 1
else:
cnt += 1
res += cnt // 2
print(res)
``` | instruction | 0 | 21,013 | 19 | 42,026 |
No | output | 1 | 21,013 | 19 | 42,027 |
Provide a correct Python 3 solution for this coding contest problem.
D --Invisible
Problem Statement
You are trying to play a card game called "Invisible" with your friends. This card game uses two types of cards, a "scoring card" and a "jamming card". A positive value is written on each score card. The rules of this card game are as follows.
* The game is played by two players, player 1 and player 2. The game starts on player 1's turn.
* There is one stack and two decks in the field. The stack consists of cards placed by two players. In addition, the deck possessed by each player consists of the score card and the obstruction card possessed by that player. Players can check the order of cards in themselves or in their opponent's deck at any time. There are no cards on the stack at the beginning of the game.
* The two players alternately perform one of the following two actions exactly once.
* Put the top card of your deck on the top of the stack. However, this action cannot be performed when there are no cards in your deck.
* Pass your turn.
* When the player passes the turn, the following processing is performed.
* Each player gets all the scoring cards in the stack that meet the following two conditions. The score card obtained is removed from the field.
1. This is the score card you put on the stack.
2. Above any disturbing cards placed by your opponent (when there are no disturbing cards in your stack, the player gets all the cards he or she puts on the stack).
* Remove all cards in the stack.
If both players pass in succession with no cards on the stack, the game ends. The final score of each player is the sum of the numbers written on the score card obtained by each player.
Each player takes the best action to maximize the value obtained by subtracting the opponent's score from his own score. Your job is to calculate the difference between Player 1's score and Player 2's score when each player behaves optimally for each player's deck given.
Input
The input consists of a single test case of the form:
$ n $ $ m $
$ a_1 $ $ a_2 $ $ \ dots $ $ a_n $
$ b_1 $ $ b_2 $ $ \ dots $ $ b_m $
The first line consists of positive integers $ n $, $ m $ ($ 1 \ le n, m \ le 50 $) representing the number of decks. The second line consists of $ n $ integers, where $ a_i $ represents the $ i $ th card from the top of player 1's deck ($ 1 \ le i \ le n $). $ a_i $ is more than $ 1 $, less than $ 1 {,} 000 {,} 000 $, or $ -1 $. The third line consists of $ m $ integers, where $ b_j $ represents the $ j $ th card from the top of player 2's deck ($ 1 \ le j \ le m $). $ b_j $ is more than $ 1 $, less than $ 1 {,} 000 {,} 000 $, or $ -1 $. When $ a_i $ and $ b_j $ are positive integers, it represents a scoring card, and when it is $ -1 $, it represents a jamming card.
Output
Output (Score of Player 1)-(Score of Player 2) when each player behaves optimally.
Sample Input 1
twenty two
100 -1
200 300
Output for the Sample Input 1
-100
<image>
<image>
<image>
Sample Input 2
3 5
10 30 -1
-1 90 20 10 -1
Output for the Sample Input 2
0
Sample Input 3
4 5
15 20 10 30
50 30 10 20 25
Output for the Sample Input 3
-60
Example
Input
2 2
100 -1
200 300
Output
-100 | instruction | 0 | 21,131 | 19 | 42,262 |
"Correct Solution:
```
n, m = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
memo = dict()
def calc(A, i):
if A[i] != -1:
return 0
point = 0
for j in range(i - 1, -1, -1):
if A[j] == -1:
break
point += A[j]
return point
def dfs(a, b, skip, turn, a_stack, b_stack):
key = (a, b, skip, turn, a_stack, b_stack)
if key in memo:
return memo[key]
if skip == 3:
return 0
if turn % 2 == 0:
memo[key] = dfs(a, b, skip + 1, (turn + 1) % 2, 0, 0) + a_stack - b_stack
if len(A) == a:
return memo[key]
if A[a] == -1:
b_stack = 0
else:
a_stack += A[a]
memo[key] = max(memo[key], dfs(a + 1, b, 0, (turn + 1) % 2, a_stack, b_stack))
else:
memo[key] = dfs(a, b, skip + 1, (turn + 1) % 2, 0, 0) - b_stack + a_stack
if len(B) == b:
return memo[key]
if B[b] == -1:
a_stack = 0
else:
b_stack += B[b]
memo[key] = min(memo[key], dfs(a, b + 1, 0, (turn + 1) % 2, a_stack, b_stack))
return memo[key]
print(dfs(0, 0, 0, 0, 0, 0))
``` | output | 1 | 21,131 | 19 | 42,263 |
Provide a correct Python 3 solution for this coding contest problem.
D --Invisible
Problem Statement
You are trying to play a card game called "Invisible" with your friends. This card game uses two types of cards, a "scoring card" and a "jamming card". A positive value is written on each score card. The rules of this card game are as follows.
* The game is played by two players, player 1 and player 2. The game starts on player 1's turn.
* There is one stack and two decks in the field. The stack consists of cards placed by two players. In addition, the deck possessed by each player consists of the score card and the obstruction card possessed by that player. Players can check the order of cards in themselves or in their opponent's deck at any time. There are no cards on the stack at the beginning of the game.
* The two players alternately perform one of the following two actions exactly once.
* Put the top card of your deck on the top of the stack. However, this action cannot be performed when there are no cards in your deck.
* Pass your turn.
* When the player passes the turn, the following processing is performed.
* Each player gets all the scoring cards in the stack that meet the following two conditions. The score card obtained is removed from the field.
1. This is the score card you put on the stack.
2. Above any disturbing cards placed by your opponent (when there are no disturbing cards in your stack, the player gets all the cards he or she puts on the stack).
* Remove all cards in the stack.
If both players pass in succession with no cards on the stack, the game ends. The final score of each player is the sum of the numbers written on the score card obtained by each player.
Each player takes the best action to maximize the value obtained by subtracting the opponent's score from his own score. Your job is to calculate the difference between Player 1's score and Player 2's score when each player behaves optimally for each player's deck given.
Input
The input consists of a single test case of the form:
$ n $ $ m $
$ a_1 $ $ a_2 $ $ \ dots $ $ a_n $
$ b_1 $ $ b_2 $ $ \ dots $ $ b_m $
The first line consists of positive integers $ n $, $ m $ ($ 1 \ le n, m \ le 50 $) representing the number of decks. The second line consists of $ n $ integers, where $ a_i $ represents the $ i $ th card from the top of player 1's deck ($ 1 \ le i \ le n $). $ a_i $ is more than $ 1 $, less than $ 1 {,} 000 {,} 000 $, or $ -1 $. The third line consists of $ m $ integers, where $ b_j $ represents the $ j $ th card from the top of player 2's deck ($ 1 \ le j \ le m $). $ b_j $ is more than $ 1 $, less than $ 1 {,} 000 {,} 000 $, or $ -1 $. When $ a_i $ and $ b_j $ are positive integers, it represents a scoring card, and when it is $ -1 $, it represents a jamming card.
Output
Output (Score of Player 1)-(Score of Player 2) when each player behaves optimally.
Sample Input 1
twenty two
100 -1
200 300
Output for the Sample Input 1
-100
<image>
<image>
<image>
Sample Input 2
3 5
10 30 -1
-1 90 20 10 -1
Output for the Sample Input 2
0
Sample Input 3
4 5
15 20 10 30
50 30 10 20 25
Output for the Sample Input 3
-60
Example
Input
2 2
100 -1
200 300
Output
-100 | instruction | 0 | 21,132 | 19 | 42,264 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n,m = LI()
a = LI()
b = LI()
fm = {}
def f(ai,bi,pt,sa,sb,t):
if pt > 2:
return 0
key = (ai,bi,pt,sa,sb,t)
if key in fm:
return fm[key]
r = f(ai,bi,pt+1,0,0,not t) + sa - sb
tr = 0
if t:
if ai < n:
if a[ai] < 0:
tr = f(ai+1,bi,0,sa,0,not t)
else:
tr = f(ai+1,bi,0,sa+a[ai],sb,not t)
if r < tr:
r = tr
else:
if bi < m:
if b[bi] < 0:
tr = f(ai,bi+1,0,0,sb,not t)
else:
tr = f(ai,bi+1,0,sa,sb+b[bi],not t)
if r > tr:
r = tr
fm[key] = r
return r
r = f(0,0,0,0,0,True)
return r
print(main())
``` | output | 1 | 21,132 | 19 | 42,265 |
Provide a correct Python 3 solution for this coding contest problem.
D --Invisible
Problem Statement
You are trying to play a card game called "Invisible" with your friends. This card game uses two types of cards, a "scoring card" and a "jamming card". A positive value is written on each score card. The rules of this card game are as follows.
* The game is played by two players, player 1 and player 2. The game starts on player 1's turn.
* There is one stack and two decks in the field. The stack consists of cards placed by two players. In addition, the deck possessed by each player consists of the score card and the obstruction card possessed by that player. Players can check the order of cards in themselves or in their opponent's deck at any time. There are no cards on the stack at the beginning of the game.
* The two players alternately perform one of the following two actions exactly once.
* Put the top card of your deck on the top of the stack. However, this action cannot be performed when there are no cards in your deck.
* Pass your turn.
* When the player passes the turn, the following processing is performed.
* Each player gets all the scoring cards in the stack that meet the following two conditions. The score card obtained is removed from the field.
1. This is the score card you put on the stack.
2. Above any disturbing cards placed by your opponent (when there are no disturbing cards in your stack, the player gets all the cards he or she puts on the stack).
* Remove all cards in the stack.
If both players pass in succession with no cards on the stack, the game ends. The final score of each player is the sum of the numbers written on the score card obtained by each player.
Each player takes the best action to maximize the value obtained by subtracting the opponent's score from his own score. Your job is to calculate the difference between Player 1's score and Player 2's score when each player behaves optimally for each player's deck given.
Input
The input consists of a single test case of the form:
$ n $ $ m $
$ a_1 $ $ a_2 $ $ \ dots $ $ a_n $
$ b_1 $ $ b_2 $ $ \ dots $ $ b_m $
The first line consists of positive integers $ n $, $ m $ ($ 1 \ le n, m \ le 50 $) representing the number of decks. The second line consists of $ n $ integers, where $ a_i $ represents the $ i $ th card from the top of player 1's deck ($ 1 \ le i \ le n $). $ a_i $ is more than $ 1 $, less than $ 1 {,} 000 {,} 000 $, or $ -1 $. The third line consists of $ m $ integers, where $ b_j $ represents the $ j $ th card from the top of player 2's deck ($ 1 \ le j \ le m $). $ b_j $ is more than $ 1 $, less than $ 1 {,} 000 {,} 000 $, or $ -1 $. When $ a_i $ and $ b_j $ are positive integers, it represents a scoring card, and when it is $ -1 $, it represents a jamming card.
Output
Output (Score of Player 1)-(Score of Player 2) when each player behaves optimally.
Sample Input 1
twenty two
100 -1
200 300
Output for the Sample Input 1
-100
<image>
<image>
<image>
Sample Input 2
3 5
10 30 -1
-1 90 20 10 -1
Output for the Sample Input 2
0
Sample Input 3
4 5
15 20 10 30
50 30 10 20 25
Output for the Sample Input 3
-60
Example
Input
2 2
100 -1
200 300
Output
-100 | instruction | 0 | 21,133 | 19 | 42,266 |
"Correct Solution:
```
n, m = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
memo = {}
def dfs(p, q, s, t, turn, pss):
if (p, q, s, t, turn, pss) in memo:
return memo[p, q, s, t, turn, pss]
if p == len(A) and q == len(B):
return s-t
res = 0
if turn:
# first
if pss < 2:
if s+t:
res = (s - t) + dfs(p, q, 0, 0, 0, 0)
else:
res = dfs(p, q, 0, 0, 0, pss+1)
else:
return 0
if p < len(A):
if A[p] == -1:
res = max(res, dfs(p+1, q, s, 0, 0, 0))
else:
res = max(res, dfs(p+1, q, s+A[p], t, 0, 0))
else:
# second
if pss < 2:
if s+t:
res = (s - t) + dfs(p, q, 0, 0, 1, 0)
else:
res = dfs(p, q, 0, 0, 1, pss+1)
else:
return 0
if q < len(B):
if B[q] == -1:
res = min(res, dfs(p, q+1, 0, t, 1, 0))
else:
res = min(res, dfs(p, q+1, s, t+B[q], 1, 0))
memo[p, q, s, t, turn, pss] = res
return res
print(dfs(0, 0, 0, 0, 1, 0))
``` | output | 1 | 21,133 | 19 | 42,267 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter.
<image>
Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.
Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?
You have to determine the winner of the game for all initial positions of the marbles.
Input
The first line of input contains two integers n and m (2 ≤ n ≤ 100, <image>).
The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.
Output
Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise.
Examples
Input
4 4
1 2 b
1 3 a
2 4 c
3 4 b
Output
BAAA
ABAA
BBBA
BBBB
Input
5 8
5 3 h
1 2 c
3 1 c
3 2 r
5 1 r
4 3 z
5 4 r
5 2 h
Output
BABBB
BBBBB
AABBB
AAABA
AAAAB
Note
Here's the graph in the first sample test case:
<image>
Here's the graph in the second sample test case:
<image> | instruction | 0 | 21,721 | 19 | 43,442 |
Tags: dfs and similar, dp, games, graphs
Correct Solution:
```
# python3
from functools import lru_cache
def readline(): return list(map(int, input().split()))
def main():
n, m = readline()
edges = [list() for __ in range(n)]
for __ in range(m):
tokens = input().split()
begin, end = map(int, tokens[:2])
weight = ord(tokens[2])
edges[begin - 1].append((end - 1, weight))
@lru_cache(maxsize=None)
def first_wins(first, second, lower=0):
for (nxt, w) in edges[first]:
if w >= lower:
if not first_wins(second, nxt, w):
return True
return False
for i in range(n):
print("".join("BA"[first_wins(i, j)] for j in range(n)))
main()
``` | output | 1 | 21,721 | 19 | 43,443 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter.
<image>
Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.
Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?
You have to determine the winner of the game for all initial positions of the marbles.
Input
The first line of input contains two integers n and m (2 ≤ n ≤ 100, <image>).
The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.
Output
Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise.
Examples
Input
4 4
1 2 b
1 3 a
2 4 c
3 4 b
Output
BAAA
ABAA
BBBA
BBBB
Input
5 8
5 3 h
1 2 c
3 1 c
3 2 r
5 1 r
4 3 z
5 4 r
5 2 h
Output
BABBB
BBBBB
AABBB
AAABA
AAAAB
Note
Here's the graph in the first sample test case:
<image>
Here's the graph in the second sample test case:
<image> | instruction | 0 | 21,722 | 19 | 43,444 |
Tags: dfs and similar, dp, games, graphs
Correct Solution:
```
# int(input())
# [int(i) for i in input().split()]
import sys
sys.setrecursionlimit(20000)
def go(v,w,last):
if game[v][w][last] >= 0: return(game[v][w][last])
flag = 0
move = 0
for p in edges_out[v]:
if p[1] >= last:
move = 1
if not go(w,p[0],p[1]):
flag = 1
break
if not move or not flag:
game[v][w][last] = 0
return(0)
else:
game[v][w][last] = 1
return(1)
n,m = [int(i) for i in input().split()]
edges_in = []
edges_out = []
for i in range(n):
edges_in.append([])
edges_out.append([])
for i in range(m):
s1,s2,s3 = input().split()
v = int(s1)-1
w = int(s2)-1
weight = ord(s3[0]) - ord('a') + 1
edges_out[v].append((w,weight))
edges_in[w].append((v,weight))
game = []
for i in range(n):
tmp1 = []
for j in range(n):
tmp2 = []
for c in range(27):
tmp2.append(-1)
tmp1.append(tmp2)
game.append(tmp1)
##for v in range(n):
## for w in range(n):
## for last in range(27):
## go(v,w,last)
for v in range(n):
s = ''
for w in range(n):
if go(v,w,0): s = s + 'A'
else: s = s + 'B'
print(s)
``` | output | 1 | 21,722 | 19 | 43,445 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter.
<image>
Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.
Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?
You have to determine the winner of the game for all initial positions of the marbles.
Input
The first line of input contains two integers n and m (2 ≤ n ≤ 100, <image>).
The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.
Output
Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise.
Examples
Input
4 4
1 2 b
1 3 a
2 4 c
3 4 b
Output
BAAA
ABAA
BBBA
BBBB
Input
5 8
5 3 h
1 2 c
3 1 c
3 2 r
5 1 r
4 3 z
5 4 r
5 2 h
Output
BABBB
BBBBB
AABBB
AAABA
AAAAB
Note
Here's the graph in the first sample test case:
<image>
Here's the graph in the second sample test case:
<image> | instruction | 0 | 21,723 | 19 | 43,446 |
Tags: dfs and similar, dp, games, graphs
Correct Solution:
```
from functools import lru_cache
def readline(): return list(map(int, input().split()))
def main():
n, m = readline()
edges = [list() for __ in range(n)]
for __ in range(m):
tokens = input().split()
begin, end = map(int, tokens[:2])
weight = ord(tokens[2])
edges[begin - 1].append((end - 1, weight))
@lru_cache(maxsize=None)
def first_wins(first, second, lower=0):
for (nxt, w) in edges[first]:
if w >= lower:
if not first_wins(second, nxt, w):
return True
return False
for i in range(n):
print("".join("BA"[first_wins(i, j)] for j in range(n)))
main()
``` | output | 1 | 21,723 | 19 | 43,447 |
Provide a correct Python 3 solution for this coding contest problem.
There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.
You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:
* 1. Choose positive integers A and B. Your score is initially 0.
* 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 4. Go back to step 2.
You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?
Constraints
* 3 \leq N \leq 10^5
* -10^9 \leq s_i \leq 10^9
* s_0=s_{N-1}=0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
s_0 s_1 ...... s_{N-1}
Output
Print the score obtained by the optimal choice of A and B.
Examples
Input
5
0 2 5 1 0
Output
3
Input
6
0 10 -7 -4 -13 0
Output
0
Input
11
0 -4 0 -99 31 14 -15 -39 43 18 0
Output
59 | instruction | 0 | 21,820 | 19 | 43,640 |
"Correct Solution:
```
n = int(input())
s = list(map(int, input().split()))
ans = 0
for i in range(1, n):
l = 0
r = n-1
cur = 0
while True:
l += i
r -= i
if l>=n or r<=i or (r%i==0 and r<=l):
break
cur += s[l] + s[r]
ans = max(ans, cur)
print(ans)
``` | output | 1 | 21,820 | 19 | 43,641 |
Provide a correct Python 3 solution for this coding contest problem.
There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.
You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:
* 1. Choose positive integers A and B. Your score is initially 0.
* 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 4. Go back to step 2.
You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?
Constraints
* 3 \leq N \leq 10^5
* -10^9 \leq s_i \leq 10^9
* s_0=s_{N-1}=0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
s_0 s_1 ...... s_{N-1}
Output
Print the score obtained by the optimal choice of A and B.
Examples
Input
5
0 2 5 1 0
Output
3
Input
6
0 10 -7 -4 -13 0
Output
0
Input
11
0 -4 0 -99 31 14 -15 -39 43 18 0
Output
59 | instruction | 0 | 21,821 | 19 | 43,642 |
"Correct Solution:
```
n=int(input())
S=tuple(map(int,input().split()))
ans=0
for c in range(1,n-1):
k=1
tmp=0
while c*k<n-1:
a=n-1-k*c
if a<=c or (a<=k*c and a%c==0):
break
tmp+=S[n-1-k*c]+S[k*c]
k+=1
ans=max(ans,tmp)
print(ans)
``` | output | 1 | 21,821 | 19 | 43,643 |
Provide a correct Python 3 solution for this coding contest problem.
There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.
You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:
* 1. Choose positive integers A and B. Your score is initially 0.
* 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 4. Go back to step 2.
You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?
Constraints
* 3 \leq N \leq 10^5
* -10^9 \leq s_i \leq 10^9
* s_0=s_{N-1}=0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
s_0 s_1 ...... s_{N-1}
Output
Print the score obtained by the optimal choice of A and B.
Examples
Input
5
0 2 5 1 0
Output
3
Input
6
0 10 -7 -4 -13 0
Output
0
Input
11
0 -4 0 -99 31 14 -15 -39 43 18 0
Output
59 | instruction | 0 | 21,822 | 19 | 43,644 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
n=int(input())
S=list(map(int,input().split()))
ans=0
for d in range(1,n):
score=0
P=set()
for x in range(n//d):
a=(n-1)-x*d
if(a<=d): break
if((x*d in P) or (n-1-x*d in P)): break
if(x*d==n-1-x*d): break
score+=S[x*d]
score+=S[n-1-x*d]
P.add(x*d)
P.add(n-1-x*d)
ans=max(ans,score)
print(ans)
resolve()
``` | output | 1 | 21,822 | 19 | 43,645 |
Provide a correct Python 3 solution for this coding contest problem.
There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.
You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:
* 1. Choose positive integers A and B. Your score is initially 0.
* 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 4. Go back to step 2.
You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?
Constraints
* 3 \leq N \leq 10^5
* -10^9 \leq s_i \leq 10^9
* s_0=s_{N-1}=0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
s_0 s_1 ...... s_{N-1}
Output
Print the score obtained by the optimal choice of A and B.
Examples
Input
5
0 2 5 1 0
Output
3
Input
6
0 10 -7 -4 -13 0
Output
0
Input
11
0 -4 0 -99 31 14 -15 -39 43 18 0
Output
59 | instruction | 0 | 21,823 | 19 | 43,646 |
"Correct Solution:
```
import sys
def single_input(F): return F.readline().strip("\n")
def line_input(F): return F.readline().strip("\n").split()
def solve():
F = sys.stdin
N = int(single_input(F))
S = [int(s) for s in line_input(F)]
maxpoint = 0
for c in range(1, N//2):
s, l = 0, N-1
point = 0
if (N-1) % c == 0:
while l > s and l > c:
point += S[s] + S[l]
maxpoint = max(point, maxpoint)
s += c
l -= c
else:
while l != s and l > c:
point += S[s] + S[l]
maxpoint = max(point, maxpoint)
s += c
l -= c
print(maxpoint)
return 0
if __name__ == "__main__":
solve()
``` | output | 1 | 21,823 | 19 | 43,647 |
Provide a correct Python 3 solution for this coding contest problem.
There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.
You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:
* 1. Choose positive integers A and B. Your score is initially 0.
* 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 4. Go back to step 2.
You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?
Constraints
* 3 \leq N \leq 10^5
* -10^9 \leq s_i \leq 10^9
* s_0=s_{N-1}=0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
s_0 s_1 ...... s_{N-1}
Output
Print the score obtained by the optimal choice of A and B.
Examples
Input
5
0 2 5 1 0
Output
3
Input
6
0 10 -7 -4 -13 0
Output
0
Input
11
0 -4 0 -99 31 14 -15 -39 43 18 0
Output
59 | instruction | 0 | 21,824 | 19 | 43,648 |
"Correct Solution:
```
N = int(input())
S = list(map(int,input().split()))
ans = 0
for d in range(1,N):
max_now = 0
if (N-1) % d == 0:
now = 0
i, j = 0, N-1
while i < j:
now += S[i] + S[j]
max_now = max(now, max_now)
i += d
j -= d
else:
now = 0
i, j = 0, N-1
while i < N-1 and j > d:
now += S[i] + S[j]
max_now = max(now, max_now)
i += d
j -= d
ans = max(max_now, ans)
print(ans)
``` | output | 1 | 21,824 | 19 | 43,649 |
Provide a correct Python 3 solution for this coding contest problem.
There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.
You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:
* 1. Choose positive integers A and B. Your score is initially 0.
* 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 4. Go back to step 2.
You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?
Constraints
* 3 \leq N \leq 10^5
* -10^9 \leq s_i \leq 10^9
* s_0=s_{N-1}=0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
s_0 s_1 ...... s_{N-1}
Output
Print the score obtained by the optimal choice of A and B.
Examples
Input
5
0 2 5 1 0
Output
3
Input
6
0 10 -7 -4 -13 0
Output
0
Input
11
0 -4 0 -99 31 14 -15 -39 43 18 0
Output
59 | instruction | 0 | 21,825 | 19 | 43,650 |
"Correct Solution:
```
N = int(input())
s = [int(i) for i in input().split()]
ans = 0
for i in range(1, N-2):
p = 0
a = N-1
k = 0
while True:
k += 1
a -= i
b = a-i
if b>0 and a!= N-1-a and a!=N-1-a-i:
p += (s[a] + s[N-1-a])
ans = max(ans, p)
else:
break
print(ans)
``` | output | 1 | 21,825 | 19 | 43,651 |
Provide a correct Python 3 solution for this coding contest problem.
There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.
You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:
* 1. Choose positive integers A and B. Your score is initially 0.
* 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
* If y = N-1, the game ends.
* If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
* If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
* 4. Go back to step 2.
You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?
Constraints
* 3 \leq N \leq 10^5
* -10^9 \leq s_i \leq 10^9
* s_0=s_{N-1}=0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
s_0 s_1 ...... s_{N-1}
Output
Print the score obtained by the optimal choice of A and B.
Examples
Input
5
0 2 5 1 0
Output
3
Input
6
0 10 -7 -4 -13 0
Output
0
Input
11
0 -4 0 -99 31 14 -15 -39 43 18 0
Output
59 | instruction | 0 | 21,826 | 19 | 43,652 |
"Correct Solution:
```
n = int(input())
s = list(map(int, input().split()))
MINUS_INF = -float("inf")
ans = 0
for c in range(1, n):
k = 1
tmp_ans = 0
while k * c < n - 1:
a = (n - 1) - k * c
b = a - c
if a <= b or b <= 0:
tmp_ans = MINUS_INF
if a % c == 0 and a // c <= k:
tmp_ans = MINUS_INF
tmp_ans += s[n-1-k*c] + s[k*c]
ans = max(ans, tmp_ans)
k += 1
print(ans)
``` | output | 1 | 21,826 | 19 | 43,653 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.