message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n. It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct. To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored. Write the program that determines which tasks will be performed and which will be ignored. Input The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. Output Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. Examples Input 4 3 1 3 2 2 2 1 3 4 3 Output 6 -1 10 Input 3 2 3 2 3 5 1 2 Output 3 3 Input 8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8 Output 6 9 30 -1 15 36 Note In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10). In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. Submitted Solution: ``` from collections import defaultdict n, q = map(int, input().split()) S = [0] * (n + 1) S[0] = 1 s = n L = defaultdict(list) pre = -1 for i in range(q): t, k, d = map(int, input().split()) for j in range(pre, t): for l1 in L[j]: for l2 in l1: S[l2] = 0 s += 1 if s >= k: tmp = [] for j in range(1, n + 1): if S[j] == 0: S[j] = 1 tmp.append(j) if len(tmp) == k: break s -= k L[t + d - 1].append(tmp) print(sum(tmp)) pre = t else: print(-1) pre = t ```
instruction
0
58,541
14
117,082
Yes
output
1
58,541
14
117,083
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n. It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct. To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored. Write the program that determines which tasks will be performed and which will be ignored. Input The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. Output Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. Examples Input 4 3 1 3 2 2 2 1 3 4 3 Output 6 -1 10 Input 3 2 3 2 3 5 1 2 Output 3 3 Input 8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8 Output 6 9 30 -1 15 36 Note In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10). In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. Submitted Solution: ``` n, q = [int(i) for i in input().split()] available = [1 for i in range(n)] for task in range(q): t, k , d = [int(i) for i in input().split()] assign = [] len_assign = 0 for i in range(n): if available[i] <= t: assign.append(i) len_assign += 1 if len_assign >= k: break if len_assign >= k: for j in range(k): available[assign[j]] = t+d print(sum(assign)+k) else: print(-1) ```
instruction
0
58,542
14
117,084
Yes
output
1
58,542
14
117,085
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n. It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct. To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored. Write the program that determines which tasks will be performed and which will be ignored. Input The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. Output Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. Examples Input 4 3 1 3 2 2 2 1 3 4 3 Output 6 -1 10 Input 3 2 3 2 3 5 1 2 Output 3 3 Input 8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8 Output 6 9 30 -1 15 36 Note In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10). In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. Submitted Solution: ``` n, q = map(int, input().split()) a = [0]*n for _ in range(q): t, k, d = map(int, input().split()) c = 0 test = 0 val = [] for i in range(n): if a[i]<=t: val.append(i) c+=(i+1) k-=1 if k==0: test = 1 break if test: for i in val: a[i]=t+d print(c) else: print(-1) ```
instruction
0
58,543
14
117,086
Yes
output
1
58,543
14
117,087
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n. It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct. To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored. Write the program that determines which tasks will be performed and which will be ignored. Input The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. Output Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. Examples Input 4 3 1 3 2 2 2 1 3 4 3 Output 6 -1 10 Input 3 2 3 2 3 5 1 2 Output 3 3 Input 8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8 Output 6 9 30 -1 15 36 Note In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10). In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. Submitted Solution: ``` for t in range(1): n,q=map(int,input().split()) time=[0]*n for i in range(q): t,k,d=map(int,input().split()) count=0 pos=0 for j in range(n): if time[j]<=t: pos+=j+1 count+=1 if count==k: break if count<k: print(-1) continue changed=0 for j in range(n): if time[j]<=t: time[j]=t+d changed+=1 if changed==count: break print(pos) ```
instruction
0
58,544
14
117,088
Yes
output
1
58,544
14
117,089
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n. It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct. To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored. Write the program that determines which tasks will be performed and which will be ignored. Input The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. Output Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. Examples Input 4 3 1 3 2 2 2 1 3 4 3 Output 6 -1 10 Input 3 2 3 2 3 5 1 2 Output 3 3 Input 8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8 Output 6 9 30 -1 15 36 Note In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10). In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. Submitted Solution: ``` import heapq n, q = [int(i) for i in input().split(' ')] server = [i for i in range(1, n+1)] running = [] for _ in range(q): t, k, d = [int(i) for i in input().split(' ')] if running: not_yet = [] while running: r = running.pop() if t - r['t'] >= r['d']: server += r['occupied'] else: not_yet.append(r) running = not_yet heapq.heapify(server) if len(server) < k: print(-1) continue else: # run_server = [heapq.heappop(server) for _ in range(k)] running.append({'t': t, 'd': d, 'occupied': server[0:k].copy()}) print(sum(server[0:k])) server = server[k:] ```
instruction
0
58,545
14
117,090
No
output
1
58,545
14
117,091
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n. It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct. To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored. Write the program that determines which tasks will be performed and which will be ignored. Input The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. Output Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. Examples Input 4 3 1 3 2 2 2 1 3 4 3 Output 6 -1 10 Input 3 2 3 2 3 5 1 2 Output 3 3 Input 8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8 Output 6 9 30 -1 15 36 Note In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10). In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. Submitted Solution: ``` f = lambda: map(int, input().split()) n, q = f() s = [0] * n for i in range(q): t, k, d = f() p = [j for j in range(n) if s[j] <= t][:k] p = [] if len(p) < k else p[:k] for j in p: s[j] = t + d print(-1 if p else sum(p) + k) ```
instruction
0
58,546
14
117,092
No
output
1
58,546
14
117,093
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n. It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct. To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored. Write the program that determines which tasks will be performed and which will be ignored. Input The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. Output Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. Examples Input 4 3 1 3 2 2 2 1 3 4 3 Output 6 -1 10 Input 3 2 3 2 3 5 1 2 Output 3 3 Input 8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8 Output 6 9 30 -1 15 36 Note In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10). In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. Submitted Solution: ``` n, q = map(int, input().split()) E = [] for i in range(q): t, k, d = map(int, input().split()) E.append([t, 1, k, i]) E.append([t + d, -1, k, i]) E.sort() b = 0 ans = [] get_it = [0] * (q + 1) for t, typ, k, i in E: if typ == -1 and get_it[i]: b -= k elif typ == 1: if b + k <= n: get_it[i] = True ans.append((b + k) * (b + k + 1) // 2 - b * (b + 1) // 2) b += k else: ans.append(-1) print('\n'.join(list(map(str, ans)))) ```
instruction
0
58,547
14
117,094
No
output
1
58,547
14
117,095
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n. It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct. To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored. Write the program that determines which tasks will be performed and which will be ignored. Input The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. Output Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. Examples Input 4 3 1 3 2 2 2 1 3 4 3 Output 6 -1 10 Input 3 2 3 2 3 5 1 2 Output 3 3 Input 8 6 1 3 20 4 2 1 6 5 5 10 1 1 15 3 6 21 8 8 Output 6 9 30 -1 15 36 Note In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10). In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. Submitted Solution: ``` a,b=map(int,input().split()) l=[a]*1000001 for i in range(b): t,k,d=map(int,input().split()) h=a r=0 g=True for i in range(t,t+d): l[i]=l[i]-k if l[i]<0: print(-1) g=False break else: h=min(l[i],h) if g: for i in range(k): r+=a-i-h print(r) ```
instruction
0
58,548
14
117,096
No
output
1
58,548
14
117,097
Provide a correct Python 3 solution for this coding contest problem. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85
instruction
0
58,666
14
117,332
"Correct Solution: ``` N=int(input()) *A,=map(int, input().split()) B=[(i+1,a) for i,a in enumerate(A)] from operator import itemgetter B.sort(reverse=True, key=itemgetter(1)) dp = [[-1] * (N+1) for _ in range(N+1)] dp[0][0] = 0 for i in range(1,N+1): idx, act = B[i-1] for j in range(i+1): k = i-j if 0<j: dp[j][k] = max(dp[j][k], dp[j-1][k] + act * abs(idx-j)) if 0<k: dp[j][k] = max(dp[j][k], dp[j][k-1] + act * abs(idx-(N-k+1))) ans=0 for j in range(N+1): ans = max(ans, dp[j][N-j]) print(ans) ```
output
1
58,666
14
117,333
Provide a correct Python 3 solution for this coding contest problem. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85
instruction
0
58,667
14
117,334
"Correct Solution: ``` def solve(n, aaa): dp = [0] aaa_with_idx = list(zip(aaa, range(n))) aaa_with_idx.sort(reverse=True) for k in range(1, n + 1): ndp = [0] * (k + 1) a, i = aaa_with_idx[k - 1] for l in range(k): # 左に配置 ndp[l + 1] = max(ndp[l + 1], dp[l] + a * abs(i - l)) # 右に配置 r = n - (k - l) ndp[l] = max(ndp[l], dp[l] + a * abs(i - r)) dp = ndp return max(dp) n = int(input()) aaa = list(map(int, input().split())) print(solve(n, aaa)) ```
output
1
58,667
14
117,335
Provide a correct Python 3 solution for this coding contest problem. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85
instruction
0
58,668
14
117,336
"Correct Solution: ``` e=enumerate n,a=open(0) d=[0]+[-1e18]*2001 for j,(a,i)in e(sorted((-int(a),i)for i,a in e(a.split()))):d=[max(t-a*abs(~i-j+k+int(n)),d[k-1]-a*abs(~i+k))for k,t in e(d)] print(max(d)) ```
output
1
58,668
14
117,337
Provide a correct Python 3 solution for this coding contest problem. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85
instruction
0
58,669
14
117,338
"Correct Solution: ``` N = int(input()) def solve(a, i, prev): r = N - len(prev) - i + 1 p = -i*a for j, s in enumerate(prev): yield p+abs(j-i)*a, s+abs(j+r)*a p = s yield s+abs(len(prev)-i)*a, pd = [0] A = map(int, input().split()) for a,i in sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True): pd = [*map(max, solve(a,i, pd))] print(max(pd)) ```
output
1
58,669
14
117,339
Provide a correct Python 3 solution for this coding contest problem. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85
instruction
0
58,670
14
117,340
"Correct Solution: ``` e=enumerate n,*a=map(int,open(0).read().split()) dp=[0] for j,(a,i)in e(sorted((a,i)for i,a in e(a))[::-1]):dp=[dp[0]+a*abs(n-j-i-1)]+[max(dp[k]+a*abs(n-j+k-i-1),dp[k-1]+a*abs(i-k+1))for k in range(1,j+1)]+[dp[j]+a*abs(i-j)] print(max(dp)) ```
output
1
58,670
14
117,341
Provide a correct Python 3 solution for this coding contest problem. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85
instruction
0
58,671
14
117,342
"Correct Solution: ``` n = int(input()) A = [(a, i) for i, a in enumerate(map(int, input().split()))] A.sort(reverse=True) dp = [[0]*(n+1) for _ in range(n+1)] ans = 0 for L in range(n+1): for R in range(n+1): if n <= L+R-1: continue a, i = A[L+R-1] if 0 <= R-1: dp[L][R] = max(dp[L][R], dp[L][R-1]+abs((n-R)-i)*a) if 0 <= L-1: dp[L][R] = max(dp[L][R], dp[L-1][R]+abs(i-(L-1))*a) ans = max(ans, dp[L][R]) print(ans) ```
output
1
58,671
14
117,343
Provide a correct Python 3 solution for this coding contest problem. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85
instruction
0
58,672
14
117,344
"Correct Solution: ``` N = int(input()) A = map(int, input().split()) AI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True) def solve(a, i, prev): pl, pr, ps = i, 0, 0 for l, r, s in prev: yield l, r-1, max(s+abs(r-i)*a, ps+abs(i-pl)*a) pl, pr, ps = l, r, s yield pl+1, pr, ps+abs(i-pl)*a prev = [(1, N, 0)] for a,i in AI: prev = [*solve(a,i, prev)] print(max(s for l, r, s in prev)) ```
output
1
58,672
14
117,345
Provide a correct Python 3 solution for this coding contest problem. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85
instruction
0
58,673
14
117,346
"Correct Solution: ``` from heapq import heappush, heappop n = int(input()) A = list(map(int, input().split())) f_inf = float('inf') dp = [[0 for _ in range(n+1)] for _ in range(n+1)] dp[0][0] = 0 hq = [] for i in range(n): heappush(hq, (-A[i], i)) ans = 0 for i in range(n): x, pi = heappop(hq) for l in range(i+1): r = i-l dp[i+1][l+1] = max(dp[i+1][l+1], dp[i][l] + (pi-l)*A[pi]) dp[i+1][l] = max(dp[i+1][l], dp[i][l] + ((n-r-1)-pi)*A[pi]) ans = 0 for i in range(n+1): ans = max(ans, dp[n][i]) print(ans) ```
output
1
58,673
14
117,347
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85 Submitted Solution: ``` # かつっぱさん見て from collections import defaultdict n=int(input()) a=list(map(int,input().split())) babies=[(q,i) for i,q in enumerate(a)] babies.sort(reverse=True) dp=defaultdict(int) ans=0 for l in range(n): for r in range(n): if l+r==n: ans=max(ans,dp[(l,r)]) break act,ind=babies[l+r] dp[(l+1,r)]=max(dp[(l+1,r)],dp[(l,r)]+act*abs(ind-l)) dp[(l,r+1)]=max(dp[(l,r+1)],dp[(l,r)]+act*abs(ind-(n-1-r))) print(ans) ```
instruction
0
58,674
14
117,348
Yes
output
1
58,674
14
117,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85 Submitted Solution: ``` def solve(): N = int(input()) A = [(a, i+1) for i, a in enumerate(map(int, input().split()))] A.sort(reverse=True) INF = float('inf') dp = [[-INF] * (N+1) for _ in range(N+1)] dp[0][0] = 0 for s in range(1, N+1): for l in range(s+1): r = s - l dp[l][r] = max(dp[l-1][r] + A[s-1][0] * abs(A[s-1][1]-l), dp[l][r-1] + A[s-1][0] * abs(N-r+1-A[s-1][1])) ans = 0 for m in range(N): if dp[m][N-m] > ans: ans = dp[m][N-m] print(ans) solve() ```
instruction
0
58,675
14
117,350
Yes
output
1
58,675
14
117,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85 Submitted Solution: ``` from heapq import heappush, heappop n = int(input()) A = list(map(int, input().split())) MX = 2005 f_inf = float('inf') dp = [[0 for _ in range(MX)] for _ in range(MX)] dp[0][0] = 0 hq = [] for i in range(n): heappush(hq, (-A[i], i)) ans = 0 for i in range(n): x, pi = heappop(hq) for l in range(i+1): r = i-l dp[i+1][l+1] = max(dp[i+1][l+1], dp[i][l] + (pi-l)*A[pi]) dp[i+1][l] = max(dp[i+1][l], dp[i][l] + ((n-r-1)-pi)*A[pi]) ans = 0 for i in range(n+1): ans = max(ans, dp[n][i]) print(ans) ```
instruction
0
58,676
14
117,352
Yes
output
1
58,676
14
117,353
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85 Submitted Solution: ``` n=int(input()) d=[0]+[-10**18]*n for j,(a,i)in enumerate(sorted((-a,i)for i,a in enumerate(map(int,input().split())))): d=[max(t-a*abs(~i-j+k+n),d[k-1]-a*abs(~i+k))for k,t in enumerate(d)] print(max(d)) ```
instruction
0
58,677
14
117,354
Yes
output
1
58,677
14
117,355
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85 Submitted Solution: ``` n = int(input()) a = [(A, i) for i, A in enumerate(map(int, input().split()))] a.sort(reverse=True) dp = [[-1]*(n+1) for _ in range(n+1)] dp[0][0] = 0 for num, (A, pos) in enumerate(a): for i in range(num+1): j = num - i dp[i+1][j] = max(dp[i+1][j], dp[i][j] + A*abs(pos-i)) # 左からi番目の時 dp[i][j+1] = max(dp[i][j+1], dp[i][j] + A*abs((n-1-j)-pos)) # 右からj番目の時 ans = 0 for i in range(n+1): j = n-i ans = max(ans, dp[i][j]) print(ans) ```
instruction
0
58,678
14
117,356
No
output
1
58,678
14
117,357
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85 Submitted Solution: ``` N=int(input()) A=list(map(int,input().split())) B=[(j,i) for i,j in enumerate(A)] B.sort(reverse=True) dp=[[0]*(N+1) for _ in range(N+1)] c=0 for v,k in B: for i in range(c+1): dp[i+1][c-i]=max(dp[i+1][c-i],dp[i][c-i]+abs(v*(k-i))) dp[i][c-i+1]=max(dp[i][c-i+1],dp[i][c-i]+abs(v*(N-(c-i)-k-1))) c+=1 ans=0 for i in range(N): ans=max(ans,dp[i][N-i]) print(ans) ```
instruction
0
58,679
14
117,358
No
output
1
58,679
14
117,359
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85 Submitted Solution: ``` e=enumerate n,a=open(0) d=[0]+[-1e18]*2000 for j,(a,i)in e(sorted((-int(a),i)for i,a in e(a.split()))):d=[max(t-a*(~i-j+k+int(n)),d[k-1]-a*abs(~i+k))for k,t in e(d)] print(max(d)) ```
instruction
0
58,680
14
117,360
No
output
1
58,680
14
117,361
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. Constraints * 2 \leq N \leq 2000 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the maximum total happiness points the children can earn. Examples Input 4 1 3 4 2 Output 20 Input 6 5 5 6 1 1 1 Output 58 Input 6 8 6 9 1 2 1 Output 85 Submitted Solution: ``` def main(): n = int(input()) active = [int(i) for i in input().split()] moved = [1 for _ in range(n)] visited = [1 for _ in range(n)] score = [] for i, a in enumerate(active): for j in range(n): score.append((a * abs(i-j), a , i, j)) score = sorted(score, key=lambda x: x[0] + 1/x[1], reverse=True) res = 0 for sc in score: infant = sc[2] pos = sc[3] if visited[pos] and moved[infant]: res += sc[0] visited[pos] = 0 moved[infant] = 0 print(res) if __name__ == '__main__': main() ```
instruction
0
58,681
14
117,362
No
output
1
58,681
14
117,363
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO
instruction
0
59,164
14
118,328
Tags: constructive algorithms, dfs and similar, dp, graphs, greedy, shortest paths Correct Solution: ``` weights = map(int, input()) weights = [cnt for cnt, x in enumerate(weights, 1) if x] m = int(input()) state = [(0, 0, 0, [])] res = "NO" while state: w, b, k, l = state.pop() if k == m: res = 'YES\n' + ' '.join(map(str, l)) break for wt in reversed(weights): if wt != w and wt > b: state.append((wt, wt-b, k+1, l+[wt])) print(res) ```
output
1
59,164
14
118,329
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO
instruction
0
59,165
14
118,330
Tags: constructive algorithms, dfs and similar, dp, graphs, greedy, shortest paths Correct Solution: ``` from sys import setrecursionlimit setrecursionlimit(2000) weights = input() m=int(input()) l = [x+1 for x in range(len(weights)) if weights[x]=='1'] sol = [] def solve(x, m, diff): if m==0: return True for p in l: if p>diff and p!=x: sol.append(p) if solve(p, m-1, p-diff): return True sol.pop() return False if solve(0,m,0): print("YES") for x in sol: print(x, end=' ') else: print("NO") ```
output
1
59,165
14
118,331
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO
instruction
0
59,166
14
118,332
Tags: constructive algorithms, dfs and similar, dp, graphs, greedy, shortest paths Correct Solution: ``` ans=[] def dfs(bal,m,last,flag): x=bal #print(bal,m,last) if(m==te): return 1 for i in w: if(i==last): continue if(flag): x+=i else: x-=i if((x>0 and flag) or (x<0 and not flag)) and dfs(x,m+1,i,not flag): ans.append(i) return 1 if(flag): x-=i else: x+=i return 0 s=input() te=int(input()) w=[] for i in range(len(s)): if(s[i]=='1'): w.append(i+1) dfs(0,0,0,1) if(ans): print("YES") print(*ans[::-1]) else: print("NO") ```
output
1
59,166
14
118,333
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO
instruction
0
59,167
14
118,334
Tags: constructive algorithms, dfs and similar, dp, graphs, greedy, shortest paths Correct Solution: ``` # Target - Expert on CF # Be Humblefool import sys # inf = float("inf") # sys.setrecursionlimit(10000000) # abc='abcdefghijklmnopqrstuvwxyz' # abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} # mod, MOD = 1000000007, 998244353 # words = {1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'quarter',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen',20:'twenty',21:'twenty one',22:'twenty two',23:'twenty three',24:'twenty four',25:'twenty five',26:'twenty six',27:'twenty seven',28:'twenty eight',29:'twenty nine',30:'half'} # vow=['a','e','i','o','u'] # dx,dy=[-1,1,0,0],[0,0,1,-1] # import random # from collections import deque, Counter, OrderedDict,defaultdict # from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace # from math import ceil,floor,log,sqrt,factorial,pi,gcd # from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() string = input() p = [i for i,j in enumerate(string, 1) if j=='1'] m = int(input()) queue = [(-1,0,0,[])] # prev, position, till_sum, list while queue: prev,position,till_sum,lst = queue.pop() if position==m: print('YES') print(*lst) exit() for q in p: if q!=prev and q>till_sum: queue.append((q, position+1, q-till_sum,lst+[q])) print('NO') ```
output
1
59,167
14
118,335
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO
instruction
0
59,168
14
118,336
Tags: constructive algorithms, dfs and similar, dp, graphs, greedy, shortest paths Correct Solution: ``` p, m = [i for i, x in enumerate(input(), 1) if x == '1'], int(input()) r, q = [(-1, 0, 0, [])], 'NO' while r: x, d, s, t = r.pop() if s == m: q = 'YES\n' + ' '.join(map(str, t)) break for y in p: if y != x and y > d: r.append((y, y - d, s + 1, t + [y])) print(q) # Made By Mostafa_Khaled ```
output
1
59,168
14
118,337
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO
instruction
0
59,169
14
118,338
Tags: constructive algorithms, dfs and similar, dp, graphs, greedy, shortest paths Correct Solution: ``` def main(): alpha = 'abcdefghijklmnopqrstuvwxyz' ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' inf = 1e17 mod = 10 ** 9 + 7 # Max = 10 ** 1 # primes = [] # prime = [True for i in range(Max + 1)] # p = 2 # while (p * p <= Max + 1): # # # If prime[p] is not # # changed, then it is a prime # if (prime[p] == True): # # # Update all multiples of p # for i in range(p * p, Max + 1, p): # prime[i] = False # p += 1 # # for p in range(2, Max + 1): # if prime[p]: # primes.append(p) # # print(primes) def factorial(n): f = 1 for i in range(1, n + 1): f = (f * i) % mod # Now f never can # exceed 10^9+7 return f def ncr(n, r): # initialize numerator # and denominator num = den = 1 for i in range(r): num = (num * (n - i)) % mod den = (den * (i + 1)) % mod return (num * pow(den, mod - 2, mod)) % mod def solve(s,m): #print(s) queue = [[0,0,0,[]]]# (diff,last,taken,answer) while queue: diff,last,taken,answer = queue.pop() if taken == m: print('YES') print(" ".join(list(map(str,answer)))) return for opt in s: if opt != last and opt - diff > 0: queue.append([opt-diff,opt,taken+1,answer+[opt]]) #print(queue) print('NO') pass t = 1#int(input()) ans = [] for _ in range(t): # n = int(input()) #c, m, p, v = map(float, input().split()) s = input() s_ = [] for i in range(len(s)): if s[i] == '1': s_.append(i+1) m = int(input()) #arr = [int(x) for x in input().split()] # a2 = [int(x) for x in input().split()] # grid = [] # for i in range(n): # grid.append([int(x) for x in input().split()][::-1]) ans.append(solve(s_,m)) # for answer in ans: # print(answer) if __name__ == "__main__": import sys, threading import bisect import math import itertools from sys import stdout # Sorted Containers import heapq from queue import PriorityQueue # Tree Problems # sys.setrecursionlimit(2 ** 32 // 2 - 1) # threading.stack_size(1 << 27) # fast io input = sys.stdin.readline thread = threading.Thread(target=main) thread.start() thread.join() ```
output
1
59,169
14
118,339
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO
instruction
0
59,170
14
118,340
Tags: constructive algorithms, dfs and similar, dp, graphs, greedy, shortest paths Correct Solution: ``` if __name__=='__main__': inp = input() S = [] for i in range(10): if inp[i]=='1': S.append(i+1) m = int(input()) L = [] ba = 0 bb = 0 ta = True for x in range(len(S)): lt=[] lt.append(S[x]) lst = S[x] ta = False ba = S[x] bb = 0 for i in range(m-1): fnd = False for s in range(len(S)): if lst!=S[s]: if ta: if (ba+S[s])>bb: ba+=S[s] lt.append(S[s]) lst = S[s] fnd = True break else: if (bb+S[s])>ba: bb+=S[s] lt.append(S[s]) lst = S[s] fnd = True break if fnd: ta=not(ta) else: break if len(lt)==m: L=lt[:] break if len(L)<m : print("NO") else: print("YES") ret = str(L[0]) for i in range(1,m): ret += ' '+str(L[i]) print(ret) ```
output
1
59,170
14
118,341
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO
instruction
0
59,171
14
118,342
Tags: constructive algorithms, dfs and similar, dp, graphs, greedy, shortest paths Correct Solution: ``` a=[i for i,j in enumerate(input(),1) if j=='1'] m=int(input()) q=[(-1,0,0,[])] p='NO' while q: x,y,z,t=q.pop() if z==m: p='YES\n'+' '.join(map(str,t)) break for i in a: if i!=x and i>y: q.append((i,i-y,z+1,t+[i])) print(p) ```
output
1
59,171
14
118,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO Submitted Solution: ``` import traceback import math from collections import defaultdict from functools import lru_cache import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def geti(): return int(input()) def gets(): return input() def getil(): return list(map(int, input().split())) def getsl(): return input().split() def get2d(nrows, ncols, n=0): return [[n] * ncols for r in range(nrows)] inf = float('inf') mod = 10 ** 9 + 7 def main(): w = gets() m = geti() W = [i+1 for i, k in enumerate(w) if k=='1'] ans = [] def do(tot, prv, k): if k == m: return True for w in W: if w == prv or w <= abs(tot): continue if do(tot+(w if k%2==0 else -w), w, k+1): ans.append(w) return True return False return do(0, -1, 0), ans[::-1] try: ans, arr = main() ans = 'YES' if ans else 'NO' print(ans) if arr: print(*arr) except Exception as e: traceback.print_exc() ```
instruction
0
59,172
14
118,344
Yes
output
1
59,172
14
118,345
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO Submitted Solution: ``` __author__ = 'ratnesh.mishra' weights = map(int, input()) weights = [cnt for cnt, x in enumerate(weights, 1) if x] m = int(input()) state = [(0, 0, 0, [])] res = "NO" while state: w, b, k, l = state.pop() if k == m: res = 'YES\n' + ' '.join(map(str, l)) break for wt in weights: if wt != w and wt > b: state.append((wt, wt-b, k+1, l+[wt])) print(res) ```
instruction
0
59,173
14
118,346
Yes
output
1
59,173
14
118,347
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO Submitted Solution: ``` import sys sys.setrecursionlimit(2000) n = input() m = int(input()) arr = [i+1 for i in range(10) if n[i]=='1'] if len(arr)<1: print('NO') exit(0) a = [] def dfs(x, y, k): if k>m: return 1 f = 0 for i in arr: if x>=0 and y!=i and (x-i)<0: f = dfs(x-i, i, k+1) elif x<0 and y!=i and (x+i)>0: f = dfs(x+i, i, k+1) if f==1: a.append(i) return 1 return 0 result = dfs(0, 0, 1) if result==1: print("YES") for i in a[::-1]: print(i, end=' ') else: print("NO") ```
instruction
0
59,174
14
118,348
Yes
output
1
59,174
14
118,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO Submitted Solution: ``` R = lambda: map(int, input().split()) ws = [i + 1 for i, x in enumerate(map(int, input())) if x] m = int(input()) acc = [0, 0] seq = [] idx = 0 def dfs(acc, seq, idx): if len(seq) == m: print('YES') print(' '.join(map(str, seq))) return 1 for w in ws: if (not seq or w != seq[-1]) and acc[idx] + w > acc[1 - idx]: acc[idx] += w seq.append(w) idx = 1 - idx if not dfs(acc, seq, idx): idx = 1 - idx acc[idx] -= w seq.pop(-1) else: return 1 if not dfs(acc, seq, idx): print('NO') ```
instruction
0
59,175
14
118,350
Yes
output
1
59,175
14
118,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO Submitted Solution: ``` weight=[x for x,y in enumerate(input(),1) if y=='1'] l,r=0,0 prev=0 c=True a=[] for i in range(int(input())): z=0 for j in weight: if c and j!=prev and l+j>r: a.append(j) l+=j prev=j c=not c z=1 break elif not c and j!=prev and r+j>l: a.append(j) r+=j prev=j c=not c z=1 break if not z: print('NO') exit(0) print('YES') print(*a) ```
instruction
0
59,176
14
118,352
No
output
1
59,176
14
118,353
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO Submitted Solution: ``` __author__ = 'ratnesh.mishra' weights = map(int, input()) weights = [cnt+1 for cnt, x in enumerate(weights) if x] m = int(input()) if m == 1 or len(weights) == 1: print('NO') else: ll = [str(weights[0])] print('YES') print(' '.join([str(weights[-1]) if ant else str(weights[0]) for ant in range(m) ])) ```
instruction
0
59,177
14
118,354
No
output
1
59,177
14
118,355
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO Submitted Solution: ``` x = input() n = int( input() ) L = [] for i in range( len(x) ): if x[i] == '1': L.append( i+1 ) if n == 1 and len(L) == 1: print( "YES" ) print( L[0] ) exit(0) if len(L) <= 1: print( "NO" ) exit(0) #print( L ) fl = True S = [] s1, s2 = 0, 0 last = -1 if L[0] == 1: del L[0] for i in range( n ): if fl: ok = False for i in L: if i != last and s1+i > s2: ok = True S.append(i) last = i s1 += i break if ok == False: print( "NO" ) exit(0) else: ok = False for i in L: if i != last and s2+i > s1: ok = True S.append(i) last = i s2 += i break if ok == False: print( "NO" ) exit(0) fl = not fl print( "YES" ) for i in S: print( i, end=' ' ) print() ```
instruction
0
59,178
14
118,356
No
output
1
59,178
14
118,357
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Examples Input 0000000101 3 Output YES 8 10 8 Input 1000000000 2 Output NO Submitted Solution: ``` import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush # from math import * from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction import copy import time # import numpy as np starttime = time.time() # import numpy as np mod = int(pow(10, 9) + 7) mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def L(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] try: # sys.setrecursionlimit(int(pow(10,6))) sys.stdin = open("input.txt", "r") # sys.stdout = open("../output.txt", "w") except: pass s=input() m=L()[0] A=[] for i in range(10): if s[i]=="1": A.append(i+1) if len(A)==1: print("NO") exit() a=-1 b=-1 for i in range(len(A)-1): if A[i]*2>A[i+1]: a=A[i] b=A[i+1] break if a==-1: print("NO") exit() print("YES") for i in range(m): if i%2==0: print(a,end=" ") else: print(b,end=" ") endtime = time.time() # print(f"Runtime of the program is {endtime - starttime}") ```
instruction
0
59,179
14
118,358
No
output
1
59,179
14
118,359
Provide tags and a correct Python 3 solution for this coding contest problem. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4.
instruction
0
59,203
14
118,406
Tags: implementation, math, sortings Correct Solution: ``` n, m = map(int, input().split()) arr = list(map(int, input().split())) price = sum(abs(a - b) for a, b in zip(arr, arr[1:])) from collections import defaultdict adj = defaultdict(list) for a,b in zip(arr, arr[1:]): if a != b: adj[a].append(b) adj[b].append(a) def median(xs): return sorted(xs)[len(xs)//2] res = 0 for i, xs in adj.items(): m = median(xs) a = sum(abs(i - x) for x in xs) b = sum(abs(m - x) for x in xs) res = max(res, a - b) print(price - res) ```
output
1
59,203
14
118,407
Provide tags and a correct Python 3 solution for this coding contest problem. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4.
instruction
0
59,204
14
118,408
Tags: implementation, math, sortings Correct Solution: ``` n, m = map(int, input().split()) s, p = 0, [[] for i in range(n + 1)] t = list(map(int, input().split())) for i in range(m - 1): if t[i] != t[i + 1]: p[t[i + 1]].append(t[i]) p[t[i]].append(t[i + 1]) for i, q in enumerate(p): if q: q.sort() k = q[len(q) // 2] d = sum(abs(i - j) - abs(k - j) for j in q) if d > s: s = d print(sum(abs(t[i + 1] - t[i]) for i in range(m - 1)) - s) ```
output
1
59,204
14
118,409
Provide tags and a correct Python 3 solution for this coding contest problem. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4.
instruction
0
59,205
14
118,410
Tags: implementation, math, sortings Correct Solution: ``` import base64,zlib exec(zlib.decompress(base64.b85decode(b'c${@mOKt-p4Bc~z*PT)6B<(U%dJ)8eYDh&g5Poz2noK66M5-4c+wWsLDg+!6Y$6|<f{$r#B!`+N&%YE^Au(XDptZs2THqOkV&ku)h<AKSBN`(nUmN|aBgG3f;Df0L0S<pc)863i*#E9+mPU`H*P0n@N-mDn!SWgK)V!hZjtaS(x;O_{Y;%-0)4aLlp<W9Q`Y2p@rK~3xDNF2j_%e^y$gMgDvOkqPuW^?A^yIQ6-S^y0^Crh`Bfe1&sLUQkDtsndRo0oAf7|W#&dag7O_hH&R&{3'))) ```
output
1
59,205
14
118,411
Provide tags and a correct Python 3 solution for this coding contest problem. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4.
instruction
0
59,206
14
118,412
Tags: implementation, math, sortings Correct Solution: ``` n, m = map(int, input().split()) a = list(map(int, input().split())) b = {} for ai in a: b[ai] = [] amt = 0 for i in range(1, len(a)): amt += abs(a[i]-a[i-1]) if a[i]-a[i-1]: b[a[i]] += [a[i-1]] b[a[i-1]] += [a[i]] diff = 0 for bi in b: if b[bi]: center = sorted(b[bi])[len(b[bi])//2] sum = 0 for page in b[bi]: sum += abs(page-center)-abs(page-bi) diff = min(diff, sum) print(amt+diff) ```
output
1
59,206
14
118,413
Provide tags and a correct Python 3 solution for this coding contest problem. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4.
instruction
0
59,207
14
118,414
Tags: implementation, math, sortings Correct Solution: ``` def median(a): if len(a) == 0: return 0 if len(a) % 2 == 1: return a[len(a) // 2] else: return (a[len(a) // 2] + a[(len(a) // 2) - 1]) // 2 def profit(a, old_val): a.sort() med = median(a) sum_old = 0 sum_new = 0 for i in a: sum_old += abs(i - old_val) sum_new += abs(i - med) return sum_old - sum_new n, m = [int(c) for c in input().split()] pages = [int(c) for c in input().split()] count = {pages[0]: []} current_page_switches = 0 for i in range(1, len(pages)): cur_i = pages[i] prev_i = pages[i - 1] if not(cur_i in count): count[cur_i] = [] if cur_i != prev_i: count[cur_i].append(prev_i) count[prev_i].append(cur_i) current_page_switches += abs(cur_i - prev_i) max_profit = 0 for i in count: if len(count[i]) > 0: tmp = profit(count[i], i) if tmp > max_profit: max_profit = tmp print(current_page_switches - max_profit) ```
output
1
59,207
14
118,415
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4. Submitted Solution: ``` n, m = map(int, input().split()) s, p = 0, [[] for i in range(n + 1)] t = list(map(int, input().split())) for i in range(m - 1): if t[i] != t[i + 1]: p[t[i + 1]].append(t[i]) p[t[i]].append(t[i + 1]) for i, q in enumerate(p): if q: q.sort() k = q[len(q) // 2] d = sum(abs(i - j) - abs(k - j) for j in q) if d > s: s = d print(sum(abs(t[i + 1] - t[i]) for i in range(m - 1)) - d) ```
instruction
0
59,208
14
118,416
No
output
1
59,208
14
118,417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4. Submitted Solution: ``` n, m = [int(c) for c in input().split()] pages = [int(c) for c in input().split()] count = {pages[0]: []} current_page_switches = 0 def median(a): if len(a) == 0: return 0 if len(a) % 2 == 1: return a[len(a) // 2] else: return (a[len(a) // 2] + a[(len(a) // 2) - 1]) // 2 def profit(a, old_val): med = median(a) sum_old = 0 sum_new = 0 for i in a: sum_old += abs(i - old_val) sum_new += abs(i - med) return sum_old - sum_new for i in range(1, len(pages)): cur_i = pages[i] prev_i = pages[i - 1] if not(cur_i in count): count[cur_i] = [] count[cur_i].append(prev_i) count[prev_i].append(cur_i) current_page_switches += abs(cur_i - prev_i) max_profit = 0 for i in count: if len(count[i]) > 0: tmp = profit(count[i], i) if tmp > max_profit: max_profit = tmp print(current_page_switches - max_profit) ```
instruction
0
59,209
14
118,418
No
output
1
59,209
14
118,419
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4. Submitted Solution: ``` n, m = [int(c) for c in input().split()] pages = [int(c) for c in input().split()] def count_switch(a, b): res = 0 for i in range(1,len(pages)): f = pages[i-1] if pages[i-1] != a else b t = pages[i] if pages[i] != a else b res += abs(f - t) return res # # # # res = [] # counted = {} # # for i in range(1,len(pages)): # if not((pages[i-1], pages[i]) in counted): # f1 = min(count_switch(pages[i-1], pages[i]), count_switch(pages[i], pages[i-1])) # counted[(pages[i-1], pages[i])] = True # counted[(pages[i], pages[i-1])] = True # if len(res) == 0 or f1 < res[0]: # res.insert(0, f1) count = {} res = -1 def add_count (a,b): f = min(a,b) s = max(a,b) cur = 0 if not((f, s) in count) else count[(f, s)] count[(f,s)] = cur + (s - f) for i in range(1, len(pages)): add_count(pages[i - 1],pages[i]) max_c = max(count.values()) for i in count.keys(): if count[i] == max_c: f1 = min(count_switch(i[0], i[1]), count_switch(i[1], i[0])) if res == -1 or f1 < res: res = f1 print(res) ```
instruction
0
59,210
14
118,420
No
output
1
59,210
14
118,421
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is <image>. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n). Output Print a single integer — the minimum number of pages Ryouko needs to turn. Examples Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4. Submitted Solution: ``` def median(a): if len(a) == 0: return 0 if len(a) % 2 == 1: return a[len(a) // 2] else: return (a[len(a) // 2] + a[(len(a) // 2) - 1]) // 2 def profit(a, old_val): a.sort() med = median(a) sum_old = 0 sum_new = 0 for i in a: sum_old += abs(i - old_val) sum_new += abs(i - med) return sum_old - sum_new n, m = [int(c) for c in input().split()] pages = [int(c) for c in input().split()] count = {pages[0]: []} current_page_switches = 0 for i in range(1, len(pages)): cur_i = pages[i] prev_i = pages[i - 1] if not(cur_i in count): count[cur_i] = [] count[cur_i].append(prev_i) count[prev_i].append(cur_i) current_page_switches += abs(cur_i - prev_i) max_profit = 0 for i in count: if len(count[i]) > 0: tmp = profit(count[i], i) if tmp > max_profit: max_profit = tmp print(current_page_switches - max_profit) ```
instruction
0
59,211
14
118,422
No
output
1
59,211
14
118,423
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a social website with n fanpages, numbered 1 through n. There are also n companies, and the i-th company owns the i-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where i follows j and at the same time j follows i. Also, a fanpage can't follow itself. Let’s say that fanpage i follows some other fanpage j0. Also, let’s say that i is followed by k other fanpages j1, j2, ..., jk. Then, when people visit fanpage i they see ads from k + 2 distinct companies: i, j0, j1, ..., jk. Exactly ti people subscribe (like) the i-th fanpage, and each of them will click exactly one add. For each of k + 1 companies j0, j1, ..., jk, exactly <image> people will click their ad. Remaining <image> people will click an ad from company i (the owner of the fanpage). The total income of the company is equal to the number of people who click ads from this copmany. Limak and Radewoosh ask you for help. Initially, fanpage i follows fanpage fi. Your task is to handle q queries of three types: * 1 i j — fanpage i follows fanpage j from now. It's guaranteed that i didn't follow j just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). * 2 i — print the total income of the i-th company. * 3 — print two integers: the smallest income of one company and the biggest income of one company. Input The first line of the input contains two integers n and q (3 ≤ n ≤ 100 000, 1 ≤ q ≤ 100 000) — the number of fanpages and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1012) where ti denotes the number of people subscribing the i-th fanpage. The third line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ n). Initially, fanpage i follows fanpage fi. Then, q lines follow. The i-th of them describes the i-th query. The first number in the line is an integer typei (1 ≤ typei ≤ 3) — the type of the query. There will be at most 50 000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty). It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. Output For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. Example Input 5 12 10 20 30 40 50 2 3 4 5 2 2 1 2 2 2 3 2 4 2 5 1 4 2 2 1 2 2 2 3 2 4 2 5 3 Output 10 36 28 40 36 9 57 27 28 29 9 57 Note <image> In the sample test, there are 5 fanpages. The i-th of them has i·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from A to B means that A follows B. The left drawing shows the initial situation. The first company gets income <image> from its own fanpage, and gets income <image> from the 2-nd fanpage. So, the total income is 5 + 5 = 10. After the first query ("2 1") you should print 10. The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <image> from the 2-nd fanpage. So, the total income is 5 + 4 = 9 now. Submitted Solution: ``` import fileinput N = 100000 n, q = [int(x) for x in input().split()] T = [None] + [int(x) for x in input().split()] F = [None] + [int(x) for x in input().split()] E = [None] + [set()] * n for i in range(1, n+1): E[F[i]] = E[F[i]].union([i]) def income(i): k = len(E[i]) value = T[i] - (k+1) * (T[i] // (k+2)) for j in E[i] | set([F[i]]): k = len(E[j]) value += T[j] // (k+2) return value V = [None] + [income(x) for x in range(1, n+1)] minv, maxv = min(V[1:]), max(V[1:]) for qi in range(q): Q = [int(x) for x in input().split()] if Q[0] == 1: i, j = Q[1], Q[2] fi = F[i] E[F[i]] = E[F[i]].difference([i]) E[j] = E[j].union([i]) F[i] = j for j in E[i] | E[F[i]] | set([fi, F[i], i, j]): V[j] = income(j) minv, maxv = min(minv, V[j]), max(maxv, V[j]) elif Q[0] == 2: i = Q[1] print(V[i]) elif Q[0] == 3: print(minv, maxv) #print(n, q, T, F) #print(E) ```
instruction
0
59,298
14
118,596
No
output
1
59,298
14
118,597