message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20 | instruction | 0 | 17,095 | 12 | 34,190 |
Tags: data structures, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
############ ---- Input Functions ---- ############
def in_int():
return (int(input()))
def in_list():
return (list(map(int, input().split())))
def in_str():
s = input()
return (list(s[:len(s) - 1]))
def in_ints():
return (map(int, input().split()))
n, m, k = in_ints()
a = [0] + in_list()
mm = [[0,0,0,0]]
for xx in range(m):
l,r , d = in_ints()
mm.append([l,r,d,0])
for kk in range(k):
x, y = in_ints()
mm[x][3] +=1
if y < m:
mm[y+1][3]-=1
count = [0]*(n+1)
for i in range(1, m+1):
mm[i][3] += mm[i-1][3]
l = mm[i][0]
r = mm[i][1]
vv = mm[i][2]*mm[i][3]
count[l] +=vv
if r < n:
count[r+1] -= vv
# print(mm)
# print(count)
for i in range(1, n+1):
count[i]+=count[i-1]
print(a[i] + count[i],end = ' ')
``` | output | 1 | 17,095 | 12 | 34,191 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20 | instruction | 0 | 17,096 | 12 | 34,192 |
Tags: data structures, implementation
Correct Solution:
```
import sys
n, m, k = [int(x) for x in sys.stdin.readline().strip().split(" ")]
arr = [int(x) for x in sys.stdin.readline().strip().split(" ")]
delta = []
delta.append(arr[0])
for i in range(1, n):
delta.append(arr[i] - arr[i - 1])
ops = []
for i in range(m):
l, r, d = [int(x) for x in sys.stdin.readline().strip().split(" ")]
op = (l, r, d)
ops.append(op)
num_calls = [0] * m
for i in range(k):
x, y = [int(x) for x in sys.stdin.readline().strip().split(" ")]
num_calls[x - 1] += 1
if y < len(num_calls):
num_calls[y] -= 1
calls = 0
for o in range(m):
calls += num_calls[o]
op = ops[o]
l = op[0]
r = op[1]
d = op[2]
c = calls * d
delta[l - 1] += c
if r < len(delta):
delta[r] -= c
o += 1
arr[0] = delta[0]
for i in range(1, n):
arr[i] = delta[i] + arr[i - 1]
print(' '.join(str(x) for x in arr))
``` | output | 1 | 17,096 | 12 | 34,193 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20 | instruction | 0 | 17,097 | 12 | 34,194 |
Tags: data structures, implementation
Correct Solution:
```
import sys
n,m,k=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))
s=sum(a)
m1=[]
for i in range(m):
g=list(map(int,sys.stdin.readline().split()))
m1.append(g)
k1=[]
for i in range(k):
g=list(map(int,sys.stdin.readline().split()))
k1.append(g)
cnt=[0]*(m+1)
for i in range(k):
cnt[k1[i][0]-1]+=1
cnt[k1[i][1]]+=-1
s=0
for i in range(m+1):
s+=cnt[i]
cnt[i]=s
cnt1=[0]*(n+1)
for i in range(m):
cnt1[m1[i][0]-1]+=cnt[i]*m1[i][2]
cnt1[m1[i][1]]+=-cnt[i]*m1[i][2]
s=0
for i in range(n):
s+=cnt1[i]
a[i]+=s
print(a[i],end=" ")
``` | output | 1 | 17,097 | 12 | 34,195 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20 | instruction | 0 | 17,098 | 12 | 34,196 |
Tags: data structures, implementation
Correct Solution:
```
from sys import stdin,stdout
nmbr=lambda:int(stdin.readline())
lst = lambda: list(map(int,stdin.readline().split()))
for _ in range(1):#nmbr():
n,m,q=lst()
a=lst()
queries=[lst() for _ in range(m)]
mp=[0]*(2+m)
for i in range(q):
l,r=lst()
mp[l]+=1
mp[r+1]-=1
for i in range(1,m+1):
mp[i]+=mp[i-1]
# print(mp)
for i in range(1,m+1):
queries[i-1][2]*=mp[i]
# print(queries)
dp=[0]*(n+2)
for l,r,cost in queries:
dp[l]+=cost
dp[r+1]-=cost
for i in range(1,n+1):
dp[i]+=dp[i-1]
for i in range(n):
stdout.write(str(dp[i+1]+a[i])+' ')
``` | output | 1 | 17,098 | 12 | 34,197 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20 | instruction | 0 | 17,099 | 12 | 34,198 |
Tags: data structures, implementation
Correct Solution:
```
import sys
def read(line):
return [int(c) for c in line.strip().split()]
def main():
test = sys.stdin.readlines()
n, m, k = read(test[0])
array = read(test[1])
ops = [read(test[2+j]) for j in range(m)]
l, r, d = [], [], []
for li, ri, di in ops:
l.append(li)
r.append(ri)
d.append(di)
queries = [read(test[2+m+j]) for j in range(k)]
inc = [0] * (m+1)
for x, y in queries:
inc[x-1] += 1
inc[y] -= 1
dInA = [0] * (n+1)
c = 0
for i in range(m):
c += inc[i]
newValueD = c * d[i]
dInA[l[i]-1] += newValueD
dInA[r[i]] -= newValueD
ans = []
c = 0
for i in range(n):
c += dInA[i]
ans.append(c+array[i])
print(*ans)
if __name__ == "__main__":
main()
# 12 - 12:35
``` | output | 1 | 17,099 | 12 | 34,199 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20 | instruction | 0 | 17,100 | 12 | 34,200 |
Tags: data structures, implementation
Correct Solution:
```
#!/usr/bin/env python3
# created : 2020. 8. 19. 00:08
import os
from sys import stdin, stdout
def solve(tc):
n, m, k = map(int, stdin.readline().split())
seq = list(map(int, stdin.readline().split()))
ops = []
for i in range(m):
ops.append(list(map(int, stdin.readline().split())))
opsAcc = [0 for i in range(m+1)]
for i in range(k):
l, r = map(int, stdin.readline().split())
opsAcc[l-1] += 1
opsAcc[r] -= 1
seqAcc = [0 for i in range(n+1)]
seqAcc[ops[0][0]-1] += ops[0][2] * opsAcc[0]
seqAcc[ops[0][1]] -= ops[0][2] * opsAcc[0]
for i in range(1, m+1):
opsAcc[i] += opsAcc[i-1]
if i < m:
seqAcc[ops[i][0]-1] += ops[i][2] * opsAcc[i]
seqAcc[ops[i][1]] -= ops[i][2] * opsAcc[i]
seq[0] += seqAcc[0]
for i in range(1, n+1):
seqAcc[i] += seqAcc[i-1]
if i < n:
seq[i] += seqAcc[i]
print(*seq)
tcs = 1
tc = 1
while tc <= tcs:
solve(tc)
tc += 1
``` | output | 1 | 17,100 | 12 | 34,201 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20 | instruction | 0 | 17,101 | 12 | 34,202 |
Tags: data structures, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
l = []
r = []
d = []
# храним операции
for i in range(m):
lx, rx, dx = map(int, input().split())
l.append(lx)
r.append(rx)
d.append(dx)
# сколько каких операций
kol = [0] * (10 ** 5 + 5)
for j in range(k):
x, y = map(int, input().split())
x -= 1
kol[x] += 1
kol[y] -= 1
for j in range(len(kol) - 1):
kol[j + 1] += kol[j]
# добавление на интервале
p = [0] * (n + 1)
for i in range(m):
le = l[i] - 1
ri = r[i]
di = d[i] * kol[i]
p[le] += di
p[ri] -= di
ans = [0] * (n + 1)
for i in range(n):
ans[i + 1] = ans[i] + p[i]
ans = ans[1:]
for i in range(n):
print(ans[i] + a[i], end=' ')
``` | output | 1 | 17,101 | 12 | 34,203 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20 | instruction | 0 | 17,102 | 12 | 34,204 |
Tags: data structures, implementation
Correct Solution:
```
""" A. Greg and Array
"""
import sys
n, m, k = map(int, sys.stdin.readline().split())
arr = [x for x in map(int, sys.stdin.readline().split())]
ops = []
for x in range(m):
ops.append((map(int, sys.stdin.readline().split())))
c = [0] * m # hold number of ops starting at i
for x in range(k):
l, r = map(int, sys.stdin.readline().split())
c[l - 1] += 1
if r < m:
c[r] -= 1 # query affects i - 1, stop at i
times_used = [0] * m # times each op is used
times_used[0] = c[0]
for x in range(1, m):
times_used[x] = times_used[x - 1] + c[x]
c1 = [0] * n # hold number of updates starting at i
for x in range(m):
l, r, d = ops[x]
c1[l - 1] += times_used[x] * d
if r < n:
c1[r] -= times_used[x] * d
amount_added = [0] * n # amount to add to arr[i]
amount_added[0] = c1[0]
arr[0] += amount_added[0]
for x in range(1, n):
amount_added[x] = amount_added[x - 1] + c1[x]
arr[x] += amount_added[x]
print(*arr, sep = " ", end = "\n")
``` | output | 1 | 17,102 | 12 | 34,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
Input
The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.
Output
Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
Examples
Input
5
1 2 3 5 7
Output
3 2 1
Input
5
1 8 1 5 1
Output
-1 | instruction | 0 | 17,111 | 12 | 34,222 |
Tags: implementation
Correct Solution:
```
n = int(input())
g = list(map(int, input().split()))
doc = []
for i in g:
for j in g:
if j != i:
k = i - j
if k > 0:
if g.count(k) > 1:
if k in g:
doc.append(i)
doc.append(j)
doc.append(k)
break
else:
continue
else:
if k in g and j != k:
doc.append(i)
doc.append(j)
doc.append(k)
break
else:
continue
else:
continue
else:
continue
if len(doc) == 3:
break
else:
continue
l = []
if len(doc) == 3:
for k in doc:
l.append(g.index(k) + 1)
g[g.index(k)] = 0
print(*l)
else:
print(-1)
``` | output | 1 | 17,111 | 12 | 34,223 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
Input
The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.
Output
Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
Examples
Input
5
1 2 3 5 7
Output
3 2 1
Input
5
1 8 1 5 1
Output
-1 | instruction | 0 | 17,112 | 12 | 34,224 |
Tags: implementation
Correct Solution:
```
n=int(input())
a=[[int(j),i] for i,j in enumerate(input().split(' '))]
b=sorted(a)[::-1]
s='-1'
for i in range(n-2):
for j in range(i+1,n-1):
for k in range(j+1,n):
if b[i][0]==b[j][0]+b[k][0]:
s=str(b[i][1]+1)+' '+str(b[j][1]+1)+' '+str(b[k][1]+1)
print(s)
``` | output | 1 | 17,112 | 12 | 34,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
Input
The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.
Output
Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
Examples
Input
5
1 2 3 5 7
Output
3 2 1
Input
5
1 8 1 5 1
Output
-1 | instruction | 0 | 17,113 | 12 | 34,226 |
Tags: implementation
Correct Solution:
```
n = int(input())
w = [int(i) for i in input().split()]
f = False
for i in range(n):
if f:
break
for j in range(n):
if f:
break
if i == j:
continue
for k in range(n):
if f:
break
if k == i or j == k:
continue
if w[i] == w[j] + w[k]:
a, b, c = i, j, k
f = True
elif w[j] == w[k] + w[i]:
a, b, c = j, i, k
f = True
elif w[k] == w[i] + w[j]:
f = True
a, b, c = k, i, j
if f:
print(a+1, b+1, c+1)
else:
print(-1)
``` | output | 1 | 17,113 | 12 | 34,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
Input
The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.
Output
Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
Examples
Input
5
1 2 3 5 7
Output
3 2 1
Input
5
1 8 1 5 1
Output
-1 | instruction | 0 | 17,114 | 12 | 34,228 |
Tags: implementation
Correct Solution:
```
n = int(input())
lst = list(map(int,input().split()))
l = len(lst)
found = False
for i in range(l):
for j in range(i+1,l):
for k in range(j+1,l):
if(lst[i] == lst[j]+lst[k] ):
print(f"{i+1} {j+1} {k+1}")
found = True
break
elif lst[j] == lst[i]+lst[k]:
print(f"{j+1} {i+1} {k+1}")
found = True
break
elif lst[k] == lst[i]+lst[j]:
print(f"{k+1} {i+1} {j+1}")
found = True
break
if found:break
if found:break
if not found:print(-1)
``` | output | 1 | 17,114 | 12 | 34,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
Input
The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.
Output
Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
Examples
Input
5
1 2 3 5 7
Output
3 2 1
Input
5
1 8 1 5 1
Output
-1 | instruction | 0 | 17,115 | 12 | 34,230 |
Tags: implementation
Correct Solution:
```
n = int(input())
arr = tuple(map(int, input().strip().split(' ')))
milon = {}
for i in range(n-1):
for j in range(i+1, n):
k = arr[i] + arr[j]
if k not in milon:
milon[k] = (i, j)
for i in range(n):
if arr[i] in milon:
v = milon[arr[i]]
print('{} {} {}'.format(i+1, v[0]+1, v[1]+1))
exit()
print(-1)
``` | output | 1 | 17,115 | 12 | 34,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
Input
The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.
Output
Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
Examples
Input
5
1 2 3 5 7
Output
3 2 1
Input
5
1 8 1 5 1
Output
-1 | instruction | 0 | 17,116 | 12 | 34,232 |
Tags: implementation
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
f=0
for i in range(0,n):
for j in range(0,n):
if(((a[i]+a[j]) in a)and i!=j):
print(a.index(a[i]+a[j])+1,i+1,j+1)
f=1
break
if(f==1):
break
if(f==0):
print(-1)
``` | output | 1 | 17,116 | 12 | 34,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
Input
The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.
Output
Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
Examples
Input
5
1 2 3 5 7
Output
3 2 1
Input
5
1 8 1 5 1
Output
-1 | instruction | 0 | 17,117 | 12 | 34,234 |
Tags: implementation
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
for j in range(n):
if a[i]+a[j] in a and i!=j!=a.index(a[i]+a[j]):
print(a.index(a[i]+a[j])+1,i+1,j+1)
exit()
print(-1)
``` | output | 1 | 17,117 | 12 | 34,235 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
Input
The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.
Output
Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
Examples
Input
5
1 2 3 5 7
Output
3 2 1
Input
5
1 8 1 5 1
Output
-1 | instruction | 0 | 17,118 | 12 | 34,236 |
Tags: implementation
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
f=0
for i in range(n):
for j in range(n):
if(l[i]-l[j] in l and l.index(l[i]-l[j])!=j):
f=1
print(i+1,j+1,l.index(l[i]-l[j])+1)
break
if(f==1):
break
if(f==0):
print(-1)
``` | output | 1 | 17,118 | 12 | 34,237 |
Provide a correct Python 3 solution for this coding contest problem.
You are given N integers; the i-th of them is A_i. Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.
Constraints
* 2 \leq N \leq 10^5
* 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_N
Output
Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.
Examples
Input
5
6
8
1
2
3
Output
21
Input
6
3
1
4
1
5
9
Output
25
Input
3
5
5
1
Output
8 | instruction | 0 | 17,509 | 12 | 35,018 |
"Correct Solution:
```
from collections import deque
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
que = deque(a)
right = que.pop()
left = que.popleft()
sums = right - left
while(bool(que)):
a = que.pop()
if not bool(que):
sums += max(abs(right-a),abs(left-a))
break
b = que.popleft()
sums += right + a - left -b
right = a
left = b
print(sums)
``` | output | 1 | 17,509 | 12 | 35,019 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.
First of all, the program should read an integer n, then make a disjoint set where each element consists of 0, 1, ... n−1 respectively.
Next, the program should read an integer q and manipulate the set for q queries. There are two kinds of queries for different operations:
* unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.
* same(x, y): determine whether x and y are in the same set.
Constraints
* 1 ≤ n ≤ 10000
* 1 ≤ q ≤ 100000
* x ≠ y
Input
n q
com1 x1 y1
com2 x2 y2
...
comq xq yq
In the first line, n and q are given. Then, q queries are given where com represents the type of queries. '0' denotes unite and '1' denotes same operation.
Output
For each same operation, print 1 if x and y are in the same set, otherwise 0, in a line.
Example
Input
5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0
Output
0
0
1
1
1
0
1
1 | instruction | 0 | 17,661 | 12 | 35,322 |
"Correct Solution:
```
class DisjointSet:
def __init__(self, size):
self.rank = [0] * size
self.p = list(range(size))
def findSet(self, x):
if x != self.p[x]:
self.p[x] = self.findSet(self.p[x])
return self.p[x]
def same(self, x, y):
return self.findSet(x) == self.findSet(y)
def link(self, x, y):
if self.rank[x] > self.rank[y]:
self.p[y] = x
else:
self.p[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def unite(self, x, y):
self.link(self.findSet(x), self.findSet(y))
if __name__ == '__main__':
n, q = [int(v) for v in input().split()]
ds = DisjointSet(n)
for i in range(q):
com, x, y = [int(v) for v in input().split()]
if com == 0:
ds.unite(x, y)
elif com == 1:
if ds.same(x, y):
print(1)
else:
print(0)
``` | output | 1 | 17,661 | 12 | 35,323 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.
First of all, the program should read an integer n, then make a disjoint set where each element consists of 0, 1, ... n−1 respectively.
Next, the program should read an integer q and manipulate the set for q queries. There are two kinds of queries for different operations:
* unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.
* same(x, y): determine whether x and y are in the same set.
Constraints
* 1 ≤ n ≤ 10000
* 1 ≤ q ≤ 100000
* x ≠ y
Input
n q
com1 x1 y1
com2 x2 y2
...
comq xq yq
In the first line, n and q are given. Then, q queries are given where com represents the type of queries. '0' denotes unite and '1' denotes same operation.
Output
For each same operation, print 1 if x and y are in the same set, otherwise 0, in a line.
Example
Input
5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0
Output
0
0
1
1
1
0
1
1 | instruction | 0 | 17,663 | 12 | 35,326 |
"Correct Solution:
```
def main():
n,q = [int(i) for i in input().split()]
#自分自身が親
par = [ i for i in range(n)]
#ランク
rank = [ 0 for i in range(n)]
#親を返す関数
def find(x):
if par[x] == x:
return x
else:
par[x] = find(par[x])
return par[x]
#木を結合する
def unite(x,y):
x = find(x)
y = find(y)
if x == y:
return
if rank[x]<rank[y]:
par[x] = y
return
elif rank[y]<rank[x]:
par[y] = x
return
else:
par[x] = y
rank[y] +=1
return
def same(x,y):
return find(x) == find(y)
for i in range(q):
com,x,y = [int(j) for j in input().split()]
#unite
if com == 0:
unite(x,y)
#same
else:
if same(x,y):
print(1)
else:
print(0)
if __name__ == '__main__':
main()
``` | output | 1 | 17,663 | 12 | 35,327 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.
First of all, the program should read an integer n, then make a disjoint set where each element consists of 0, 1, ... n−1 respectively.
Next, the program should read an integer q and manipulate the set for q queries. There are two kinds of queries for different operations:
* unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.
* same(x, y): determine whether x and y are in the same set.
Constraints
* 1 ≤ n ≤ 10000
* 1 ≤ q ≤ 100000
* x ≠ y
Input
n q
com1 x1 y1
com2 x2 y2
...
comq xq yq
In the first line, n and q are given. Then, q queries are given where com represents the type of queries. '0' denotes unite and '1' denotes same operation.
Output
For each same operation, print 1 if x and y are in the same set, otherwise 0, in a line.
Example
Input
5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0
Output
0
0
1
1
1
0
1
1 | instruction | 0 | 17,667 | 12 | 35,334 |
"Correct Solution:
```
class DisjointSet():
def __init__(self, size):
self.rank = [None] * size
self.p = [None] * size
for i in range(size):
self.makeSet(i)
def makeSet(self, x):
self.p[x] = x
self.rank[x] = 0
def same(self,x,y):
return self.findSet(x) == self.findSet(y)
def unite(self,x,y):
self.link(self.findSet(x), self.findSet(y))
def link(self, x, y):
if self.rank[x] > self.rank[y]:
self.p[y] = x
else:
self.p[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def findSet(self, x):
if x != self.p[x]:
self.p[x] = self.findSet(self.p[x])
return self.p[x]
n,q = map(int, input().split())
ds = DisjointSet(n)
for i in range(q):
t,a,b = map(int, input().split())
if t == 0:
ds.unite(a,b)
elif t == 1:
if ds.same(a,b):
print("1")
else:
print("0")
``` | output | 1 | 17,667 | 12 | 35,335 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.
First of all, the program should read an integer n, then make a disjoint set where each element consists of 0, 1, ... n−1 respectively.
Next, the program should read an integer q and manipulate the set for q queries. There are two kinds of queries for different operations:
* unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.
* same(x, y): determine whether x and y are in the same set.
Constraints
* 1 ≤ n ≤ 10000
* 1 ≤ q ≤ 100000
* x ≠ y
Input
n q
com1 x1 y1
com2 x2 y2
...
comq xq yq
In the first line, n and q are given. Then, q queries are given where com represents the type of queries. '0' denotes unite and '1' denotes same operation.
Output
For each same operation, print 1 if x and y are in the same set, otherwise 0, in a line.
Example
Input
5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0
Output
0
0
1
1
1
0
1
1
Submitted Solution:
```
p=[]
rank=[]
answer=[]
def makeset(x):
p[x] = x
rank[x] = 0
def union(x,y):
link(findset(x),findset(y))
def link(x,y):
if rank[x] > rank[y]:
p[y] = x
else:
p[x] = y
if rank[x] == rank[y]:
rank[y] = rank[y] + 1
def findset(x):
if x != p[x]:
p[x] = findset(p[x])
return p[x]
t,s = map(int,input().split())
for i in range(t):
p.append(None)
rank.append(None)
while True:
try:
o,x,y = map(int,input().split())
except:
break
else:
if o == 0:
if p[x] == None:
makeset(x)
if p[y] == None:
makeset(y)
union(x,y)
else:
if p[x] == None:
makeset(x)
if p[y] == None:
makeset(y)
if findset(x) == findset(y):
answer.append(1)
else:
answer.append(0)
for i in range(len(answer)):
print(answer[i])
``` | instruction | 0 | 17,669 | 12 | 35,338 |
Yes | output | 1 | 17,669 | 12 | 35,339 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x.
A stick length a_i is called almost good for some integer t if |a_i - t| ≤ 1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer t and the total cost of changing is minimum possible. The value of t is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of t and the minimum cost. If there are multiple optimal choices for t, print any of them.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sticks.
The second line contains n integers a_i (1 ≤ a_i ≤ 100) — the lengths of the sticks.
Output
Print the value of t and the minimum possible cost. If there are multiple optimal choices for t, print any of them.
Examples
Input
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
2 0
Note
In the first example, we can change 1 into 2 and 10 into 4 with cost |1 - 2| + |10 - 4| = 1 + 6 = 7 and the resulting lengths [2, 4, 4] are almost good for t = 3.
In the second example, the sticks lengths are already almost good for t = 2, so we don't have to do anything. | instruction | 0 | 17,739 | 12 | 35,478 |
Tags: brute force, implementation
Correct Solution:
```
# -*- coding: utf-8 -*-
# @Time : 2019/1/20 20:06
# @Author : LunaFire
# @Email : gilgemesh2012@gmail.com
# @File : A. Salem and Sticks.py
def main():
n = int(input())
a = list(map(int, input().split()))
ret = [0, 100 * 1000]
for x in range(1, 101):
total_cost = 0
for v in a:
total_cost += min(abs(x + 1 - v), abs(x - v), abs(x - 1 - v))
if total_cost < ret[1]:
ret = [x, total_cost]
print(ret[0], ret[1])
if __name__ == '__main__':
main()
``` | output | 1 | 17,739 | 12 | 35,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x.
A stick length a_i is called almost good for some integer t if |a_i - t| ≤ 1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer t and the total cost of changing is minimum possible. The value of t is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of t and the minimum cost. If there are multiple optimal choices for t, print any of them.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sticks.
The second line contains n integers a_i (1 ≤ a_i ≤ 100) — the lengths of the sticks.
Output
Print the value of t and the minimum possible cost. If there are multiple optimal choices for t, print any of them.
Examples
Input
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
2 0
Note
In the first example, we can change 1 into 2 and 10 into 4 with cost |1 - 2| + |10 - 4| = 1 + 6 = 7 and the resulting lengths [2, 4, 4] are almost good for t = 3.
In the second example, the sticks lengths are already almost good for t = 2, so we don't have to do anything. | instruction | 0 | 17,740 | 12 | 35,480 |
Tags: brute force, implementation
Correct Solution:
```
n = int(input())
arr = list(map(int,input().split()))
x = max(arr)
ans = 99999999
for t in range(1,x+2):
sum = 0
for i in arr:
sum += min(abs(t-i),abs(t-i-1),abs(t-i+1))
if sum<ans:
ans = sum
T = t
print(T,ans)
``` | output | 1 | 17,740 | 12 | 35,481 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x.
A stick length a_i is called almost good for some integer t if |a_i - t| ≤ 1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer t and the total cost of changing is minimum possible. The value of t is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of t and the minimum cost. If there are multiple optimal choices for t, print any of them.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sticks.
The second line contains n integers a_i (1 ≤ a_i ≤ 100) — the lengths of the sticks.
Output
Print the value of t and the minimum possible cost. If there are multiple optimal choices for t, print any of them.
Examples
Input
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
2 0
Note
In the first example, we can change 1 into 2 and 10 into 4 with cost |1 - 2| + |10 - 4| = 1 + 6 = 7 and the resulting lengths [2, 4, 4] are almost good for t = 3.
In the second example, the sticks lengths are already almost good for t = 2, so we don't have to do anything. | instruction | 0 | 17,741 | 12 | 35,482 |
Tags: brute force, implementation
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
t = 0
mn = 1000000000
for i in range(1,100):
cur = 0
for j in range(n):
cur += max(0,abs(i-a[j])-1)
if cur < mn:
mn = cur
t = i
print(t,mn)
``` | output | 1 | 17,741 | 12 | 35,483 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x.
A stick length a_i is called almost good for some integer t if |a_i - t| ≤ 1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer t and the total cost of changing is minimum possible. The value of t is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of t and the minimum cost. If there are multiple optimal choices for t, print any of them.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sticks.
The second line contains n integers a_i (1 ≤ a_i ≤ 100) — the lengths of the sticks.
Output
Print the value of t and the minimum possible cost. If there are multiple optimal choices for t, print any of them.
Examples
Input
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
2 0
Note
In the first example, we can change 1 into 2 and 10 into 4 with cost |1 - 2| + |10 - 4| = 1 + 6 = 7 and the resulting lengths [2, 4, 4] are almost good for t = 3.
In the second example, the sticks lengths are already almost good for t = 2, so we don't have to do anything. | instruction | 0 | 17,742 | 12 | 35,484 |
Tags: brute force, implementation
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
c,t = min((sum(max(0, abs(t-x)-1) for x in a), t) for t in range(1,max(a)+1))
print(t,c)
``` | output | 1 | 17,742 | 12 | 35,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x.
A stick length a_i is called almost good for some integer t if |a_i - t| ≤ 1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer t and the total cost of changing is minimum possible. The value of t is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of t and the minimum cost. If there are multiple optimal choices for t, print any of them.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sticks.
The second line contains n integers a_i (1 ≤ a_i ≤ 100) — the lengths of the sticks.
Output
Print the value of t and the minimum possible cost. If there are multiple optimal choices for t, print any of them.
Examples
Input
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
2 0
Note
In the first example, we can change 1 into 2 and 10 into 4 with cost |1 - 2| + |10 - 4| = 1 + 6 = 7 and the resulting lengths [2, 4, 4] are almost good for t = 3.
In the second example, the sticks lengths are already almost good for t = 2, so we don't have to do anything. | instruction | 0 | 17,743 | 12 | 35,486 |
Tags: brute force, implementation
Correct Solution:
```
import math
from statistics import median
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
min_changes = math.inf
min_mean = None
for mean in range(2, 100):
changes = 0
for a in arr:
diff = abs(a - mean)
if diff > 1:
changes += diff - 1
if changes < min_changes:
min_changes = changes
min_mean = mean
else:
break
print(min_mean, min_changes)
``` | output | 1 | 17,743 | 12 | 35,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x.
A stick length a_i is called almost good for some integer t if |a_i - t| ≤ 1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer t and the total cost of changing is minimum possible. The value of t is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of t and the minimum cost. If there are multiple optimal choices for t, print any of them.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sticks.
The second line contains n integers a_i (1 ≤ a_i ≤ 100) — the lengths of the sticks.
Output
Print the value of t and the minimum possible cost. If there are multiple optimal choices for t, print any of them.
Examples
Input
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
2 0
Note
In the first example, we can change 1 into 2 and 10 into 4 with cost |1 - 2| + |10 - 4| = 1 + 6 = 7 and the resulting lengths [2, 4, 4] are almost good for t = 3.
In the second example, the sticks lengths are already almost good for t = 2, so we don't have to do anything. | instruction | 0 | 17,744 | 12 | 35,488 |
Tags: brute force, implementation
Correct Solution:
```
import sys
import math
import itertools
import functools
import collections
import operator
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def wr(arr): return ' '.join(map(str, arr))
def revn(n): return str(n)[::-1]
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def sieve(n):
if n < 2: return list()
prime = [True for _ in range(n + 1)]
p = 3
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 2
r = [2]
for p in range(3, n + 1, 2):
if prime[p]:
r.append(p)
return r
def divs(n, start=1):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def divn(n, primes):
divs_number = 1
for i in primes:
if n == 1:
return divs_number
t = 1
while n % i == 0:
t += 1
n //= i
divs_number *= t
def prime(n):
if n == 2: return True
if n % 2 == 0 or n <= 1: return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0: return False
return True
def convn(number, base):
newnumber = 0
while number > 0:
newnumber += number % base
number //= base
return newnumber
def cdiv(n, k): return n // k + (n % k != 0)
n = ii()
a = li()
ans = 10 ** 6
t = 0
for i in range(1, 101, 1):
ts = 0
for j in range(n):
if a[j] < i:
ts += i - 1 - a[j]
elif a[j] > i:
ts += a[j] - i - 1
if ts < ans:
t = i
ans = ts
print(t, ans)
``` | output | 1 | 17,744 | 12 | 35,489 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x.
A stick length a_i is called almost good for some integer t if |a_i - t| ≤ 1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer t and the total cost of changing is minimum possible. The value of t is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of t and the minimum cost. If there are multiple optimal choices for t, print any of them.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sticks.
The second line contains n integers a_i (1 ≤ a_i ≤ 100) — the lengths of the sticks.
Output
Print the value of t and the minimum possible cost. If there are multiple optimal choices for t, print any of them.
Examples
Input
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
2 0
Note
In the first example, we can change 1 into 2 and 10 into 4 with cost |1 - 2| + |10 - 4| = 1 + 6 = 7 and the resulting lengths [2, 4, 4] are almost good for t = 3.
In the second example, the sticks lengths are already almost good for t = 2, so we don't have to do anything. | instruction | 0 | 17,745 | 12 | 35,490 |
Tags: brute force, implementation
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
max1=99999
for i in range(1,101):
sum1=0
for j in range(len(l)):
if(l[j]>i):
sum1=sum1+abs(l[j]-i-1)
elif(l[j]<i):
sum1=sum1+abs(l[j]+1-i)
else:
sum1=sum1+0
if(max1>sum1):
max1=sum1
k=i
print(k,max1,end=" ")
``` | output | 1 | 17,745 | 12 | 35,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x.
A stick length a_i is called almost good for some integer t if |a_i - t| ≤ 1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer t and the total cost of changing is minimum possible. The value of t is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of t and the minimum cost. If there are multiple optimal choices for t, print any of them.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sticks.
The second line contains n integers a_i (1 ≤ a_i ≤ 100) — the lengths of the sticks.
Output
Print the value of t and the minimum possible cost. If there are multiple optimal choices for t, print any of them.
Examples
Input
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
2 0
Note
In the first example, we can change 1 into 2 and 10 into 4 with cost |1 - 2| + |10 - 4| = 1 + 6 = 7 and the resulting lengths [2, 4, 4] are almost good for t = 3.
In the second example, the sticks lengths are already almost good for t = 2, so we don't have to do anything. | instruction | 0 | 17,746 | 12 | 35,492 |
Tags: brute force, implementation
Correct Solution:
```
n=int(input())
a=[int(i) for i in input().split()]
t=1
mins=1000000
while t<100:
su=0
for e in a:
if e>t:
su+=e-t-1
elif e<t:
su+=t-1-e
if su<mins:
mins=su
res=t
t+=1
print(res,mins)
``` | output | 1 | 17,746 | 12 | 35,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given sequence a1, a2, ..., an and m queries lj, rj (1 ≤ lj ≤ rj ≤ n). For each query you need to print the minimum distance between such pair of elements ax and ay (x ≠ y), that:
* both indexes of the elements lie within range [lj, rj], that is, lj ≤ x, y ≤ rj;
* the values of the elements are equal, that is ax = ay.
The text above understands distance as |x - y|.
Input
The first line of the input contains a pair of integers n, m (1 ≤ n, m ≤ 5·105) — the length of the sequence and the number of queries, correspondingly.
The second line contains the sequence of integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Next m lines contain the queries, one per line. Each query is given by a pair of numbers lj, rj (1 ≤ lj ≤ rj ≤ n) — the indexes of the query range limits.
Output
Print m integers — the answers to each query. If there is no valid match for some query, please print -1 as an answer to this query.
Examples
Input
5 3
1 1 2 3 2
1 5
2 4
3 5
Output
1
-1
2
Input
6 5
1 2 1 3 2 3
4 6
1 3
2 5
2 4
1 6
Output
2
2
3
-1
2 | instruction | 0 | 18,091 | 12 | 36,182 |
Tags: *special, data structures
Correct Solution:
```
import sys
class Fenwick(object):
def __init__(self, n):
self.n = n
self.a = [10**9 for i in range(n)]
self.w= 10**9
def zag(self, i, zn):
self.w= min(self.w, zn)
while i < self.n:
self.a[i] = min(self.a[i], zn)
i = (i | (i + 1))
def pol(self, r):
ans= 10**9
while r >= 0:
if ans> self.a[r]:
ans= self.a[r]
if ans== self.w:
break
r = (r & (r + 1)) - 1
return ans
n, m = [int(x) for x in sys.stdin.readline().split()]
a = [int(x) for x in sys.stdin.readline().split()]
nd= [-1 for i in range(0, len(a))]
vi= {}
for i in range(0, len(a)):
if a[i] in vi:
nd[i] = vi[a[i]]
vi[a[i]] = i
inp= sys.stdin.readlines()
och= [[] for i in range(n)]
for i in range(m):
l, r = inp[i].split()
och[int(r) - 1].append((int(l) - 1, i))
der = Fenwick(2 ** 19)
ans= [None for i in range(0, m)]
le= -1
for r in range(n):
if nd[r] != -1:
der.zag(500000 - nd[r] + 1, r - nd[r])
le = max(le, nd[r])
for (l, ind) in och[r]:
if l > le:
ans[ind] = -1
continue
zn= der.pol(500000 - l + 1)
if zn== 10**9:
zn= -1
ans[ind] = zn
print('\n'.join(str(zn) for zn in ans))
``` | output | 1 | 18,091 | 12 | 36,183 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a consisting of n integers. You may partition this sequence into two sequences b and c in such a way that every element belongs exactly to one of these sequences.
Let B be the sum of elements belonging to b, and C be the sum of elements belonging to c (if some of these sequences is empty, then its sum is 0). What is the maximum possible value of B - C?
Input
The first line contains one integer n (1 ≤ n ≤ 100) — the number of elements in a.
The second line contains n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100) — the elements of sequence a.
Output
Print the maximum possible value of B - C, where B is the sum of elements of sequence b, and C is the sum of elements of sequence c.
Examples
Input
3
1 -2 0
Output
3
Input
6
16 23 16 15 42 8
Output
120
Note
In the first example we may choose b = {1, 0}, c = { - 2}. Then B = 1, C = - 2, B - C = 3.
In the second example we choose b = {16, 23, 16, 15, 42, 8}, c = {} (an empty sequence). Then B = 120, C = 0, B - C = 120. | instruction | 0 | 18,273 | 12 | 36,546 |
Tags: greedy
Correct Solution:
```
n = int(input())
arr1 = input()
arr2 = arr1.split()
arr = []
for i in arr2:
arr.append(int(i))
left = []
right = []
for i in arr:
if i <= 0:
right.append(i)
else:
left.append(i)
k1 = sum(left)
k2 = sum(right)
diff = k1 - k2
print(diff)
``` | output | 1 | 18,273 | 12 | 36,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a consisting of n integers. You may partition this sequence into two sequences b and c in such a way that every element belongs exactly to one of these sequences.
Let B be the sum of elements belonging to b, and C be the sum of elements belonging to c (if some of these sequences is empty, then its sum is 0). What is the maximum possible value of B - C?
Input
The first line contains one integer n (1 ≤ n ≤ 100) — the number of elements in a.
The second line contains n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100) — the elements of sequence a.
Output
Print the maximum possible value of B - C, where B is the sum of elements of sequence b, and C is the sum of elements of sequence c.
Examples
Input
3
1 -2 0
Output
3
Input
6
16 23 16 15 42 8
Output
120
Note
In the first example we may choose b = {1, 0}, c = { - 2}. Then B = 1, C = - 2, B - C = 3.
In the second example we choose b = {16, 23, 16, 15, 42, 8}, c = {} (an empty sequence). Then B = 120, C = 0, B - C = 120. | instruction | 0 | 18,274 | 12 | 36,548 |
Tags: greedy
Correct Solution:
```
def test_1():
n = list(map(int, input().split(' ')))[0]
data = list(map(int, input().split(' ')))
data_1 = sorted(data)
data_2 = []
data_3 = []
for i in range(n):
if data_1[i]<0:
data_2.append(data_1[i])
else:
data_3.append(data_1[i])
out = sum(data_3) - sum(data_2)
print(out)
test_1()
``` | output | 1 | 18,274 | 12 | 36,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a consisting of n integers. You may partition this sequence into two sequences b and c in such a way that every element belongs exactly to one of these sequences.
Let B be the sum of elements belonging to b, and C be the sum of elements belonging to c (if some of these sequences is empty, then its sum is 0). What is the maximum possible value of B - C?
Input
The first line contains one integer n (1 ≤ n ≤ 100) — the number of elements in a.
The second line contains n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100) — the elements of sequence a.
Output
Print the maximum possible value of B - C, where B is the sum of elements of sequence b, and C is the sum of elements of sequence c.
Examples
Input
3
1 -2 0
Output
3
Input
6
16 23 16 15 42 8
Output
120
Note
In the first example we may choose b = {1, 0}, c = { - 2}. Then B = 1, C = - 2, B - C = 3.
In the second example we choose b = {16, 23, 16, 15, 42, 8}, c = {} (an empty sequence). Then B = 120, C = 0, B - C = 120. | instruction | 0 | 18,275 | 12 | 36,550 |
Tags: greedy
Correct Solution:
```
n=int(input())
li=list(map(int,input().split()))
a=[]
b=[]
li.sort()
for i in li:
if i<=0:
a.append(i)
else:
b.append(i)
print(sum(b)-sum(a))
``` | output | 1 | 18,275 | 12 | 36,551 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4 | instruction | 0 | 18,549 | 12 | 37,098 |
"Correct Solution:
```
a=[]
q=int(input())
def pushback(a,x):
a.append(x)
return a
def randomaccess(a,p):
print(a[p])
def popback(a):
a.pop(len(a)-1)
return a
for i in range(q):
query=list(map(int,input().split()))
if query[0]==0:
a=pushback(a,query[1])
elif query[0]==1:
randomaccess(a,query[1])
else:
a=popback(a)
``` | output | 1 | 18,549 | 12 | 37,099 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4 | instruction | 0 | 18,550 | 12 | 37,100 |
"Correct Solution:
```
def pushBack(ary, elem):
ary.append(elem)
def randomAccess(ary, idx):
print(ary[idx])
def popBack(ary):
ary.pop()
def main():
n = int(input())
ary = []
for i in range(n):
query = input()
if (query[0] == '0'):
elem = int(query[2:])
pushBack(ary, elem)
elif (query[0] == '1'):
idx = int(query[2:])
randomAccess(ary, idx)
elif (query[0] == '2'):
popBack(ary)
if __name__ == '__main__':
main()
``` | output | 1 | 18,550 | 12 | 37,101 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4 | instruction | 0 | 18,551 | 12 | 37,102 |
"Correct Solution:
```
n = int(input())
ans = []
for i in range(n):
lst = list(map(int,input().split()))
if lst[0] == 0:
ans.append(lst[1])
elif lst[0] == 1:
print(ans[lst[1]])
else:
del ans[-1]
``` | output | 1 | 18,551 | 12 | 37,103 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4 | instruction | 0 | 18,552 | 12 | 37,104 |
"Correct Solution:
```
A=[]
def pushBack(x):
A.append(x)
def randomAccess(x):
if len(A)==0:
return
print(A[x])
def popBack():
if len(A)==0:
return
A.pop()
q=int(input())
for i in range(q):
a=list(map(int,input().split()))
if a[0]==0:
pushBack(a[1])
elif a[0]==1:
randomAccess(a[1])
elif a[0]==2:
popBack()
``` | output | 1 | 18,552 | 12 | 37,105 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4 | instruction | 0 | 18,553 | 12 | 37,106 |
"Correct Solution:
```
q=int(input())
lis=[]
lq=[]
for i in range(q):
lq.append([int(x) for x in input().split(' ')])
for i in lq:
order=i[0]
if order==0:
lis.append(i[1])
elif order==1:
print(lis[i[1]])
elif order==2:
lis.pop(-1)
``` | output | 1 | 18,553 | 12 | 37,107 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4 | instruction | 0 | 18,554 | 12 | 37,108 |
"Correct Solution:
```
N= int(input())
P = []
for i in range(N):
a, *b = (int(b) for b in input().split())
b= b[0] if b else None
if a == 0:
P.append(b)
elif a == 1:
print(P[b])
else:
del P[-1]
``` | output | 1 | 18,554 | 12 | 37,109 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4 | instruction | 0 | 18,555 | 12 | 37,110 |
"Correct Solution:
```
stack = []
q = int(input())
for i in range(q):
op = list(map(int, input().split(' ')))
if op[0] == 0:
stack.append(op[1])
elif op[0] == 1:
print(stack[op[1]])
elif op[0] == 2:
stack.pop()
``` | output | 1 | 18,555 | 12 | 37,111 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4 | instruction | 0 | 18,556 | 12 | 37,112 |
"Correct Solution:
```
q=int(input())
query=[]
A=[]
for i in range(q):
query=list(map(int,input().split()))
if query[0]==0:
A.append(query[1])
elif query[0]==1:
print(A[query[1]])
else:
A.pop(-1)
``` | output | 1 | 18,556 | 12 | 37,113 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4
Submitted Solution:
```
# AOJ ITP2_1_A: Vector
# Python3 2018.6.24 bal4u
from collections import deque
Q = deque()
q = int(input())
for i in range(q):
a = input()
if a[0] == '0': Q.append(a[2:]) # pushBack
elif a[0] == '1': print(Q[int(a[2:])]) # randomAccess
else: Q.pop() # popBack
``` | instruction | 0 | 18,560 | 12 | 37,120 |
Yes | output | 1 | 18,560 | 12 | 37,121 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4
Submitted Solution:
```
# AOJ ITP2_1_A: Vector
# Python3 2018.6.24 bal4u
from collections import deque
Q = deque()
q = int(input())
for i in range(q):
a = input()
print(a[2:])
if a[0] == '0': Q.append(a[2:]) # pushBack
elif a[0] == '1': print(Q[int(a[2:])]) # randomAccess
else: Q.pop() # popBack
``` | instruction | 0 | 18,562 | 12 | 37,124 |
No | output | 1 | 18,562 | 12 | 37,125 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* pushBack($x$): add element $x$ at the end of $A$
* randomAccess($p$):print element $a_p$
* popBack(): delete the last element of $A$
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $p$
or
2
where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.
randomAccess and popBack operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
Output
1
2
4
Submitted Solution:
```
def pushBack(lists, x):
return lists.insert(-1, x)
def randomAccess(lists, p):
print(lists[p])
def popBack(lists):
return lists.pop(-1)
if __name__ == '__main__':
l = list()
n = int(input())
for i in range(n):
j= input().split()
if int(j[0]) == 0:
pushBack(l, x)
elif int(j[0]) == 1:
randomAccess(l, x)
else:
popBack(l)
``` | instruction | 0 | 18,564 | 12 | 37,128 |
No | output | 1 | 18,564 | 12 | 37,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n arrays that can have different sizes. You also have a table with w columns and n rows. The i-th array is placed horizontally in the i-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.
You need to find the maximum sum of the integers in the j-th column for each j from 1 to w independently.
<image> Optimal placements for columns 1, 2 and 3 are shown on the pictures from left to right.
Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero.
Input
The first line contains two integers n (1 ≤ n ≤ 10^{6}) and w (1 ≤ w ≤ 10^{6}) — the number of arrays and the width of the table.
Each of the next n lines consists of an integer l_{i} (1 ≤ l_{i} ≤ w), the length of the i-th array, followed by l_{i} integers a_{i1}, a_{i2}, …, a_{il_i} (-10^{9} ≤ a_{ij} ≤ 10^{9}) — the elements of the array.
The total length of the arrays does no exceed 10^{6}.
Output
Print w integers, the i-th of them should be the maximum sum for column i.
Examples
Input
3 3
3 2 4 8
2 2 5
2 6 3
Output
10 15 16
Input
2 2
2 7 8
1 -8
Output
7 8
Note
Illustration for the first example is in the statement. | instruction | 0 | 18,627 | 12 | 37,254 |
Tags: data structures, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
n,w=[int(i) for i in input().rstrip('\n').split()]
arr=[]
tem=[]
for i in range(n):
t=[int(i) for i in input().rstrip('\n').split()]
tem.append(t[0])
arr.append(t[1:])
l=[0 for i in range(w)]
wi=[0 for i in range(w+1)]
for j in range(n):
st=deque()
c=w-tem[j]
for i in range(tem[j]):
while st and st[0]<i-c:st.popleft()
while st and arr[j][st[-1]]<arr[j][i]:st.pop()
st.append(i)
if arr[j][st[0]]>0 or (i>=c and i<w-c):
l[i]+=arr[j][st[0]]
ind=tem[j]
while st and arr[j][st[0]]>0:
wi[ind]+=arr[j][st[0]]
wi[w-(tem[j]-st[0])+1]+=-arr[j][st[0]]
ind=w-(tem[j]-st[0])+1
st.popleft()
curr=0
for i in range(w):
curr+=wi[i]
l[i]+=curr
print(" ".join(str(e) for e in l))
``` | output | 1 | 18,627 | 12 | 37,255 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n arrays that can have different sizes. You also have a table with w columns and n rows. The i-th array is placed horizontally in the i-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.
You need to find the maximum sum of the integers in the j-th column for each j from 1 to w independently.
<image> Optimal placements for columns 1, 2 and 3 are shown on the pictures from left to right.
Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero.
Input
The first line contains two integers n (1 ≤ n ≤ 10^{6}) and w (1 ≤ w ≤ 10^{6}) — the number of arrays and the width of the table.
Each of the next n lines consists of an integer l_{i} (1 ≤ l_{i} ≤ w), the length of the i-th array, followed by l_{i} integers a_{i1}, a_{i2}, …, a_{il_i} (-10^{9} ≤ a_{ij} ≤ 10^{9}) — the elements of the array.
The total length of the arrays does no exceed 10^{6}.
Output
Print w integers, the i-th of them should be the maximum sum for column i.
Examples
Input
3 3
3 2 4 8
2 2 5
2 6 3
Output
10 15 16
Input
2 2
2 7 8
1 -8
Output
7 8
Note
Illustration for the first example is in the statement. | instruction | 0 | 18,628 | 12 | 37,256 |
Tags: data structures, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
def slidemax(X, k):
q = deque([])
ret = []
for i in range(len(X)):
while q and q[-1][1] <= X[i]:
q.pop()
deque.append(q, (i+k, X[i]))
if q[0][0] == i:
deque.popleft(q)
if i >= k-1:
ret.append(q[0][1])
return ret
N, W = map(int, input().split())
A = [0] * W
s = 0
for _ in range(N):
l, *B = map(int, input().split())
if l*2 < W:
C = slidemax([0]*(l-1)+B+[0]*(l-1), l)
m = max(B + [0])
s += m
for i in range(l-1):
A[i] += C[i] - m
A[-i-1] += C[-i-1] - m
else:
C = slidemax([0]*(W-l)+B+[0]*(W-l), W - l + 1)
A = [a+c for a, c in zip(A, C)]
print(*[a+s for a in A])
``` | output | 1 | 18,628 | 12 | 37,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n arrays that can have different sizes. You also have a table with w columns and n rows. The i-th array is placed horizontally in the i-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.
You need to find the maximum sum of the integers in the j-th column for each j from 1 to w independently.
<image> Optimal placements for columns 1, 2 and 3 are shown on the pictures from left to right.
Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero.
Input
The first line contains two integers n (1 ≤ n ≤ 10^{6}) and w (1 ≤ w ≤ 10^{6}) — the number of arrays and the width of the table.
Each of the next n lines consists of an integer l_{i} (1 ≤ l_{i} ≤ w), the length of the i-th array, followed by l_{i} integers a_{i1}, a_{i2}, …, a_{il_i} (-10^{9} ≤ a_{ij} ≤ 10^{9}) — the elements of the array.
The total length of the arrays does no exceed 10^{6}.
Output
Print w integers, the i-th of them should be the maximum sum for column i.
Examples
Input
3 3
3 2 4 8
2 2 5
2 6 3
Output
10 15 16
Input
2 2
2 7 8
1 -8
Output
7 8
Note
Illustration for the first example is in the statement.
Submitted Solution:
```
# import math
# import sys
from collections import deque
n,w = [int(x) for x in input().strip().split(" ")]
ans = [0]*w
for _ in range(n):
a = [int(x) for x in input().strip().split(" ")]
i = 0
k = min(w-a[0]+1,a[0])
val = [0]*w
maxm = -10000000000
Qi = deque([])
while i<k and i<a[0]:
while (len(Qi)>0 and a[i+1] >= a[Qi[-1]+1]):
Qi.pop()
Qi.append(i)
maxm = max(maxm,a[Qi[0]+1])
val[i] = maxm
i+=1
while i<a[0]:
while (len(Qi)>0 and Qi[0] <= i-k):
Qi.popleft()
while (len(Qi)>0 and a[i+1] >= a[Qi[-1]+1]) :
Qi.pop()
Qi.append(i)
maxm = max(maxm,a[Qi[0]+1])
val[i] = maxm
i+=1
while i<w:
val[i] = maxm
i+=1
maxm = -10000000000
i=w
while i>(w-k):
maxm = max(maxm,a[a[0]-w+i])
val[i-1] = maxm
i-=1
for j in range(w-a[0]):
val[j] = max(0,val[j])
for j in range(a[0],w):
val[j] = max(0,val[j])
for j in range(w):
ans[j] += val[j]
for i in range(w):
print(ans[i],end=" ")
``` | instruction | 0 | 18,629 | 12 | 37,258 |
No | output | 1 | 18,629 | 12 | 37,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n arrays that can have different sizes. You also have a table with w columns and n rows. The i-th array is placed horizontally in the i-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.
You need to find the maximum sum of the integers in the j-th column for each j from 1 to w independently.
<image> Optimal placements for columns 1, 2 and 3 are shown on the pictures from left to right.
Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero.
Input
The first line contains two integers n (1 ≤ n ≤ 10^{6}) and w (1 ≤ w ≤ 10^{6}) — the number of arrays and the width of the table.
Each of the next n lines consists of an integer l_{i} (1 ≤ l_{i} ≤ w), the length of the i-th array, followed by l_{i} integers a_{i1}, a_{i2}, …, a_{il_i} (-10^{9} ≤ a_{ij} ≤ 10^{9}) — the elements of the array.
The total length of the arrays does no exceed 10^{6}.
Output
Print w integers, the i-th of them should be the maximum sum for column i.
Examples
Input
3 3
3 2 4 8
2 2 5
2 6 3
Output
10 15 16
Input
2 2
2 7 8
1 -8
Output
7 8
Note
Illustration for the first example is in the statement.
Submitted Solution:
```
from collections import deque
class MaxQueue:
def __init__(self):
self.p = list()
self.q = list()
pass
def __len__(self):
return len(self.p) + len(self.q)
def __add__(self, other):
return self.enqueue(other)
def enqueue(self, other):
min_val = other if len(self.p) == 0 else max(other, self.p[-1][1])
self.p.append((other, min_val))
def dequeue(self):
if len(self.q) == 0:
while len(self.p) > 0:
item = self.p.pop()[0]
min_val = item if len(self.q) == 0 else max(item, self.q[-1][1])
self.q.append((item, min_val))
if len(self.q) > 0:
return self.q.pop()[0]
def max(self):
if len(self) == 0:
return None
if len(self.p) == 0:
return self.q[-1][1]
elif len(self.q) == 0:
return self.p[-1][1]
else:
return max(self.p[-1][1], self.q[-1][1])
class MaxWindowedQueue(MaxQueue):
def __init__(self, wnd_size):
super().__init__()
self.wnd_size = wnd_size
def enqueue(self, other):
super().enqueue(other)
if len(self) > self.wnd_size:
return super().dequeue()
def max_of_window(A, wnd):
mwq = MaxWindowedQueue(wnd)
ret = list()
mwq.enqueue(0)
for elm in A:
mwq.enqueue(elm)
ret.append(mwq.max())
last_max = mwq.max()
for _ in range(wnd - 1):
mwq.enqueue(0)
if len(mwq) > 1:
last_max = mwq.max()
ret.append(last_max)
return ret
def main():
n, w = map(int, input().split())
A = [0] * n
for _ in range(n):
arr = deque(map(int, input().split()))
wnd = w - arr.popleft() + 1
if wnd > 1:
# lr
max_suffix = max_of_window(arr, wnd)
for i in range(n):
A[i] += max_suffix[i]
else:
for i in range(n):
A[i] += arr[i]
print(*A)
import sys
input = sys.stdin.readline
if __name__ == "__main__":
main()
``` | instruction | 0 | 18,630 | 12 | 37,260 |
No | output | 1 | 18,630 | 12 | 37,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n arrays that can have different sizes. You also have a table with w columns and n rows. The i-th array is placed horizontally in the i-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.
You need to find the maximum sum of the integers in the j-th column for each j from 1 to w independently.
<image> Optimal placements for columns 1, 2 and 3 are shown on the pictures from left to right.
Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero.
Input
The first line contains two integers n (1 ≤ n ≤ 10^{6}) and w (1 ≤ w ≤ 10^{6}) — the number of arrays and the width of the table.
Each of the next n lines consists of an integer l_{i} (1 ≤ l_{i} ≤ w), the length of the i-th array, followed by l_{i} integers a_{i1}, a_{i2}, …, a_{il_i} (-10^{9} ≤ a_{ij} ≤ 10^{9}) — the elements of the array.
The total length of the arrays does no exceed 10^{6}.
Output
Print w integers, the i-th of them should be the maximum sum for column i.
Examples
Input
3 3
3 2 4 8
2 2 5
2 6 3
Output
10 15 16
Input
2 2
2 7 8
1 -8
Output
7 8
Note
Illustration for the first example is in the statement.
Submitted Solution:
```
n, w = [int(x) for x in input().split()]
a = []
for _ in range(n):
r = [int(x) for x in input().split()]
r.pop(0)
a.append(r)
res = []
for j in range(w):
s = 0
for r in a:
e = w - j
d = max(r[-e:j+1])
if d>=0:
s += d
else:
l = len(r)
if l > j and l > e:
s += d
res.append(s)
print(" ".join([str(x) for x in res]))
``` | instruction | 0 | 18,631 | 12 | 37,262 |
No | output | 1 | 18,631 | 12 | 37,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n arrays that can have different sizes. You also have a table with w columns and n rows. The i-th array is placed horizontally in the i-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.
You need to find the maximum sum of the integers in the j-th column for each j from 1 to w independently.
<image> Optimal placements for columns 1, 2 and 3 are shown on the pictures from left to right.
Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero.
Input
The first line contains two integers n (1 ≤ n ≤ 10^{6}) and w (1 ≤ w ≤ 10^{6}) — the number of arrays and the width of the table.
Each of the next n lines consists of an integer l_{i} (1 ≤ l_{i} ≤ w), the length of the i-th array, followed by l_{i} integers a_{i1}, a_{i2}, …, a_{il_i} (-10^{9} ≤ a_{ij} ≤ 10^{9}) — the elements of the array.
The total length of the arrays does no exceed 10^{6}.
Output
Print w integers, the i-th of them should be the maximum sum for column i.
Examples
Input
3 3
3 2 4 8
2 2 5
2 6 3
Output
10 15 16
Input
2 2
2 7 8
1 -8
Output
7 8
Note
Illustration for the first example is in the statement.
Submitted Solution:
```
from collections import deque
n,w=[int(i) for i in input().split()]
arr=[]
nw=w
for i in range(n):
arr.append([int(i) for i in input().split()][1:])
l=[0 for i in range(w)]
# print(nw)
wi=[0 for i in range(w+1)]
for j in range(n):
st=deque()
c=nw-len(arr[j])
# print(c)
for i in range(len(arr[j])):
# print(i,len(arr[j]))
while len(st) and st[0]<i-c:st.popleft()
while i < len(arr[j]) and len(st) and arr[j][st[-1]]<arr[j][i]:st.pop()
if i<len(arr[j]):st.append(i)
if arr[j][st[0]]>0 or (i>=c and i<w-c):
l[i]+=arr[j][st[0]]
# print(l,st)
ind=len(arr[j])
tr=1
while st and st[0]>0:
# print(ind,end=" ")
wi[ind]+=arr[j][st[0]]
wi[w-(len(arr[j])-st[0])+1]+=-arr[j][st[0]]
ind=w-(len(arr[j])-st[0])+1
# print(ind)
st.popleft()
# print(wi)
curr=0
for i in range(w):
curr+=wi[i]
l[i]+=curr
print(" ".join(str(e) for e in l))
# givesum(l)
``` | instruction | 0 | 18,632 | 12 | 37,264 |
No | output | 1 | 18,632 | 12 | 37,265 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.