message stringlengths 2 48.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 318 108k | cluster float64 8 8 | __index_level_0__ int64 636 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Dormi received a histogram with n bars of height a_1, a_2, …, a_n for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.
To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times:
* Select an index i (1 ≤ i ≤ n) where a_i>0, and assign a_i := a_i-1.
Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.
However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.
Consider the following example where the histogram has 4 columns of heights 4,8,9,6:
<image>
The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is 4+4+1+3+6 = 18, so if Little Dormi does not modify the histogram at all, the ugliness would be 18.
However, Little Dormi can apply the operation once on column 2 and twice on column 3, resulting in a histogram with heights 4,7,7,6:
<image>
Now, as the total vertical length of the outline (red lines) is 4+3+1+6=14, the ugliness is 14+3=17 dollars. It can be proven that this is optimal.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). Description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 4 ⋅ 10^5).
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 4 ⋅ 10^5.
Output
For each test case output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.
Example
Input
2
4
4 8 9 6
6
2 1 7 4 0 0
Output
17
12
Note
Example 1 is the example described in the statement.
The initial histogram for example 2 is given below:
<image>
The ugliness is currently 2+1+6+3+4=16.
By applying the operation once on column 1, six times on column 3, and three times on column 4, we can end up with a histogram with heights 1,1,1,1,0,0:
<image>
The vertical length of the outline is now 1+1=2 and Little Dormi made 1+6+3=10 operations, so the final ugliness is 2+10=12, which can be proven to be optimal.
Submitted Solution:
```
# import sys
# sys.stdin=open('input.txt','r')
# sys.stdout=open('output.txt','w')
def gcd(x, y):
while (y):
x, y = y, x % y
return x
def lcm(x,y):
return x*y//gcd(x,y)
def LSPtable(pattern):
n=len(pattern)
l=[0]*n
j=0
for i in range(1,n):
while j>0 and pattern[i]!=pattern[j]:
j=l[j-1]
if pattern[i]==pattern[j]:
l[i]=j+1
j+=1
else:
l[i]=0
return l
def KMPsearch(pattern,string):
lsp=LSPtable(pattern)
j=0
for i in range(len(string)):
while j>0 and string[i]!=pattern[j]:
j=lsp[j-1]
if string[i]==pattern[j]:
j+=1
if j== len(pattern):
return i-j+1
return -1
def getsum(BITTree,i):
s=0
while i>0:
s+=BITTree[i]
i-=i&(-i)
return s
def updatebit(BITTree,n,i,v):
i=i+1
while i<=n:
BITTree[i]+=v
i+=i&(-i)
def constructor(arr, n):
BITTree =[0]*(n+1)
for i in range(n):
updatebit(BITTree,n,i,arr[i])
return BITTree
def arrIn():
return list(map(int,input().split()))
def mapIn():
return map(int,input().split())
for ii in range(int(input())):
n=int(input())
for i in range(n):
x=1
arr = arrIn()
if n==1:
x=arr[0]
print(x)
continue
ans = 0
if arr[0] > arr[1]:
ans += arr[0] - arr[1]
arr[0] = arr[1]
y=2
z=1
x=ans
ans=x
for i in range(1, n - 1):
if arr[i] > max(arr[i + 1], arr[i - 1]):
ans += arr[i] - max(arr[i - 1], arr[i + 1])
arr[i] = max(arr[i - 1], arr[i + 1])
if arr[n - 2] < arr[n - 1]:
ans += arr[n - 1] - arr[n - 2]
arr[n - 1] = arr[n - 2]
ans += 2*max(arr)
print(ans)
``` | instruction | 0 | 91,988 | 8 | 183,976 |
No | output | 1 | 91,988 | 8 | 183,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Dormi received a histogram with n bars of height a_1, a_2, …, a_n for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.
To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times:
* Select an index i (1 ≤ i ≤ n) where a_i>0, and assign a_i := a_i-1.
Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.
However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.
Consider the following example where the histogram has 4 columns of heights 4,8,9,6:
<image>
The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is 4+4+1+3+6 = 18, so if Little Dormi does not modify the histogram at all, the ugliness would be 18.
However, Little Dormi can apply the operation once on column 2 and twice on column 3, resulting in a histogram with heights 4,7,7,6:
<image>
Now, as the total vertical length of the outline (red lines) is 4+3+1+6=14, the ugliness is 14+3=17 dollars. It can be proven that this is optimal.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). Description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 4 ⋅ 10^5).
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 4 ⋅ 10^5.
Output
For each test case output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.
Example
Input
2
4
4 8 9 6
6
2 1 7 4 0 0
Output
17
12
Note
Example 1 is the example described in the statement.
The initial histogram for example 2 is given below:
<image>
The ugliness is currently 2+1+6+3+4=16.
By applying the operation once on column 1, six times on column 3, and three times on column 4, we can end up with a histogram with heights 1,1,1,1,0,0:
<image>
The vertical length of the outline is now 1+1=2 and Little Dormi made 1+6+3=10 operations, so the final ugliness is 2+10=12, which can be proven to be optimal.
Submitted Solution:
```
# Rishabh Rao (https://github.com/rishabhrao)
import sys
MOD = 1000000007
def inp(): return sys.stdin.readline().strip()
def ii(): return int(inp())
def iis(): return [int(i) for i in inp().split()]
def solve():
n = ii()
a = [0] + iis()
min_ugliness = 0
for i in range(1, n):
if a[i] > a[i - 1] and a[i] > a[i + 1]:
biggest_neighbour = max(a[i - 1], a[i + 1])
min_ugliness += a[i] - biggest_neighbour
a[i] = biggest_neighbour
min_ugliness += abs(a[i] - a[i - 1])
min_ugliness += abs(a[n] - a[n - 1]) + a[n]
return min_ugliness
t = ii()
for _ in range(t):
print(solve())
``` | instruction | 0 | 91,989 | 8 | 183,978 |
No | output | 1 | 91,989 | 8 | 183,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Dormi received a histogram with n bars of height a_1, a_2, …, a_n for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.
To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times:
* Select an index i (1 ≤ i ≤ n) where a_i>0, and assign a_i := a_i-1.
Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.
However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.
Consider the following example where the histogram has 4 columns of heights 4,8,9,6:
<image>
The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is 4+4+1+3+6 = 18, so if Little Dormi does not modify the histogram at all, the ugliness would be 18.
However, Little Dormi can apply the operation once on column 2 and twice on column 3, resulting in a histogram with heights 4,7,7,6:
<image>
Now, as the total vertical length of the outline (red lines) is 4+3+1+6=14, the ugliness is 14+3=17 dollars. It can be proven that this is optimal.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). Description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 4 ⋅ 10^5).
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 4 ⋅ 10^5.
Output
For each test case output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.
Example
Input
2
4
4 8 9 6
6
2 1 7 4 0 0
Output
17
12
Note
Example 1 is the example described in the statement.
The initial histogram for example 2 is given below:
<image>
The ugliness is currently 2+1+6+3+4=16.
By applying the operation once on column 1, six times on column 3, and three times on column 4, we can end up with a histogram with heights 1,1,1,1,0,0:
<image>
The vertical length of the outline is now 1+1=2 and Little Dormi made 1+6+3=10 operations, so the final ugliness is 2+10=12, which can be proven to be optimal.
Submitted Solution:
```
def check(a):
ans=a[0]+a[n-1]
for i in range(1,n):
ans+=abs(a[i]-a[i-1])
return ans
for t in range(int(input())):
n=int(input())
a=list(map(int,input().split()))+[0]
l1=list(sorted(set(a),reverse=True))
ans=check(a)
m=True
l=0
for i in range(1,len(l1)):
for j in range(n):
if a[j]>l1[i]:
l+=abs(a[j]-l1[i])
a[j]=l1[i]
k=check(a)
if k+l>ans:
print(ans)
m=False
break
ans=min(ans,k+l)
if m:
print(ans)
``` | instruction | 0 | 91,990 | 8 | 183,980 |
No | output | 1 | 91,990 | 8 | 183,981 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
<image>
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
1. Add xi liters of water to the pi-th vessel;
2. Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Examples
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5 | instruction | 0 | 92,082 | 8 | 184,164 |
Tags: data structures, dsu, implementation, trees
Correct Solution:
```
def get_next_av(next_av, p):
q = p
while next_av[q] != q:
q = next_av[q]
while p != q:
next_av[p], p = q, next_av[p]
return p
def main():
from array import array
from sys import stdin
input = stdin.readline
n = int(input())
a = tuple(map(int, input().split()))
next_av = array('i', range(n + 1))
res = array('q', [0] * (n + 1))
for _ in range(int(input())):
inp = input()
if inp.startswith('1'):
_2, p, x = map(int, inp.split())
p -= 1
while p < n and res[p] + x >= a[p]:
x = res[p] + x - a[p]
res[p] = a[p]
next_av[p] = p = get_next_av(next_av, p + 1)
res[p] += x
else:
_3, k = map(int, inp.split())
print(res[k - 1])
main()
``` | output | 1 | 92,082 | 8 | 184,165 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
<image>
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
1. Add xi liters of water to the pi-th vessel;
2. Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Examples
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5 | instruction | 0 | 92,083 | 8 | 184,166 |
Tags: data structures, dsu, implementation, trees
Correct Solution:
```
n = int(input())
m = [int(i) for i in input().split()]
k = [0] * n
p = int(input())
l = list(range(1,n + 1))
j = []
for i in range(p):
t = [int(i) for i in input().split()]
if t[0] == 1:
h = []
a ,b= t[1] - 1,t[2]
while b > 0 and a < n:
if b <=m[a] - k[a]:
k[a] += b
break
else:
b = b -(m[a] - k[a])
k[a] = m[a]
h.append(a)
a=l[a]
for i in h:
l[i] = a
else:
j.append(k[t[1] - 1])
print(*j,sep='\n')
``` | output | 1 | 92,083 | 8 | 184,167 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
<image>
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
1. Add xi liters of water to the pi-th vessel;
2. Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Examples
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5 | instruction | 0 | 92,084 | 8 | 184,168 |
Tags: data structures, dsu, implementation, trees
Correct Solution:
```
#kylin1993
n=int(input());
water=(n+2)*[0];
vol=[int(i) for i in input().split()];
trace=(n+2)*[0];
next = [i+1 for i in range(n+2)];
m=int(input());
out=[]
for i in range(m):
c=[int(i) for i in input().split()];
if c[0]==1 :
w=c[2];
k=c[1]-1;
r=0;
while((w>0)and(k<n)):
if(w<=vol[k]-water[k]):
water[k]=water[k]+w;
break;
else:
w=w-(vol[k]-water[k]);
water[k]=vol[k];
trace[r]=k;
r=r+1;
k=next[k];
for j in range(r):
next[trace[j]]=k;
if c[0]==2:
out.append(water[c[1]-1]);
for i in out:
print(i)
``` | output | 1 | 92,084 | 8 | 184,169 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
<image>
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
1. Add xi liters of water to the pi-th vessel;
2. Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Examples
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5 | instruction | 0 | 92,085 | 8 | 184,170 |
Tags: data structures, dsu, implementation, trees
Correct Solution:
```
n=int(input());
water=(n+2)*[0];
vol=[int(i) for i in input().split()];
trace=(n+2)*[0];
next = [i+1 for i in range(n+2)];
m=int(input());
out=[]
for i in range(m):
c=[int(i) for i in input().split()];
if c[0]==1 :
w=c[2];
k=c[1]-1;
r=0;
while((w>0)and(k<n)):
if(w<=vol[k]-water[k]):
water[k]=water[k]+w;
break;
else:
w=w-(vol[k]-water[k]);
water[k]=vol[k];
trace[r]=k;
r=r+1;
k=next[k];
for j in range(r):
next[trace[j]]=k;
if c[0]==2:
out.append(water[c[1]-1]);
for i in out:
print(i)
``` | output | 1 | 92,085 | 8 | 184,171 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
<image>
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
1. Add xi liters of water to the pi-th vessel;
2. Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Examples
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5 | instruction | 0 | 92,086 | 8 | 184,172 |
Tags: data structures, dsu, implementation, trees
Correct Solution:
```
n = int(input()) + 1
a = list(map(int, input().split())) + [1 << 50]
l, p, r = [0] * n, list(range(n)), []
for i in range(int(input())):
t = list(map(int, input().split()))
if t[0] == 2:
r.append(l[t[1] - 1])
else:
x = t[1] - 1
s, d = [x], t[2]
while True:
if p[x] != x:
x = p[x]
s.append(x)
continue
if l[x] + d < a[x]:
l[x] += d
break
d -= a[x] - l[x]
l[x] = a[x]
x += 1
s.append(x)
for j in s:
p[j] = x
print('\n'.join(map(str, r)))
``` | output | 1 | 92,086 | 8 | 184,173 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
<image>
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
1. Add xi liters of water to the pi-th vessel;
2. Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Examples
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5 | instruction | 0 | 92,087 | 8 | 184,174 |
Tags: data structures, dsu, implementation, trees
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
m = int(input())
v = [0] * n
f = list(range(1, n + 1))
l = []
for i in range(m):
q = list(map(int, input().split()))
if q[0] == 1:
p, x = q[1], q[2]
u = []
j = p - 1
while j < n:
z = x + v[j] - a[j]
if z <= 0:
v[j] += x
break
else:
x = z
v[j] = a[j]
u.append(j)
j = f[j]
for z in u:
f[z] = j
else:
l.append(v[q[1] - 1])
for i in l:
print(i)
``` | output | 1 | 92,087 | 8 | 184,175 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
<image>
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
1. Add xi liters of water to the pi-th vessel;
2. Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Examples
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5 | instruction | 0 | 92,088 | 8 | 184,176 |
Tags: data structures, dsu, implementation, trees
Correct Solution:
```
from sys import stdin, stdout
n = int(stdin.readline()) + 1
a = list(map(int, stdin.readline().split())) + [1 << 50]
l, p, r = [0] * n, list(range(n)), []
for i in range(int(stdin.readline())):
t = list(map(int, stdin.readline().split()))
if t[0] == 2: r.append(l[t[1] - 1])
else:
x = t[1] - 1
s, d = [x], t[2]
while True:
if p[x] != x:
x = p[x]
s.append(x)
continue
if l[x] + d < a[x]:
l[x] += d
break
d -= a[x] - l[x]
l[x] = a[x]
x += 1
s.append(x)
for j in s: p[j] = x
stdout.write('\n'.join(map(str, r)))
``` | output | 1 | 92,088 | 8 | 184,177 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.
<image>
Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.
Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:
1. Add xi liters of water to the pi-th vessel;
2. Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
Input
The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).
Output
For each query, print on a single line the number of liters of water in the corresponding vessel.
Examples
Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5 | instruction | 0 | 92,089 | 8 | 184,178 |
Tags: data structures, dsu, implementation, trees
Correct Solution:
```
n=int(input())
a=[int(i) for i in input().split()]
v=[0]*n
m=int(input())
f=[i+1 for i in range(n)]
ans=[]
for i in range(m):
q=[int(i) for i in input().split()]
if q[0]==1:
p,x=q[1],q[2]
xyz=[]
j=p-1
while j<n:
tmp=x+v[j]-a[j]
if tmp<=0:
v[j] = x+v[j]
break
else:
x=tmp
v[j]=a[j]
xyz.append(j)
j=f[j]
for z in xyz:
f[z]=j
else:
ans.append(v[q[1]-1])
for i in ans:
print(i)
``` | output | 1 | 92,089 | 8 | 184,179 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | instruction | 0 | 92,369 | 8 | 184,738 |
Tags: greedy
Correct Solution:
```
""" 616C """
""" 1152B """
import math
# import sys
def main():
# n ,m= map(int,input().split())
# arr = list(map(int,input().split()))
# b = list(map(int,input().split()))
# n = int(input())
# string = str(input())
n ,k,l = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
index = -1
for i in range((n*k)-1,-1,-1):
if((a[i]-a[0])<=l):
index = i
break
if((index+1)<n):
print(0)
return
extra = (index+1)-n
start = 0
ans = 0
cnt = 0
while cnt<n:
ans += a[start]
start += min(extra,k-1)+1
extra -= min(extra,k-1)
cnt+=1
if extra<0:
extra = 0
print(ans)
return
main()
# def test():
# t = int(input())
# while t:
# main()
# t-=1
# test()
``` | output | 1 | 92,369 | 8 | 184,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | instruction | 0 | 92,370 | 8 | 184,740 |
Tags: greedy
Correct Solution:
```
s = [int(x) for x in input().split()]
n, k, ll = s[0], s[1], s[2]
s = [int(x) for x in input().split()]
s.sort()
if s[n - 1] - s[0] > ll:
print(0)
else:
x = 0
while x < n * k and s[x] <= ll + s[0]:
x += 1
if x > k * (n - 1):
q = 0
for i in range(n):
q += s[i * k]
print(q)
else:
add = []
left = x - n
while left > 0:
if left >= k - 1:
add.append(k - 1)
left -= k - 1
else:
add.append(left)
left = 0
add += [0 for _ in range(n)]
diff = [add[i] + 1 for i in range(n - 1)]
# print(diff)
q = s[0]
p = 0
for i in range(n - 1):
p += diff[i]
q += s[p]
print(q)
``` | output | 1 | 92,370 | 8 | 184,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | instruction | 0 | 92,371 | 8 | 184,742 |
Tags: greedy
Correct Solution:
```
n,k,L=map(int,input().split())
l=list(map(int,input().split()))
l=sorted(l)
s=0
v=n-1
p=0
ye=True
for i in range(len(l)-1,-1,-1) :
if abs(l[0]-l[i])>L :
p+=1
else :
if p>=k-1 and p>0 :
p-=k-1
s+=l[i]
else :
v=i
ye=False
break
if ye :
if p==0 :
print(s)
else :
print(0)
exit()
for i in range(0,v+1,k) :
s+=l[i]
print(s)
``` | output | 1 | 92,371 | 8 | 184,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | instruction | 0 | 92,372 | 8 | 184,744 |
Tags: greedy
Correct Solution:
```
import bisect
import sys
input = sys.stdin.readline
n, k, l = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
ans = [[] for i in range(n)]
max_min = a[0] + l
cnt = 0
for i in range(bisect.bisect_right(a, max_min)):
ans[cnt // k].append(a[i])
cnt += 1
nokori = 0
for i in range(n):
if not ans[i]:
nokori += 1
res = 0
for i in range(n)[::-1]:
for j, num in enumerate(ans[i][::-1]):
if nokori == 0:
break
if j == len(ans[i]) - 1:
continue
res += num
nokori -= 1
if nokori > 0:
print(0)
else:
for i in range(n):
if ans[i]:
res += ans[i][0]
print(res)
``` | output | 1 | 92,372 | 8 | 184,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | instruction | 0 | 92,373 | 8 | 184,746 |
Tags: greedy
Correct Solution:
```
n, k, l = [int(i) for i in input().split()]
arr = sorted([int(i) for i in input().split()])
ma = n*k-1
for i in range(n*k):
if arr[0] + l < arr[i]:
ma = i - 1
break
#print(arr)
#print(ma)
if ma < n-1:
print(0)
exit()
ans = 0
for i in range(0, ma + 1, k):
ans += arr[i]
n-=1
arr[i] = 0
if (n == 0):
break
for i in range(ma, -1, -1):
if n == 0:
break
if arr[i] == 0:
continue
n -= 1
ans += arr[i]
print(ans)
``` | output | 1 | 92,373 | 8 | 184,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | instruction | 0 | 92,374 | 8 | 184,748 |
Tags: greedy
Correct Solution:
```
def search(array, i, j, l):
if i > j or array[i] - array[0] > l:
return -1
mid = (i+j)//2
if array[mid] - array[0] > l:
return search(array, i, mid-1, l)
else:
return max(mid, search(array, mid+1, j, l))
if __name__ == '__main__':
n, k, l = input().split()
n = int(n)
k = int(k)
l = int(l)
a = input().split()
a = [int(x) for x in a]
a.sort()
ans = 0
if k == 1:
if a[n*k-1] - a[0] <= l:
i = 0
while i < n*k:
ans += a[i]
i += 1
else:
ans = 0
else:
ind = search(a,0, n*k-1, l)
if ind + 1 < n: ans = 0
else:
count = (n*k - ind - 1)//(k-1)
i = 0
while i < count:
ans += a[ind - i]
i+=1
i = 0
while i < n - count:
ans += a[i*k]
i += 1
print(ans)
``` | output | 1 | 92,374 | 8 | 184,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | instruction | 0 | 92,375 | 8 | 184,750 |
Tags: greedy
Correct Solution:
```
n,k,l=map(int,input().split())
a=sorted(list(map(int,input().split())))
dontuse=0
ans=0
for i in range(n*k-1,-1,-1):
dontuse+=1
if a[i]-a[0]<=l and dontuse>=k:
ans+=a[i]
dontuse-=k
if dontuse>0:
print(0)
else:
print(ans)
``` | output | 1 | 92,375 | 8 | 184,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | instruction | 0 | 92,376 | 8 | 184,752 |
Tags: greedy
Correct Solution:
```
n,k,l=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
x=0
while x<n*k-1 and a[x+1]-a[0]<=l:x+=1
if x+1<n:print(0);exit()
o=i=0
u=x-n+1
while i<=x:
o+=a[i]
u+=1
i=min(i+k,u)
print(o)
``` | output | 1 | 92,376 | 8 | 184,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
Submitted Solution:
```
n, k, l = list(map(int, input().split()))
arr = list(map(int, input().split()))
arr = sorted(arr)
if arr[n - 1] - arr[0] > l:
print(0)
else:
maxVol = arr[0] + l
index = 0
while index < n * k - 1 and arr[index + 1] <= maxVol: #index will be at most n * k - 1
index += 1
s = arr[0]
i = 0
remaining = n - 1
while remaining > 0 and i + k + remaining - 1 <= index: #jumps of k
i += k
remaining -= 1
s += arr[i]
if remaining > 0 and index - (remaining - 1) > 1 + i: #can now only make a jump of at most k - 1
i = (index - (remaining - 1))
s += arr[i]
remaining -= 1
while remaining > 0:
i += 1
s += arr[i]
remaining -= 1
print(s)
``` | instruction | 0 | 92,377 | 8 | 184,754 |
Yes | output | 1 | 92,377 | 8 | 184,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
Submitted Solution:
```
def doit(n, k, l, a):
m = n * k
a.sort()
hen = 0
tai = m
while ((hen < m) and (a[hen] <= a[0] + l)):
hen += 1
if (hen < n):
return 0
for i in range(n):
tai -= k
if (hen > tai):
break
hen -= 1
a[hen], a[tai] = a[tai], a[hen]
temp = 0
for i in range(n):
temp += a[i * k]
return temp
n, k, l = input().split()
a = input().split()
for i in range(len(a)):
a[i] = int(a[i])
print(doit(int(n), int(k), int(l), a))
``` | instruction | 0 | 92,378 | 8 | 184,756 |
Yes | output | 1 | 92,378 | 8 | 184,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
Submitted Solution:
```
def main():
n, k, l = map(int, input().split())
m = n * k
a = list(map(int, input().split()))
a.sort()
f = a[0]
i = 0
while (i < m and a[i] <= f + l):
i += 1
if (i != m):
i -= 1
else:
ans = 0
ind = 0
for i in range(n):
ans += a[ind]
ind += k
print(ans)
return
last = i
if (last + 1 < n):
print(0)
return
count = m - last - 1
ost = last + 1
ans = 0
i = last
while (i >= 0):
c = min(k - 1, count)
count -= c
i -= (k - c)
ans += a[i + 1]
print(ans)
main()
``` | instruction | 0 | 92,379 | 8 | 184,758 |
Yes | output | 1 | 92,379 | 8 | 184,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
Submitted Solution:
```
(n, k, l) = [int(i) for i in input().split()]
lengths = input().split()
for i in range(n * k):
lengths[i] = int(lengths[i])
lengths.sort()
smallest = lengths[0]
biggest = smallest + l
stop = len(lengths)
for i in range(len(lengths)):
if lengths[i] > biggest:
stop = i
break
if stop < n:
print(0)
else:
ans = 0
pos = 0
todo = n
for i in range(n):
ans += lengths[pos]
pos += 1
for j in range(k - 1):
if (stop - pos) > n - i - 1:
pos += 1
print(ans)
``` | instruction | 0 | 92,380 | 8 | 184,760 |
Yes | output | 1 | 92,380 | 8 | 184,761 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
Submitted Solution:
```
#-*- coding:utf-8 -*-
tmpstr = input().strip()
tmpvec = tmpstr.split(' ')
n = int(tmpvec[0])
k = int(tmpvec[1])
l = int(tmpvec[2])
tmpstr = input().strip()
tmpvec = tmpstr.split(' ')
vec_len = len(tmpvec)
data = map(int, tmpvec)
dlist = list(data)
dlist.sort()
index = 0
for i in range(n*k):
if dlist[i] > dlist[0] + l:
break
if(i != n * k -1):
i = i - 1
count = i + 1
tt = count
if i < n - 1:
print(0)
exit(0)
tail = i
start = 0
volume = 0
for i in range(n):
volume = volume + dlist[start]
if n + k <= count:
start = start + k
count = count - k
else:
diff = count - n
start = start + diff + 1
count = count - diff - 1
if n == 50000 and k ==2:
print(tt)
print(start)
print(volume)
``` | instruction | 0 | 92,381 | 8 | 184,762 |
No | output | 1 | 92,381 | 8 | 184,763 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
Submitted Solution:
```
#-*- coding:utf-8 -*-
tmpstr = input().strip()
tmpvec = tmpstr.split(' ')
n = int(tmpvec[0])
k = int(tmpvec[1])
l = int(tmpvec[2])
tmpstr = input().strip()
tmpvec = tmpstr.split(' ')
vec_len = len(tmpvec)
data = map(int, tmpvec)
dlist = list(data)
dlist.sort()
index = 0
for i in range(n*k):
if dlist[i] > dlist[0] + l:
break
if(i != n * k -1):
i = i - 1
count = i + 1
if i < n - 1:
print(0)
exit(0)
tail = i
start = 0
volume = 0
for i in range(n):
volume = volume + dlist[start]
if n + k <= count:
start = start + k
count = count - k
else:
diff = count - n
start = start + diff + 1
count = count - diff - 1
print(volume)
``` | instruction | 0 | 92,382 | 8 | 184,764 |
No | output | 1 | 92,382 | 8 | 184,765 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
Submitted Solution:
```
n, k, l = list(map(int, input().split()))
a = sorted(list(map(int, input().split(' '))))
# print(n, k, l, a)
if a[0] + l + 1 in a:
ok = a.index(a[0] + l + 1) - n
else:
ok = n*k - n
if ok < 0:
print(0)
else:
ans = 0
cur = 0
for i in range(n):
ans += a[cur]
use = min(k-1, ok)
ok -= use
cur += 1 + use
print(ans)
``` | instruction | 0 | 92,383 | 8 | 184,766 |
No | output | 1 | 92,383 | 8 | 184,767 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
<image>
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
Input
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Output
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Examples
Input
4 2 1
2 2 1 2 3 2 2 3
Output
7
Input
2 1 0
10 10
Output
20
Input
1 2 1
5 2
Output
2
Input
3 2 1
1 2 3 4 5 6
Output
0
Note
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
Submitted Solution:
```
n,k,l = map(int, input().split())
a=sorted(list(map(int, input().split())))
t1,t2=n-1,n*k
while t2-t1 > 1:
if a[(t1+t2)//2] <= a[0] + l:
t1=(t1+t2)//2
else:
t2=(t1+t2)//2
ans=0
for i in range(n):
if a[i*k] > a[t2+i-n]:
ans+=a[t2+i-n]
else:
ans+=a[i*k]
print(ans)
``` | instruction | 0 | 92,384 | 8 | 184,768 |
No | output | 1 | 92,384 | 8 | 184,769 |
Provide a correct Python 3 solution for this coding contest problem.
The following sorting operation is repeated for the stacked blocks as shown in Fig. A.
1. Stack all the bottom blocks (white blocks in Figure a) on the right edge (the remaining blocks will automatically drop one step down, as shown in Figure b).
2. If there is a gap between the blocks, pack it to the left to eliminate the gap (from Fig. B to Fig. C).
For an integer k greater than or equal to 1, a number represented by k × (k + 1) / 2 (example: 1, 3, 6, 10, ...) is called a triangular number. If the total number of blocks is a triangular number, it is expected that if the above sorting is repeated, the height of the left end will be 1 and the total number will increase by 1 toward the right (Fig. D shows the total number). For 15).
<image>
When the first sequence of blocks is given, when the triangle of the block as explained above is created by the operation less than the predetermined number of times, create a program that outputs the minimum number of operations until the triangle is obtained. please.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
N
b1 b2 ... bN
Each dataset has two rows and represents the first sequence of blocks. N (1 ≤ N ≤ 100) indicates the number of blocks in the bottom row. bi (1 ≤ bi ≤ 10000) indicates the number of blocks stacked in the i-th position from the left. bi and bi + 1 are separated by a single space. The total number of blocks is 3 or more.
The number of datasets does not exceed 20.
output
For each data set, the number of sorting operations performed until the triangle is formed is output on one line. However, if a triangle cannot be created or the number of operations exceeds 10000, -1 is output.
Example
Input
6
1 4 1 3 2 4
5
1 2 3 4 5
10
1 1 1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1 1
12
1 4 1 3 2 4 3 3 2 1 2 2
1
5050
3
10000 10000 100
0
Output
24
0
10
-1
48
5049
-1 | instruction | 0 | 92,532 | 8 | 185,064 |
"Correct Solution:
```
K = [i+1 for i in range(2000)]
while 1:
N = int(input())
if N == 0:
break
*S, = map(int, input().split())
if S == K[:len(S)]:
print(0)
continue
i = 1; s = 0; su = sum(S)
while s < su:
s += i; i += 1
if s != su:
print(-1)
continue
for t in range(10000):
T = []
for s in S:
if s > 1:
T.append(s-1)
T.append(len(S))
S, T = T, S
if S == K[:len(S)]:
print(t+1)
break
else:
print(-1)
``` | output | 1 | 92,532 | 8 | 185,065 |
Provide a correct Python 3 solution for this coding contest problem.
The following sorting operation is repeated for the stacked blocks as shown in Fig. A.
1. Stack all the bottom blocks (white blocks in Figure a) on the right edge (the remaining blocks will automatically drop one step down, as shown in Figure b).
2. If there is a gap between the blocks, pack it to the left to eliminate the gap (from Fig. B to Fig. C).
For an integer k greater than or equal to 1, a number represented by k × (k + 1) / 2 (example: 1, 3, 6, 10, ...) is called a triangular number. If the total number of blocks is a triangular number, it is expected that if the above sorting is repeated, the height of the left end will be 1 and the total number will increase by 1 toward the right (Fig. D shows the total number). For 15).
<image>
When the first sequence of blocks is given, when the triangle of the block as explained above is created by the operation less than the predetermined number of times, create a program that outputs the minimum number of operations until the triangle is obtained. please.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
N
b1 b2 ... bN
Each dataset has two rows and represents the first sequence of blocks. N (1 ≤ N ≤ 100) indicates the number of blocks in the bottom row. bi (1 ≤ bi ≤ 10000) indicates the number of blocks stacked in the i-th position from the left. bi and bi + 1 are separated by a single space. The total number of blocks is 3 or more.
The number of datasets does not exceed 20.
output
For each data set, the number of sorting operations performed until the triangle is formed is output on one line. However, if a triangle cannot be created or the number of operations exceeds 10000, -1 is output.
Example
Input
6
1 4 1 3 2 4
5
1 2 3 4 5
10
1 1 1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1 1
12
1 4 1 3 2 4 3 3 2 1 2 2
1
5050
3
10000 10000 100
0
Output
24
0
10
-1
48
5049
-1 | instruction | 0 | 92,533 | 8 | 185,066 |
"Correct Solution:
```
while True:
N = int(input())
if N == 0 : break
b = list(map(int, input().split()))
cnt = 0
while cnt <= 10000 :
tri_num = True
if b[0] != 1 : tri_num = False
for i in range( 1, len(b) ) :
if b[i] != b[i-1]+1 :
tri_num = False
break
if tri_num : break
new_column = len(b)
b_tmp = []
for i in b :
if i > 1 : b_tmp.append( i-1 )
b = b_tmp
b.append( new_column )
cnt += 1
if cnt <= 10000 : print(cnt)
else : print(-1)
``` | output | 1 | 92,533 | 8 | 185,067 |
Provide a correct Python 3 solution for this coding contest problem.
The following sorting operation is repeated for the stacked blocks as shown in Fig. A.
1. Stack all the bottom blocks (white blocks in Figure a) on the right edge (the remaining blocks will automatically drop one step down, as shown in Figure b).
2. If there is a gap between the blocks, pack it to the left to eliminate the gap (from Fig. B to Fig. C).
For an integer k greater than or equal to 1, a number represented by k × (k + 1) / 2 (example: 1, 3, 6, 10, ...) is called a triangular number. If the total number of blocks is a triangular number, it is expected that if the above sorting is repeated, the height of the left end will be 1 and the total number will increase by 1 toward the right (Fig. D shows the total number). For 15).
<image>
When the first sequence of blocks is given, when the triangle of the block as explained above is created by the operation less than the predetermined number of times, create a program that outputs the minimum number of operations until the triangle is obtained. please.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
N
b1 b2 ... bN
Each dataset has two rows and represents the first sequence of blocks. N (1 ≤ N ≤ 100) indicates the number of blocks in the bottom row. bi (1 ≤ bi ≤ 10000) indicates the number of blocks stacked in the i-th position from the left. bi and bi + 1 are separated by a single space. The total number of blocks is 3 or more.
The number of datasets does not exceed 20.
output
For each data set, the number of sorting operations performed until the triangle is formed is output on one line. However, if a triangle cannot be created or the number of operations exceeds 10000, -1 is output.
Example
Input
6
1 4 1 3 2 4
5
1 2 3 4 5
10
1 1 1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1 1
12
1 4 1 3 2 4 3 3 2 1 2 2
1
5050
3
10000 10000 100
0
Output
24
0
10
-1
48
5049
-1 | instruction | 0 | 92,534 | 8 | 185,068 |
"Correct Solution:
```
import math
def is_sankaku(v):
x = (math.sqrt(8*v + 1) - 1 ) / 2
return x == int(x)
def check(lst):
for i,v in enumerate(lst):
if v != i + 1:
return False
elif i == len(lst)-1:
return True
while 1:
N = int(input())
if N == 0:break
lst = list(map(int,input().split()))
if not is_sankaku(sum(lst)):
print(-1)
continue
result = -1
for count in range(10000):
if check(lst):
result = count
break
spam = len(lst)
lst = [x-1 for x in lst if x-1 > 0]
lst.append(spam)
print(result)
``` | output | 1 | 92,534 | 8 | 185,069 |
Provide a correct Python 3 solution for this coding contest problem.
The following sorting operation is repeated for the stacked blocks as shown in Fig. A.
1. Stack all the bottom blocks (white blocks in Figure a) on the right edge (the remaining blocks will automatically drop one step down, as shown in Figure b).
2. If there is a gap between the blocks, pack it to the left to eliminate the gap (from Fig. B to Fig. C).
For an integer k greater than or equal to 1, a number represented by k × (k + 1) / 2 (example: 1, 3, 6, 10, ...) is called a triangular number. If the total number of blocks is a triangular number, it is expected that if the above sorting is repeated, the height of the left end will be 1 and the total number will increase by 1 toward the right (Fig. D shows the total number). For 15).
<image>
When the first sequence of blocks is given, when the triangle of the block as explained above is created by the operation less than the predetermined number of times, create a program that outputs the minimum number of operations until the triangle is obtained. please.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
N
b1 b2 ... bN
Each dataset has two rows and represents the first sequence of blocks. N (1 ≤ N ≤ 100) indicates the number of blocks in the bottom row. bi (1 ≤ bi ≤ 10000) indicates the number of blocks stacked in the i-th position from the left. bi and bi + 1 are separated by a single space. The total number of blocks is 3 or more.
The number of datasets does not exceed 20.
output
For each data set, the number of sorting operations performed until the triangle is formed is output on one line. However, if a triangle cannot be created or the number of operations exceeds 10000, -1 is output.
Example
Input
6
1 4 1 3 2 4
5
1 2 3 4 5
10
1 1 1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1 1
12
1 4 1 3 2 4 3 3 2 1 2 2
1
5050
3
10000 10000 100
0
Output
24
0
10
-1
48
5049
-1 | instruction | 0 | 92,535 | 8 | 185,070 |
"Correct Solution:
```
tri_nums = [i * (i + 1) // 2 for i in range(1500)]
while True:
n = int(input())
if n == 0:
break
blst = list(map(int, input().split()))
if sum(blst) not in tri_nums:
print(-1)
continue
end = [i + 1 for i in range(tri_nums.index(sum(blst)))]
lenb = n
cnt = 0
while blst != end:
if cnt > 10000:
print(-1)
break
blst = [i - 1 for i in blst if i > 1]
blst.append(lenb)
lenb = len(blst)
cnt += 1
else:
print(cnt)
``` | output | 1 | 92,535 | 8 | 185,071 |
Provide a correct Python 3 solution for this coding contest problem.
The following sorting operation is repeated for the stacked blocks as shown in Fig. A.
1. Stack all the bottom blocks (white blocks in Figure a) on the right edge (the remaining blocks will automatically drop one step down, as shown in Figure b).
2. If there is a gap between the blocks, pack it to the left to eliminate the gap (from Fig. B to Fig. C).
For an integer k greater than or equal to 1, a number represented by k × (k + 1) / 2 (example: 1, 3, 6, 10, ...) is called a triangular number. If the total number of blocks is a triangular number, it is expected that if the above sorting is repeated, the height of the left end will be 1 and the total number will increase by 1 toward the right (Fig. D shows the total number). For 15).
<image>
When the first sequence of blocks is given, when the triangle of the block as explained above is created by the operation less than the predetermined number of times, create a program that outputs the minimum number of operations until the triangle is obtained. please.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
N
b1 b2 ... bN
Each dataset has two rows and represents the first sequence of blocks. N (1 ≤ N ≤ 100) indicates the number of blocks in the bottom row. bi (1 ≤ bi ≤ 10000) indicates the number of blocks stacked in the i-th position from the left. bi and bi + 1 are separated by a single space. The total number of blocks is 3 or more.
The number of datasets does not exceed 20.
output
For each data set, the number of sorting operations performed until the triangle is formed is output on one line. However, if a triangle cannot be created or the number of operations exceeds 10000, -1 is output.
Example
Input
6
1 4 1 3 2 4
5
1 2 3 4 5
10
1 1 1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1 1
12
1 4 1 3 2 4 3 3 2 1 2 2
1
5050
3
10000 10000 100
0
Output
24
0
10
-1
48
5049
-1 | instruction | 0 | 92,536 | 8 | 185,072 |
"Correct Solution:
```
l = list(range(1, 100000))
tri_num = set(i*(i+1)//2 for i in range(1, 100000))
while True:
N = int(input())
if N==0:
break
b = list(map(int, input().split()))
if sum(b) not in tri_num:
print(-1)
continue
ans = 0
while True:
flag = True
for i in range(len(b)):
if b[i]!=l[i]:
flag = False
if flag:
print(ans)
break
for i in range(len(b)):
b[i] -= 1
b.append(len(b));
b = [bi for bi in b if bi!=0]
ans += 1
if ans>10000:
print(-1)
break
``` | output | 1 | 92,536 | 8 | 185,073 |
Provide a correct Python 3 solution for this coding contest problem.
The following sorting operation is repeated for the stacked blocks as shown in Fig. A.
1. Stack all the bottom blocks (white blocks in Figure a) on the right edge (the remaining blocks will automatically drop one step down, as shown in Figure b).
2. If there is a gap between the blocks, pack it to the left to eliminate the gap (from Fig. B to Fig. C).
For an integer k greater than or equal to 1, a number represented by k × (k + 1) / 2 (example: 1, 3, 6, 10, ...) is called a triangular number. If the total number of blocks is a triangular number, it is expected that if the above sorting is repeated, the height of the left end will be 1 and the total number will increase by 1 toward the right (Fig. D shows the total number). For 15).
<image>
When the first sequence of blocks is given, when the triangle of the block as explained above is created by the operation less than the predetermined number of times, create a program that outputs the minimum number of operations until the triangle is obtained. please.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
N
b1 b2 ... bN
Each dataset has two rows and represents the first sequence of blocks. N (1 ≤ N ≤ 100) indicates the number of blocks in the bottom row. bi (1 ≤ bi ≤ 10000) indicates the number of blocks stacked in the i-th position from the left. bi and bi + 1 are separated by a single space. The total number of blocks is 3 or more.
The number of datasets does not exceed 20.
output
For each data set, the number of sorting operations performed until the triangle is formed is output on one line. However, if a triangle cannot be created or the number of operations exceeds 10000, -1 is output.
Example
Input
6
1 4 1 3 2 4
5
1 2 3 4 5
10
1 1 1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1 1
12
1 4 1 3 2 4 3 3 2 1 2 2
1
5050
3
10000 10000 100
0
Output
24
0
10
-1
48
5049
-1 | instruction | 0 | 92,537 | 8 | 185,074 |
"Correct Solution:
```
# Aizu Problem 0267: Triangle of Blocks
import sys, math, os, datetime
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
def is_triangle(heights):
if heights[0] != 1:
return False
L = len(heights)
diffs = [heights[k] - heights[k-1] for k in range(1, L)]
return diffs == [1] * (L - 1)
def solve(n, heights):
steps = 0
while steps < 10000:
if is_triangle(heights):
return steps
L = len(heights)
for k in range(L):
heights[k] -= 1
heights.append(L)
while heights.count(0) > 0:
del heights[heights.index(0)]
steps += 1
return -1
while True:
n = int(input())
if n == 0:
break
heights = [int(_) for _ in input().split()]
print(solve(n, heights))
``` | output | 1 | 92,537 | 8 | 185,075 |
Provide a correct Python 3 solution for this coding contest problem.
The following sorting operation is repeated for the stacked blocks as shown in Fig. A.
1. Stack all the bottom blocks (white blocks in Figure a) on the right edge (the remaining blocks will automatically drop one step down, as shown in Figure b).
2. If there is a gap between the blocks, pack it to the left to eliminate the gap (from Fig. B to Fig. C).
For an integer k greater than or equal to 1, a number represented by k × (k + 1) / 2 (example: 1, 3, 6, 10, ...) is called a triangular number. If the total number of blocks is a triangular number, it is expected that if the above sorting is repeated, the height of the left end will be 1 and the total number will increase by 1 toward the right (Fig. D shows the total number). For 15).
<image>
When the first sequence of blocks is given, when the triangle of the block as explained above is created by the operation less than the predetermined number of times, create a program that outputs the minimum number of operations until the triangle is obtained. please.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
N
b1 b2 ... bN
Each dataset has two rows and represents the first sequence of blocks. N (1 ≤ N ≤ 100) indicates the number of blocks in the bottom row. bi (1 ≤ bi ≤ 10000) indicates the number of blocks stacked in the i-th position from the left. bi and bi + 1 are separated by a single space. The total number of blocks is 3 or more.
The number of datasets does not exceed 20.
output
For each data set, the number of sorting operations performed until the triangle is formed is output on one line. However, if a triangle cannot be created or the number of operations exceeds 10000, -1 is output.
Example
Input
6
1 4 1 3 2 4
5
1 2 3 4 5
10
1 1 1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1 1
12
1 4 1 3 2 4 3 3 2 1 2 2
1
5050
3
10000 10000 100
0
Output
24
0
10
-1
48
5049
-1 | instruction | 0 | 92,538 | 8 | 185,076 |
"Correct Solution:
```
def Sankakusu(N) :
tmp = 0
i = 1
while tmp < N :
tmp = (i * (i + 1)) / 2
i += 1
if tmp == N :
return True
else :
return False
def Step_Check(List) :
for i in range(len(List)) :
if List[i] != i + 1 :
return False
return True
while True :
n = int(input())
if n == 0 :
break
count = 0
block = list(map(int, input().split()))
if not Sankakusu(sum(block)) :
print(-1)
else :
while True :
if Step_Check(block) :
print(count)
break
else :
count += 1
len_b = len(block)
for j in range(len_b) :
block[j] -= 1
block.append(len_b)
while 0 in block :
block.remove(0)
if count == 10001 :
print(-1)
break
``` | output | 1 | 92,538 | 8 | 185,077 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything. | instruction | 0 | 93,083 | 8 | 186,166 |
Tags: brute force, greedy, strings
Correct Solution:
```
def deep(arr):
temp = []
for i in arr:
temp.append(list(i))
return temp
n = int(input())
arr = []
for _ in range(n):
arr.append(list(map(int, input())))
temp = deep(arr)
ans = 0
for i in range(n):
arr = deep(temp)
for j in range(n):
if arr[i][j] == 0:
for k in range(n):
arr[k][j] = 1 - arr[k][j]
tot = 0
for j in range(n):
yay = 0
for k in range(n):
yay += arr[j][k]
if yay == n:
tot += 1
ans = max(ans, tot)
print(ans)
``` | output | 1 | 93,083 | 8 | 186,167 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything. | instruction | 0 | 93,084 | 8 | 186,168 |
Tags: brute force, greedy, strings
Correct Solution:
```
n=int(input())
z=[[int(k) for k in input().split()] for i in range(n)]
ans=0
for i in z:
ans=max(ans , z.count(i))
print(ans)
``` | output | 1 | 93,084 | 8 | 186,169 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything. | instruction | 0 | 93,085 | 8 | 186,170 |
Tags: brute force, greedy, strings
Correct Solution:
```
a=int(input())
d={}
for _ in " "*a:
z=input()
t=()
for i in range(a):
if z[i]=='0':t+=(i,)
d[t]=d.get(t,0)+1
print(max(d.values()))
``` | output | 1 | 93,085 | 8 | 186,171 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything. | instruction | 0 | 93,086 | 8 | 186,172 |
Tags: brute force, greedy, strings
Correct Solution:
```
n=int(input())
b=[list(map(int,list(input()))) for _ in range(n)]
print(max(sum(sum(b[j][k]^b[i][k]^1 for k in range(n))==n for j in range(n)) for i in range(n)))
``` | output | 1 | 93,086 | 8 | 186,173 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything. | instruction | 0 | 93,087 | 8 | 186,174 |
Tags: brute force, greedy, strings
Correct Solution:
```
n = int(input())
room = []
for _ in range(n):
row = tuple(input())
room.append(row)
memo = {}
for i in range(n):
try: memo[room[i]] += 1
except KeyError:
memo[room[i]] = 1
max_rows = max([(value, key) for key, value in memo.items()])
print(max_rows[0])
``` | output | 1 | 93,087 | 8 | 186,175 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything. | instruction | 0 | 93,088 | 8 | 186,176 |
Tags: brute force, greedy, strings
Correct Solution:
```
n = int(input())
c = []
for i in range(n):
c.append(input())
max_count = 0
for i in range(n):
count = 0
for j in range(n):
if c[i] == c[j]:
count += 1
if count > max_count:
max_count = count
print(max_count)
``` | output | 1 | 93,088 | 8 | 186,177 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything. | instruction | 0 | 93,089 | 8 | 186,178 |
Tags: brute force, greedy, strings
Correct Solution:
```
n = int(input())
cnt = {}
for i in range(n):
line = input()
if line not in cnt:
cnt[line] = 1
else:
cnt[line] += 1
print(max([cnt[r] for r in cnt]))
``` | output | 1 | 93,089 | 8 | 186,179 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything. | instruction | 0 | 93,090 | 8 | 186,180 |
Tags: brute force, greedy, strings
Correct Solution:
```
import sys
n = int(input())
s = [x for x in sys.stdin.read().split()]
m = 0
for a in s:
m = max(m, s.count(a))
print(m)
``` | output | 1 | 93,090 | 8 | 186,181 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything.
Submitted Solution:
```
n=int(input())
a=[]
while(n):
n-=1
a.append(input())
maxc=0
for i in range(len(a)):
c=0
for j in range(i,len(a)):
if(a[i]==a[j]):
c+=1
maxc=max(c,maxc)
print(maxc)
``` | instruction | 0 | 93,091 | 8 | 186,182 |
Yes | output | 1 | 93,091 | 8 | 186,183 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything.
Submitted Solution:
```
n=int(input())
s=[input() for i in range(n)]
print(max(s.count(x) for x in s))
``` | instruction | 0 | 93,092 | 8 | 186,184 |
Yes | output | 1 | 93,092 | 8 | 186,185 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything.
Submitted Solution:
```
a = [input() for i in range(int(input()))]
print(max([a.count(s) for s in a]))
``` | instruction | 0 | 93,093 | 8 | 186,186 |
Yes | output | 1 | 93,093 | 8 | 186,187 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything.
Submitted Solution:
```
n = int(input())
x = []
for i in range(n):
x.append(str(input()))
y = list(set(x))
ans = []
for i in range(len(y)):
ans.append(x.count(y[i]))
print(max(ans))
``` | instruction | 0 | 93,094 | 8 | 186,188 |
Yes | output | 1 | 93,094 | 8 | 186,189 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything.
Submitted Solution:
```
import sys
n = int(sys.stdin.readline().strip())
line = {}
max_val = 0
for l in range(n):
tmp = sys.stdin.readline().strip()
if tmp in line:
line[tmp] = line[tmp]+1
max_val = max_val if max_val > line[tmp] else line[tmp]
else:
line[tmp] = 1
print(max_val)
``` | instruction | 0 | 93,095 | 8 | 186,190 |
No | output | 1 | 93,095 | 8 | 186,191 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything.
Submitted Solution:
```
def sweep(a):
temp=0
for i in range(len(a)):
s=0
for j in range(len(a)):
if a[i] == a[j]:
s +=1
temp = max(temp,s)
return temp
###################################################
buffer_size = int(input())
a=[]
for i in range(buffer_size):
a.append(input())
print(a)
print(sweep(a))
``` | instruction | 0 | 93,096 | 8 | 186,192 |
No | output | 1 | 93,096 | 8 | 186,193 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything.
Submitted Solution:
```
# ===================================
# (c) MidAndFeed aka ASilentVoice
# ===================================
# import math
# import collections
# import string
# ===================================
n = int(input())
q = [str(x) for _ in range(n) for x in input().split() ]
ans = [[q.count(x), x] for x in set(q)]
ans.sort(reverse = True)
print(ans[0][1])
``` | instruction | 0 | 93,097 | 8 | 186,194 |
No | output | 1 | 93,097 | 8 | 186,195 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.
Return the maximum number of rows that she can make completely clean.
Input
The first line of input will be a single integer n (1 ≤ n ≤ 100).
The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.
Output
The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.
Examples
Input
4
0101
1000
1111
0101
Output
2
Input
3
111
111
111
Output
3
Note
In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.
In the second sample, everything is already clean, so Ohana doesn't need to do anything.
Submitted Solution:
```
__author__ = 'Adam'
import sys
n = sys.stdin.readline().replace('\n', '')
n = int(n)
x = []
for i in range(n):
x.append(sys.stdin.readline().replace('\n', ''))
print(n - len(set(x)) + 1)
``` | instruction | 0 | 93,098 | 8 | 186,196 |
No | output | 1 | 93,098 | 8 | 186,197 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.