message stringlengths 2 43.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 853 107k | cluster float64 24 24 | __index_level_0__ int64 1.71k 214k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy. | instruction | 0 | 14,231 | 24 | 28,462 |
Tags: games, greedy, math
Correct Solution:
```
n = int(input())
a = input()
sum1 = 0
for i in range(n//2):
if(a[i] == '?'):
sum1 += 4.5
else:
sum1 += int(a[i])
for i in range(n//2, n):
if(a[i] == '?'):
sum1 -= 4.5
else:
sum1 -= int(a[i])
if(sum1 == 0):
print('Bicarp')
else:
print('Monocarp')
``` | output | 1 | 14,231 | 24 | 28,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy. | instruction | 0 | 14,232 | 24 | 28,464 |
Tags: games, greedy, math
Correct Solution:
```
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
Date:09/06/2020
'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def solve():
n=ii()
s=si()
lsum,rsum=0,0
lcnt,rcnt=0,0
for i in range(n//2):
if s[i]=='?':
lcnt+=1
else:
lsum+=ord(s[i])-48
for i in range(n//2,n):
if s[i]=='?':
rcnt+=1
else:
rsum+=ord(s[i])-48
maxr1=rsum+ceil(rcnt/2)*9
maxr2=lsum+ceil(lcnt/2)*9
# print(maxr1,maxr2)
if(maxr2==maxr1):
print("Bicarp")
else:
print("Monocarp")
if __name__ =="__main__":
solve()
``` | output | 1 | 14,232 | 24 | 28,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy. | instruction | 0 | 14,233 | 24 | 28,466 |
Tags: games, greedy, math
Correct Solution:
```
n = int(input())
s = input()
lsum = lcount = rsum = rcount = 0
for ch in s[:n//2]:
if ch == '?':
lcount += 1
else:
lsum += int(ch)
for ch in s[n//2:]:
if ch == '?':
rcount += 1
else:
rsum += int(ch)
if lcount > rcount:
delta = rsum - lsum
count = lcount - rcount
else:
delta = lsum - rsum
count = rcount - lcount
if delta == (count//2)*9:
print('Bicarp')
else:
print('Monocarp')
``` | output | 1 | 14,233 | 24 | 28,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy. | instruction | 0 | 14,234 | 24 | 28,468 |
Tags: games, greedy, math
Correct Solution:
```
#585_D
n = int(input())
ln = list(input())
sm1 = 0
sm2 = 0
qs1 = 0
qs2 = 0
for i in range(0, n // 2):
if ln[i] != "?":
qs1 += 1
sm1 += int(ln[i])
if ln[n // 2 + i] != "?":
qs2 += 1
sm2 += int(ln[n // 2 + i])
qs1 = n // 2 - qs1
qs2 = n // 2 - qs2
qs = qs1 + qs2
m = False
if not qs:
if sm1 != sm2:
m = True
mx = sm1 + min(qs1, qs // 2) * 9
lft = min(qs2, qs2 - ((qs // 2) - qs1))
if mx > lft * 9 + sm2:
m = True
mx = sm2 + min(qs2, qs // 2) * 9
lft = min(qs1, qs1 - ((qs // 2) - qs2))
if mx > lft * 9 + sm1:
m = True
if m:
print("Monocarp")
else:
print("Bicarp")
``` | output | 1 | 14,234 | 24 | 28,469 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy. | instruction | 0 | 14,235 | 24 | 28,470 |
Tags: games, greedy, math
Correct Solution:
```
import sys
# sys.setrecursionlimit(10**6)
from sys import stdin, stdout
import bisect #c++ upperbound
import math
import heapq
def modinv(n,p):
return pow(n,p-2,p)
def cin():
return map(int,sin().split())
def ain(): #takes array as input
return list(map(int,sin().split()))
def sin():
return input()
def inin():
return int(input())
import math
def Divisors(n) :
l = []
for i in range(1, int(math.sqrt(n) + 1)) :
if (n % i == 0) :
if (n // i == i) :
l.append(i)
else :
l.append(i)
l.append(n//i)
return l
import statistics
"""*******************************************************"""
def main():
n=inin()
s=sin()
x=y=j=k=0
for i in range(n):
if(i<n//2):
if(s[i]=="?"):
x+=1
else:
j+=int(s[i])
else:
if(s[i]=="?"):
y+=1
else:
k+=int(s[i])
p=x+y
if(p%2==1):
print("Monocarp")
else:
if(j==k and x==y):
print("Bicarp")
elif((j-k)*(x-y)>=0):
print("Monocarp")
else:
f=abs(j-k)
g=abs(x-y)//2
if(f/g==9):
print("Bicarp")
else:
print("Monocarp")
######## Python 2 and 3 footer by Pajenegod and c1729
# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.
# So on cf, use PyPy2 for best string performance.
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
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')
# Cout implemented in Python
import sys
class ostream:
def __lshift__(self,a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero = 0):
conv = ord if py2 else lambda x:x
A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
try:
while True:
if s[i] >= b'0' [0]:
numb = 10 * numb + conv(s[i]) - 48
elif s[i] == b'-' [0]: sign = -1
elif s[i] != b'\r' [0]:
A.append(sign*numb)
numb = zero; sign = 1
i += 1
except:pass
if s and s[-1] >= b'0' [0]:
A.append(sign*numb)
return A
if __name__== "__main__":
main()
``` | output | 1 | 14,235 | 24 | 28,471 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy.
Submitted Solution:
```
n = int(input())
s = input()
s1 = s[:n//2]
s2 = s[n//2:]
sum1 = sum(map(int, filter(str.isdigit, s1)))
sum2 = sum(map(int, filter(str.isdigit, s2)))
free1 = s1.count("?")
free2 = s2.count("?")
ans = ""
if free1 == free2:
if sum1 == sum2:
ans = "Bicarp"
else:
ans = "Monocarp"
else:
if sum1 > sum2:
free1, free2 = free2, free1
sum1, sum2 = sum2, sum1
if free1 <= free2:
ans = "Monocarp"
else:
if (free1 - free2) // 2 * 9 == sum2 - sum1:
ans = "Bicarp"
else:
ans = "Monocarp"
print(ans)
``` | instruction | 0 | 14,237 | 24 | 28,474 |
Yes | output | 1 | 14,237 | 24 | 28,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy.
Submitted Solution:
```
n=int(input())
s=input()
s1,s2,c1,c2,ans=0,0,0,0,0
for i in range(n//2):
if s[i]=='?':
c1+=1
else:
s1+=int(s[i])
for i in range(n//2,n):
if s[i]=='?':
c2+=1
else:
s2+=int(s[i])
if s2<=s1 and s2+9*c2>=s1+9*c1:
print('Bicarp')
else:
print('Monocarp')
``` | instruction | 0 | 14,240 | 24 | 28,480 |
No | output | 1 | 14,240 | 24 | 28,481 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy.
Submitted Solution:
```
n=int(input())
arr=input()
lsum=0
rsum=0
lq=0
rq=0
for i in range(n//2):
if(arr[i]=='?'):
lq+=1
else:
lsum+=int(arr[i])
for i in range(n//2,n):
if(arr[i]=='?'):
rq+=1
else:
rsum+=int(arr[i])
if(lsum==rsum and lq==rq):
print("Bicarp")
elif(lq+rq%2==1):
print("Monocarp")
elif(lsum-rsum==9 and rq>lq):
print("Bicarp")
elif(rsum-lsum==9 and lq>rq):
print("Bicarp")
else:
print("Monocarp")
``` | instruction | 0 | 14,242 | 24 | 28,484 |
No | output | 1 | 14,242 | 24 | 28,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.
Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.
Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.
If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.
Input
The first line contains one even integer n (2 β€ n β€ 2 β
10^{5}) β the number of digits in the ticket.
The second line contains a string of n digits and "?" characters β the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.
Output
If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).
Examples
Input
4
0523
Output
Bicarp
Input
2
??
Output
Bicarp
Input
8
?054??0?
Output
Bicarp
Input
6
???00?
Output
Monocarp
Note
Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.
In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy.
Submitted Solution:
```
n=int(input())
s=input()
p=int(n/2)
s1=s[:p]
s2=s[p:]
k1=s1.count("?")
print(k1)
k2=s2.count("?")
print(k2)
if (k1==-1 and k2==-1) or (k1==k2):
k1=0
k2=0
for i in range(p):
if (s1[i]!="?"):
k1=int(s1[i])+k1
if (s2[i]!="?"):
k2=int(s2[i])+k2
if (k1==k2):
print("Bicarp")
else:
print("Monocarp")
elif (k1<k2):
print("Bicarp")
else:
print("Monocarp")
``` | instruction | 0 | 14,243 | 24 | 28,486 |
No | output | 1 | 14,243 | 24 | 28,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000. | instruction | 0 | 15,058 | 24 | 30,116 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n,d = map(int,input().split())
ai = list(map(int,input().split()))
arr = sorted(ai,reverse=True)[:d]
s = sum(arr)
lst = []
last = -1
i = 0
while len(arr) > 0:
if ai[i] in arr:
lst.append(i-last)
last = i
arr.remove(ai[i])
i += 1
lst[-1] += len(ai) - i
print(s)
print(*lst)
``` | output | 1 | 15,058 | 24 | 30,117 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000. | instruction | 0 | 15,059 | 24 | 30,118 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n,k=input().split()
n=int(n)
k=int(k)
list1=[int(x) for x in input().split()]
index=[]
maxis=[]
for i in range(k):
temp=max(list1)
maxis.append(temp)
temp2=list1.index(temp)
index.append(temp2)
list1[temp2]=-1
print(sum(maxis))
index.sort()
counter=1
flag=0
for i in range(len(list1)):
if flag==k-1:
print(n-i)
break
if i in index:
print(counter,end=" ")
flag+=1
counter=1
else:
counter+=1
``` | output | 1 | 15,059 | 24 | 30,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000. | instruction | 0 | 15,060 | 24 | 30,120 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n, days = list(map(int,input().split()))
diff = list(map(int,input().split()))
index = []
for i in range(n):
index.append([diff[i], i])
index.sort(reverse = True)
temp = []
sum = 0
for i in range(days):
sum += index[i][0]
temp.append(index[i][1])
print(sum)
temp.sort()
temp[-1] = n - 1
j = -1
for i in temp:
print(i - j,end = " ")
j = i
``` | output | 1 | 15,060 | 24 | 30,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000. | instruction | 0 | 15,061 | 24 | 30,122 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n, k = map(int, input().split())
nth_p = list(map(int, input().split()))
sor = sorted(nth_p)
ods = []
count = od_max = tp_co = 0
for i in range(n-1, n-k-1, -1):
count += sor[i]
i_n = nth_p.index(sor[i])
ods.append(i_n)
nth_p[i_n] = -1
print(count)
ods.sort()
ods = [-1]+ods
for i in range(1,len(ods)-1):
print(ods[i]-ods[i-1], end=' ')
print(n-ods[-2]-1)
``` | output | 1 | 15,061 | 24 | 30,123 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000. | instruction | 0 | 15,062 | 24 | 30,124 |
Tags: greedy, implementation, sortings
Correct Solution:
```
def main():
n, m = map(int, input().split())
l = list(map(int, input().split()))
f = l.__getitem__
idx = sorted(sorted(range(n), key=f)[-m:])
print(sum(map(f, idx)))
idx[0] = 0
print(*[b - a for a, b in zip(idx, idx[1:] + [n])])
if __name__ == '__main__':
main()
``` | output | 1 | 15,062 | 24 | 30,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000. | instruction | 0 | 15,063 | 24 | 30,126 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
if k >= 2:
cell_max = []
cell_index = [-1]
for i in range(k):
max1 = 0
index_max1 = 0
for j in range(n):
if a[j] > max1:
max1 = a[j]
index_max1 = j
a[index_max1] = 0
cell_max.append(max1)
cell_index.append(index_max1)
print(sum(cell_max))
cell_index = sorted(cell_index)
ans = []
for i in range(k - 1):
ans.append(cell_index[i + 1] - cell_index[i])
ans.append(n - cell_index[-2] - 1)
print(" ".join(map(str, ans)))
else:
print(max(a))
print(n)
``` | output | 1 | 15,063 | 24 | 30,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000. | instruction | 0 | 15,064 | 24 | 30,128 |
Tags: greedy, implementation, sortings
Correct Solution:
```
import sys
sys.setrecursionlimit(10000000)
mod = 10 ** 9 + 7
digl=['0','1','2','3','4','5','6','7','8','9']
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n, k=f()
l=sorted(enumerate(il()), key=lambda x: x[1], reverse=True)
l2=sorted(l[:k])
sm=sum(y for x, y in l2)
print(sm)
l2=[x for x, y in l2]
l2[0]=0
l2+=[n]
print(*(y-x for x,y in zip(l2,l2[1:])))
``` | output | 1 | 15,064 | 24 | 30,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000. | instruction | 0 | 15,065 | 24 | 30,130 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n,m=map(int,input().split())
s=list(map(int,input().split()))
s1=[]
s1=[]
for i in range(n):
s1.append([s[i],i])
s1.sort()
s1=s1[::-1]
s2=[-1]
p=0
for i in range(m):
p+=s1[i][0]
s2.append(s1[i][1])
print(p)
s2.sort()
s2[-1]=(n-1)
for i in range(1,len(s2)):
print(s2[i]-s2[i-1],end=" ")
``` | output | 1 | 15,065 | 24 | 30,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
need = sorted(a, reverse=True)
ind = list()
for el in need[:k]:
ind.append(a.index(el))
a[a.index(el)] = 0
ind.sort()
print(sum(need[:k]))
ind[-1] = n - 1
ind.insert(0, -1)
ans = [el - ind[i] for i, el in enumerate(ind[1:])]
print(' '.join(map(str, ans)))
``` | instruction | 0 | 15,066 | 24 | 30,132 |
Yes | output | 1 | 15,066 | 24 | 30,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000.
Submitted Solution:
```
#!/usr/bin/env python3
# B. Π’ΡΠ΅Π½ΠΈΡΠΎΠ²ΠΊΠ° ΠΠΎΠ»ΠΈΠΊΠ°ΡΠΏΠ°
n, k = map(int, input().split())
a = list(map(int, input().split()))
a2 = []
for i in range(n):
a2.append((i, a[i]))
a_max = sorted(a2, key=lambda x: (x[1]))[n - k:]
a_max.sort(key=lambda x: (x[0]))
summa = 0
for ai in a_max:
summa += ai[1]
print(summa)
if k > 2:
print(a_max[0][0] + 1, end=' ')
for i in range(1, k-1, 1):
print(a_max[i][0] - a_max[i-1][0], end=' ')
print(n - a_max[-2][0] - 1)
elif k == 2:
print(a_max[0][0] + 1, n - a_max[0][0] - 1)
else:
print(n)
``` | instruction | 0 | 15,067 | 24 | 30,134 |
Yes | output | 1 | 15,067 | 24 | 30,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000.
Submitted Solution:
```
#!/usr/bin/env python3
# encoding: utf-8
#----------
# Constants
#----------
#----------
# Functions
#----------
# The function that solves the task
def calc(n, k, a):
b = list(enumerate(a))
b.sort(key=lambda x: x[1], reverse=True)
b = b[:k]
b.sort()
#print('b=', str(b))
s = 0
last = 0
ln = list()
for cut in b:
index, item = cut
s += item
ln.append(index-last)
last = index
ln[0] += 1
ln[-1] += len(a) - last - 1
return s, ln
# Reads a string from stdin, splits it by space chars, converts each
# substring to int, adds it to a list and returns the list as a result.
def get_ints():
return [ int(n) for n in input().split() ]
# Reads a string from stdin, splits it by space chars, converts each substring
# to floating point number, adds it to a list and returns the list as a result.
def get_floats():
return [ float(n) for n in input().split() ]
#----------
# Execution start point
#----------
if __name__ == "__main__":
a = get_ints()
assert len(a) == 2
n, k = a[0], a[1]
a = get_ints()
assert len(a) == n
res, ln = calc(n, k, a)
print(res)
print(' '.join([str(i) for i in ln]))
``` | instruction | 0 | 15,068 | 24 | 30,136 |
Yes | output | 1 | 15,068 | 24 | 30,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000.
Submitted Solution:
```
n, k = map(int, input().split())
problems = list(map(int, input().split()))
indices = {}
for x in range(n):
p = problems[x]
if p not in indices:
indices[p] = []
indices[p].append(x)
problems = sorted(problems)[::-1]
days = []
profit = 0
for x in range(k):
p = problems[x]
days.append(indices[p][0])
del indices[p][0]
profit += problems[x]
days = sorted(days)
for x in range(1, k - 1):
days[x] = days[x + 1] - days[x]
days[-1] = n - days[-1]
days[0] = n - sum(days[1:])
print(profit)
print(" ".join(map(str, days)))
``` | instruction | 0 | 15,069 | 24 | 30,138 |
Yes | output | 1 | 15,069 | 24 | 30,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000.
Submitted Solution:
```
n,k=map(int ,input().split())
l=input()
l=l.split()
l1=[]
sum=0
for i in l:
i=int(i)
l1.append(i)
sum+=i
div=sum//k
c=0
c2=0
hold=0
l=[]
while c < n:
#print('l=',l1[c])
hold+=l1[c]
# print('hold = ', hold)
if hold < div:
c2+=1
elif hold >= div :
# print('c2=',c2)
# print('Hold2=',hold)
hold=0
c2+=1
l.append(c2)
c2=0
c+=1
st=''
for i in l:
st+=str(i)
st+=' '
l1.sort()
l1.reverse()
hold=0
for i in l1[0:len(l)]:
hold+=i
print(hold)
print(st)
``` | instruction | 0 | 15,070 | 24 | 30,140 |
No | output | 1 | 15,070 | 24 | 30,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000.
Submitted Solution:
```
import sys
inp = sys.stdin.readlines(); ii = 0
out = []
n, k = map(int, inp[ii].split())
ii += 1
probs = [int(z) for z in inp[ii].split()]
ii += 1
probsorted = sorted(probs)
maxindexes = []
for maxneeded in range(k):
maxindexes.append(probs.index(probsorted[-1 - maxneeded]))
maxindexes.sort()
maxindexes.append(n)
res = []
for x in range(1, k+1):
res.append(maxindexes[x] - maxindexes[x-1])
sys.stdout.write(str(res))
``` | instruction | 0 | 15,071 | 24 | 30,142 |
No | output | 1 | 15,071 | 24 | 30,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000.
Submitted Solution:
```
MIS = lambda: list(map(int,input().split()))
n, k = MIS()
a = MIS()
b=[]
if k==1:
print(1)
print(sum(a))
else:
if n%k!=0:
for i in range(0,k-n%k):
a.append(0)
b.append(n%k)
s=list(zip(*[iter(a)]*k))
sm=0
for i in s:
sm+=max(i)
b.append(k)
b.remove(k)
print(sm)
for i in b:
print(i,end=" ")
``` | instruction | 0 | 15,072 | 24 | 30,144 |
No | output | 1 | 15,072 | 24 | 30,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.
Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.
The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is max_{l β€ i β€ r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.
You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.
For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2000) β the number of problems and the number of days, respectively.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2000) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
Output
In the first line of the output print the maximum possible total profit.
In the second line print exactly k positive integers t_1, t_2, ..., t_k (t_1 + t_2 + ... + t_k must equal n), where t_j means the number of problems Polycarp will solve during the j-th day in order to achieve the maximum possible total profit of his practice.
If there are many possible answers, you may print any of them.
Examples
Input
8 3
5 4 2 6 5 1 9 2
Output
20
3 2 3
Input
5 1
1 1 1 1 1
Output
1
5
Input
4 2
1 2000 2000 2
Output
4000
2 2
Note
The first example is described in the problem statement.
In the second example there is only one possible distribution.
In the third example the best answer is to distribute problems in the following way: [1, 2000], [2000, 2]. The total profit of this distribution is 2000 + 2000 = 4000.
Submitted Solution:
```
'''https://codeforces.com/problemset/problem/1006/B'''
from typing import List, Tuple
def solv(arr: List[int], days: int) -> Tuple[int, List[int]]:
if days == 1:
return (max(arr), [len(arr)])
permutation = [(arr[i], i) for i in range(len(arr))]
permutation.sort(reverse=True, key=lambda x: x[0])
breaks = [permutation[i][1] for i in range(days)]
breaks.sort()
total_prof = sum(map(lambda x: x[0], permutation[:days]))
daily_probs = [0] * days
daily_probs[0] = breaks[0] + 1
tot = 0
for i in range(1, len(breaks)):
daily_probs[i] = breaks[i] - daily_probs[i - 1] + 1
tot += daily_probs[i - 1]
daily_probs[-1] = len(arr) - tot
return (total_prof, daily_probs)
_, days = input().split()
arr_str = input()
arr = [int(v) for v in arr_str.split()]
total, daily = solv(arr, int(days))
print(total)
print(' '.join(map(str, daily)))
``` | instruction | 0 | 15,073 | 24 | 30,146 |
No | output | 1 | 15,073 | 24 | 30,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
Submitted Solution:
```
tc = int(input())
while tc:
tc -= 1
s = input().strip()
# print(s)
adj = [ [] for i in range(27)]
for ind in range(1, len(s)):
# print(ord(s[ind - 1])-ord('a'))
adj[ord(s[ind]) - ord('a')].append(ord(s[ind-1]) - ord('a'))
adj[ord(s[ind - 1]) - ord('a')].append(ord(s[ind]) - ord('a'))
flag = 0
# print(adj)
for i in range(26):
adj[i] = list(set(adj[i]))
if len(adj[i]) > 2:
print("NO")
flag = 1
break
if flag:
continue
res = []
fl = [0] * 27
for i in range( 26):
if not fl[i] and len(adj[i]) < 2:
st = i
while not fl[st]:
fl[st] = 1
res.append(st)
for j in range(len(adj[st])):
if not fl[adj[st][j]]:
st = adj[st][j]
break
# print(res)
if len(res) != 26:
print("NO")
else:
print("YES")
ans = ""
for i in range(26):
ch = res[i] + ord('a')
ans += chr(ch)
print(ans)
``` | instruction | 0 | 15,206 | 24 | 30,412 |
Yes | output | 1 | 15,206 | 24 | 30,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
Submitted Solution:
```
import string
def cal(s):
for a, b in zip(s[:-1], s[1:]):
if a == b:
return 'NO'
ans = s[0]
i = 0
found = {s[0]}
for a, b in zip(s[:-1], s[1:]):
if b in found:
if i + 1 < len(ans) and ans[i + 1] == b:
i += 1
elif i - 1 >= 0 and ans[i - 1] == b:
i -= 1
else:
return 'NO'
else:
if i == len(ans) - 1:
ans += b
i += 1
elif i == 0:
ans = b + ans
else:
return 'NO'
found.add(b)
return 'YES\n' + ans + str(''.join(set(string.ascii_lowercase) - found))
T = int(input())
for _ in range(T):
s = input()
print(cal(s))
``` | instruction | 0 | 15,207 | 24 | 30,414 |
Yes | output | 1 | 15,207 | 24 | 30,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
Submitted Solution:
```
from math import *
t=int(input())
dd=[chr(97+i) for i in range(26)]
while(t):
t-=1
s=input()
if(len(s)==1):
print("YES")
for i in dd:
print(i,end="")
print()
continue
re=[s[0],s[1]]
p=0
sr=set(s[:2])
flag=0
pr=1
for i in s[2:]:
c=0
try:
if(re[pr-1]==i and pr-1!=-1):
pr=pr-1
continue
else:
c+=1
except:
c+=1
try:
if(re[pr+1]==i):
pr=pr+1
continue
else:
c+=1
except:
c+=1
if(c==2):
if(i in sr or (pr>0 and pr<len(re)-1)):
flag=3
break
if(pr==0):
re.insert(0,i)
pr=0
else:
re.insert(pr+1,i)
pr+=1
sr.add(i)
else:
continue
if(flag==3):
print("NO")
else:
print("YES")
for i in re:
print(i,end="")
for i in dd:
if(i not in sr):
print(i,end="")
print()
``` | instruction | 0 | 15,208 | 24 | 30,416 |
Yes | output | 1 | 15,208 | 24 | 30,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
Submitted Solution:
```
def dfs(e,v,res,done):
res.append(v)
done.add(v)
for u in e[v]:
if u not in done:
dfs(e,u,res,done)
def go():
s = input()
if len(s)==1:
return True, 'bacdefghijklmnopqrstuvwxyz'
e = {ss:set() for ss in s}
d = {}
for i in range(len(s) - 1):
a, b = s[i], s[i + 1]
if a not in e[b]:
e[a].add(b)
e[b].add(a)
d[a] = d.get(a, 0) + 1
d[b] = d.get(b, 0) + 1
one = False
start = None
two = False
for a, dd in d.items():
if dd > 2:
return False, ''
if dd == 2:
two = True
if dd == 1:
one = True
start = a
if two and not one:
return False, ''
res=[]
done = set()
dfs(e,start,res, done)
alph = set('bacdefghijklmnopqrstuvwxyz')
ans = ''.join(res) + ''.join(alph-set(res))
return True, ans
t = int(input())
for _ in range(t):
res, ans = go()
if res:
print('YES')
print(ans)
else:
print('NO')
``` | instruction | 0 | 15,209 | 24 | 30,418 |
Yes | output | 1 | 15,209 | 24 | 30,419 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
Submitted Solution:
```
import sys
import math
input=sys.stdin.readline
def bo(i):
return ord(i)-ord('a')
t=int(input())
while t>0:
t-=1
s=list(input().rstrip())
a=[[] for i in range(27)]
vis=[[0 for i in range(27)] for j in range(27)]
n=len(s)
for i in range(n-1):
#print(bo(s[i]),bo(s[i+1]))
if vis[bo(s[i])][bo(s[i+1])]==0:
vis[bo(s[i])][bo(s[i+1])]=1
vis[bo(s[i+1])][bo(s[i])]=1
a[bo(s[i])].append(bo(s[i+1]))
a[bo(s[i+1])].append(bo(s[i]))
flag=0
for i in range(n):
if len(a[bo(s[i])])>2:
flag=1
break
if flag==1:
print("NO")
continue
#print(a)
q="abcdefghijklmnopqrstuvwxyz"
v=[0 for i in range(27)]
fi=0
z=-9
uu=""
while fi<5:
#print(z,q[z])
if z==-9:
for i in range(27):
if len(a[i])==1:
#print(q[i],end="")
uu+=q[i]
z=a[i][0]
v[i]=1
break
else:
if len(a[z])==1:
#print(q[z],end="")
uu+=q[z]
v[z]=1
break
elif v[a[z][0]]==1 and v[a[z][1]]==1:
#print(q[z],end="")
uu+=q[z]
v[z]=1
break
#print(q[z],end="")
v[z]=1
for j in a[z]:
if v[j]==0:
#print(q[j],end="")
uu+=q[z]
z=j
v[j]=1
break
if z==-9:
fi=-9
break
for i in q:
#print(i,v[bo(i)])
if v[bo(i)]==0:
uu+=i
if fi==-9:
print("NO")
continue
print("YES")
print(uu)
``` | instruction | 0 | 15,210 | 24 | 30,420 |
No | output | 1 | 15,210 | 24 | 30,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
Submitted Solution:
```
t = int(input())
for i in range(t):
s = input()
f = True
st = set()
answer = ''
pos = 0
n = 0
for l in s:
if l not in st:
st.add(l)
n += 1
if pos == n - 1:
answer = answer + l
pos += 1
elif pos == 1:
answer = l + answer
else:
if pos < n:
if answer[pos] == l:
pos += 1
continue
if pos > 1:
if answer[pos - 2] == l:
pos -= 1
continue
f = False
break
if f:
print('YES')
print(answer, end='')
for l in 'abcdefghijklmnopqrstuvwxyz':
if l not in st:
print(l, end='')
print()
else:
print('NO')
``` | instruction | 0 | 15,211 | 24 | 30,422 |
No | output | 1 | 15,211 | 24 | 30,423 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import defaultdict
t = int(input())
for _ in range(t):
s = input().strip()
d = defaultdict(set)
b = None
for c in s:
if b is None:
b = c
continue
d[c].add(b)
d[b].add(c)
b = c
fns = []
zns = []
r = False
for c in "abcdefghijklmnopqrstuvwxyz":
if len(d[c]) >3:
r = True
break
if len(d[c]) == 1:
fns.append(c)
if not len(d[c]):
zns.append(c)
if r:
print("NO")
continue
if len(fns) != 2:
print("NO")
continue
stack = [fns.pop()]
vs = set()
while stack:
v = stack.pop()
vs.add(v)
zns.append(v)
for u in d[v]:
if u in vs:
continue
stack.append(u)
print("YES")
print("".join(zns))
``` | instruction | 0 | 15,212 | 24 | 30,424 |
No | output | 1 | 15,212 | 24 | 30,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him β his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).
Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T (1 β€ T β€ 1000) β the number of test cases.
Then T lines follow, each containing one string s (1 β€ |s| β€ 200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.
Output
For each test case, do the following:
* if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
* otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters β the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
Submitted Solution:
```
from sys import stdin
from bisect import bisect_left as bl
from bisect import bisect_right as br
def input():
return stdin.readline()[:-1]
def intput():
return int(input())
def sinput():
return input().split()
def intsput():
return map(int, sinput())
class RangedList:
def __init__(self, start, stop, val=0):
self.shift = 0 - start
self.start = start
self.stop = stop
self.list = [val] * (stop - start)
def __setitem__(self, key, value):
self.list[key + self.shift] = value
def __getitem__(self, key):
return self.list[key + self.shift]
def __repr__(self):
return str(self.list)
def __iter__(self):
return iter(self.list)
# Code
alphas = 'abcdefghijklmnopqrstuvwxyz'
t = intput()
for _ in range(t):
s = input()
if len(s) == 1:
print(alphas)
continue
nextto = {}
for alph in alphas:
nextto[alph] = set()
for i in range(len(s)):
for j in (i - 1, i + 1):
if j in range(len(s)):
nextto[s[i]].add(s[j])
if any(len(x) >= 3 for x in nextto.values()):
print("NO")
continue
if not any(len(x) == 1 for x in nextto.values()):
print("NO")
continue
for x in nextto:
if len(nextto[x]) == 1:
start = x
ans = [start]
covered = {}
for x in alphas:
covered[x] = False
covered[start] = True
gotcha = False
while not all(covered.values()):
if len(nextto[ans[-1]]) == 0 or len(nextto[ans[-1]]) == 1 and covered[list(nextto[ans[-1]])[0]]:
for k in covered:
if not covered[k] and len(nextto[k]) < 2:
ans.append(k)
break
else:
print("NO")
gotcha = True
break
else:
for d in nextto[ans[-1]]:
if not covered[d]:
ans.append(d)
covered[ans[-1]] = True
'''
print(ans)
print(covered)
input()
'''
if not gotcha:
print("YES")
print(''.join(ans))
``` | instruction | 0 | 15,213 | 24 | 30,426 |
No | output | 1 | 15,213 | 24 | 30,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. | instruction | 0 | 15,557 | 24 | 31,114 |
Tags: greedy
Correct Solution:
```
from collections import defaultdict
N, M = map(int, input().split())
ori = list(map(int, input().split()))
best = N // M
c = defaultdict(int)
for o in ori:
if 1 <= o <= M:
c[o] += 1
counter = {}
more = set()
less = set()
for i in range(1, M+1):
counter[i] = c[i] - best if i in c else -best
if best < c[i]:
more.add(i)
elif best > c[i]:
less.add(i)
# print("more")
# print(more)
# print("less")
# print(less)
# print(counter)
move = 0
for i in range(N):
if ori[i] not in counter:
if less:
k = next(iter(less))
ori[i] = k
counter[k] += 1
if counter[k] == 0:
less.remove(k)
move += 1
elif ori[i] in more:
if less:
counter[ori[i]] -= 1
if counter[ori[i]] == 0:
more.remove(ori[i])
k = next(iter(less))
ori[i] = k
counter[k] += 1
if counter[k] == 0:
less.remove(k)
move += 1
print(str(best) + " " + str(move))
print(" ".join(map(str, ori)))
``` | output | 1 | 15,557 | 24 | 31,115 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. | instruction | 0 | 15,558 | 24 | 31,116 |
Tags: greedy
Correct Solution:
```
import copy
n, m = map(int, input().split())
a = list(map(int, input().split()))
d = copy.copy(a)
a.sort()
s = n // m
b = [0] * m
cnt = 0
q = 0
for el in a:
if el <= m and b[el - 1] < s:
b[el - 1] += 1
q += 1
ans = (n - q - n % m)
print(s, ans)
a = d
q = copy.copy(b) #
pos = 0
for el in a:
if el <= m and b[el - 1] > 0:
b[el - 1] -= 1
print(el, end = ' ')
else:
while pos < m and q[pos] >= s:
pos += 1
if pos < m:
q[pos] += 1
print(pos + 1, end = ' ')
else:
print(el, end = ' ')
#print(d)
``` | output | 1 | 15,558 | 24 | 31,117 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. | instruction | 0 | 15,559 | 24 | 31,118 |
Tags: greedy
Correct Solution:
```
n,m = (int(x) for x in input().split())
l = [ int(x) for x in input().split() ]
count = [0]*(m+1)
overcount = 0
dielist = []
changelist = []
maxmin = int(n/m)
for ind,i in enumerate(l):
if i <= m:
count[i] += 1
if count[i] > maxmin:
dielist.append(ind)
else:
dielist.append(ind)
changes = 0
for i in range(1,len(count)):
c = count[i]
if c < maxmin:
changes += maxmin - c
for j in range(maxmin - c):
l[dielist[overcount]] = i
overcount += 1
print(maxmin, changes)
print(' '.join([str(x) for x in l]))
``` | output | 1 | 15,559 | 24 | 31,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. | instruction | 0 | 15,560 | 24 | 31,120 |
Tags: greedy
Correct Solution:
```
n,m=[int(item) for item in input().split()]
a=[int(item) for item in input().split()]
mx=n//m
r=n%m
c=0
counts=[a.count(i) for i in range(1,m+1)]
queue=[]
for i in range(1,m+1):
queue=[i]*(mx-counts[i-1])+queue
for i in range(n):
if a[i]>m:
if queue:
curr=queue.pop()
a[i]=curr
counts[a[i]-1]+=1
c+=1
else:
break
for i in range(n):
if not queue:
break
if counts[a[i]-1]>mx:
counts[a[i]-1]-=1
a[i]=queue.pop()
counts[a[i]-1]+=1
c+=1
print(mx,c)
print(" ".join([str(item) for item in a]))
``` | output | 1 | 15,560 | 24 | 31,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. | instruction | 0 | 15,561 | 24 | 31,122 |
Tags: greedy
Correct Solution:
```
from collections import Counter
n, m = map(int, input().split())
nums = list(map(int, input().split()))
cnts = dict(Counter(nums))
for i in range(1, m+1):
if i not in cnts:
cnts[i] = 0
def minner():
return min(cnts.items(), key=lambda x: x[1])
n //= m
res = 0
for i, num in enumerate(nums):
if num > m or cnts[num] > n:
for r in range(1, m+1):
if cnts[r] < n:
cnts[num] -= 1
nums[i] = r
cnts[r] += 1
res += 1
break
print(n, res)
print(' '.join(map(str, nums)))
``` | output | 1 | 15,561 | 24 | 31,123 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. | instruction | 0 | 15,562 | 24 | 31,124 |
Tags: greedy
Correct Solution:
```
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [0] * (m + 1)
low = int(n / m)
changes = 0
for i in a:
if i <= m:
b[i] += 1
bi = 1
for i in range(n):
done = False
while b[bi] >= low:
bi += 1
if bi > m:
done = True
break
if done:
break
if a[i] > m:
changes += 1
a[i] = bi
b[bi] += 1
else:
if b[a[i]] > low:
b[a[i]] -= 1
b[bi] += 1
a[i] = bi
changes += 1
print(low, changes)
print(" ".join([str(i) for i in a]))
``` | output | 1 | 15,562 | 24 | 31,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. | instruction | 0 | 15,563 | 24 | 31,126 |
Tags: greedy
Correct Solution:
```
n, m = map(int, input().split())
a = list(map(int, input().split()))
good = n // m
ans = 0
cnt = [0] * m
ch = [0] * n
for i in range(n):
if 1 <= a[i] <= m:
cnt[a[i] - 1] += 1
if cnt[a[i] - 1] <= good:
ch[i] = 1
for i in range(m):
ans += max(0, good - cnt[i])
print(good, ans)
j = 0
for i in range(n):
if ch[i]:
continue
while j < m and good <= cnt[j]:
j += 1
if j == m:
break
a[i] = j + 1
cnt[j] += 1
print(*a)
``` | output | 1 | 15,563 | 24 | 31,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. | instruction | 0 | 15,564 | 24 | 31,128 |
Tags: greedy
Correct Solution:
```
import collections
n,m = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
ctr = collections.Counter(a)
nummin = n//m
inses = []
for b in range(1,m+1):
if ctr[b] < nummin:
inses.extend([b]*(nummin - ctr[b]))
num_ins = 0
for i in range(n):
b = a[i]
if ctr[b] > nummin or b > m:
if num_ins >= len(inses):
break
a[i] = inses[num_ins]
ctr[b] -= 1
num_ins += 1
print(nummin, len(inses))
print(' '.join(map(str,a)))
``` | output | 1 | 15,564 | 24 | 31,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
Submitted Solution:
```
#!/usr/bin/env python3
from collections import Counter
n, m = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
tgt = n // m
b = Counter(a)
rd = sum(b[x] for x in b if x > m)
r = 0
for i in range(1, m+1):
while rd and b[i] < tgt:
for j in range(n):
if a[j] > m:
b[a[j]] -= 1
b[i] += 1
a[j] = i
rd -= 1
r += 1
break
while b[i] < tgt:
for j in range(n):
if b[a[j]] > tgt:
b[a[j]] -= 1
b[i] += 1
a[j] = i
r += 1
break
print(tgt, r)
print(" ".join(str(x) for x in a))
``` | instruction | 0 | 15,565 | 24 | 31,130 |
Yes | output | 1 | 15,565 | 24 | 31,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
Submitted Solution:
```
#python3
# utf-8
from collections import Counter
songs_nr, fav_groups_nr = (int(x) for x in input().split())
song_idx___group_idx = [int(x) for x in input().split()]
fav_group_idx___songs_nr = {}
for fav_group_idx in range(1, fav_groups_nr + 1):
fav_group_idx___songs_nr[fav_group_idx] = 0
for group_idx in song_idx___group_idx:
if group_idx <= fav_groups_nr:
fav_group_idx___songs_nr[group_idx] += 1
min_max_value = songs_nr // fav_groups_nr
possib_replacements_nr = 0
for song_idx in range(songs_nr):
group_idx = song_idx___group_idx[song_idx]
if group_idx <= fav_groups_nr:
continue
possib_replacements_nr += 1
for fav_group_idx, fav_songs_nr in fav_group_idx___songs_nr.items():
if fav_songs_nr > min_max_value:
possib_replacements_nr += fav_songs_nr - min_max_value
replacements_nr = 0
while True:
curr_group_idx = min(fav_group_idx___songs_nr, key=lambda x: fav_group_idx___songs_nr[x])
if min(fav_group_idx___songs_nr.values()) >= min_max_value:
break
elif replacements_nr == possib_replacements_nr:
break
for song_idx in range(songs_nr):
group_idx = song_idx___group_idx[song_idx]
if group_idx > fav_groups_nr:
replacements_nr += 1
song_idx___group_idx[song_idx] = curr_group_idx
fav_group_idx___songs_nr[curr_group_idx] += 1
break
elif fav_group_idx___songs_nr[group_idx] > min_max_value:
replacements_nr += 1
song_idx___group_idx[song_idx] = curr_group_idx
fav_group_idx___songs_nr[group_idx] -= 1
fav_group_idx___songs_nr[curr_group_idx] += 1
break
print(min_max_value, replacements_nr)
print(*song_idx___group_idx)
``` | instruction | 0 | 15,566 | 24 | 31,132 |
Yes | output | 1 | 15,566 | 24 | 31,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
Submitted Solution:
```
n, m = map(int, input().split())
target = n//m
A = [int(i) for i in input().split()]
B = [0 for _ in range(2005)]
R = B[:]
ch = 0
for i in A:
if i <= m:
B[i] += 1
for i in range(2005):
R[i] = target - B[i]
j = 1
for i in range(n):
if A[i] > m:
while j <= m and R[j] <= 0:
j += 1
if j <= m and R[j] > 0:
ch += 1
R[j] -= 1
A[i] = j
for i in range(n):
if A[i] <= m and R[A[i]] < 0:
while j <= m and R[j] <= 0:
j += 1
if j <= m and R[j] > 0:
R[j] -= 1
R[A[i]] += 1
A[i] = j
ch += 1
print(target, ch)
print(*A)
``` | instruction | 0 | 15,567 | 24 | 31,134 |
Yes | output | 1 | 15,567 | 24 | 31,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
Submitted Solution:
```
n,m = map(int, input().split())
A = dict()
per = list(map(int, input().split()))
cou =[0] * m
for j in range(n):
if per[j] <= m:
cou[per[j]-1] +=1
ans = 0
ans2 = n//m
s = 0
for j in range(n):
num = per[j]
while s <= m-1:
if cou[s] < ans2:
if num > m:
per[j] = s+1
cou[s] +=1
if cou[s] >= ans2:
s +=1
ans +=1
break
else:
if num != s+1 and cou[num-1] > ans2:
cou[num-1] -= 1
ans +=1
cou[s] +=1
per[j] = s+1
if cou[s] >= ans2:
s+=1
break
else:
break
else:
s+=1
else:
break
print(ans2, ans)
print(' '.join(map(str, per)))
``` | instruction | 0 | 15,568 | 24 | 31,136 |
Yes | output | 1 | 15,568 | 24 | 31,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
Submitted Solution:
```
def main():
from sys import stdin, stdout
i = 0
for line in stdin.readlines():
if (i == 0):
n, m, *rest = line.rstrip().split()
n, m = int(n), int(m)
elif (i == 1):
nums = line.rstrip().split()
## list of a_i in str
i += 1
counts = {}
changes = 0
for i in range(1, m+1):
counts[i] = 0
output_playlist = []
i = 0
for num in nums:
i += 1
num = int(num)
if (num <= m):
counts[num] += 1
output_playlist.append(num)
elif (num > m):
min_num = min(counts, key = counts.get)
if (equal_count(counts) and i == len(nums)):
output_playlist.append(num)
else:
output_playlist.append(min_num)
counts[min_num] += 1
changes += 1
max_num = max(counts, key = counts.get)
min_num = min(counts, key = counts.get)
diff = counts[max_num] - counts[min_num]
while (diff > 1):
counts[max_num] -= 1
counts[min_num] += 1
for i in range(len(output_playlist)):
if output_playlist[i] == max_num:
output_playlist[i] = min_num
changes += 1
break
max_num = max(counts, key = counts.get)
min_num = min(counts, key = counts.get)
diff = counts[max_num] - counts[min_num]
stdout.write(str(counts[min_num]) + " " + str(changes) + '\n')
stdout.write(to_string(output_playlist))
def to_string(l):
s = ""
for num in l:
s += str(num) + " "
return s.rstrip()
def equal_count(counts):
l = list(counts.values())
count = l[0]
for i in l:
if (count != i):
return False
return True
if __name__ == "__main__":
main()
``` | instruction | 0 | 15,569 | 24 | 31,138 |
No | output | 1 | 15,569 | 24 | 31,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
Submitted Solution:
```
d=input().split()
n,m=int(d[0]),int(d[1])
d=input().split()
d=[int(x) for x in d]
p=0
di={}
for i in range(1,1+m):
di[i]=0
for i in d:
if i<=m:
di[i]+=1
for i in range(1,1+m):
if di[i]<n//m:
p+=abs(di[i]-n//m)
print(n//m,p)
D=[]
for i in range(1,m+1):
if di[i]<n//m:
D+=[i]*(n//m-di[i])
for i in range(n):
if D!=[]:
if d[i]>m :
d[i]=D.pop(-1)
elif di[d[i]]>(n//m):
d[i]=D.pop(-1)
for i in d:
print(i,end=" ")
``` | instruction | 0 | 15,570 | 24 | 31,140 |
No | output | 1 | 15,570 | 24 | 31,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
Submitted Solution:
```
n, m = map(int, input().split())
a = list(map(int, input().split()))
d = dict()
d2 = dict()
a2 = [0] * n
counter = 0
s = []
ans = n // m
ost = n % m
for i in range(m):
d2[i + 1] = 0
for i in range(n):
if a[i] in d:
d[a[i]] += 1
else:
d[a[i]] = 1
if a[i] <= m and d[a[i]] <= ans:
a2[i] = a[i]
d2[a2[i]] += 1
elif a[i] <= m and d[a[i]] <= ans + ost:
a2[i] = a[i]
d2[a2[i]] += 1
ost -= 1
#print(d2)
playlist = sorted(d2.keys())
for elem in playlist:
if d2[elem] < ans:
s += [elem] * (ans - d2[elem])
if len(s) > 0:
s += [s[-1]] * ost
#print(a2)
#print(s)
j = 0
for i in range(n):
if a2[i] == 0:
counter += 1
a2[i] = s[j]
j += 1
print(ans, counter)
print(*a2)
``` | instruction | 0 | 15,571 | 24 | 31,142 |
No | output | 1 | 15,571 | 24 | 31,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
Input
4 2
1 2 3 2
Output
2 1
1 2 1 2
Input
7 3
1 3 2 2 2 2 1
Output
2 1
1 3 3 2 2 2 1
Input
4 4
1000000000 100 7 1000000000
Output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
Submitted Solution:
```
## necessary imports
import sys
input = sys.stdin.readline
# from math import *
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp
## gcd function
def gcd(a,b):
if a == 0:
return b
return gcd(b%a, a)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return res
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0):
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x:
lo = mid+1
else:
hi = mid
return lo
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e6 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve()
def factoriazation(x):
ret = {};
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1;
x = x//spf[x]
return ret
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()))
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
n, m = int_array(); a = int_array();
ans = n // m; change = 0;
ctr = [0]*3000;
for i in range(n):
if a[i] <= m:
ctr[ a[i] ] += 1;
for i in range(1, m+1):
change += max(0, ans - ctr[i]);
for i in range(n):
if a[i] > m:
for j in range(1, m+1):
if ctr[j] < ans:
ctr[j] += 1;
a[i] = j; break;
else:
if ctr[ a[i] ] <= m:
continue;
else:
for j in range(1, m+1):
if ctr[j] < ans:
ctr[j] += 1;
a[i] = j; break;
print(*(ans, change));
print(*a);
``` | instruction | 0 | 15,572 | 24 | 31,144 |
No | output | 1 | 15,572 | 24 | 31,145 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is an introvert person. In fact he is so much of an introvert that he plays "Monsters and Potions" board game alone. The board of the game is a row of n cells. The cells are numbered from 1 to n from left to right. There are three types of cells: a cell containing a single monster, a cell containing a single potion or a blank cell (it contains neither a monster nor a potion).
Polycarp has m tokens representing heroes fighting monsters, which are initially located in the blank cells s_1, s_2, ..., s_m. Polycarp's task is to choose a single cell (rally point) and one by one move all the heroes into this cell. A rally point can be a cell of any of three types.
After Policarp selects a rally point, he picks a hero and orders him to move directly to the point. Once that hero reaches the point, Polycarp picks another hero and orders him also to go to the point. And so forth, until all the heroes reach the rally point cell. While going to the point, a hero can not deviate from the direct route or take a step back. A hero just moves cell by cell in the direction of the point until he reaches it. It is possible that multiple heroes are simultaneously in the same cell.
Initially the i-th hero has h_i hit points (HP). Monsters also have HP, different monsters might have different HP. And potions also have HP, different potions might have different HP.
If a hero steps into a cell which is blank (i.e. doesn't contain a monster/potion), hero's HP does not change.
If a hero steps into a cell containing a monster, then the hero and the monster fight. If monster's HP is strictly higher than hero's HP, then the monster wins and Polycarp loses the whole game. If hero's HP is greater or equal to monster's HP, then the hero wins and monster's HP is subtracted from hero's HP. I.e. the hero survives if his HP drops to zero, but dies (and Polycarp looses) if his HP becomes negative due to a fight. If a hero wins a fight with a monster, then the monster disappears, and the cell becomes blank.
If a hero steps into a cell containing a potion, then the hero drinks the potion immediately. As a result, potion's HP is added to hero's HP, the potion disappears, and the cell becomes blank.
Obviously, Polycarp wants to win the game. It means that he must choose such rally point and the order in which heroes move, that every hero reaches the rally point and survives. I.e. Polycarp loses if a hero reaches rally point but is killed by a monster at the same time. Polycarp can use any of n cells as a rally point β initially it can contain a monster, a potion, or be a blank cell with or without a hero in it.
Help Polycarp write a program to choose a rally point and the order in which heroes move.
Input
The first line of the input contains two integers n and m (1 β€ n β€ 100; 1 β€ m β€ n) β length of the game board and the number of heroes on it.
The following m lines describe heroes. Each line contains two integers s_i and h_i (1 β€ s_i β€ n; 1 β€ h_i β€ 10^6), where s_i is the initial position and h_i is the initial HP of the i-th hero. It is guaranteed that each cell s_i is blank. It is also guaranteed that all s_i are different.
The following line contains n integers a_1, a_2, ..., a_n (-10^6 β€ a_j β€ 10^6), where a_j describes the i-th cell of the game board:
* a_j=0 means that the i-th cell is blank,
* a_j<0 means that the i-th cell contains monster with positive HP of -a_j,
* a_j>0 means that the i-th cell contains potion with a_j HP.
Output
On the first line of the output print the index of the rally point cell.
On the second line print m integers β the order in which heroes should move to the rally point. Heroes are numbered from 1 to m in the order they are given in the input.
If there are multiple solutions, print any of them.
If it is impossible to find a rally point which can be reached by all heroes, print a single integer -1 in the output.
Examples
Input
8 3
8 2
1 3
4 9
0 3 -5 0 -5 -4 -1 0
Output
6
3 1 2
Input
1 1
1 1
0
Output
1
1
Input
3 2
1 1
3 1
0 -5000 0
Output
-1
Input
8 3
1 15
5 10
8 1
0 -5 -5 -5 0 -5 -5 0
Output
7
2 1 3
Note
The picture illustrates the first example:
<image> | instruction | 0 | 16,860 | 24 | 33,720 |
Tags: brute force, dp, greedy, implementation
Correct Solution:
```
import sys
n, m = tuple(int(x) for x in sys.stdin.readline().split())
heroes = []
for i in range(m):
s, h = tuple(int(x) for x in sys.stdin.readline().split())
heroes.append((s - 1, h, i))
heroes = tuple(sorted(heroes))
a = tuple(int(x) for x in sys.stdin.readline().split())
def move_right(heroes, a):
a = list(a)
order = []
remaining_h = None
for j in range(len(heroes)):
s, h, i = heroes[j]
if remaining_h is not None:
if h > remaining_h:
order.append(remaining_i)
else:
order.append(i)
h = remaining_h
i = remaining_i
is_last_hero = (j == len(heroes) - 1)
target = len(a) - 1 if is_last_hero else heroes[j + 1][0]
while s != target:
s += 1
h += a[s]
a[s] = 0
if h < 0:
order.append(i)
return s - 1, order
if is_last_hero:
order.append(i)
return s, order
remaining_h = h
remaining_i = i
def move_left(heroes, a):
a = list(a)
order = []
remaining_h = None
for j in range(len(heroes) - 1, -1, -1):
s, h, i = heroes[j]
if remaining_h is not None:
if h > remaining_h:
order.append(remaining_i)
else:
order.append(i)
h = remaining_h
i = remaining_i
is_last_hero = (j == 0)
target = 0 if is_last_hero else heroes[j - 1][0]
while s != target:
s -= 1
h += a[s]
a[s] = 0
if h < 0:
order.append(i)
return s + 1, order
if is_last_hero:
order.append(i)
return s, order
remaining_h = h
remaining_i = i
point_right, order_right = move_right(heroes, a)
order_right = list(reversed(order_right))
point_left, order_left = move_left(heroes, a)
order_left = list(reversed(order_left))
if point_right >= point_left - 1:
print(point_right + 1)
order = order_right + [i for i in order_left if i not in order_right]
print(" ".join(str(i + 1) for i in order))
else:
print(-1)
``` | output | 1 | 16,860 | 24 | 33,721 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.