message
stringlengths
2
43.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
853
107k
cluster
float64
24
24
__index_level_0__
int64
1.71k
214k
Provide tags and a correct Python 3 solution for this coding contest problem. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
instruction
0
78,735
24
157,470
Tags: brute force, dp Correct Solution: ``` # @author: guoyc # @date: 2018/7/21 N, a, b = [int(_) for _ in input().split()] h = [int(_) for _ in input().split()] t = h[-1] // b + 1 h[-1] -= t * b h[-2] -= t * a h[-3] -= t * b import sys res = [sys.maxsize, None] cnt = [0] * N # print(t) # print(h) def search(idx, cur): # print(idx, cur) if idx == N - 1: y = (h[idx - 1] // a + 1) if h[idx - 1] >= 0 else 0 # print('y: ', y) if cur + y < res[0]: res[0] = cur + y cnt[N - 2] += (t + y) res[1] = cnt[:] return x = (h[idx - 1] // b + 1) if h[idx - 1] >= 0 else 0 while cur + x < res[0]: h[idx - 1] -= x * b h[idx] -= x * a h[idx + 1] -= x * b cnt[idx] = x search(idx + 1, cur + x) h[idx - 1] += x * b h[idx] += x * a h[idx + 1] += x * b x += 1 search(1, t) print(res[0]) s = [] for i in range(N): for _ in range(res[1][i]): s.append(str(i + 1)) print(' '.join(s)) ```
output
1
78,735
24
157,471
Provide tags and a correct Python 3 solution for this coding contest problem. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
instruction
0
78,736
24
157,472
Tags: brute force, dp Correct Solution: ``` #!/usr/bin/env python def main(): n, a, b = map(int, input().split()) health = [0] health.extend(map(int, input().split())) target = [0] * len(health) def need(t, d): nonlocal health p = health[t] return (p // d + 1) if p >= 0 else 0 least_target = [] least_thrown = sum(health) last_need = max(need(n - 1, a), need(n, b)) def beat(left, sofar): nonlocal health, target, n, a, b, need nonlocal least_thrown, least_target, last_need if sofar >= least_thrown: return center = left + 1 right = center + 1 if right == n: m = max(need(left, b), last_need) thrown = sofar + m if thrown < least_thrown: least_thrown = thrown least_target = target[:] least_target[center] += m return must = need(left, b) for m in range(must, max(must, need(center, a)) + 1): ma = m * a mb = m * b health[left] -= mb health[center] -= ma health[right] -= mb target[center] += m beat(center, sofar + m) target[center] -= m health[right] += mb health[center] += ma health[left] += mb beat(1, 0) def target_sequence(targets): for t, k in enumerate(targets): if k: s = str(t) yield from (s for _ in range(k)) if least_target: print(least_thrown) print(" ".join(target_sequence(least_target))) else: print(0) print() if __name__ == '__main__': main() ```
output
1
78,736
24
157,473
Provide tags and a correct Python 3 solution for this coding contest problem. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
instruction
0
78,737
24
157,474
Tags: brute force, dp Correct Solution: ``` res = 10000 path = [] n, a, b = map(int, input().split()) def solve(cnt, idx, arc, p): global res global path if idx == n - 1: if res > cnt: res = cnt path = p.copy() return if cnt > res: return if arc[idx] < 0 and arc[idx - 1] < 0: solve(cnt, idx + 1, arc, p) else: min_t, max_t = arc[idx - 1] // b + 1, arc[idx] + 2 // a + 1 min_t = max(min_t, 0) max_t = max(min_t, max_t) for i in range(min_t, max_t + 1): arc[idx - 1] -= i * b arc[idx] -= i * a arc[idx + 1] -= i * b p.extend([idx + 1] * i) solve(cnt + i, idx + 1, arc, p) for _ in range(i): p.pop() arc[idx - 1] += i * b arc[idx] += i * a arc[idx + 1] += i * b h = list(map(int, input().split())) t = h[n - 1] // b + 1 h[n - 1] -= t * b h[n - 2] -= t * a h[n - 3] -= t * b tmp = [] tmp.extend([n - 1] * t) solve(t, 1, h, tmp) print(res) print(' '.join(str(i) for i in path)) ```
output
1
78,737
24
157,475
Provide tags and a correct Python 3 solution for this coding contest problem. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
instruction
0
78,738
24
157,476
Tags: brute force, dp Correct Solution: ``` __author__ = 'Darren' def solve(): n, a, b = map(int, input().split()) h = [0] h.extend(map(int, input().split())) fires = [] count = h[1] // b + 1 if h[1] >= 0 else 0 fires.extend([2 for i in range(count)]) h[1] -= b * count h[2] -= a * count h[3] -= b * count count = h[n] // b + 1 if h[n] >= 0 else 0 fires.extend([n-1 for i in range(count)]) h[n] -= b * count h[n-1] -= a * count h[n-2] -= b * count temp = fires.copy() for i in range(2, n): count = h[i] // a + 1 fires.extend([i for j in range(count)]) def search(pos): nonlocal n, a, b, h, fires, temp if pos == n and h[pos-1] < 0: if len(fires) > len(temp): fires = temp.copy() return balls = 0 count = h[pos-1] // b + 1 if h[pos-1] >= 0 else 0 temp.extend([pos for i in range(count)]) h[pos-1] -= b * count h[pos] -= a * count h[pos+1] -= b * count balls += count while h[pos] >= 0: search(pos+1) temp.append(pos) h[pos-1] -= b h[pos] -= a h[pos+1] -= b balls += 1 search(pos+1) h[pos-1] += b * balls h[pos] += a * balls h[pos+1] += b * balls for i in range(balls): temp.pop() search(2) print(len(fires)) print(' '.join(map(str, fires))) if __name__ == '__main__': solve() ```
output
1
78,738
24
157,477
Provide tags and a correct Python 3 solution for this coding contest problem. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
instruction
0
78,739
24
157,478
Tags: brute force, dp Correct Solution: ``` n,a,b=map(int,input().split()) h=list(map(int,input().split())) t=h[-1]//b+1 h[-1]-=t*b h[-2]-=t*a h[-3]-=t*b import sys res=[sys.maxsize,None] c=[0]*n def search(i,cur): if(i==n-1): res[0]=cur c[n-2]+=t res[1]=c[:] return if(h[i-1]>=0): x=(h[i-1]//b+1) else: x=0 while(cur+x<res[0]): h[i-1]-=x*b h[i]-=x*a h[i+1]-=x*b c[i]=x search(i+1,cur+x) h[i-1]+=x*b h[i]+=x*a h[i+1]+=x*b x+=1 search(1,t) print(res[0]) s=[] for i in range(n): for j in range(res[1][i]): s.append(str(i+1)) print(' '.join(s)) ```
output
1
78,739
24
157,479
Provide tags and a correct Python 3 solution for this coding contest problem. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
instruction
0
78,740
24
157,480
Tags: brute force, dp Correct Solution: ``` import sys n,a,b=map(int,input().split()) h=list(map(int,input().split())) t=h[-1] // b+1 h[-1]-=t*b h[-2]-=t*a h[-3]-=t*b res=[sys.maxsize,None] cnt=[0]*n def search(idx,cur): if idx==n-1: res[0]=cur cnt[n-2]+=t res[1]=cnt[:] return x=(h[idx-1]// b+1) if h[idx-1]>=0 else 0 while cur+x<res[0]: h[idx-1]-=x*b h[idx]-=x*a h[idx+1]-=x*b cnt[idx]=x search(idx+1,cur+x) h[idx-1]+=x*b h[idx]+=x*a h[idx+1]+=x*b x+=1 search(1,t) print(res[0]) s=[] for i in range(n): for j in range(res[1][i]): s.append(str(i+1)) print(' '.join(s)) ```
output
1
78,740
24
157,481
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 Submitted Solution: ``` n, a, b = list(map(int, input().split(" "))) h = list(map(lambda x: int(x) + 1, input().split(" "))) dp = [[[-1] * 17 for j in range(17)] for i in range(n)] num = [[[0] * 17 for j in range(17)] for i in range(n)] health = [[[0] * 17 for j in range(17)] for i in range(n)] result = [] def recursion(i, j, k): if(i == n - 1): return float("inf") if (j > 0) else 0 if(dp[i][j][k] != -1): return dp[i][j][k] dp[i][j][k] = float("inf") for s in range(17): if(j <= s * b): bt = s + recursion(i + 1, max(0, h[i] - k * b - s * a), s) if(bt < dp[i][j][k]): dp[i][j][k] = bt num[i][j][k] = s health[i][j][k] = max(0, h[i] - k * b - s * a) return dp[i][j][k] def compute(i, j, k): global result if(i == n - 1): return result += [str(i + 1)] * num[i][j][k] compute(i + 1, health[i][j][k], num[i][j][k]) while(h[0] > 0): h[0] = max(h[0] - b, 0) h[1] = max(h[1] - a, 0) h[2] = max(h[2] - b, 0) result.append(str(2)) while(h[n - 1] > 0): h[n - 1] = max(h[n - 1] - b, 0) h[n - 2] = max(h[n - 2] - a, 0) h[n - 3] = max(h[n - 3] - b, 0) result.append(str(n - 1)) recursion(1, 0, 0) compute(1, 0, 0) print(len(result)) print(" ".join(result)) ```
instruction
0
78,744
24
157,488
Yes
output
1
78,744
24
157,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 Submitted Solution: ``` n, a, b = [int(p) for p in input().split()] h = [int(p) for p in input().split()] ind = [] i = 1 while h[0] >= 0: h[0] -= b h[1] -= a h[2] -= b ind.append(2) while i < n-1: while h[i] >= 0: h[i] -= a h[i-1] -= b h[i+1] -= b ind.append(i+1) i += 1 while h[n-1] >= 0: h[n-1] -= b ind.append(n-1) print(len(ind)) print(*ind) ```
instruction
0
78,745
24
157,490
No
output
1
78,745
24
157,491
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 Submitted Solution: ``` n, a, b = map(int, input().split()) arr = list(map(int, input().split())) res = [] def helper(arr, path, memo): global res is_finish = True status = "" for val in arr: if val >= 0: is_finish = False status += str(val) # print(arr) if status in memo: return else: memo.add(status) if is_finish: if len(res) == 0 or len(path) < len(res): res = path.copy() return for i in range(1, len(arr) - 1): if arr[i] < 0 and arr[i - 1] < 0 and arr[i + 1] < 0: continue arr[i] -= a arr[i - 1] -= b arr[i + 1] -= b path.append(i) helper(arr, path, memo) path.pop() arr[i] += a arr[i - 1] += b arr[i + 1] += b return memo = set() helper(arr, [], memo) print([x + 1 for x in res]) ```
instruction
0
78,747
24
157,494
No
output
1
78,747
24
157,495
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
instruction
0
79,183
24
158,366
Tags: binary search, greedy Correct Solution: ``` n, m = map(int, input().split()) A = list(map(int, input().split())) A.sort() A.reverse() def func(i): ans = 0 for j in range(0, n): ans += max(A[j] - j // i, 0) return ans >= m l = 1 r = n while (r - l) > 1: s = (l + r) >> 1 if func(s) == 1: r = s else: l = s if func(l) == 1: print(l) elif func(r) == 1: print(r) else: print('-1') ```
output
1
79,183
24
158,367
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
instruction
0
79,184
24
158,368
Tags: binary search, greedy Correct Solution: ``` import math,sys from sys import stdin, stdout from collections import Counter, defaultdict, deque input = stdin.readline I = lambda:int(input()) li = lambda:list(map(int,input().split())) def solve(a,mid,s): c=0 i=0 p=0 while(i<len(a)): c+=max(0,a[i]-p) i=i+1 if(not i%mid): p+=1 if(c>=s): return(1) return(0) def case(test): n,s=li() a=li() if(sum(a)<s): print(-1) return a.sort(reverse=True) l,r=1,n m=n while(l<=r): #print(l,r) mid=(l+r)//2 p=solve(a,mid,s) if(p): r=mid-1 m=min(m,mid) else: l=mid+1 print(m) for test in range(1): case(test+1) ```
output
1
79,184
24
158,369
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
instruction
0
79,185
24
158,370
Tags: binary search, greedy Correct Solution: ``` import sys input = sys.stdin.readline def judge(x): b = a[:] for i in range(n): b[i] = max(0, b[i]-i//x) return sum(b)>=m def binary_search(): l, r = 1, n while l<=r: m = (l+r)//2 if judge(m): r = m-1 else: l = m+1 return l n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) ans = binary_search() if ans==n+1: print(-1) else: print(ans) ```
output
1
79,185
24
158,371
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
instruction
0
79,186
24
158,372
Tags: binary search, greedy Correct Solution: ``` def check_possibility(days, m, coffee): sum_coffee = 0 for i, cup in enumerate(coffee): tax = i // days if sum_coffee >= m or tax >= cup: break sum_coffee += cup - tax return sum_coffee >= m n, m = map(int, input().split()) coffee = sorted(map(int, input().split()), reverse=True) bad = 0 good = len(coffee) while good - bad > 1: days = (bad + good) // 2 if check_possibility(days, m, coffee): good = days else: bad = days possible = check_possibility(good, m, coffee) print(good if possible else -1) ```
output
1
79,186
24
158,373
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
instruction
0
79,187
24
158,374
Tags: binary search, greedy Correct Solution: ``` import sys,os,io from sys import stdin from math import log, gcd, ceil from collections import defaultdict, deque, Counter from heapq import heappush, heappop from bisect import bisect_left , bisect_right import math alphabets = list('abcdefghijklmnopqrstuvwxyz') def isPrime(x): for i in range(2,x): if i*i>x: break if (x%i==0): return False return True def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p def primeFactors(n): l = [] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: l.append(int(i)) n = n / i if n > 2: l.append(n) return list(set(l)) def power(x, y, p) : res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res def SieveOfEratosthenes(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def si(): return input() def prefix_sum(arr): r = [0] * (len(arr)+1) for i, el in enumerate(arr): r[i+1] = r[i] + el return r def divideCeil(n,x): if (n%x==0): return n//x return n//x+1 def ii(): return int(input()) def li(): return list(map(int,input().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') def power_set(L): cardinality=len(L) n=2 ** cardinality powerset = [] for i in range(n): a=bin(i)[2:] subset=[] for j in range(len(a)): if a[-j-1]=='1': subset.append(L[j]) powerset.append(subset) powerset_orderred=[] for k in range(cardinality+1): for w in powerset: if len(w)==k: powerset_orderred.append(w) return powerset_orderred def fastPlrintNextLines(a): # 12 # 3 # 1 #like this #a is list of strings print('\n'.join(map(str,a))) def sortByFirstAndSecond(A): A = sorted(A,key = lambda x:x[0]) A = sorted(A,key = lambda x:x[1]) return list(A) #__________________________TEMPLATE__________________OVER_______________________________________________________ if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w") else: input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def check(d,m,a): curSum = sum(a[:d]) minus = 1 # if d==4: # print(a) # print(curSum) if curSum>=m: return True temp = d for i in range(d,len(a)): curSum+=max(a[i]-minus,0) temp-=1 if temp==0: minus+=1 temp = d if curSum>=m: return True return False def solve(): n,m = li() a = li() l = 1 r = m ans = -1 a.sort(reverse=True) while(l<=r): mid = (l+r)//2 # print(mid) if check(mid,m,a): r = mid-1 ans = mid else: l = mid+1 print(ans) t = 1 # t = int(input()) for _ in range(t): solve() ```
output
1
79,187
24
158,375
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
instruction
0
79,188
24
158,376
Tags: binary search, greedy Correct Solution: ``` n, m = map(int, input().split()) A = list(map(int, input().split())) if sum(A) < m: print(-1) exit() A.sort(reverse=True) def is_ok(x): temp = 0 for i in range(n): temp += max(0, A[i]-i//x) if temp >= m: return True else: return False ng = 0 ok = n while ng+1 < ok: c = (ng+ok)//2 if is_ok(c): ok = c else: ng = c print(ok) ```
output
1
79,188
24
158,377
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
instruction
0
79,189
24
158,378
Tags: binary search, greedy Correct Solution: ``` import sys import math input = sys.stdin.readline for _ in range(1): n,m = map(int,input().split()) l = list(map(int,input().split())) l.sort(reverse = True) if sum(l) < m: print(-1) continue ans = 10**18 low = 1 high = n while low < high: mid = low+((high-low)//2) som = 0 for i in range(n): x = l[i] som = som+max((x-(i//mid)),0) if som >= m: high = mid else: low = mid+1 print(high) ```
output
1
79,189
24
158,379
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
instruction
0
79,190
24
158,380
Tags: binary search, greedy Correct Solution: ``` n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort(reverse=True) def check(d): s=0 for i in range(len(a)): s+=max(0,a[i]-i//d) return s>=m if sum(a)<m: print(-1) else: l, r = 1,n mid = l+r>>1 while l<r: if check(mid): r=mid else: l=mid+1 mid=l+r>>1 print(l) ```
output
1
79,190
24
158,381
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): for i in arr: stdout.write(str(i)+' ') stdout.write('\n') range = xrange # not for python 3.0+ # main code def check(n,m,l,mid): temp=0 c=0 k=mid for i in range(0,n,k): for j in range(i,min(n,i+k)): #print mid,i,j,l[j]-c temp+=max(0,l[j]-c) c+=1 #print temp,m if temp>=m: return 1 return 0 n,m=in_arr() l=in_arr() if sum(l)<m: print -1 exit() l.sort(reverse=True) l1=1 r1=n ans=n while l1<=r1: mid=(l1+r1)/2 if check(n,m,l,mid): ans=mid r1=mid-1 else: l1=mid+1 pr_num(ans) ```
instruction
0
79,191
24
158,382
Yes
output
1
79,191
24
158,383
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` # -*- coding: utf-8 -*- # @Time : 2019/2/24 22:29 # @Author : LunaFire # @Email : gilgemesh2012@gmail.com # @File : D2. Coffee and Coursework (Hard Version).py def check(a, k, m): s = 0 for i, v in enumerate(a): s += max(0, a[i] - i // k) return s >= m def main(): n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) left, right, ret = 1, n, n + 1 while left <= right: mid = (left + right) // 2 if check(a, mid, m): right = mid - 1 ret = mid else: left = mid + 1 print(ret if ret <= n else -1) if __name__ == '__main__': main() ```
instruction
0
79,192
24
158,384
Yes
output
1
79,192
24
158,385
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` def check(sz): aa=0 for i in range(n): aa+=max(0,a[i]-i//sz) if aa>=M: return True return False n,M=map(int,input().split()) a=list(map(int,input().split())) if sum(a)<M:print(-1);exit() a.sort(reverse=True) l=1;r=n while l<r: m=l+r m>>=1 if check(m): r=m else: l=m+1 print(l) ```
instruction
0
79,193
24
158,386
Yes
output
1
79,193
24
158,387
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` n,m=map(int,input().split()) a=list(map(int,input().split())) a.sort(reverse=True) def search(i): s=0 for j in range(n): s+=max(0,a[j]-j//i) return(s>=m) l,r=1,n while r-l>1: mid=(l+r)//2 if search(mid):r=mid else:l=mid if search(l):print(l) elif search(r):print(r) else:print(-1) ```
instruction
0
79,194
24
158,388
Yes
output
1
79,194
24
158,389
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` import sys from collections import defaultdict as dd from collections import deque from fractions import Fraction as f def eprint(*args): print(*args, file=sys.stderr) zz=1 from math import * import copy #sys.setrecursionlimit(10**6) if zz: input=sys.stdin.readline else: sys.stdin=open('input.txt', 'r') sys.stdout=open('all.txt','w') def li(): return [int(x) for x in input().split()] def fi(): return int(input()) def si(): return list(input().rstrip()) def mi(): return map(int,input().split()) def gh(): sys.stdout.flush() def bo(i): return ord(i)-ord('a') from copy import * from bisect import * from itertools import permutations def can(mid): c=0 for i in range(n): c+=max(0,a[i]-i//mid) return c>=m n,m=mi() a=li() if sum(a)<m: print(-1) exit(0) a.sort(reverse=True) l=1 r=10**15 while l<=r: mid=(l+r)//2 if can(mid): ans=mid r=mid-1 else: l=mid+1 print(ans) ```
instruction
0
79,195
24
158,390
Yes
output
1
79,195
24
158,391
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` # Preprocessing input n_cups, target = [int(i) for i in input().split()] cups = [int(i) for i in input().split()] cups = sorted(cups, reverse=True) found = False if sum(cups) < target: print(-1) found = True # Helper function def find_value(c, days): val = 0 for i in range(len(c)): val += max(c[i] - i // days, 0) return val # Binary search low = 1 high = n_cups while not found: current = (low + high) // 2 val = find_value(cups, current) if low == current or high == current: if val > target: print(current) found = True break else: print(current + 1) found = True break if val < target: low = current elif val > target: high = current else: print(current) found = True break ```
instruction
0
79,196
24
158,392
No
output
1
79,196
24
158,393
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` # Preprocessing input n_cups, target = [int(i) for i in input().split()] cups = [int(i) for i in input().split()] cups = sorted(cups, reverse=True) found = False if sum(cups) < target: print(-1) found = True # Helper function def find_value(c, days): val = 0 for i in range(len(c)): val += max(c[i] - i // days, 0) return val # Binary search low = 1 high = n_cups while not found: current = (low + high) // 2 val = find_value(cups, current) if low == current or high == current: if val > target: print(current) found = True else: print(current + 1) found = True if val < target: low = current elif val > target: high = current else: print(current) found = True ```
instruction
0
79,197
24
158,394
No
output
1
79,197
24
158,395
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` def drink(lst): s = 0 for i in range(len(lst)): s += max(lst[i] - i,0) return s #print(drink([2,1,1])) def page(day,n,arr): candy = [0 for i in range(day)] #j = 0 while n > 0: for j in range(day): if n > 0: candy[j] += 1 n -= 1 candy.sort() arr_sum = [] t = 0 for i in candy: arr_sum.append(arr[t:t+i]) t += i #print(arr_sum) total = 0 for k in arr_sum: total += drink(k) return total #return s #print(page(1,5,[3, 2, 2, 1, 1])) def binary(n,m,arr): #arr.sort() # ko can sortttttt #print(arr) l = 1 r = n ans = -1 while l <= r: mid = (l+r) //2 pages = page(mid,n,arr) # if mid == 1: # page = cfe # elif mid == n: # page = sum(arr) # else: # page = sum(arr[-mid+1::]) + cfe if pages >= m: r = mid - 1 ans = mid elif pages < m: l = mid + 1 return ans import sys # n, m = map(int,(sys.stdin.readline().split())) # cafe = list(map(int,sys.stdin.readline().split())) n,m = map(int,input().split()) cafe = list(map(int,input().split())) cafe.sort(reverse=True) print(binary(n,m,cafe)) ```
instruction
0
79,198
24
158,396
No
output
1
79,198
24
158,397
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup Polycarp has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` def check(num): l1,l2=n//num,n//num+1 c1,c2=num-n%num,n%num j,count=0,0 for i in range(c1): for k in range(l1): count+=max(0,c[j]-k) j+=1 for i in range(c2): for k in range(l2): count+=max(0,c[j]-k) j+=1 if count>=m: return True return False n,m=map(int,input().split()) c=list(map(int,input().split())) c.sort(reverse=True) if sum(c)<m: print (-1) exit() low=1 high=n while (low<high): mid=(low+high)//2 if check(mid): high=mid-1 else: low=mid+1 if check(low): print (low) else: print (low+1) ```
instruction
0
79,199
24
158,398
No
output
1
79,199
24
158,399
Provide tags and a correct Python 3 solution for this coding contest problem. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements.
instruction
0
79,604
24
159,208
Tags: dfs and similar, expression parsing, implementation, strings Correct Solution: ``` ls = [[] for _ in range(10**6//4)] maxd = 0 s=input().split(",") i = 0 stack = [1000000] for i in range(0, len(s), 2): w,c = s[i],int(s[i+1]) ls[len(stack)-1].append(w) stack[-1]-=1 stack.append(c) while stack[-1]==0: stack.pop() maxd=max(maxd,len(stack)) print(maxd) for i in range(maxd): print(*ls[i]) ```
output
1
79,604
24
159,209
Provide tags and a correct Python 3 solution for this coding contest problem. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements.
instruction
0
79,605
24
159,210
Tags: dfs and similar, expression parsing, implementation, strings Correct Solution: ``` t = input().split(',') w = t[::2] c = list(map(lambda x : int(x), t[1::2])) max_d = 0 cur_d = 0 cnt = [10**6] ans = [[] for i in range(10**6)] i = 0 while i < len(w): ans[cur_d].append(w[i]) cnt[cur_d] -= 1 if c[i] > 0: cnt.append(c[i]) cur_d += 1 max_d = max(max_d, cur_d) else: while cnt[cur_d] == 0: cnt.pop() cur_d -= 1 i += 1 print(max_d + 1) for i in range(max_d + 1): print(' '.join(ans[i])) ```
output
1
79,605
24
159,211
Provide tags and a correct Python 3 solution for this coding contest problem. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements.
instruction
0
79,606
24
159,212
Tags: dfs and similar, expression parsing, implementation, strings Correct Solution: ``` import sys from typing import List slist = input().split(',') # type: List[str] stack = [1<<30] # type: List[int] res = [[]] for s in slist: if s.isnumeric(): stack.append(int(s)) if len(stack) > len(res): res.append([]) else: while stack[-1] == 0: stack.pop() stack[-1] -= 1 res[len(stack)-1].append(s) while res[-1] == []: res.pop() print(len(res)) for l in res: if l: print(*l) ```
output
1
79,606
24
159,213
Provide tags and a correct Python 3 solution for this coding contest problem. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements.
instruction
0
79,607
24
159,214
Tags: dfs and similar, expression parsing, implementation, strings Correct Solution: ``` a = input().split(',') o = 0 for i in range(1, len(a), 2): a[i] = int(a[i]) b = [[] for i in range(300000)] c = [] for i in range(0, len(a), 2): b[len(c)] += [a[i]] if a[i + 1] == 0: if len(c) != 0: c[len(c) - 1] -= 1 else: if len(c) != 0: c[len(c) - 1] -= 1 c += [a[i + 1]] while len(c) > 0 and c[len(c) - 1] == 0: c.pop() o = max(o, len(c)) #print(*c) print(o + 1) for i in range(300000): if b[i] == []: break else: print(*b[i]) ```
output
1
79,607
24
159,215
Provide tags and a correct Python 3 solution for this coding contest problem. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements.
instruction
0
79,608
24
159,216
Tags: dfs and similar, expression parsing, implementation, strings Correct Solution: ``` line = input() line = line.split(',') A = 'abcdefghijklmnopqrstuvwxyz' depth = 1 comments = [[]] cur_cnt, cur_depth = [0], 1 for token in line: if token[0] in A or token[0] in A.upper(): comments[cur_depth - 1].append(token) cur_cnt[cur_depth - 1] -= 1 else: cnt = int(token) if cnt > 0: cur_depth += 1 if cur_depth > len(cur_cnt): cur_cnt.append(0) cur_cnt[cur_depth - 1] = cnt if cur_depth > depth: comments.append([]) depth = max(cur_depth, depth) else: while cur_depth > 1 and cur_cnt[cur_depth - 1] == 0: cur_depth -= 1 print(len(comments)) for line in comments: print(' '.join(line)) ```
output
1
79,608
24
159,217
Provide tags and a correct Python 3 solution for this coding contest problem. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements.
instruction
0
79,609
24
159,218
Tags: dfs and similar, expression parsing, implementation, strings Correct Solution: ``` t = input().split(',') k, m = 0, len(t) p, d = [m] * m, [[] for i in range(m)] for s, n in zip(t[::2], t[1::2]): while not p[k]: k -= 1 p[k] -= 1 d[k].append(s) k += 1 p[k] = int(n) while d[k]: k += 1 print(k) for q in d[:k]: print(*q) ```
output
1
79,609
24
159,219
Provide tags and a correct Python 3 solution for this coding contest problem. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements.
instruction
0
79,610
24
159,220
Tags: dfs and similar, expression parsing, implementation, strings Correct Solution: ``` tokens = input().split(',') depths = [] stk = [len(tokens)] for i in range(len(tokens)//2): tok = tokens[2*i] children = int(tokens[2*i+1]) y = stk.pop() y -= 1 stk.append(y) while len(depths) <= len(stk): depths.append([]) depths[len(stk)].append(tok) stk.append(children) while stk and stk[-1] == 0: stk.pop() print(len(depths)-1) for xs in depths[1:]: print(' '.join(xs)) ```
output
1
79,610
24
159,221
Provide tags and a correct Python 3 solution for this coding contest problem. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements.
instruction
0
79,611
24
159,222
Tags: dfs and similar, expression parsing, implementation, strings Correct Solution: ``` t = input().split(',') m = len(t) p, d, k = [m] * m, [[] for i in range(m)], 0 for i in range(0, m, 2): s, n = t[i], int(t[i + 1]) while p[k] == 0: k -= 1 p[k] -= 1 d[k].append(s) k += 1 p[k] = n while d[k]: k += 1 print(k) for q in d[:k]: print(*q) ```
output
1
79,611
24
159,223
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements. Submitted Solution: ``` #!/usr/bin/env python3 from collections import defaultdict def ri(): return map(int, input().split()) temp = input().split(',') node = temp[0::2] count = temp[1::2] count = list(map(int, count)) level = defaultdict(list) stack = [] l = 0 for j in range(len(node)): level[l].append(j) if count[j]: count[j] -= 1 stack.append(j) l += 1 else: while len(stack): if count[stack[-1]] == 0: stack.pop() l -= 1 else: count[stack[-1]] -= 1 break print(len(level)) for l in level: print(' '.join([node[i] for i in level[l]])) ```
instruction
0
79,612
24
159,224
Yes
output
1
79,612
24
159,225
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements. Submitted Solution: ``` levels = [] left = [-1] inp = input().split(",") i = 0 while i < len(inp): if len(levels) <= len(left) - 1: levels.append([]) t = inp[i] n = int(inp[i + 1]) levels[len(left) - 1].append(t) if n > 0: left.append(n) while left[-1] == 0: del left[-1] left[-1] -= 1 i += 2 print(len(levels)) for i in levels: print(" ".join(i)) ```
instruction
0
79,613
24
159,226
Yes
output
1
79,613
24
159,227
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements. Submitted Solution: ``` from queue import Queue def parse(s): A = s.split(',') n = len(A)//2 B = [(A[2*i], int(A[2*i+1])) for i in range(n)] stack = [] E = {i:[] for i in range(n+1)} comment = {} depth = {i:0 for i in range(n+1)} for i, b in enumerate(reversed(B)): comment[i+1] = b[0] for j in range(b[1]): k = stack.pop() E[i+1].append(k) depth[i+1] = max(depth[i+1], depth[k]) depth[i+1] += 1 stack.append(i+1) E[0].extend(reversed(stack)) depth[0] = max(depth[i] for i in stack) + 1 comment[0] = None Q = Queue() Q.put(0) res = [[] for i in range(depth[0])] while not Q.empty(): v = Q.get() d = depth[v] - 1 res[d].append(comment[v]) for u in E[v]: depth[u] = d Q.put(u) res.pop() return res s = input().rstrip() res = parse(s) print(len(res)) for layer in reversed(res): print(' '.join(layer)) ```
instruction
0
79,614
24
159,228
Yes
output
1
79,614
24
159,229
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements. Submitted Solution: ``` import sys s = sys.stdin.readline().strip().split(",") n = len(s) // 2 levels = [[]] stack = [] for i in range(n): st = s[i * 2] if (len(stack) + 1 > len(levels)): levels.append([]) levels[len(stack)].append(st) if (len(stack)): stack[-1] -= 1 nm = int(s[i * 2 + 1]) stack.append(nm) while (len(stack) and stack[-1] == 0): stack.pop() sys.stdout.write("{}\n".format(len(levels)) + '\n'.join(map(lambda x: ' '.join(x), levels)) + '\n') sys.stdout.flush() ```
instruction
0
79,615
24
159,230
Yes
output
1
79,615
24
159,231
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements. Submitted Solution: ``` line = input() line = line.split(',') A = 'abcdefghijklmnopqrstuvwxyz' depth, cur_depth = 1, 1 comments = [[]] cur_cnt = 0 for token in line: if token[0] in A or token[0] in A.upper(): comments[cur_depth - 1].append(token) if cur_depth > 1 and cur_cnt == 0: cur_depth -= 1 else: cur_cnt = int(token) if cur_cnt > 0: cur_depth += 1 if cur_depth > depth: comments.append([]) depth = cur_depth print(len(comments)) for line in comments: print(' '.join(line)) ```
instruction
0
79,616
24
159,232
No
output
1
79,616
24
159,233
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements. Submitted Solution: ``` #import math test = 1 vis = [False for i in range(int(1e6))] depth = [0 for i in range(int(1e6))] numlist = [] strlist = [] cur = 0 def dfs(v): vis[v]=1 global cur for i in range(numlist[v]): cur+=1 depth[cur] = depth[v]+1 dfs(cur) for tes in range(test): s = str(input()) s = s.split(',') for x in s: if x.isnumeric(): numlist.append(int(x)) else: strlist.append(x) print(strlist) for x in range(len(numlist)): if not vis[x]: cur = x dfs(x) ans = [[] for i in range(len(numlist)+1)] mxlen = 0 for x in range(len(numlist)): ans[depth[x]].append(strlist[x]) mxlen = max(mxlen, depth[x]+1) print(mxlen) for x in range(mxlen): for a in ans[x]: print(a, end=" ") print() ```
instruction
0
79,617
24
159,234
No
output
1
79,617
24
159,235
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements. Submitted Solution: ``` import sys Rcomments = list(sys.stdin.readline().strip().split(",")) comments=[] for i in range(0,len(Rcomments),2): comments.append([Rcomments[i],int(Rcomments[i+1])]) print(comments) visit = [False for i in comments] cind=0 ans = ["" for i in visit] def assign(val): global cind if visit[cind]: return visit[cind]=True ans[val] += (comments[cind][0]+" ") n = comments[cind][1] cind+=1 for i in range(n): assign(val+1) while cind<len(comments) and not visit[cind]: assign(0) for i in ans: if i!="": print(i) ```
instruction
0
79,618
24
159,236
No
output
1
79,618
24
159,237
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: * at first, the text of the comment is written; * after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); * after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: <image> then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: * at first, print a integer d — the maximum depth of nesting comments; * after that print d lines, the i-th of them corresponds to nesting level i; * for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Output Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Output 2 a A a A a A Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J Note The first example is explained in the statements. Submitted Solution: ``` #!/usr/bin/env python3 from collections import defaultdict def ri(): return map(int, input().split()) temp = input().split(',') node = temp[0::2] count = temp[1::2] count = list(map(int, count)) level = defaultdict(list) stack = [] l = 0 for j in range(len(node)): level[l].append(j) if count[j]: count[j] -= 1 stack.append(j) l += 1 else: while len(stack): if count[stack[-1]] == 0: stack.pop() l -= 1 else: count[stack[-1]] -= 1 break for l in level: print(' '.join([node[i] for i in level[l]])) ```
instruction
0
79,619
24
159,238
No
output
1
79,619
24
159,239
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if n = 5 and a = [1, 5, 4, 3, 2], then Polycarp could make the following moves: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the rightmost stone. After this move a = [5, 4, 3]; * Destroy the leftmost stone. After this move a = [4, 3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the leftmost stone. After this move a = [4, 3, 2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer t (1 ≤ t ≤ 100). Then t test cases follow. The first line of each test case contains one integer n (2 ≤ n ≤ 100) — the number of stones. The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 Output 2 4 5 3 2
instruction
0
80,181
24
160,362
Tags: brute force, dp, greedy Correct Solution: ``` t=int(input()) for i in range(t): n=int(input()) arr = list(map(int,input().split())) mn = min(arr) mx = max(arr) mni = arr.index(mn) mxi = arr.index(mx) if mxi>mni: print(min(mxi+1,n-mni,mni+1+n-mxi)) else: print(min(mni+1,n-mxi,mxi+1+n-mni)) ```
output
1
80,181
24
160,363
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if n = 5 and a = [1, 5, 4, 3, 2], then Polycarp could make the following moves: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the rightmost stone. After this move a = [5, 4, 3]; * Destroy the leftmost stone. After this move a = [4, 3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the leftmost stone. After this move a = [4, 3, 2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer t (1 ≤ t ≤ 100). Then t test cases follow. The first line of each test case contains one integer n (2 ≤ n ≤ 100) — the number of stones. The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 Output 2 4 5 3 2
instruction
0
80,182
24
160,364
Tags: brute force, dp, greedy Correct Solution: ``` t=int(input()) for i in range(t): n=int(input()) lst=list(map(int,input().split(' '))) mx=max(lst) mn=min(lst) mni=0 mnx=0 for i in range(n): if lst[i]==mn: mni=i if lst[i]==mx: mnx=i if mni<mnx: print(min(mnx+1,n-mni,mni+1+n-mnx)) else: print(min(mni+1,n-mnx,mnx+1+n-mni)) ```
output
1
80,182
24
160,365
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if n = 5 and a = [1, 5, 4, 3, 2], then Polycarp could make the following moves: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the rightmost stone. After this move a = [5, 4, 3]; * Destroy the leftmost stone. After this move a = [4, 3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the leftmost stone. After this move a = [4, 3, 2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer t (1 ≤ t ≤ 100). Then t test cases follow. The first line of each test case contains one integer n (2 ≤ n ≤ 100) — the number of stones. The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 Output 2 4 5 3 2
instruction
0
80,183
24
160,366
Tags: brute force, dp, greedy Correct Solution: ``` import math for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) a=max(l) b=min(l) c=l.index(a)+1 d=l.index(b)+1 e=n-c+1 f=n-d+1 l=min(c+f,d+e,max(c,d),max(e,f)) print(l) ```
output
1
80,183
24
160,367
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if n = 5 and a = [1, 5, 4, 3, 2], then Polycarp could make the following moves: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the rightmost stone. After this move a = [5, 4, 3]; * Destroy the leftmost stone. After this move a = [4, 3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the leftmost stone. After this move a = [4, 3, 2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer t (1 ≤ t ≤ 100). Then t test cases follow. The first line of each test case contains one integer n (2 ≤ n ≤ 100) — the number of stones. The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 Output 2 4 5 3 2
instruction
0
80,184
24
160,368
Tags: brute force, dp, greedy Correct Solution: ``` for t in range(int(input())): n = int(input()) l = [int(x) for x in input().split()] mni=0 mxi=0 for i in range(n): if(l[i]==1): mni=i if(l[i]==n): mxi=i a=min(mni,mxi) b=max(mni,mxi) p1=b+1 p2=n-a p3=(a+1)+(n-b) print(min(p1,p2,p3)) ```
output
1
80,184
24
160,369
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if n = 5 and a = [1, 5, 4, 3, 2], then Polycarp could make the following moves: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the rightmost stone. After this move a = [5, 4, 3]; * Destroy the leftmost stone. After this move a = [4, 3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the leftmost stone. After this move a = [4, 3, 2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer t (1 ≤ t ≤ 100). Then t test cases follow. The first line of each test case contains one integer n (2 ≤ n ≤ 100) — the number of stones. The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 Output 2 4 5 3 2
instruction
0
80,185
24
160,370
Tags: brute force, dp, greedy Correct Solution: ``` for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) mx,mn=0,0 for i in range(1,n): if(l[mx]<l[i]): mx=i elif (l[mn]>l[i]): mn=i if(mx<mn): mx,mn=mn,mx print(n-max(mn,mx-mn-1,n-mx-1)) ```
output
1
80,185
24
160,371
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if n = 5 and a = [1, 5, 4, 3, 2], then Polycarp could make the following moves: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the rightmost stone. After this move a = [5, 4, 3]; * Destroy the leftmost stone. After this move a = [4, 3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the leftmost stone. After this move a = [4, 3, 2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer t (1 ≤ t ≤ 100). Then t test cases follow. The first line of each test case contains one integer n (2 ≤ n ≤ 100) — the number of stones. The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 Output 2 4 5 3 2
instruction
0
80,186
24
160,372
Tags: brute force, dp, greedy Correct Solution: ``` from math import * sInt = lambda: int(input()) mInt = lambda: map(int, input().split()) lInt = lambda: list(map(int, input().split())) py = lambda: print("YES") pn = lambda: print("NO") t = sInt() for _ in range(t): n = sInt() a = lInt() b = sorted(a) ind = a.index(b[-1]) ind1 = a.index(b[0]) ans = [] ans.append(max(ind,ind1)+1) ans.append(ind+1+n-ind1) ans.append(ind1+1+n-ind) ans.append(max(n-ind,n-ind1)) print(min(ans)) ```
output
1
80,186
24
160,373
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if n = 5 and a = [1, 5, 4, 3, 2], then Polycarp could make the following moves: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the rightmost stone. After this move a = [5, 4, 3]; * Destroy the leftmost stone. After this move a = [4, 3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the leftmost stone. After this move a = [4, 3, 2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer t (1 ≤ t ≤ 100). Then t test cases follow. The first line of each test case contains one integer n (2 ≤ n ≤ 100) — the number of stones. The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 Output 2 4 5 3 2
instruction
0
80,187
24
160,374
Tags: brute force, dp, greedy Correct Solution: ``` for i in range(int(input())): n = int(input()) ar = list(map(int,input().split())) l_pos = ar.index(1)+1 r_pos = ar.index(n)+1 if l_pos>r_pos: temp = l_pos l_pos = r_pos r_pos = temp ans1 = l_pos-1 + n-r_pos +2 ans2 = n-l_pos+1 ans3 = r_pos-1+1 print(min(ans1, ans2, ans3)) ```
output
1
80,187
24
160,375
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if n = 5 and a = [1, 5, 4, 3, 2], then Polycarp could make the following moves: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the rightmost stone. After this move a = [5, 4, 3]; * Destroy the leftmost stone. After this move a = [4, 3]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: * Destroy the leftmost stone. After this move a = [5, 4, 3, 2]; * Destroy the leftmost stone. After this move a = [4, 3, 2]. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer t (1 ≤ t ≤ 100). Then t test cases follow. The first line of each test case contains one integer n (2 ≤ n ≤ 100) — the number of stones. The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 Output 2 4 5 3 2
instruction
0
80,188
24
160,376
Tags: brute force, dp, greedy Correct Solution: ``` def solve(arr,n,ans): i1,i2 = arr.index(min(arr)),arr.index(max(arr)) i1,i2 = min(i1,i2),max(i1,i2) min_val = min(i1+1+n-i2,i2+1,n-i1) ans.append(str(min_val)) def main(): t = int(input()) ans = [] for i in range(t): n = int(input()) arr = list(map(int,input().split())) solve(arr,n,ans) print('\n'.join(ans)) main() ```
output
1
80,188
24
160,377