text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Tags: constructive algorithms, greedy, math
Correct Solution:
```
[n,k]=[int(x) for x in input().split()]
m=k*(k+1)/2
limit=n//m
lul="-1"
def nice(x):
global lul
lul=""
sum=0
a=[0]*k
for i in range(1,k):
a[i-1]=str(i*x)
sum+=i*x
a[k-1]=str(n-sum)
print(" ".join(a))
pep=-1
for i in range(1,1000000):
if i*i>n:
break
if i>limit:
break
if n%i>0:
continue
if n//i<=limit:
nice(n//i)
break
pep=i
if lul=="-1" and pep!=-1:
nice(pep)
print(lul)
```
| 107,500 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import math
import sys
array=list(map(int,input().split()))
n = array[0]
k = array[1]
maxi = -1
for i in range(1, int(math.sqrt(n)) + 1):
if n % i != 0:
continue
del1 = int(i)
del2 = int(n / i)
sum1 = del1 * k * (k - 1) / 2
sum2 = del2 * k * (k - 1) / 2
if n - sum1 > (k - 1) * del1:
maxi = max(maxi, del1)
if n - sum2 > (k - 1) * del2:
maxi = max(maxi, del2)
if maxi == -1:
print(-1)
sys.exit()
sum = 0
ans = []
for i in range(1, k):
ans.append(i * maxi)
sum += i * maxi
ans.append(n - sum)
print(" ".join(map(str,ans)))
```
| 107,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
#Library Info(ACL for Python/Pypy) -> https://github.com/not522/ac-library-python
def input():
return sys.stdin.readline().rstrip()
DXY = [(0, -1), (1,0), (0, 1), (-1,0)] #L,D,R,Uの順番
def divisors(n : int):
res = []
for i in range(1,n + 1):
if i * i > n:
break
if n % i == 0:
if i * i == n:
res.append(i)
else:
res.append(n // i)
res.append(i)
res.sort(reverse = True)
return res
def main():
n,k = map(int,input().split())
for d in divisors(n):
if n // d >= k * (k + 1) // 2:
ans = [0]*(k)
tot = n // d
for i in range(k):
if i != k - 1:
ans[i] = (i + 1) * d
tot -= (i + 1)
else:
ans[i] = tot * d
print(*ans)
return 0
print(-1)
return 0
if __name__ == "__main__":
main()
```
| 107,502 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
#import random
from bisect import bisect_left as lb
from collections import deque
#sys.setrecursionlimit(10**8)
from queue import PriorityQueue as pq
from math import *
input_ = lambda: sys.stdin.readline().strip("\r\n")
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab,bc,cd : ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x : print(x)
prinT = lambda x : print(x)
f = lambda : sys.stdout.flush()
inv =lambda x:pow(x,mod-2,mod)
mod = 10**9 + 7
n,k = il()
s = k*(k+1)//2
if (s > n) :
print(-1)
exit(0)
if (s == n) :
for i in range (1,k+1) :
print(i,end=" ")
print()
exit(0)
p = 1
a = []
while (p*p <= n) :
if (n%p) == 0 :
a.append(p)
if (p*p) != n :
a.append(n//p)
p += 1
a.sort()
ans = 0
for d in a :
if (n//d) >= s :
ans = d
else :
break
if (ans) :
for i in range(1,k) :
print(i*ans,end=" ")
n -= i*ans
print(n)
else :
print(-1)
```
| 107,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import math
n, k = map(int, input().split())
if (k*(k+1))/2 > n:
print(-1)
else:
c = int( n/ ((k*(k+1))/2))
a = []
for i in range(1, int( math.sqrt(n) + 1 ) ):
if i*i == n:
a.append(i)
elif n%i == 0:
a.append(i)
a.append(n//i)
a = sorted(a)
s = 0
for i in range(len(a)):
s+=1
if a[i] > c:
break
c = a[ s - 2]
for i in range(1, k):
print(c*i, end= " ")
print(str( int(n - c*(k*(k-1)/2) ) ))
```
| 107,504 |
Provide tags and a correct Python 2 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
def fun(x,n,k):
sm=n-((x*k*(k-1))/2)
return (sm>0 and sm/x>=k)
n,k=inp()
div=0
i=1
while i*i<=n:
if n%i==0:
if fun(i,n,k):
div=max(div,i)
if fun(n/i,n,k):
div=max(div,n/i)
i+=1
if not div:
pr_num(-1)
else:
sm=0
i=div
while i<=div*(k-1):
pr(str(i)+' ')
sm+=i
i+=div
pr(str(n-sm))
```
| 107,505 |
Provide tags and a correct Python 2 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
def fun(x,n,k):
sm=n-((x*k*(k-1))/2)
return (sm>0 and sm/x>=k)
n,k=in_arr()
div=0
i=1
while i*i<=n:
if n%i==0:
if fun(i,n,k):
div=max(div,i)
if fun(n/i,n,k):
div=max(div,n/i)
i+=1
if not div:
pr_num(-1)
else:
sm=0
i=div
while i<=div*(k-1):
pr(str(i)+' ')
sm+=i
i+=div
pr(str(n-sm))
```
| 107,506 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
import math
n, k = map(int, input().split())
def solve(i):
a = i * k * (k + 1) // 2
if(n >= a):
return (n - a)
else:
return -1
i = 1
r = -1
while(i * i <= n):
if(n % i == 0):
if(solve(i) > -1):
r = max(r, i);
if(i * i != n):
if(solve(n / i) > -1):
r = max(r, n / i);
i = i + 1;
if(r == -1):
print(r)
else:
b = []
for i in range(1, k):
b.append(i * r)
b.append(k * r + solve(r))
for i in b:
print(int(i), end=' ')
print()
```
Yes
| 107,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
def factor(n):
rtn = []
p = 2
tmp = n
while p * p <= tmp:
q = 0
while tmp % p == 0:
tmp //= p
q += 1
if 0 < q:
rtn.append((p, q))
p += 1
if 1 < tmp:
rtn.append((tmp, 1))
return rtn
def divs(n):
rtn = [1]
arr = factor(n)
for p, q in arr:
ds = [p**i for i in range(1, q + 1)]
tmp = rtn[:]
for d in ds:
for t in tmp:
rtn.append(d * t)
return list(sorted(rtn))
n, k = map(int, input().split())
ds = divs(n)
l = 0
r = len(ds) - 1
while l + 1 < r:
c = (l + r) // 2
if ds[c] * k * (k + 1) // 2 <= n:
l = c
else:
r = c
if l == 0 and n < k * (k + 1) // 2:
print(-1)
else:
d = ds[l]
ans = [d * (i + 1) for i in range(k)]
ans[-1] += n - sum(ans)
print(' '.join(map(str, ans)))
```
Yes
| 107,508 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
# cook your dish here
import math
import sys
n, k = map(int, input().strip().split())
divlist=[]
rev=[]
for i in range(1, int(math.sqrt(n))+1):
if n%i==0:
divlist.append(i)
rev.append(n//i)
for i in range(len(rev)):
divlist.append(rev[len(rev)-i-1])
if 2*n < k*(k+1):
print(-1)
else:
beta=-1
for i in divlist:
if 2*i>=k*(k+1):
beta = i
break
alpha = n//beta
for i in range(k-1):
sys.stdout.write(str(alpha*(i+1)) + " ")
beta -= (i+1)
print(alpha*beta)
```
Yes
| 107,509 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
import sys
inf = 1 << 30
def solve():
n, k = map(int, input().split())
lim = k * (k + 1) // 2
if n < lim:
print(-1)
return
d_max = 1
for d in range(1, n + 1):
if d*d > n:
break
if n % d != 0:
continue
q = n // d
if d >= lim:
d_max = q
break
elif q >= lim:
d_max = d
else:
break
ans = []
j = 1
for i in range(k - 1):
ans.append(d_max * j)
j += 1
ans.append(n - sum(ans))
print(*ans)
if __name__ == '__main__':
solve()
```
Yes
| 107,510 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
n,a= map(int,input().split())
if((a*(a+1)/2) > n):
print(-1)
exit(0)
c = a*(a+1)/2;
res = 1
q = int((n+10)**(1/2))
for i in range(1,q):
if(n%i): continue
if(c*i > n): continue
i1 = n/i
if(i1*c <= n): res = max(res,i1)
res=max(res,i)
res = int(res)
s1 = 0
for i in range(a-1):
print((i+1)*res,end=' ')
s1 += (i+1)*res
print(n-s1)
```
No
| 107,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
import math
n, k = map(int, input().split())
Ans = []
if n < (k+1)*k//2:
print(-1)
else:
d = n
nd = 1
for i in range(int(math.sqrt(n)), 1, -1):
if n%i == 0:
if i > nd and n//i >=(k+1)*k//2:
nd = i
elif n//i > nd and i >=(k+1)*k//2:
nd = n//i
d = n//nd
for x in range(1, k):
Ans.append(nd*x)
d -= x
Ans.append(d*nd)
if len(Ans) != 0:
print(*Ans)
else:
print(-1)
```
No
| 107,512 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
import math
n, k = map(int, input().split())
Ans = []
d = (k+1)*k//2
if n < d:
print(-1)
elif d > n//2:
for x in range(1, k):
Ans.append(x)
n -= x
Ans.append(n)
print(*Ans)
else:
if d >= int(math.sqrt(n)):
for i in range(int(math.sqrt(n)), n//2+1):
if n % i == 0:
l = i
nod = n//i
if l >= d:
for x in range(1, k):
Ans.append(nod*x)
l -= x
Ans.append(l*nod)
break
else:
for i in range(1, int(math.sqrt(n))):
if n % i == 0:
l = i
nod = n//l
if l >= d:
for x in range(1, k):
Ans.append(nod*x)
l -= x
Ans.append(l*nod)
break
if len(Ans) != 0:
print(*Ans)
else:
print(-1)
```
No
| 107,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
from math import sqrt
n , k = (int(i) for i in input().split())
def Provera (n, k):
if (k * (k + 1)) // 2 > n:
return True
if (k == 1):
print (n)
exit()
if (Provera(n,k)):
print (-1)
else:
for i in range(int(sqrt(n)) + 1, 2, -1):
if (n % i == 0):
if not (Provera (i , k)):
if (k * (k + 1)) // 2 == i:
for j in range(1, k + 1):
print ((n // i) * j,end = ' ')
exit()
if (k * (k + 1)) // 2 < i:
for j in range(1, k):
print ((n // i) * j,end = ' ')
print (n - (n // i) * ((k * (k - 1)) // 2))
exit()
for i in range(2, int(sqrt(n)) + 1):
if (n % i == 0):
if not (Provera (n // i , k)):
if (k * (k + 1)) // 2 == (n // i):
for j in range(1, k + 1):
print (i * j,end = ' ')
exit()
if (k * (k + 1)) // 2 < (n // i):
for j in range(1, k):
print (i * j,end = ' ')
print (n - i * ((k * (k - 1)) // 2))
exit()
if (k * (k + 1)) // 2 == n:
for i in range(1, k + 1):
print (i, end = ' ')
else:
for i in range(1, k):
print (i, end = ' ')
print (n - (k * (k + 1)) // 2)
```
No
| 107,514 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Tags: implementation
Correct Solution:
```
if __name__ == "__main__":
n, c1, c2 = list(map(int, input().split()))
g = list(map(int, input().split()))
count = 0
deny = 0
for i in range(n):
if g[i] == 1:
if c1 > 0:
c1 -= 1
elif c1 <= 0 and c2 > 0:
c2 -= 1
count += 1
elif c2 <= 0 and count > 0:
count -= 1
else:
deny += 1
else:
if c2 > 0:
c2 -= 1
else:
deny += 2
print(deny)
```
| 107,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Tags: implementation
Correct Solution:
```
n,a,b=map(int,input().split())
l=list(map(int,input().split()))
ans=0
c=0
for i in range(n):
if l[i]==2:
if b==0:
ans+=2
else:
b-=1
else:
if a!=0:
a-=1
elif b!=0:
b-=1
c+=1
elif c!=0:
c-=1
else:
ans+=1
print(ans)
```
| 107,516 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Tags: implementation
Correct Solution:
```
n = input().split()
a = int(n[1])
b = int(n[2])
n = int(n[0])
t = [int(i) for i in input().split()]
lost = 0
bm = 2 * b
b2 = b
b1 = 0
for i in t:
if i == 1:
if a > 0:
a -= 1
else:
if bm > 0:
if b2 > 0:
b2 -= 1
b1 += 1
bm -= 1
else:
b1 -= 1
bm -= 1
else:
lost += 1
else:
if b2 > 0:
b2 -= 1
bm -= 2
else:
lost += 2
print(lost)
```
| 107,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Tags: implementation
Correct Solution:
```
n, a, b = map(int, input().split())
t = list(map(int, input().split()))
ans = 0
cnta = 0
cntb1 = 0
cntb2 = 0
for ti in t:
if ti == 1:
if cnta < a:
cnta += 1
elif cntb1 + cntb2 < b:
cntb1 += 1
elif 0 < cntb1:
cntb1 -= 1
cntb2 += 1
else:
ans += 1
else:
if cntb1 + cntb2 < b:
cntb2 += 1
else:
ans += 2
print(ans)
```
| 107,518 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Tags: implementation
Correct Solution:
```
from sys import stdin, stdout
n, a, b = map(int, stdin.readline().split())
values = list(map(int, stdin.readline().split()))
cnt = 0
c = 0
for i in range(n):
if values[i] == 2:
if b:
b -= 1
else:
cnt += 2
else:
if a:
a -= 1
elif b:
b -= 1
c += 1
elif c:
c -= 1
else:
cnt += 1
stdout.write(str(cnt))
```
| 107,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Tags: implementation
Correct Solution:
```
primLinea=input().split()
iteraciones=int(primLinea[0])
indiv=int(primLinea[1])
par=int(primLinea[2])
parParaIndiv=0
personas=input().split()
negados=0
for i in range(0,iteraciones):
costumer=int(personas[i])
if costumer==1:
if indiv>0:
indiv-=1
elif par>0:
par-=1
parParaIndiv+=1
elif parParaIndiv>0:
parParaIndiv-=1
else:
negados+=1
elif costumer==2:
if par>0:
par-=1
else:
negados+=2
print (negados)
```
| 107,520 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Tags: implementation
Correct Solution:
```
n, a, b = [int(nab) for nab in str(input()).split(' ')]
t = [int(ti) for ti in str(input()).split(' ')]
tables = {1: {0: a, 1: 0}, 2: {0: b, 1: 0, 2: 0}}
noservice = 0
for ti in t:
if (ti == 1):
if (tables[1][0] > 0):
tables[1][0] -= 1
tables[1][1] += 1
elif (tables[2][0] > 0):
tables[2][0] -= 1
tables[2][1] += 1
elif (tables[2][1] > 0):
tables[2][1] -= 1
tables[2][2] += 1
else:
noservice += 1
elif (tables[2][0] > 0):
tables[2][0] -= 1
tables[2][2] += 1
else:
noservice += 2
print(noservice)
```
| 107,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Tags: implementation
Correct Solution:
```
n, a, b = map(int, input().split())
str = input()
ls = [int(u) for u in str.split()]
#print(ls)
t=0
count=0
sum = 0
for u in ls:
sum += u
if u==1 and a>0:
a -=1
count += 1
elif u==1 and a==0 and t>0 and b==0:
t-=1
count += 1
elif u==1 and a==0 and b>0:
t+=1
b -= 1
count += 1
elif u==2 and b>0:
b -= 1
count += 2
print(sum-count)
```
| 107,522 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Submitted Solution:
```
par=[int(i) for i in str(input()).split()]
n,a,b=par[0],par[1],par[2]
group=[int(i) for i in str(input()).split()]
fill_1=0
fill_2_1_left=0
fill_2_1_full=0
fill_2_2=0
denied=0
for i in range(len(group)):
if group[i]==2:
if b-fill_2_1_left-fill_2_2>0:
fill_2_2+=1
else:
denied+=2
else:
if a-fill_1>0:
fill_1+=1
continue
if b-fill_2_1_left-fill_2_2>0:
fill_2_1_left+=1
fill_2_1_full+=1
continue
if fill_2_1_full>0:
fill_2_1_full-=1
continue
denied+=1
print(denied)
```
Yes
| 107,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Submitted Solution:
```
n,a,b = [int(i) for i in input().split()]
nums = [int(i) for i in input().split()]
count = 0
c = 0
for item in nums:
if item!=1:
if b==0:
count+=2
else:
b-=1
else:
if a>0:
a-=1
elif b>0:
b-=1
c+=1
elif c>0:
c-=1
else:
count+=1
print(count)
```
Yes
| 107,524 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Submitted Solution:
```
n, ones, twos = list(map(int,input().split()))
semi = 0
deny = 0
humans = list(map(int,input().split()))
for k in humans:
if k==1:
if ones>0:
ones-=1
elif ones==0:
if twos>0:
twos-=1
semi+=1
elif twos==0:
if semi>0:
semi-=1
else:
deny+=1
elif k==2:
if twos>0:
twos-=1
else:
deny+=2
print(deny)
```
Yes
| 107,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Submitted Solution:
```
#code
#code
n,a,b=map(int,input().strip().split(' '))
hb=0
c=0
l1=list(map(int,input().strip().split(' ')))
for k in l1:
if k==2:
if b==0:
c+=2
else:
b-=1
else:
if a==0:
if b==0:
if hb==0:
c+=1
else:
hb-=1
else:
b-=1
hb+=1
else:
a-=1
print(c)
```
Yes
| 107,526 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Submitted Solution:
```
n,a,b=[int(x) for x in input().split(' ')]
SumOfTables= a + (b * 2)
CountOfGroups = 0
GroupOfPeople = [int(x) for x in input().split(' ')]
i = 0
while i < len(GroupOfPeople):
CountOfGroups += GroupOfPeople[i]
i+=1
print(abs(CountOfGroups - SumOfTables))
```
No
| 107,527 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Submitted Solution:
```
n,a,b = map(int,input().split())
c = list(map(int,input().split()))
d = 0
for i in range(len(c)):
if c[i]==1:
if a>=1:
a-=1
elif b>=0.5:
b-=0.5
else:
d+=1
elif c[i]==2:
if b>=1:
b-=1
else:
d+=2
print(d)
```
No
| 107,528 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Submitted Solution:
```
a = input().split()
n = int(a[0])
b = int(a[2])
a = int(a[1])
data = input().split()
rej = 0
for i in range(n):
if int(data[i]) == 1:
if a > 0:
a -= 1
elif b > 0:
b -= 1
a += 1
else:
rej += 1
else:
if b > 0:
b -= 1
else:
rej += 2
print(rej)
```
No
| 107,529 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
Input
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Output
Print the total number of people the restaurant denies service to.
Examples
Input
4 1 2
1 2 1 1
Output
0
Input
4 1 1
1 1 2 1
Output
2
Note
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
Submitted Solution:
```
#!usr/bin/python 3
def main():
a=input().split(' ')
a=[int(x) for x in a]
lst=input().split(' ')
lst=[int(x) for x in lst]
rejected=0
for x in lst:
if a[1]>0 or a[2]>0:
if x==1:
if a[1]==0:
a[2]-=1
a[1]+=1
else:
a[1]-=1
elif x==2:
if a[2]==0:
rejected+=2
else:
a[2]-=1
else:
print('fail')
else:
rejected+=x
print(rejected)
if __name__=='__main__':
main()
```
No
| 107,530 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Tags: math
Correct Solution:
```
from sys import stdin, stdout
from math import factorial
n = int(stdin.readline())
values = list(map(int, stdin.readline().split()))
ans = 0
cnt = 1
for i in range(1, n):
if values[i] != values[i - 1]:
ans += cnt * (cnt + 1) // 2
cnt = 1
else:
cnt += 1
ans += cnt * (cnt + 1) // 2
stdout.write(str(ans))
```
| 107,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Tags: math
Correct Solution:
```
import math
def choose(n, k):
return n - k + 1
class CodeforcesTask84BSolution:
def __init__(self):
self.result = ''
self.n = 0
self.array = []
def read_input(self):
self.n = int(input())
self.array = [int(x) for x in input().split(" ")]
def process_task(self):
magic_arrays = 0
cr = 10 ** 10
cn = 0
for a in self.array:
if a == cr:
cn += 1
else:
if cn:
for x in range(1, cn + 1):
magic_arrays += choose(cn, x)
cr = a
cn = 1
for x in range(1, cn + 1):
magic_arrays += choose(cn, x)
self.result = str(int(magic_arrays))
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask84BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
```
| 107,532 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Tags: math
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
MOD=1000000007
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
n=Int()
a=array()
i=0
ans=0
while(i<n):
l=1
i+=1
while(i<n and a[i]==a[i-1]):
l+=1
i+=1
ans+=l*(l+1)//2
print(ans)
```
| 107,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Tags: math
Correct Solution:
```
input()
c=a=r=0
for i in input().split():c=(a==i)*c+1;a=i;r+=c
print(r)
# Made By Mostafa_Khaled
```
| 107,534 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Tags: math
Correct Solution:
```
n = int(input())
arr = list(map(int,input().split()))
ans = 0
cnt = 1
for i in range(n-1):
if arr[i] == arr[i+1]:
cnt += 1
continue
ans += (cnt*(cnt+1))//2
cnt = 1
ans += (cnt*(cnt+1))//2
print(ans)
```
| 107,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Tags: math
Correct Solution:
```
n = int(input())
A = list(map(int, input().split()))
j = 0
ans = 0
for i in range(n):
while j < n:
if A[i] == A[j]:
j += 1
else:
break
ans += j-i
if i == j:
j += 1
print(ans)
```
| 107,536 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Tags: math
Correct Solution:
```
n=int(input())
arr = [int(x) for x in input().strip().split()]
arr1=arr[0]
c=1
s=0
for ele in arr[1:]:
if arr1==ele:
c+=1
else :
s+=(c*(c-1))//2
c=1
arr1=ele
s+=(c*(c-1))//2
print(n+s)
```
| 107,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Tags: math
Correct Solution:
```
input()
L,ans='',0
for x in input().split():
if x==L:
z+=1
else:
if L!='':
ans+=z*(z+1)//2
z,L=1,x
ans+=z*(z+1)//2
print(ans)
```
| 107,538 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
cur = 0
ans = 0
for i in range(n):
if i and a[i] != a[i-1]:
cur = 0
cur += 1
ans += cur
print(ans)
```
Yes
| 107,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Submitted Solution:
```
import sys
import math
from collections import defaultdict
import itertools
MAXNUM = math.inf
MINNUM = -1 * math.inf
def getInt():
return int(sys.stdin.readline().rstrip())
def getInts():
return map(int, sys.stdin.readline().rstrip().split(" "))
def getString():
return sys.stdin.readline().rstrip()
def printOutput(ans):
sys.stdout.write(str(ans) + "\n")
def solve(arr):
# min = max iff all elements in subarray are the same
# amount of subarrays increases by len of consec elemnts
total = 0
curEle = None
ln = 1
for ele in arr:
if ele == curEle:
ln += 1
else:
ln = 1
curEle = ele
total += ln
return total
def readinput():
arrLen = getInt()
arr = list(getInts())
printOutput(solve(arr))
readinput()
```
Yes
| 107,540 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Submitted Solution:
```
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353
# from sys import stdin
# input = stdin.readline
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
try:
# sys.setrecursionlimit(int(pow(10,6)))
sys.stdin = open("input.txt", "r")
# sys.stdout = open("../output.txt", "w")
except:
pass
def pmat(A):
for ele in A:
print(ele,end="\n")
n=L()[0]
A=L()
ans=0
i=0
while(i<n):
j=i
while(j<n and A[i]==A[j]):
j+=1
ans+=(j-i)*(j-i+1)//2
i=j
print(ans)
endtime = time.time()
# print(f"Runtime of the program is {endtime - starttime}")
```
Yes
| 107,541 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Submitted Solution:
```
input()
c=a=r=0
for i in input().split():c=(a==i)*c+1;a=i;r+=c
print(r)
```
Yes
| 107,542 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Submitted Solution:
```
n=int(input())
arr=[int(x) for x in input().split()]
arr=sorted(arr)
sub_st_array=[]
cnt=0
for i in range(len(arr)):
temp=[]
for j in range(i,len(arr)):
temp=arr[i:j+1]
sub_st_array.append(temp)
for i in range(len(sub_st_array)):
if sub_st_array[i][0]==sub_st_array[i][-1]:
cnt+=1
print(cnt)
```
No
| 107,543 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Submitted Solution:
```
n=int(input())
arr = sorted([int(x) for x in input().strip().split()])
arr1=arr[0]
c=1
s=0
for ele in arr[1:]:
if arr1==ele:
c+=1
else :
s+=(c*(c-1))//2
c=1
arr1=ele
print(n+s)
```
No
| 107,544 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Examples
Input
4
2 1 1 4
Output
5
Input
5
-2 -2 -2 0 1
Output
8
Note
Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
Submitted Solution:
```
input()
c=a=ans=0
for i in input().split():
if(a!=i): ans+=c*(c+1)/2; c=1;
else: c+=1; ans+=1;
a=i;
print(int(ans))
```
No
| 107,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Tags: greedy
Correct Solution:
```
import sys
import math as mt
import bisect
#input=sys.stdin.readline
#t=int(input())
t=1
def solve():
i,j,ans=0,0,0
ind=[0]*(n)
suma,ex=1,0
j=0
for i in range(n):
if j<n and ind[i]==0:
while j<n:
if l[j]-l[i]+1<=m:
if j-i+1-ex>=k:
diff=(j-i+1-ex)-k+1
for j1 in range(j,j-diff,-1):
#print(j1,diff)
ind[j1]=1
ans+=diff
ex+=diff
else:
if k==1:
ans+=1
break
j+=1
else:
ex-=1
return ans
for _ in range(t):
#n=int(input())
#a=int(input())
#b=int(input())
n,m,k=map(int,input().split())
#x,y,k=map(int,input().split())
#n,h=(map(int,input().split()))
l=list(map(int,input().split()))
#l2=list(map(int,input().split()))
l.sort()
print(solve())
```
| 107,546 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Tags: greedy
Correct Solution:
```
n, m, k = map(int, input().split())
a = map(int, input().split())
a = list(sorted(a))
s = []
r = 0
for x in a:
if len(s) and s[0] < x - m + 1:
del s[0]
if len(s) < k - 1:
s.append(x)
else:
r += 1
print(r)
```
| 107,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Tags: greedy
Correct Solution:
```
from sys import stdin, exit, setrecursionlimit
from collections import deque
from string import ascii_lowercase
from itertools import *
from math import *
input = stdin.readline
lmi = lambda: list(map(int, input().split()))
mi = lambda: map(int, input().split())
si = lambda: input().strip('\n')
ssi = lambda: input().strip('\n').split()
mod = 10**9+7
n, m, k= mi()
a = lmi()
a.sort()
q = deque()
cnt = 0
for i in range(n):
q.append(i)
while a and a[q[-1]] - a[q[0]] > m-1:
q.popleft()
while len(q) >= k:
q.pop()
cnt += 1
print(cnt)
```
| 107,548 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Tags: greedy
Correct Solution:
```
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left , bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')
def isPrime(x):
for i in range(2,x):
if i*i>x:
break
if (x%i==0):
return False
return True
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
return list(set(l))
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 # y = y/2
x = (x * x) % p
return res
def SieveOfEratosthenes(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
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def si():
return input()
def prefix_sum(arr):
r = [0] * (len(arr)+1)
for i, el in enumerate(arr):
r[i+1] = r[i] + el
return r
def divideCeil(n,x):
if (n%x==0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int,input().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
def power_set(L):
"""
L is a list.
The function returns the power set, but as a list of lists.
"""
cardinality=len(L)
n=2 ** cardinality
powerset = []
for i in range(n):
a=bin(i)[2:]
subset=[]
for j in range(len(a)):
if a[-j-1]=='1':
subset.append(L[j])
powerset.append(subset)
#the function could stop here closing with
#return powerset
powerset_orderred=[]
for k in range(cardinality+1):
for w in powerset:
if len(w)==k:
powerset_orderred.append(w)
return powerset_orderred
def fastPlrintNextLines(a):
# 12
# 3
# 1
#like this
#a is list of strings
print('\n'.join(map(str,a)))
def sortByFirstAndSecond(A):
A = sorted(A,key = lambda x:x[0])
A = sorted(A,key = lambda x:x[1])
return list(A)
#__________________________TEMPLATE__________________OVER_______________________________________________________
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = 1
# t = int(input())
for _ in range(t):
n,m,k = li()
a = li()
a.sort()
a1 = []
cnt = 0
for i in range(n):
a1.append(a[i])
if len(a1)>=k and a1[-1]-a1[len(a1)-k]<m:
a1.pop()
cnt+=1
print(cnt)
```
| 107,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Tags: greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import *
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
t = [0]*(10**6+100)
for ai in a:
t[ai] = 1
now = 0
ans = 0
for i in range(10**6+100):
now += t[i]
if i>=m:
now -= t[i-m]
if now==k:
t[i] = 0
now -= 1
ans += 1
print(ans)
```
| 107,550 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Tags: greedy
Correct Solution:
```
n, m, k=[int(x) for x in input().split()]
alarms=[int(x) for x in input().split()]
alarms.sort()
al2=[]
cont=0
for i in range(len(alarms)):
al2.append(alarms[i])
if len(al2)>=k and al2[-1]-al2[len(al2)-k]<m:
al2.pop()
cont+=1
print(cont)
```
| 107,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Tags: greedy
Correct Solution:
```
n, m, k = (int(x) for x in input().split())
ns = set((int(x) for x in input().split()))
from collections import deque
span = deque((0 for i in range(m)))
kk = 0
res = 0
mm = -1
while len(ns) > 0:
mm += 1
x = span.popleft()
if x == 1:
kk -= 1
if mm not in ns:
span.append(0)
else:
ns.remove(mm)
y = 1
if kk == k - 1:
res += 1
y = 0
span.append(y)
kk+=y
print(res)
```
| 107,552 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Tags: greedy
Correct Solution:
```
from collections import deque
n,m,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = sorted(a)
a = deque(a)
first = - 10e7
r = deque([])
s = 0
result = 0
while n > 0:
e = a[0]
c = e - m + 1
while r != deque([]):
if r[0] < c:
r.popleft()
s -= 1
else:
break
if s + 1 >= k:
result += 1
else:
r.append(e)
s += 1
a.popleft()
n -= 1
# print(e, c, s, r, result)
print(result)
```
| 107,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Submitted Solution:
```
n,m,k = map(int,input().split())
lis = sorted(map(int,input().split()))+[1000000000000000]
j=zer=ans=0
for i in range(n):
if lis[i]==0:
zer-=1
continue
j=max(j,i)
while j<=n and lis[j]-lis[i]<m:
j+=1
if j-i-zer>=k:
a=j-1
ext=j-i-zer-k+1
t=0
while a>=0 and ext>0:
if lis[a]!=0:
lis[a]=0
t+=1
zer+=1
ext-=1
a-=1
ans+=t
if lis[i]==0:
zer-=1
print(ans)
```
Yes
| 107,554 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Submitted Solution:
```
import heapq
import sys
input = sys.stdin.readline
for _ in range(1):
n,m,k = map(int,input().split())
l = list(map(int,input().split()))
if k == 1:
print(n)
continue
l.sort()
h = []
heapq.heapify(h)
ans = 0
for i in l:
if len(h)+1 == k:
if i-h[0]+1 <= m:
ans += 1
continue
while len(h) > 0:
if i - h[0] + 1 > m:
heapq.heappop(h)
else:
break
heapq.heappush(h, i)
else:
heapq.heappush(h,i)
print(ans)
```
Yes
| 107,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Submitted Solution:
```
n, m, k = map(int, input().split())
times = list(map(int, input().split()))
times.sort()
ans = 0
start = 0
cnt = 0
j = 0
prohibited = []
for i in range(n):
prohibited += [1]
for i in range(n):
if times[i] < start + m:
cnt += 1
else:
while times[i] >= start + m:
if prohibited[j] == 1:
start = times[j]
if j != 0:
cnt -= 1
j += 1
cnt += 1
if cnt == k:
cnt -= 1
ans += 1
prohibited[i] = 0
if k == 1:
print(n)
else:
print(ans)
```
Yes
| 107,556 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Submitted Solution:
```
n,m,k=map(int,input().split())
cnt=[0]*(10**6+5)
l=[int(i) for i in input().split()]
for i in l:
cnt[i]+=1
sm=0
for i in range(10**6+5):
sm+=cnt[i]
if(i>=m):
sm-=cnt[i-m]
if(sm>=k):
sm-=1
cnt[i]=0
print(n-sum(cnt))
```
Yes
| 107,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Submitted Solution:
```
n, m, k = map(int, input().split())
ls = list(map(int, input().split()))
ls.sort()
l = len(ls)
i = 0
j = 1
t = n
if n == 1:
print(1)
elif k == 1:
print(n)
else:
while t != 0:
# print(i, j)
while j != l and ls[j] - ls[i] + 1 <= m:
j += 1
x = j - i
if x - k < 0:
t = 0
else:
t = min(t, x - k +1)
i += 1
if j == l:
break
print(t)
```
No
| 107,558 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Submitted Solution:
```
n,m,k = list(map(int,input().split()))
clock = list(map(int,input().split()))
clock.sort()
unlock = [clock[0]]
count = 0
j = 0
con = 1
for i in range(1,n):
if clock[i] - unlock[j] < m:
if con < k - 1:
con += 1
unlock += [clock[i]]
else:
count += 1
else:
while j == len(unlock) or clock[i] - unlock[j] < m:
j += 1
con -= 1
unlock += [clock[i]]
if j == len(unlock) - 1:
j += 1
print(count)
```
No
| 107,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Submitted Solution:
```
from collections import *
al=defaultdict(int)
n,u,k=map(int,input().split())
z=list(map(int,input().split()))
z.sort()
m=z.copy()
if(k==1):
print(len(z))
exit()
al=defaultdict(int)
total=0
point=z[0]+1
for i in range(len(z)):
if(point>=len(z)):
break;
if(i==0):
point=m[k-1]
count=0
for j in range(k-1,len(z)):
if(m[j]-z[i]+1>u):
break;
else:
count+=1
al[j]=1
total+=count
point=j
gap=k
else:
if(al[i]==0):
gap-=1
while(gap<k):
if(al[point]==0):
point+=1
gap+=1
else:
point+=1
count=0
for j in range(point,len(z)):
if(z[j]-z[i]+1>u):
break;
else:
count+=1
al[j]=1
total+=count
point=j
gap=k
else:
continue;
print(total)
```
No
| 107,560 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.
Input
First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.
Output
Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.
Examples
Input
3 3 2
3 5 1
Output
1
Input
5 10 3
12 8 18 25 1
Output
0
Input
7 7 2
7 3 4 1 6 5 2
Output
6
Input
2 2 2
1 3
Output
0
Note
In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks.
Submitted Solution:
```
n, m, k = map(int, input().split())
times = list(map(int, input().split()))
times.sort()
ans = 0
start = 0
cnt = 0
j = 0
for i in range(n):
if times[i] <= start + m:
cnt += 1
if cnt == k:
cnt -= 1
ans += 1
j += 1
else:
while times[i] > start + m:
start = times[j]
j += 1
cnt -= 1
cnt += 1
print(ans)
```
No
| 107,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import sys
ints = (int(x) for x in sys.stdin.read().split())
a,b,p,x = (next(ints) for i in range(4))
def ext_gcd(a, b):
"""return (g, x, y) such that a*x + b*y = g = gcd(a, b) and abs(x)<=b+1 ans abs(y)<=a+1"""
x, x1, y, y1 = 0, 1, 1, 0
while a != 0:
(q, a), b = divmod(b, a), a
y, y1 = y1, y - q * y1
x, x1 = x1, x - q * x1
return b, x, y
def mod_inv(a, mod):
"""return x such that (x * a) % mod = gcd(a, mod)"""
g, x, y = ext_gcd(a, mod)
return x % mod
ans, x = divmod(x, (p-1)*p)
ans = (p-1)*ans-(b==0)
an = 1
for i in range(p-1):
k = (mod_inv((an*(p-1))%p, p)*(b-i*an)) % p
ans += k*(p-1)+i<=x
an = (an*a)%p
print(ans)
if 0:
a, b, p = 2, 1, 5
for k in range(p+1):
h = [((k*(p-1)+i)*(a**i))%p for i in range(0, p-1)]
print(*h)
```
| 107,562 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))
def gcd2(a, b):
l = []
while b:
l.append(divmod(a,b))
a, b = b, a%b
x, y = 1, 0
for aa,bb in l[::-1]:
x, y = y, x - aa*y
return a, x, y
def modinv(x, M):
a,xx,yy = gcd2(x,M)
return a,xx%M
def solve(a,b,n):
g,xx,yy = gcd2(a,n)
if b%g!=0:
return None
a //= g
n //= g
b //= g
ainv = modinv(a, n)[1]
x = ainv*b%n
return x
def crt(rs, ms):
r0 = 0
m0 = 1
for r1,m1 in zip(rs, ms):
if m0<m1:
m0, m1 = m1, m0
r0, r1 = r1, r0
if m0%m1==0:
if r0%m1 != r1:
return None,None
else:
continue
# print(m0,m1)
g,im = modinv(m0, m1)
u1 = m1//g
if (r1-r0)%g!=0:
return None,None
x = (r1-r0) // g % u1 * im % u1
r0 += x * m0
m0 *= u1
if r0<0:
r0 += m0
return r0,m0
a,b,p,x = list(map(int, input().split()))
c = 1
index = [[] for _ in range(p)]
for i in range(p-1):
index[c].append(i)
c *= a
c %= p
ans = 0
for i in range(1,p):
j = b*pow(i, p-2, p)%p
for jj in index[j]:
res = crt((i, jj), (p, p-1))
if res[0] is not None:
r,m = res
# print(res)
ans += max(0, x-r+m)//m
print(ans)
```
| 107,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
def inp():
return sys.stdin.readline().rstrip()
def mpint():
return map(int, inp().split(' '))
def itg():
return int(inp())
# ############################## import
# ############################## main
def main():
a, b, p, x = mpint()
ans = 0
# 5: 4, 3, 2, 1
by = b
for j in reversed(range(p - 1)):
by = by * a % p
# y = pow(a, p - 1 - j, p)
# by = b * y
i = (j - by) % p # smallest i s.t. the equation
if i > (x - j) // (p - 1):
continue
ans += 1 + ((x - j) // (p - 1) - i) // p
print(ans)
DEBUG = 0
URL = 'https://codeforces.com/contest/919/problem/E'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: interactive, 3: debug
if DEBUG == 1:
import requests
from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
runner = TestCaseRunner(main, URL)
inp = runner.input_stream
print = runner.output_stream
runner.checking()
else:
if DEBUG != 3:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
_print = print
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
```
| 107,564 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
a,b,p,x=map(int,input().split())
ans=0
for j in range(p-1):
y=b*pow(pow(a,j,p),p-2,p)%p
i=(j-y+p)%p
t=i*(p-1)+j
if(x-t>=0):
ans+=((x-t)//(p*(p-1))+1)
print(ans)
```
| 107,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
def inp():
return sys.stdin.readline().rstrip()
def mpint():
return map(int, inp().split(' '))
def itg():
return int(inp())
# ############################## import
# ############################## main
def main():
a, b, p, x = mpint()
ans = 0
for j in range(p - 1):
y = pow(a, p - 1 - j, p)
by = b * y
i = (j - by) % p # smallest i s.t. the equation
if i > (x - j) // (p - 1):
continue
ans += 1 + ((x - j) // (p - 1) - i) // p
print(ans)
DEBUG = 0
URL = 'https://codeforces.com/contest/919/problem/E'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: interactive, 3: debug
if DEBUG == 1:
import requests
from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
runner = TestCaseRunner(main, URL)
inp = runner.input_stream
print = runner.output_stream
runner.checking()
else:
if DEBUG != 3:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
_print = print
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
```
| 107,566 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
a,b,p,x = [int(x) for x in input().split()]
ainv = pow(a,p-2,p)
count = 0
n = 1
exp = ainv*b%p
while n<p:
m = n
if exp>n:
m += (n+p-exp)*(p-1)
else:
m += (n-exp)*(p-1)
if m<=x: count += 1 + (x-m)//(p*(p-1))
n += 1
exp = (exp*ainv)%p
print(count)
```
| 107,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
def inp():
return sys.stdin.readline().rstrip()
def mpint():
return map(int, inp().split(' '))
def itg():
return int(inp())
# ############################## import
# ############################## main
def main():
a, b, p, x = mpint()
ans = 0
by = b
for j in reversed(range(p - 1)):
by = by * a % p
# y = pow(a, p - 1 - j, p)
# by = b * y
i = (j - by) % p # smallest i s.t. the equation
# if i > (x - j) // (p - 1):
# continue
ans += 1 + ((x - j) // (p - 1) - i) // p
print(ans)
DEBUG = 0
URL = 'https://codeforces.com/contest/919/problem/E'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: interactive, 3: debug
if DEBUG == 1:
import requests
from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
runner = TestCaseRunner(main, URL)
inp = runner.input_stream
print = runner.output_stream
runner.checking()
else:
if DEBUG != 3:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
_print = print
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
```
| 107,568 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
# import sys
# sys.stdin = open('in.txt', 'r')
a, b, p, x = map(int, input().split())
# A = [0, 1, 2, .. p-2, p-1] .. length = p
# B = [1, a, a^2, .. a^(p-2)] .. length = p - 1
# x * inv(x) * b = b
# a^x -> inv(a*x) * b
# 4 6 7 13
# [0, 1, 2, 3, 4, 5, 6]
# [0, 1, 2, 3, 4, 5]
# 2 3 5 8
# [0, 1, 2, 3]
# [0, 1, 2]
x += 1
res = 0
res += x//(p*(p-1)) * (p-1)
x -= x//(p*(p-1)) * (p*(p-1))
for i in range(p-1):
ap = (pow(a, (i)*(p-2)%(p-1), p) * b) % p
l = (x // (p-1)) + (1 if i < x%(p-1) else 0)
if i >= ap and i-ap < l:
res += 1
elif i < ap and i+p-ap < l:
res += 1
# print(ap, l, res)
print(res)
# for i in range(1, x+1):
# print(i*pow(a,i,p)%p, end=' ')
# if i % (p-1) == 0:
# print()
# print()
```
| 107,569 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a, b, p, x = map(int, input().split())
print(sum(map(lambda j: 1 + ((x - j) // (p - 1) - (j - b * pow(a, p - 1 - j, p)) % p) // p, range(p - 1))))
```
Yes
| 107,570 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a,b,p,x = map(int,input().split())
res = 0
for i in range(1,p):
n = (b * pow(pow(a,i,p),p-2,p))%p
u = (n - i + p )%p
u = (u * pow(p-1,p-2,p))%p
me = u * (p-1) + i
if me>x: continue
res+= (x-me) // (p*(p-1))+1
print(res)
# (u*(p-1) + i) * a^i = b mod p
# u*(p-1) + i = b * inv (a^i) % mod p
```
Yes
| 107,571 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
heap.append(item)
heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
if heap and item < heap[0]:
item, heap[0] = heap[0], item
heapq._siftup_max(heap, 0)
return item
from math import gcd as GCD, modf
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
def LCM(n,m):
if n or m:
return abs(n)*abs(m)//math.gcd(n,m)
return 0
def CRT(lst_r,lst_m):
r,m=lst_r[0],lst_m[0]
for r0,m0 in zip(lst_r[1:],lst_m[1:]):
if (r0,m0)==(-1,0):
r,m=-1,0
break
r0%=m0
g=math.gcd(m,m0)
l=LCM(m,m0)
if r%g!=r0%g:
r,m=-1,0
break
r,m=(r0+m0*(((r-r0)//g)*Extended_Euclid(m0//g,m//g)[0]%(m//g)))%l,l
return r,m
def Extended_Euclid(n,m):
stack=[]
while m:
stack.append((n,m))
n,m=m,n%m
if n>=0:
x,y=1,0
else:
x,y=-1,0
for i in range(len(stack)-1,-1,-1):
n,m=stack[i]
x,y=y,x-(n//m)*y
return x,y
class MOD:
def __init__(self,mod):
self.mod=mod
def Pow(self,a,n):
a%=self.mod
if n>=0:
return pow(a,n,self.mod)
else:
assert math.gcd(a,self.mod)==1
x=Extended_Euclid(a,self.mod)[0]
return pow(x,-n,self.mod)
def Build_Fact(self,N):
assert N>=0
self.factorial=[1]
for i in range(1,N+1):
self.factorial.append((self.factorial[-1]*i)%self.mod)
self.factorial_inv=[None]*(N+1)
self.factorial_inv[-1]=self.Pow(self.factorial[-1],-1)
for i in range(N-1,-1,-1):
self.factorial_inv[i]=(self.factorial_inv[i+1]*(i+1))%self.mod
return self.factorial_inv
def Fact(self,N):
return self.factorial[N]
def Fact_Inv(self,N):
return self.factorial_inv[N]
def Comb(self,N,K):
if K<0 or K>N:
return 0
s=self.factorial[N]
s=(s*self.factorial_inv[K])%self.mod
s=(s*self.factorial_inv[N-K])%self.mod
return s
a,b,p,x=map(int,readline().split())
if a%p==0 and b%p==0:
ans=x
elif a%p==0:
ans=0
elif b%p==0:
ans=x//p
else:
ans=0
MD=MOD(p)
for i in range(p-1):
R,M=[i,b*MD.Pow(a,-i)],[p-1,p]
r,m=CRT(R,M)
if r==-1:
continue
ans+=(x+m-r)//m
print(ans)
```
Yes
| 107,572 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
def AC():
a, b, p, x = map(int, input().split())
ans = 0
m = 1
for i in range(p - 2):
m = (m * a) % p
rem = 1
inv = 1
Ch = p * (p - 1)
for n in range(1, p):
rem = (rem * a) % p
inv = (inv * m) % p
cur = min(p, ((n * rem - b) * inv + p) % p)
rep = n + cur * (p - 1)
ans += max(0, (x - rep + Ch) // Ch)
print(ans)
AC()
```
Yes
| 107,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a, b, p, x = map(int, input().split())
count = 0
for n in range(1,x+1):
left = (n%p*(((a%p)**n)%p))%p
right = b % p
if left == right:
count += 1
```
No
| 107,574 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a, b, p, x = input().split()
a = int(a)
b = int(b)
p = int(p)
x = int(x)
count = 0
for n in range(1, x+1):
binary = "{0:b}".format(n)
mult = 1
total = 1
for i in range(len(binary)-1, -1, -1):
if binary[i]:
total *= a**mult
mult *= 2
if (n%p * total%p)%p == b%p:
count += 1
print(count)
```
No
| 107,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a,b,p,x = list(map(int,input().split()))
d = p*x+b
n = 1
k = n*a**n
t = 0
while(d > 1 and n <= x):
d //= k
n+=1
k = n*a**n
if(d): t+=1
print(t)
```
No
| 107,576 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
inputt = [int(x) for x in input().split()]
a = inputt[0]
b = inputt[1]
p = inputt[2]
x = inputt[3]
N = 10 ** 6 + 10
A = [0] * (N)
A[0] = 1
ans = 0
for i in range(1, p):
A[i] = A[i - 1] * a % p
for j in range(1, p):
y = b * A[p - 1 - j] % p
i = (j - y + p) % p
tmp = i * (p - 1 ) + j
if x>=tmp:
ans += (x - tmp)/p/(p - 1) + 1
print(int(ans))
```
No
| 107,577 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Tags: dp
Correct Solution:
```
import sys
n, m, k = map(int, input().split())
table = [input() for _ in range(n)]
dp = [0]*(k+1)
for a in table:
one = []
for i in range(m):
if a[i] == '1':
one.append(i)
if not one:
continue
ni = len(one)
subdp = [10**9] * (ni+1)
subdp[-1] = 0
for i in range(ni):
for j in range(i, ni):
subdp[ni-(j-i+1)] = min(subdp[ni-(j-i+1)], one[j]-one[i]+1)
next_dp = [10**9]*(k+1)
for i in range(k, -1, -1):
for j in range(ni+1):
if i+j > k:
break
next_dp[i+j] = min(next_dp[i+j], dp[i] + subdp[j])
dp = next_dp
print(min(dp))
```
| 107,578 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Tags: dp
Correct Solution:
```
R = lambda: map(int, input().split())
n, m, k = R()
cls = [list(i for i, x in enumerate(map(int, input())) if x) for _ in range(n)]
dp = [[n * m] * (k + 1) for i in range(n + 1)]
dp.append([0] * (k + 1))
for i in range(n):
row = cls[i]
c2l = [m + 1] * (m + 1)
c2l[0] = row[-1] - row[0] + 1 if row else 0
c2l[len(row)] = 0
for r in range(len(row)):
for l in range(r + 1):
c2l[len(row) - (r - l + 1)] = min(c2l[len(row) - (r - l + 1)], row[r] - row[l] + 1)
for j in range(k + 1):
for c, l in enumerate(c2l):
if j + c <= k and l < m + 1:
dp[i][j] = min(dp[i][j], dp[i - 1][j + c] + l)
print(min(dp[n - 1]))
```
| 107,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Tags: dp
Correct Solution:
```
def min_sub_array(day, k):
if not day:
return [0] * (k + 1)
n = len(day)
best = [float('inf')] * (n + 1)
best[0] = 0
best[1] = 1
for size in range(2, n + 1):
for i in range(n + 1 - size):
best[size] = min(best[size], day[i + size - 1] - day[i] + 1)
output = [0] * (k + 1)
for i in range(k + 1):
if n - i > 0:
output[i] = best[n - i]
return output
N, M, K = map(int, input().split())
day = [i for i, val in enumerate(input()) if val == '1']
best = min_sub_array(day, K)
for _ in range(N - 1):
day = [i for i, val in enumerate(input()) if val == '1']
new_day_best = min_sub_array(day, K)
new_best = [float('inf')] * (K + 1)
for i in range(K + 1):
for j in range(i + 1):
new_best[i] = min(new_best[i], new_day_best[j] + best[i - j])
best = new_best
print(best[K])
```
| 107,580 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Tags: dp
Correct Solution:
```
import queue
intput = lambda:map(int, input().split())
N, M, K = intput()
ht = [[] for _ in range(N)]
for _ in range(N):
day = input()
ht[_] = [i for i in range(M) if day[i] == '1']
# req[i][j] -- required hours for day i if j lessons skipped
# dp[i][j] -- required hours up to day i if j lesson skipped
# dp[i+1][j+z] = dp[i][j] + req[i][z]
tc = [1,2,3,8,9]
# just return dp[-1][-1]
req = [[0 for _ in range(M+1)] for __ in range(N)]
dp = [[0 for _ in range(K+1)] for __ in range(N)]
for i in range(N):
# cost to skip j lessons today
for j in range(len(ht[i])):
req[i][j] = ht[i][-1] - ht[i][0] + 1 # default large num
# if start at the first-th lesson
for first in range(j+1):
last = first + len(ht[i])-j-1
cost = ht[i][last]-ht[i][first]+1
if last >= first:
req[i][j] = min(req[i][j], cost)
for i in range(min(len(req[0]), len(dp[0]))):
dp[0][i] = req[0][i]
for i in range(1, N):
# total skipped up to this point
for j in range(K+1):
dp[i][j] = dp[i-1][j] + req[i][0]
# additional skipped for this day -- min of (skips left, curr skips, num lessons)
for z in range(1+min(j, len(ht[i]))):
dp[i][j] = min(dp[i][j], dp[i-1][j-z] + req[i][z])
# print('{}\n{}'.format(req,dp))
print(dp[-1][-1])
```
| 107,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Tags: dp
Correct Solution:
```
import sys
input = sys.stdin.readline
def int_array():
return list(map(int, input().strip().split()))
def float_array():
return list(map(float, input().strip().split()))
def str_array():
return input().strip().split()
from collections import Counter
import math
import bisect
from collections import deque
n,m,lesson=int_array()
dp=[[250005 for i in range(lesson+2)]for j in range(n+1)]
days=[[] for i in range(n)]
for i in range(n):
s=input()
for j in range(m):
if s[j]=="1":
days[i].append(j+1)
m=[[250005 for i in range(lesson+2)]for j in range(n+1)]
for i in range(n):
for j in range(lesson+1):
if j<=len(days[i]):
if j==len(days[i]):
m[i][j]=0
continue
else:
for k in range(0,j+1):
var=days[i][0+k]
var1=days[i][-1*max(1,1+(j-k))]
m[i][j]=min(m[i][j],var1-var+1)
for i in range(lesson+1):
dp[0][i]=m[0][i]
for i in range(1,n):
for j in range(lesson+1):
for k in range(j+1):
dp[i][j]=min(dp[i][j],dp[i-1][j-k]+m[i][k])
#dp[i][j] = min(dp[i][j], dp[i - 1][k]+m[i][j-k])
print(min(dp[n-1]))
```
| 107,582 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Tags: dp
Correct Solution:
```
n, m, k = input().split(' ')
n = int(n)
m = int(m)
k = int(k)
ind = []
pre = []
for _ in range(n):
s = input()
ind.append([])
for i, c in enumerate(s):
if c == '1':
ind[-1].append(i)
for i in range(n):
pre.append([])
for j in range(k + 1):
pre[i].append([])
if len(ind[i]) > j:
pre[i][j] = ind[i][-1] - ind[i][0] + 1
else:
pre[i][j] = 0
continue
for x in range(j + 1):
y = len(ind[i]) - 1 - j + x
if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]:
pre[i][j] = ind[i][y] - ind[i][x] + 1
dp = [[]]
for i in range(k + 1):
dp[0].append(pre[0][i])
for i in range(1, n):
dp.append([])
for j in range(0, k + 1):
dp[i].append(pre[i][j] + dp[i - 1][0])
for z in range(j + 1):
dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z])
print (dp[n - 1][k])
```
| 107,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Tags: dp
Correct Solution:
```
import math
#import math
#------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now----------------------------------------------------import math
def calc(st,j):
ans=9999999999999999999999
#print(st)
if j>=len(st):
return 0
j=len(st)-j
for i in range(j-1,len(st)):
ans=min(ans,st[i]-st[i-j+1]+1)
return ans
n,m,k=map(int,input().split())
s=[]
for i in range(n):
s.append(input())
inf=99999999999999999999
dp=[[inf for i in range(k+1)]for j in range(n+1)]
for i in range(k+1):
dp[0][i]=0
for i in range(1,n+1):
st=[]
for ik in range(len(s[i-1])):
if s[i-1][ik]=='1':
st.append(ik)
for j in range(k+1):
no=calc(st,j)
#print(no,j)
for t in range(k+1-j):
dp[i][t+j]=min(dp[i][t+j],no+dp[i-1][t])
#print(dp)
print(dp[n][k])
```
| 107,584 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Tags: dp
Correct Solution:
```
n, m, k = map(int, input().split())
DATA = [input() for i in range(n)]
#dp[n_day][used_cost]
#ans = min(dp[n_day][used_cost] for used_cost in range(k + 1))
#dp[n_day][used_cost] := min(dp[n_day - 1][prev_cost] + cost(pay used_cost - prev_cost in n_day) for prev_cost in range(used_cost + 1))
INF = 1 << 60
dp = [[INF]*(k + 10) for i in range(n + 10)]
dp[0][0] = 0
COST = [[INF]*(k + 10) for i in range(n + 10)]
for i, string in enumerate(DATA):
#COST[i + 1]
stack = []
for j in range(m):
if string[j] == "1":
stack.append(j)
L = len(stack)
for j in range(k + 10):
if j >= L:
COST[i + 1][j] = 0
continue
else:
for pos in range(j + 1):
l = pos
r = pos + L - 1 - j
COST[i+1][j] = min(COST[i+1][j], stack[r] - stack[l] + 1)
for day in range(1, n + 1):
for used_cost in range(k + 1):
dp[day][used_cost] = min(dp[day - 1][prev_cost] + COST[day]
[used_cost - prev_cost] for prev_cost in range(used_cost + 1))
ans = min(dp[n][used_cost] for used_cost in range(k + 1))
print(ans)
```
| 107,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Submitted Solution:
```
from collections import deque
n, m, k = map(int, input().split())
field = []
for i in range(n):
field.append(input())
save = [deque() for i in range(n)]
for i in range(n):
cur = [[0, 0, 0] for i in range(m)]
last = -1
for j in range(m - 1 , -1, -1):
if (field[i][j] == "1"):
if (last == -1):
cur[j][1] = 1
else:
cur[j][1] = last - j
last = j
last = -1
for j in range(m):
if (field[i][j] == "1"):
if (last == -1):
cur[j][0] = 1
else:
cur[j][0] = j - last
last = j
cur[j][2] = j
save[i].append(cur[j])
for cnt in range(k):
mx = [-1, -1, -1]
for i in range(n):
if (len(save[i])):
if (len(save[i]) == 1):
mx = max(mx, [1, i, 0])
else:
mx = max(mx, [save[i][0][1], i, 0], [save[i][-1][0], i, -1])
if (mx[0] == -1):
break
else:
if (mx[2] == 0):
save[mx[1]].popleft()
else:
save[mx[1]].pop()
ans = 0
for i in range(n):
if (len(save[i])):
ans += save[i][-1][2] - save[i][0][2] + 1
print(ans)
```
No
| 107,586 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Submitted Solution:
```
n,m,k = map(int,input().split())
a = [0]*n
s = [0]*n
for i in range(n):
a[i] = list(map(int,list(input())))
s[i] = m-a[i][::-1].index(1)-a[i].index(1)
if min(s)<=k:
if min(s)==k:
t = s.index(min(s))
s[t] = 0
a[t] = [0]*m
k = 0
s = 0
for i in range(n):
if 1 in a[i]:
s+=(m-a[i][::-1].index(1)-a[i].index(1))
print (s)
else:
while min(s)<k:
for i in range(n):
if s[i]<k:
k-=s[i]
s[i] = 0
a[i] = [0]*m
s = 0
for i in range(n):
if 1 in a[i]:
s+=(m-a[i][::-1].index(1)-a[i].index(1))
print (s-k)
else:
s = 0
for i in range(n):
if 1 in a[i]:
s+=(m-a[i][::-1].index(1)-a[i].index(1))
print (s-k)
```
No
| 107,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Submitted Solution:
```
n,m,k=map(int,input().split())
ls=[]
hs=0
for i in range(n):
ls.append([])
c=-1
a=input()
for j in a:
if j=='1':
if c!=-1:
ls[i]+=[c]
hs+=c
c=1
else:
if c!=-1:
c+=1
if c!=-1:
ls[i]+=[1]
hs+=1
for i in range(k):
ma,ima,jma=0,1,1
for j in range(n):
if len(ls[j])==0:
continue
elif len(ls[j])==1 and 1>ma:
ma,jma,ima=1,j,0
elif ls[j][0]>ma:
ma,jma,ima=ls[j][0],j,0
elif ls[j][-2]>ma:
ma,jma,ima=ls[j][-2],j,-1
if ima!=1:
hs-=ma
ls[jma].pop(ima)
else:
1/0
print(hs)
```
No
| 107,588 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Output
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
Examples
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
Note
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
Submitted Solution:
```
import sys
import math
from collections import defaultdict
n,m,k=map(int,sys.stdin.readline().split())
s=sys.stdin.readline()[:-1]
dp=[0 for _ in range(k+1)]
days=[]
for i in range(m):
if s[i]=='1':
days.append(i)
x=len(days)
for i in range(min(x,k+1)):
dp[i]=(days[-1]-days[i]+1)
if x<k+1:
last=0
for i in range(x,k+1):
dp[i]=last
#print(dp,'dp',k+1,x)
for _ in range(n-1):
newdp=[1e10 for _ in range(k+1)]
s=sys.stdin.readline()[:-1]
#print(s,'s')
days=[]
for i in range(m):
if s[i]=='1':
days.append(i)
x=len(days)
#last=days[-1]
if x==0:
days=[0]
if x<k+1 and x!=0:
for i in range(x,k+1):
days.append(days[-1])
for i in range(k+1):
if days[-1]!=0:
newdp[i]=dp[i]+days[-1]-days[0]+1
else:
newdp[i]=dp[i]
#print(i,'i',days[-1],'last',days[0],'first',newdp[i])
for j in range(i):
if i-j>=x and days[-1]==0:
newdp[i]=min(newdp[i],dp[j]+0)
else:
#print(i,'i',j,'j')
newdp[i]=min(newdp[i],dp[j]+days[-1]-days[i-j]+1)
dp=[i for i in newdp]
#print(dp,'dp')
print(dp[-1])
```
No
| 107,589 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Tags: brute force, implementation
Correct Solution:
```
a = list(map(int, input().split()))
n = 14
f = lambda a: sum(x for x in a if x & 1 ^ 1)
def g(a, x):
b = a[:]
y = a[x]
b[x] = 0
for i in range(n):
b[i] += y // n
y %= n
for i in range(x + 1, y + x + 1):
b[i % 14] += 1
return f(b)
print(max(g(a, i) for i in range(n)))
```
| 107,590 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Tags: brute force, implementation
Correct Solution:
```
n = 14
a = None
def checkCase(defList, i):
global n
a = list(defList)
stone = a[i] % n
stoneAll = a[i] // n
a[i] = 0
j = i+1
while (stone > 0):
if (j >= n):
j = 0
a[j] += 1
j += 1
stone -= 1
pickedUp = 0
for k in range(n):
a[k] += stoneAll
if (a[k] % 2 == 0):
pickedUp += a[k]
return pickedUp
def main():
global n, a
a = [int(e) for e in input().split()]
maxStones = 0
for i in range(n):
if (a[i] > 0):
maxStones = max(maxStones, checkCase(a, i))
print(maxStones)
main()
```
| 107,591 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Tags: brute force, implementation
Correct Solution:
```
if __name__ == "__main__":
numbers = list(map(int, input().split()))
v = []
for i in range(14):
v.append(numbers[i])
result = 0
for i in range(14):
k = numbers[i]
v[i] = 0
c = int(k / 14)
d = k % 14
for j in range(14):
v[j] = v[j] + c
j = i + 1
while j <= 13 and d > 0:
v[j] = v[j] + 1
j = j + 1
d = d - 1
for j in range(d):
v[j] = v[j] + 1
suma = 0
for j in range(14):
if v[j] % 2 == 0:
suma = suma + v[j]
result = max(result, suma)
for j in range(14):
v[j] = numbers[j]
print(result)
```
| 107,592 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Tags: brute force, implementation
Correct Solution:
```
stones = list(map(int,input().split()))
initial_sum = 0
def even_sum(arr):
temp_sum = 0
for each in arr:
if(each%2 == 0):
temp_sum += each
return temp_sum
initial_sum = even_sum(stones)
dup_sum = initial_sum
for i in range(14):
duplicate = list(stones)
temp = stones[i]
duplicate[i] = 0
j = i
for each in range(14):
duplicate[each] += temp//14
temp = temp%14
while temp > 0 :
if( j == 13):
j = -1
j += 1
duplicate[j] += 1
temp -= 1
ts = even_sum(duplicate)
if(ts > initial_sum ):
initial_sum = ts
print(initial_sum)
```
| 107,593 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Tags: brute force, implementation
Correct Solution:
```
arr = list(map(int, input().split()))
ans = 0
for i in range(len(arr)):
pol = arr[i] // 14
os = arr[i] % 14
cur = 0
if((pol) % 2 == 0):
cur += pol
j = i + 1
if(j == 14):
j = 0
for u in range(13):
num = 0
if(u < os):
num += 1
num += arr[j] + pol
if(num % 2 == 0):
cur += num
j = j + 1
if(j == 14):
j = 0
if(cur > ans):
#print(arr[i])
ans = cur
print(ans)
```
| 107,594 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Tags: brute force, implementation
Correct Solution:
```
a=list(map(int,input().split()))
s=0
for i in range(14):
l=0
if a[i]%2==1:
k=a[i]//14
p=a[i]%14
b=a[:]
b[i]=0
for j in range(1,15):
b[(i+j)%14]+=k
for j in range(1,p+1):
b[(i+j)%14]=b[(i+j)%14]+1
for j in range(14):
if b[j]%2==0:
l+=b[j]
#print(b,k,p,i)
s=max(s,l)
print(s)
```
| 107,595 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Tags: brute force, implementation
Correct Solution:
```
def count(idx, nums):
val = nums[idx]
nums[idx] = 0
suma = 0
for i in range(1,15):
x = nums[(idx+i)%14] + (val-i+1+13)//14
if x%2==0:
suma += x
nums[idx] = val
return suma
nums = list(map(int, input().strip().split()))
maxi = 0
for i in range(len(nums)):
maxi = max(maxi, count(i, nums))
print(maxi)
```
| 107,596 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Tags: brute force, implementation
Correct Solution:
```
arr = list(map(int, input().split()))
summ = 0
for i in range(14):
copy = [0] * 14
now = arr[i]
for j in range(14):
copy[j] = arr[j]
copy[i] = 0
for k in range(i + 1, 14):
if now <= 0:
break
copy[k] += 1
now -= 1
s = now // 14
for k in range(14):
copy[k] += s
for k in range(now % 14):
copy[k] += 1
local_s = 0
for el in copy:
if el > 0 and el % 2 == 0:
local_s += el
if local_s > summ:
summ = local_s
print(summ)
```
| 107,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Submitted Solution:
```
l=list(map(int,input().split()))
l*=2
ans=0
for i in range(14):
s=0
if l[i]!=0:
a=l[i]
x=l[i]%14
y=l[i]//14
l[i]=0
l[i+14]=0
for j in range(1,15):
if j<=x:
if (l[j+i]+1+y) %2==0:
s+=l[j+i]+1+y
else:
if (l[j+i]+ y)%2==0:
s+=l[j+i]+ y
l[i]=a
l[i+14]=a
ans=max(ans,s)
print(ans)
```
Yes
| 107,598 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
<image>
Initially, each hole has a_i stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.
Input
The only line contains 14 integers a_1, a_2, …, a_{14} (0 ≤ a_i ≤ 10^9) — the number of stones in each hole.
It is guaranteed that for any i (1≤ i ≤ 14) a_i is either zero or odd, and there is at least one stone in the board.
Output
Output one integer, the maximum possible score after one move.
Examples
Input
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
4
Input
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
8
Note
In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.
Submitted Solution:
```
#!/usr/bin/env python3
from sys import stdin, stdout
def rint():
return map(int, stdin.readline().split())
#lines = stdin.readlines()
def solve(i):
tot_stone = a[i]
same_dist = tot_stone//14
res_dist = tot_stone%14
ans = 0
aa = a[:]
aa[i] = 0
for ii in range(14):
aa[ii] += same_dist
for ii in range(i+1, i+1+res_dist):
aa[ii%14] += 1
for ii in range(14):
if aa[ii] % 2 == 0:
ans += aa[ii]
return ans
a = list(rint())
max_ans = 0
for i in range(14):
s = solve(i)
max_ans = max(s, max_ans)
print(max_ans)
```
Yes
| 107,599 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.