s_id stringlengths 10 10 | p_id stringlengths 6 6 | u_id stringlengths 10 10 | date stringlengths 10 10 | language stringclasses 1
value | original_language stringclasses 11
values | filename_ext stringclasses 1
value | status stringclasses 1
value | cpu_time stringlengths 1 5 | memory stringlengths 1 7 | code_size stringlengths 1 6 | code stringlengths 1 539k |
|---|---|---|---|---|---|---|---|---|---|---|---|
s576353698 | p03821 | u277429554 | 1593408716 | Python | Python (3.8.2) | py | Runtime Error | 27 | 8996 | 239 | n = int(input())
a = []
b = []
for j in range(n):
l = list(map(int, input().split()))
a.append(l[0])
b.append(l[1])
c = 0
for i in range(n-1, -1, -1):
if (a[i]+c) % b[i] != 0:
c += (b[i] - (a[i]+c) % b[i])
print(c) |
s353943603 | p03821 | u228232845 | 1592162957 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 641 | import math
import sys
from typing import Reversible
def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()
n = I()
a = []
b = []
for _ in range(n):
ai, bi = LI()
a.append(ai)
b.append(bi)
p = 0
for ai, bi in zip(a[::-1], b[::-1]):
ai += p
if ai % bi == 0:
continue
elif bi > ai:
p += bi - ai
else:
p += bi - (ai % bi)
print(p)
|
s724599032 | p03821 | u075304271 | 1592000391 | Python | PyPy3 (2.4.0) | py | Runtime Error | 190 | 39856 | 571 | import numpy as np
import math
import collections
import fractions
import itertools
def iput(): return int(input())
def mput(): return map(int, input().split())
def lput(): return list(map(int, input().split()))
def solve():
n = int(input())
cost = 0
a, b = [0]*n, [0]*n
for i in range(n):
a[i], b[i] = mput()
for i in range(n-1, -1, -1):
mult = math.ceil(a[i]/b[i])*b[i]
cost += mult - a[i]
for j in range(0, i+1):
a[j] += mult - a[i]
print(cost)
return 0
if __name__ == "__main__":
solve()
|
s877917127 | p03821 | u060736237 | 1590528636 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 261 | n = int(input())
A, B = [], []
for _ in range(n):
a, b = map(int, input().split())
A.append(a)
B.append(b)
result = 0
for a, b in zip(A[::-1], B[::-1]):
piyo = (a+result) % b
if piyo == 0:
continue
result += b - piyo
print(result |
s727104646 | p03821 | u056358163 | 1589936642 | Python | Python (3.4.3) | py | Runtime Error | 37 | 5048 | 407 | import fractions
def lcm(a, b):
return a * b / fractions.gcd(a, b)
N, M = map(int, input().split())
S = input()
T = input()
d_N = {}
for n in range(N):
d = fractions.gcd(n, N)
d_N[(n // d, N // d)] = n
for m in range(M):
d = fractions.gcd(m, M)
k = (m // d, M // d)
if k in d_N:
if S[d_N[k]] != T[m]:
print(-1)
exit()
L = int(lcm(N, M))
print(L) |
s363839959 | p03821 | u276204978 | 1589679122 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 371 | import numpy as np
N = int(input())
A = np.zeros(N).astype(int)
B = np.zeros(N).astype(int)
for i in range(N):
A[i], B[i] = map(int, input().split())
ans = 0
for i in reversed(range(N)):
A[i] = A[i] + ans
if A[i] % B[i] == 0:
continue
elif B[i] - A[i] > 0:
ans += B[i] - A[i]
else:
ans += B[i] - (A[i] % B[i])
print(ans) |
s250815795 | p03821 | u479638406 | 1589645373 | Python | Python (3.4.3) | py | Runtime Error | 340 | 11048 | 516 | def solve():
n = int(input())
a = []
b = []
for _ in range(n):
A, B = map(int, input().split())
if B != 1:
a.append(A)
b.append(B)
ans = b[-1] - a[-1]%b[-1]
if a[-1]%b[-1] == 0: ans = 0
for i in range(1, len(a)):
c = a[-(i+1)]+ans
if c > b[-(i+1)]:
dis = b[-(i+1)]-c%b[-(i+1)]
ans += dis
else:
dis = b[-(i+1)] - c
ans += dis
return max(0, ans)
print(solve())
|
s701141632 | p03821 | u479638406 | 1589644827 | Python | Python (3.4.3) | py | Runtime Error | 341 | 11036 | 478 | def solve():
n = int(input())
a = []
b = []
for _ in range(n):
A, B = map(int, input().split())
if B != 1:
a.append(A)
b.append(B)
ans = b[-1] - a[-1]%b[-1]
for i in range(1, len(a)):
c = a[-(i+1)]+ans
if c > b[-(i+1)]:
dis = b[-(i+1)]-c%b[-(i+1)]
ans += dis
else:
dis = b[-(i+1)] - c
ans += dis
return max(0, ans)
print(solve())
|
s403129434 | p03821 | u479638406 | 1589644436 | Python | Python (3.4.3) | py | Runtime Error | 359 | 15860 | 565 | def solve():
n = int(input())
a = []
b = []
for _ in range(n):
A, B = map(int, input().split())
if B != 1:
a.append(A)
b.append(B)
dis = b[-1] - a[-1]%b[-1]
ans = [dis]
for i in range(1, len(a)):
c = a[-(i+1)]+ans[i-1]
if c > b[-(i+1)]:
dis = b[-(i+1)]-c%b[-(i+1)]
ans.append(ans[i-1]+dis)
elif c == b[-(i+1)]:
ans.append(0)
else:
dis = c
ans.append(dis)
return max(0, ans[-1])
print(solve())
|
s494186186 | p03821 | u479638406 | 1589643786 | Python | Python (3.4.3) | py | Runtime Error | 353 | 15860 | 565 | def solve():
n = int(input())
a = []
b = []
for _ in range(n):
A, B = map(int, input().split())
if B != 1:
a.append(A)
b.append(B)
dis = b[-1] - a[-1]%b[-1]
ans = [dis]
for i in range(1, len(a)):
c = a[-(i+1)]+ans[i-1]
if c > b[-(i+1)]:
dis = b[-(i+1)]-c%b[-(i+1)]
ans.append(ans[i-1]+dis)
elif c == b[-(i+1)]:
ans.append(0)
else:
dis = c
ans.append(dis)
return max(0, ans[-1])
print(solve())
|
s863984076 | p03821 | u479638406 | 1589643475 | Python | Python (3.4.3) | py | Runtime Error | 356 | 15860 | 511 | def solve():
n = int(input())
a = []
b = []
for _ in range(n):
A, B = map(int, input().split())
if B != 1:
a.append(A)
b.append(B)
dis = b[-1] - a[-1]%b[-1]
ans = [dis]
for i in range(1, len(a)):
c = a[-(i+1)]+ans[i-1]
if c >= b[-(i+1)]:
dis = b[-(i+1)]-c%b[-(i+1)]
ans.append(ans[i-1]+dis)
else:
dis = c
ans.append(dis)
return max(0, ans[-1])
print(solve())
|
s764086221 | p03821 | u479638406 | 1589643388 | Python | Python (3.4.3) | py | Runtime Error | 355 | 15860 | 503 | def solve():
n = int(input())
a = []
b = []
for _ in range(n):
A, B = map(int, input().split())
if B != 1:
a.append(A)
b.append(B)
dis = b[-1] - a[-1]%b[-1]
ans = [dis]
for i in range(1, len(a)):
c = a[-(i+1)]+ans[i-1]
if c >= b[-(i+1)]:
dis = b[-(i+1)]-c%b[-(i+1)]
ans.append(ans[i-1]+dis)
else:
dis = c
ans.append(dis)
return ans[-1]
print(solve())
|
s098881454 | p03821 | u077003677 | 1589592837 | Python | Python (3.4.3) | py | Runtime Error | 18 | 2940 | 648 | import sys
import os
def file_input():
f = open('AGC009/input.txt', 'r')
sys.stdin = f
def main():
#file_input()
N=int(input())
A=[]
B=[]
for i in range(N):
tmp=list(map(int, input().split()))
A.append(tmp[0])
B.append(tmp[1])
cnt=0
for j in range(N):
# while (A[N-1-j]+cnt)%B[N-1-j]!=0:
# cnt+=1
tmp=A[N-1-j]+cnt
if not tmp==0:
# cnt=B[N-1-j]
elif tmp<B[N-1-j]:
cnt+=B[N-1-j]-tmp
elif tmp%B[N-1-j]!=0:
cnt+=B[N-1-j]-tmp%B[N-1-j]
print(cnt)
if __name__ == '__main__':
main()
|
s692813138 | p03821 | u077003677 | 1589589641 | Python | Python (3.4.3) | py | Runtime Error | 388 | 11108 | 764 | import sys
import os
def file_input():
f = open('AGC009/input.txt', 'r')
sys.stdin = f
def main():
#file_input()
N=int(input())
A=[]
B=[]
for i in range(N):
tmp=list(map(int, input().split()))
A.append(tmp[0])
B.append(tmp[1])
cnt=0
for j in range(N):
# while (A[N-1-j]+cnt)%B[N-1-j]!=0:
# cnt+=1
tmp=A[N-1-j]+cnt
# print(str(tmp))
if not B[N-1-j]==1:
if tmp>B[N-1-j]:
# print(str(B[N-1-j]-tmp%B[N-1-j]))
cnt+=B[N-1-j]-tmp%B[N-1-j]
else:
# print(str(B[N-1-j]%tmp+B[N-1-j]))
cnt+=B[N-1-j]%tmp
# print(cnt)
print(cnt)
if __name__ == '__main__':
main()
|
s903634817 | p03821 | u408375121 | 1589383198 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 422 | n = int(input())
l = []
r = []
for _ in range(n):
a, b = map(int, input())
l.append(a)
r.append(b)
before_sa = 0
for i in range(1, n+1):
j = 0
a, b = l[-i], r[-i]
while True:
if j == 0 and a % b == 0:
sa = 0
elif j == 0 and a % b != 0:
sa = (a//b + 1) * b - a
elif j > 0:
sa = (a//b + j) * b - a
if before_sa <= sa:
before_sa = sa
break
j += 1
print(before_sa) |
s667807867 | p03821 | u807772568 | 1586227758 | Python | Python (3.4.3) | py | Runtime Error | 273 | 31732 | 1445 | import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
return list(map(int,input().split()))
def m(): #複数文字
return map(int,input().split())
def onem(): #Nとかの取得
return int(input())
def s(x): #圧縮
a = []
if len(x) == 0:
return []
aa = x[0]
su = 1
for i in range(len(x)-1):
if aa != x[i+1]:
a.append([aa,su])
aa = x[i+1]
su = 1
else:
su += 1
a.append([aa,su])
return a
def jo(x): #listをスペースごとに分ける
return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
return max(map(max,x))
def In(x,a): #aがリスト(sorted)
k = bs.bisect_left(a,x)
if k != len(a) and a[k] == x:
return True
else:
return False
def pow_k(x, n):
ans = 1
while n:
if n % 2:
ans *= x
x *= x
n >>= 1
return ans
"""
def nibu(x,n,r):
ll = 0
rr = r
while True:
mid = (ll+rr)//2
if rr == mid:
return ll
if (ここに評価入れる):
rr = mid
else:
ll = mid+1
"""
n = onem()
po = []
for i in range(n):
po.append(l())
co = 0
for i in range(n):
on = po[-(i+1)]
on[0] += co
co += ((on[1] - on[0]) % on[1]) if on[1] % on[0] != 0 else 0
print(co)
|
s659578275 | p03821 | u088552457 | 1583112704 | Python | PyPy3 (2.4.0) | py | Runtime Error | 2109 | 95604 | 837 | def count_baisuu(a, b):
return b - (a % b)
def main():
n = int(input())
alist = []
blist = []
ab = []
for i in range(n):
a, b = input_list()
ab.append((a, b))
alist.append(a)
blist.append(b)
dp = [0]*n
i = n - 1
for a, b in reversed(ab):
if i != n-1:
a += sum(dp)
if a % b == 0:
continue
if a < b:
dp[i] = b - a
else:
dp[i] = count_baisuu(a, b)
i -= 1
print(sump(dp))
import math
import fractions
from functools import reduce
def input_list():
return map(int, input().split())
def lcm_base(x, y):
return (x * y) // fractions.gcd(x, y)
def lcm_list(numbers):
return reduce(lcm_base, numbers, 1)
def gcd(*numbers):
return reduce(fractions.gcd, numbers)
def gcd_list(numbers):
return reduce(fractions.gcd, numbers)
main() |
s159773311 | p03821 | u348868667 | 1582733717 | Python | PyPy3 (2.4.0) | py | Runtime Error | 162 | 38384 | 386 | import numpy as np
def main():
N = int(input())
A = np.array([None]*N)
B = np.array([None]*N)
for i in range(N):
A[i],B[i] = map(int,input().split())
ans = 0
for i in range(N-1,-1,-1):
if A[i]%B[i] == 0:
continue
ans += (B[i] - A[i]%B[i])
A += (B[i] - A[i]%B[i])
print(ans)
if __name__ == '__main__':
main() |
s997140817 | p03821 | u857428111 | 1582517974 | Python | PyPy3 (2.4.0) | py | Runtime Error | 321 | 69356 | 1194 | # coding: utf-8
# Your code here!
# coding: utf-8
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(200000000)
from inspect import currentframe
# my functions here!
#標準エラー出力
def printargs2err(*args):
names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
print(', '.join(names.get(id(arg),'???')+' : '+repr(arg) for arg in args),file=sys.stderr)
def debug(x):
print(x,file=sys.stderr)
def printglobals():
for (symbol, value) in globals().items():
print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
def printlocals():
for (symbol, value) in locals().items():
print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
#入力(後でいじる)
def pin(type=int):
return map(type,input().split())
"""
"""
#solution:
#input
x,y=pin()
t=abs(y)-abs(x)
if x*y<0:
t=abs(t)+1
elif t*(y-x)<0:
t=abs(t)+2
print(t)
#print(["No","Yes"][cond])
#print([["NA","YYMM"],["MMYY","AMBIGUOUS"]][cMMYY][cYYMM])
"""
#printデバッグ消した?
#前の問題の結果見てないのに次の問題に行くの?
"""
"""
お前カッコ閉じるの忘れてるだろ
""" |
s690790834 | p03821 | u417014669 | 1577674441 | Python | PyPy3 (2.4.0) | py | Runtime Error | 180 | 38384 | 403 | N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
A=[]
for x in T:
if x == 's':
A.append('r')
if x == 'p':
A.append('s')
if x == 'r':
A.append('p')
for i in range(K,N):
if A[i-K] == A[i]:
A[i] = 'x'
ans = 0
for x in A:
if x == 's':
ans += S
if x == 'p':
ans += P
if x == 'r':
ans += R
print(ans) |
s205884359 | p03821 | u905582793 | 1577347832 | Python | PyPy3 (2.4.0) | py | Runtime Error | 164 | 38384 | 169 | n=int(input())
ab=[list(map(int,input())) for i in range(n)]
ans = 0
for i in range(n-1,-1,-1):
k = (ab[i][0]+ans)%ab[i][1]
if k!=0:
ans += ab[i][1]-k
print(ans) |
s328237061 | p03821 | u644907318 | 1577018942 | Python | Python (3.4.3) | py | Runtime Error | 310 | 3060 | 112 | N = int(input())
cnt = 0
for _ in range(N):
a,b = map(int,input().split())
cnt += (a-b)%a
print(cnt) |
s430620686 | p03821 | u724742135 | 1576982402 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 227 | from sys import stdin
N = int(stdin.readline().rstrip())
AB = [list(map(int, stdin.readline().rstrip().split())) for _ in range(N)]
ans = 0
for i in range(N-1, -1, -1):
a, b = AB[i]
ans += (b - (a + ans) % b) % b
print(ans) |
s693240883 | p03821 | u252828980 | 1576099440 | Python | Python (3.4.3) | py | Runtime Error | 354 | 14896 | 587 | n = int(input())
L1,L2 = [],[]
for i in range(n):
a,b = map(int,input().split())
L1.append(b)
L2.append(a)
L1 = L1[::-1]
L2 = L2[::-1]
#print(L1)
#print(L2)
cnt = 0
for i in range(n):
L1[i] += cnt
#print(L1[i])
if L1[i] >= L2[i]:
cnt += L1[i]%L2[i]
elif L1[i] < L2[i]:
c = 2
while True:
if L1[i] >= L2[i]:
cnt += L1[i]%L2[i]
#print(cnt,i,L1[i])
break
else:
L1[i] *= c
c +=1
#cnt += L1[i]%L2[i]
print(cnt) |
s893546509 | p03821 | u936985471 | 1575133693 | Python | Python (3.4.3) | py | Runtime Error | 383 | 14864 | 283 | N=int(input())
A=[0]*N
B=[0]*N
for i in range(N):
a,b=map(int,input().split())
A[i]=a
B[i]=b
ans=0
for i in range(N-1,-1,-1):
plus=0
A[i]+=ans
if B[i]==1:
plus=0
elif A[i]<B[i]:
plus=B[i]%A[i]
elif A[i]>B[i]:
plus=B[i]-A[i]%B[i]
ans+=plus
print(ans) |
s976208302 | p03821 | u911575040 | 1574132336 | Python | PyPy3 (2.4.0) | py | Runtime Error | 178 | 38544 | 55 | a,b,c=map(int,input().split())
print(min(b+c,a+2*b+1))
|
s854794830 | p03821 | u573272932 | 1573184383 | Python | Python (3.4.3) | py | Runtime Error | 403 | 27380 | 161 | N = int(input())
cnt = 0
L = []
for i in range(N):
L.append(list(map(int, input().split())))
for i in range(N):
A, B = L[-i+1]
cnt += -(A + cnt)%B
print(cnt) |
s151814206 | p03821 | u573272932 | 1573184357 | Python | Python (3.4.3) | py | Runtime Error | 483 | 28916 | 162 | N = int(input())
cnt = 0
L = []
for i in range(N):
L.append(list(map(int, input().split())))
for i in range(N):
A, B = L[-i+1]
cnt += -(A + cnt)%B
print(cnt) |
s430143825 | p03821 | u777283665 | 1569434084 | Python | PyPy3 (2.4.0) | py | Runtime Error | 163 | 38256 | 317 | import numpy as np
n = int(input())
A, B = np.zeros(n), np.zeros(n)
for i in range(n):
a, b = map(int, input().split())
A[i] = a
B[i] = b
ans = 0
for i in range(n-1, -1, -1):
if not A[i] % B[i] == 0:
temp = B[i]*((A[i]//B[i])+1) - A[i]
ans += temp
A += temp
print(int(ans))
|
s864853598 | p03821 | u777283665 | 1569432941 | Python | PyPy3 (2.4.0) | py | Runtime Error | 165 | 38384 | 335 | import numpy as np
n = int(input())
A, B = np.zeros(n), np.zeros(n)
for i in range(n):
a, b = map(int, input().split())
A[i] = a
B[i] = b
ans = 0
for i in range(n-1, -1, -1):
if A[i] % B[i] == 0:
pass
else:
temp = B[i]*((A[i]//B[i])+1) - A[i]
ans += temp
A += temp
print(int(ans)) |
s022148258 | p03821 | u777283665 | 1569432398 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 130 | n = int(input())
A, B = np.zeros(n), np.zeros(n)
for i in range(n):
a, b = map(int, input().split())
A[i] = a
B[i] = b |
s238690297 | p03821 | u777283665 | 1569432321 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 55 | ans = 0
for i in range(10**9):
ans += 1
print(ans |
s854869476 | p03821 | u077291787 | 1569149039 | Python | Python (3.4.3) | py | Runtime Error | 65 | 25124 | 261 | # AGC009A - Multiple Array
def main():
N, *AB = map(int, open(0).read().split())
ans = 0
for i, j in zip(*[iter(A[::-1])] * 2): # decide from the last one greedily
ans += -(i + ans) % j
print(ans)
if __name__ == "__main__":
main() |
s446523836 | p03821 | u993622994 | 1567811120 | Python | Python (3.4.3) | py | Runtime Error | 423 | 31348 | 414 | N = int(input())
ab = [list(map(int, input().split())) for _ in range(N)]
ab.reverse()
count = 0
for i in range(N):
ab[i][0] += count
if ab[i][1] == 1:
continue
else:
if ab[i][0] > ab[i][1]:
num = ab[i][1] - ab[i][0] % ab[i][1]
elif ab[i][0] < ab[i][1]:
num = ab[i][0] - ab[i][1] % ab[i][0] - 1
count += num
ab[i][0] += num
print(count) |
s288003993 | p03821 | u498620941 | 1566525492 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3060 | 294 | A = []
B = []
for i in range(N):
a,b = map(int,input().split())
A.append(a)
B.append(b)
k = 0
for i in range(N-1,-1,-1):
if A[i] % B[i] == 0 :
continue
else :
c = (B[i] - A[i] % B[i])
k += c
for j in range(i):
A[j] += c
print(k) |
s864384772 | p03821 | u388415336 | 1559891851 | Python | Python (3.4.3) | py | Runtime Error | 326 | 11052 | 297 | seq_a = []
seq_b = []
seq_n = int(input())
for i in range(seq_n):
num_a,num_b=map(int,input().split())
seq_a.append(num_a)
seq_b.append(num_b)
x = 0
for i in range(seq_n - 1, -1 ,-1):
seq_n , elem_b = seq_a[i],seq_b[i]
y = (seq_n + x)% elem_b
if y != 0: x += (m-y)
print(x) |
s672444704 | p03821 | u388415336 | 1559891769 | Python | Python (3.4.3) | py | Runtime Error | 330 | 11052 | 299 | seq_a = []
seq_b = []
seq_n = int(input())
for i in range(seq_n):
num_a,num_b=map(int,input().split())
seq_a.append(num_a)
seq_b.append(num_b)
x = 0
for i in range(seq_n - 1, -1 ,-1):
elem_a , elem_b = seq_a[i],seq_b[i]
y = (elem_a + x)% elem_b
if y != 0: x += (m-y)
print(x) |
s336075198 | p03821 | u388415336 | 1559891706 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 291 | seq_a = []
seq_b = []
seq_n = int(input())
for i in range(seq_n):
num_a,num_b=map(int,input().split())
a.append(num_a)
b.append(num_b)
x = 0
for i in range(seq_n - 1, -1 ,-1):
elem_a , elem_b = seq_a[i],seq_b[i]
y = (elem_a + x)% elem_b
if y != 0: x += (m-y)
print(x) |
s352014053 | p03821 | u905715926 | 1552528791 | Python | Python (3.4.3) | py | Runtime Error | 316 | 26712 | 190 | n = int(input())
ans = 0
li = []
for i in range(n):
a,b = map(int,input().split())
li.append(map(int,input().split()))
for (a,b) in li[::-1]:
ans += (b-((a+ans)%b))%b
print(ans)
|
s885009770 | p03821 | u270681687 | 1550845640 | Python | PyPy3 (2.4.0) | py | Runtime Error | 182 | 45168 | 409 | import sys
sys.setrecursionlimit(10**7)
n = int(input())
rev_g = [[] for _ in range(n+1)]
for i in range(n-1):
a = int(input())
rev_g[a].append(i+2)
def f(v):
m = len(rev_g[v])
if m == 0:
return 0
q = []
for nv in rev_g[v]:
q.append(f(nv))
q.sort()
dp = [0] * (m+1)
for i in range(m):
dp[i+1] = max(q[i]+1, dp[i]+1)
return dp[m]
print(f(1))
|
s069351557 | p03821 | u595893956 | 1550821664 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 155 | n=int(input())
a=[]
b=[]
ret = 0
for i in range(n):
a[i],b[i]=map(int,input().split())
for i in range(n):
ret+=b[-1-i]-(ret+a[-1-i])%b[-1-i]
print(ret) |
s671260720 | p03821 | u434208140 | 1547076446 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3828 | 136 | n=int(input())
c=[list(map(int,unput().split())) for _ in [0]*n].reverse()
t=0
for i in c:
a=c[0]
b=c[2]
t+=b-1-(a+t-1)%b
print(t) |
s929416302 | p03821 | u459233539 | 1546493174 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 203 | n = int(input())
push = 0
for i in range(n):
a, b = map(int, input().split())
if a < b:
push += b - a
else a > b:
for j in range(b):
if (a + i * j)%b==0:
push += j
print(push) |
s101284367 | p03821 | u427344224 | 1540564459 | Python | Python (3.4.3) | py | Runtime Error | 373 | 11048 | 381 | N = int(input())
a_list = []
b_list = []
for i in range(N):
a, b = map(int, input().split())
a_list.append(a)
b_list.append(b)
count = 0
for i in range(N-1, -1, -1):
a = a_list[i]
b = b_list[i]
if b == 1:
continue
if b % a == 0:
continue
if count > 0:
a += count
margin = b - (a % b)
count += margin
print(count) |
s498882651 | p03821 | u451017206 | 1535676987 | Python | Python (3.4.3) | py | Runtime Error | 363 | 11816 | 255 | N = int(input())
A = []
B = []
for i in range(N):
a, b = map(int, input().split())
A.append(a)
B.append(b)
A = A[::-1]
B = B[::-1]
ans = 0
for a, b in zip(A, B):
a += ans
if a % b!= 0:
m = b - (a % b)
ans += m
print(ans) |
s439268851 | p03821 | u451017206 | 1535676218 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 269 | N = int(input())
A = []
B = []
for i in range(N):
a, b = map(int, input().split())
A.append(a)
B.append(b)
A = A[::-1]
B = B[::-1]
ans = 0
for a, b in zip(A, B):
a += ans
m = b - (a % b) if b != 1 else 0
m = b if a == 0
ans += m
print(ans) |
s824105834 | p03821 | u391475811 | 1528491258 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 260 | N=int(input())
A=[]
B=[]
for i in range(N):
a,b=map(int,input().split())
A.append(a)
B.append(b)
ans=0
for i in range(N-1,-1,-1):
a=A[i]+ans
b=B[i]
if a < b:
ans+=(b-a)
elif a % b==0
continue
else:
ans+=(a+b)//b*b-a
print(ans) |
s651071625 | p03821 | u952708174 | 1527790819 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 514 | def a_multiple_array_(N, A, B):
ans = 0 # ボタンを押した回数
for k in range(N - 1, -1, -1): # 末尾から貪欲に決めていく
remainder = (A[k] + ans) % B[k] # A[k]+ansが現在のA[k]の値
if remainder != 0: # 定義の仕方より、reminderは非負整数
ans += B[k] - remainder
return ans
N = int(Input())
A, B = [], []
for _ in range(N):
a, b = [int(i) for i in input().split()]
A.append(a)
B.append(b)
print(a_multiple_array_(N, A, B)) |
s925049031 | p03821 | u362700058 | 1522003091 | Python | Python (2.7.6) | py | Runtime Error | 10 | 2568 | 478 | N = int(raw_input())
A,B = [],[]
for _ in range(N):
a, b = raw_input().split()
a,b = int(a), int(b)
A.append(a)
B.append(b)
press = [0 for _ in range(N)]
for i in range(N)[::-1]:
a = A[i]
b = B[i]
# see if there's Amari
c = a % b
# if Amari exists, how many presses are needed to make it dividable?
if c>0: c = b - c
for j in range(i):
A[j] += c
# もしあまりがあるなら
press[i] = c
print sum(press) |
s066454226 | p03821 | u730606460 | 1516928709 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 261 | a = []
b = []
n = int(input())
ind = -1
count = 0
for i in range(n):
c,d = int(input().split())
a.append(c)
b.append(d)
for i in range((n-1),0,-1):
if a[i]+count % b[i] != 0:
count+= b[i] - (a[i] + count) % b[i]
print(count)
|
s545238072 | p03821 | u048472001 | 1487630283 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 283 |
N = int(input())
total = 0
A = [None] * N
B = [None] * N
for i in range(N):
A[i], B[i] = map(int, input().split())
for t in range(N):
i = N + 1 - t
a = A[i] % B[i]
if a == 0:
pass
else:
total = B[i]-a:
for j in range (i):
A[i] = A[i] + B[i] - a
print (total)
|
s032767995 | p03821 | u851455395 | 1485621184 | Python | Python (2.7.6) | py | Runtime Error | 140 | 4344 | 288 | from sys import stdin
n = stdin.readline()
a = []
b = []
#read input
for line in stdin:
a.append(int(line[0]))
b.append(int(line[2]))
counter = 0
i = int(n)-1
while i >= 0:
if( (a[i]+counter)%b[i]):
counter += b[i] - ((a[i]+counter)%b[i])
i-=1
print counter
|
s405245376 | p03821 | u631914718 | 1485541087 | Python | Python (3.4.3) | py | Runtime Error | 23 | 3064 | 215 | n = int(input())
a, b = [], []
for i in range(n):
a[i], b[i] = map(int, input().split())
ans = 0
for i in range(n)[::-1]:
a_, b_ = a[i] + ans, b[i]
if a_%b_ != 0:
ans += ((a_ // b_ + 1) * b_ - a_)
print(ans)
|
s545214903 | p03821 | u631914718 | 1485541080 | Python | Python (3.4.3) | py | Runtime Error | 23 | 3064 | 213 | n = int(input())
a, b = [], []
for i in range(n):
a[i], b[i] = map(int, input().split())
ans = 0
for i in range(n)[::-1]:
a_, b_ = a[i] + ans, b[i]
if a_%b_ != 0:
ans += ((a_ // b_ + 1) * b_ - a_)
print(p)
|
s698031805 | p03821 | u631914718 | 1485540710 | Python | Python (3.4.3) | py | Runtime Error | 369 | 10996 | 215 | n = int(input())
a, b = [0]*n, [0]*n
for i in range(n):
a[i], b[i] = map(int, input().split())
ans = 0
for i in range(n)[::-1]:
a_, b_ = a[i] + p, b[i]
if a_%b_ != 0:
ans += (a_ // b_ + 1) * b_ - a_
print(p)
|
s239157077 | p03821 | u631914718 | 1485540659 | Python | Python (3.4.3) | py | Runtime Error | 377 | 10996 | 212 | n = int(input())
a, b = [0]*n, [0]*n
for i in range(n):
a[i], b[i] = map(int, input().split())
ans = 0
for i in range(n)[::-1]:
a_, b_ = a[i] + p, b[i]
if a%b != 0:
ans += (a_ // b_ + 1) * b_ - a_
print(p) |
s425593334 | p03821 | u966695411 | 1485372942 | Python | Python (3.4.3) | py | Runtime Error | 29 | 3064 | 264 | N = int(input())
cnt = 0
L = [list(map(int, input().split())) for x in range(N)]
for a, b in L[::-1]:
a += cnt
if a < b && a != 0 : cnt += b - a
if a > b:
if a % b:
cnt += b - a % b
else:
cnt += a % b
print(cnt) |
s055007266 | p03821 | u090994275 | 1485305639 | Python | Python (3.4.3) | py | Runtime Error | 23 | 3064 | 249 | N = input()
A = []
B = []
for i in range(N):
a, b = input().split()
A.append(a)
B.append(b)
ans = 0
for i in reversed(range(N)):
diff = B[i] - A[i] % B[i]
for j in range(i):
A[j] += diff
ans += diff
print(ans)
|
s538033103 | p03821 | u090994275 | 1485305530 | Python | Python (3.4.3) | py | Runtime Error | 22 | 3192 | 248 | N = input()
A = []
B = []
for i in range(N):
a, b = input().split()
A.append(a)
B.append(b)
ans = 0
for i in reversed(range(N)):
diff = B[i] - A[i] % B[i]
for j in range(i):
A[j] += diff
ans += diff
return ans |
s488582758 | p03821 | u107077660 | 1485144212 | Python | Python (3.4.3) | py | Runtime Error | 59 | 14260 | 502 | N = int(input())
B = [[] for _ in range(N)]
unchecked = [i for i in range(N)]
for i in range(1,N):
t = int(input()) - 1
B[t].append(i)
ans = [0]*N
for i in range(N):
if not B[i]:
unchecked.remove(i)
while 0 in unchecked:
for i in unchecked:
f = 0
C = []
for a in B[i]:
if a in unchecked:
f = 1
break
else:
C.append(ans[a])
if not f:
C.sort()
C.reverse()
l = len(C)
ans[i] = max([j+C[j]+1 for j in range(l)])
unchecked.remove(i)
print(ans[0]) |
s475748727 | p03821 | u481333386 | 1485143603 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3064 | 555 | def calc(a, b):
push_count = 0
while True:
if a % b == 0:
break
a += 1
push_count += 1
return push_count
def add_a(answer, a):
return answer + a
def main(nums):
answer = 0
for a, b in reversed(nums):
if answer != 0:
a = add_a(answer, a)
answer += calc(a, b)
return answer
if __name__ == '__main__':
n = int(input())
nums = []
for i in range(n):
nums[i] = [int(e) for e in input().split()]
answer_ = main(nums)
print(answer_) |
s729384588 | p03821 | u481333386 | 1485142983 | Python | Python (3.4.3) | py | Runtime Error | 22 | 3192 | 564 | def calc(a, b):
push_count = 0
while True:
if a % b == 0:
break
a += 1
push_count += 1
return push_count
def add_a(answer, a):
return answer + a
def main(nums):
answer = 0
nums.reverse()
for a, b in nums:
if answer != 0:
a = add_a(answer, a)
answer += calc(a, b)
return answer
if __name__ == '__main__':
n = int(input())
nums = []
for i in range(n):
nums[i] = [int(e) for e in input().split()]
answer_ = main(nums)
print(answer_) |
s195568729 | p03821 | u481333386 | 1485140158 | Python | Python (3.4.3) | py | Runtime Error | 23 | 3192 | 618 | def calc(a, b):
push_count = 0
while True:
if a % b == 0:
break
a += 1
push_count += 1
return push_count
def add_a(answer, a):
return answer + a
def main(nums):
answer = 0
nums.reverse()
for a, b in nums:
if answer != 0:
a = add_a(answer, a)
answer += calc(a, b)
return answer
if __name__ == '__main__':
n = int(input())
nums = []
append = nums.append
for _ in range(n):
a_and_b = [(a, b) for a, b in input().split()]
append(a_and_b)
answer_ = main(nums)
print(answer_)
|
s990238408 | p03821 | u093843560 | 1485139745 | Python | Python (2.7.6) | py | Runtime Error | 16 | 2568 | 431 | mport numpy as np
N = int(raw_input())
a=np.array([])
b=np.array([])
for i in range(N-1):
A, B = map(int, raw_input().split())
a = np.append(a,A)
b = np.append(b,B)
A,B = map(int, raw_input().split())
a = np.append(a,A)
b = np.append(b,B)
s=0
for i in range(N):
if a[N-i-1] % b[N-i-1] ==0:
t = 0
else:
t = b[N-i-1] -(a[N-i-1] % b[N-i-1])
a = a+t*np.ones(N)
s = t + s
print int(s)
|
s677940065 | p03821 | u425351967 | 1485137952 | Python | Python (3.4.3) | py | Runtime Error | 22 | 3064 | 332 | N = int(input())
A=[]
B=[]
for i in range(N):
a,b=[int(n) for n in input().split()]
A.append(a)
B.append(b)
cnt=0
# print(list(reversed(range(N))))
for i in reversed(range(N)):
if A[i]%B[i]==0:
r=0
else
r=B[i]-A[i]%B[i]
cnt+=r
A=[A[n]+r if n<=i else A[n] for n in range(N)]
print(cnt)
|
s117466177 | p03822 | u457901067 | 1600361200 | Python | PyPy3 (7.3.0) | py | Runtime Error | 531 | 220376 | 751 | import sys
input = sys.stdin.readline
import heapq
from collections import deque
N = int(input())
X = [[] for _ in range(N)]
for i in range(N-1):
a = int(input())
X[a-1].append(i+1)
# dfsをして、Depthを計算しつつ、depth大きい順に取り出せるようにheapqを使う
h = []
seen = [False] * N
def dfs(a,depth):
seen[a] = True
heapq.heappush(h, (-depth, a))
for nxt in X[a]:
#if seen[nxt]:
#continue
dfs(nxt, depth+1)
dfs(0,0)
#rint(len(h))
DP = [0] * N
while h:
depth, a = heapq.heappop(h)
cand = []
for child in X[a]:
cand.append(DP[child])
if not cand:
DP[a] = 1
continue
cand.sort(reverse=True)
for i in range(len(cand)):
DP[a] = max(DP[a], cand[i]+i+1)
print(DP[0]-1) |
s429326804 | p03822 | u536034761 | 1600094446 | Python | Python (3.8.2) | py | Runtime Error | 271 | 23320 | 289 | n = int(input())
graph = [[] for _ in range(n)]
for i in range(1, n):
graph[int(input()) - 1].append(i)
def dfs(x):
arr = [dfs(y) for y in graph[x]]
if not (arr):
return 0
arr.sort(reverse=True)
return max(i + a for i, a in enumerate(arr, 1))
print(dfs(0))
|
s832765667 | p03822 | u368796742 | 1599083875 | Python | Python (3.8.2) | py | Runtime Error | 263 | 23132 | 289 | n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
x = int(input())
e[x-1].append(i)
def dfs(x):
count = len(e[x])
if count == 0:
return 0
now = 0
for nex in e[x]:
now = max(now,dfs(nex))
return max(count,now+1)
print(dfs(0)) |
s771251925 | p03822 | u654470292 | 1597789260 | Python | PyPy3 (7.3.0) | py | Runtime Error | 103 | 74656 | 1001 | import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
return sys.stdin.readline()[:-1]
def ruiseki(lst):
return [0]+list(accumulate(lst))
def celi(a,b):
return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]
def main()
n=int(input())
a=[int(input()) for i in range(n-1)]
# print(a)
dic=defaultdict(list)
for i in range(n-1):
p=a[i]
dic[p].append(i+2)
# print(dic)
lst=[-1]*(n+1)
def solve(m):
if lst[m]!=-1:
return lst[m]
tmp=[]
for i in dic[m]:
tmp.append(solve(i))
tmp.sort(reverse=True)
rtn=0
for i in range(len(tmp)):
rtn=max(rtn,tmp[i]+i+1)
lst[m]=rtn
return rtn
solve(1)
# print(lst)
print(max(lst))
main() |
s404495915 | p03822 | u550061714 | 1592398175 | Python | Python (3.4.3) | py | Runtime Error | 619 | 123036 | 312 | import sys
sys.setrecursionlimit(10 ** 5)
N = int(input())
T = [[] for _ in range(N)]
for i in range(1, N):
x = int(input()) - 1
T[x].append(i)
def f(x):
candidates = sorted([f(y) for y in T[x]], reverse=True)
return max((c + i for i, c in enumerate(candidates, 1)), default=0)
print(f(0))
|
s156707832 | p03822 | u287132915 | 1591887794 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 538 | import sys
sys.setrecursionlimit(10**10)
n = int(input())
dic = {}
for i in range(n+1):
if i <= 1: continue
ai = int(input())
if ai in dic:
dic[ai].append(i)
else:
dic[ai] = [i]
def dfs(nxt):
lst = []
if nxt in dic:
while dic[nxt] != []:
child = dic[nxt].pop()
tempmax = dfs(child)
lst.append(tempmax)
else:
return 0
lst.sort(reverse=True)
for i in range(len(lst)):
lst[i] += i+1
return max(lst)
ans = dfs(1)
print(ans) |
s905833495 | p03822 | u287132915 | 1591887683 | Python | Python (3.4.3) | py | Runtime Error | 428 | 26016 | 496 | n = int(input())
dic = {}
for i in range(n+1):
if i <= 1: continue
ai = int(input())
if ai in dic:
dic[ai].append(i)
else:
dic[ai] = [i]
def dfs(nxt):
lst = []
if nxt in dic:
while dic[nxt] != []:
child = dic[nxt].pop()
tempmax = dfs(child)
lst.append(tempmax)
else:
return 0
lst.sort(reverse=True)
for i in range(len(lst)):
lst[i] += i+1
return max(lst)
ans = dfs(1)
print(ans) |
s204073054 | p03822 | u227082700 | 1591790002 | Python | Python (3.4.3) | py | Runtime Error | 467 | 19196 | 217 | n=int(input())
edge=[[]for _ in range(n)]
for i in range(n-1):edge[int(input())-1].append(i+1)
def dfs(v):
d=[-1]+sorted([dfs(i)for i in edge[v]])
l=len(d)
return max(l-i+j for i,j in enumerate(d))
print(dfs(0)) |
s265527616 | p03822 | u102445737 | 1591709867 | Python | PyPy3 (2.4.0) | py | Runtime Error | 936 | 178380 | 615 | #from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
def ddprint(x):
if DBG:
print(x)
def dep(v):
if len(dst[v])==0:
return 0
a = []
for u in dst[v]:
a.append((dep(u),u))
a.sort(reverse=True)
return max([a[i][0]+i+1 for i in range(len(a))])
n = inn()
dst = [{} for i in range(n+1)]
for i in range(2,n+1):
dst[inn()][i] = 1
print(dep(1))
|
s413800862 | p03822 | u424768586 | 1591480654 | Python | PyPy3 (2.4.0) | py | Runtime Error | 266 | 56668 | 2413 | import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする
from functools import lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False):
inds=sorted(range(len(s)), key=lambda k: s[k])
if return_sorted: return inds, [s[i] for i in inds]
return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
K,N,M=len(B),len(A),len(B[0])
return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
N,size=len(v),len(M)
return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
n,m=len(M),len(M[0])
return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
mod = 10**9+7
#w.sort(key=itemgetter(1),reversed=True) #二個目の要素で降順並び替え
N = int(input())
#N, K = map(int, input().split())
#A = tuple(map(int, input().split())) #1行ベクトル
A = tuple(int(input()) for i in range(N-1)) #改行ベクトル
#S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
a=Counter(A)
d0=a[1]
a[1]=0
d1=max(a.values())+1
d2=math.ceil(math.log2(N))
#print(d1,d2,d0)
print(max(d0,d1,d2))
if __name__ == "__main__":
main() |
s380595815 | p03822 | u169350228 | 1590365582 | Python | Python (3.4.3) | py | Runtime Error | 371 | 20740 | 479 | import queue
n = int(input())
wl = [[] for i in range(n+1)]
for i in range(2,n+1):
ai = int(input())
wl[ai].append(i)
dp = [-1 for i in range(n+1)]
def calc_dp(a):
if len(wl[a]) == 0:
return 0
else:
dps = []
for i in wl[a]:
dps.append(calc_dp(i))
dps.sort(reverse=True)
ma = 0
for i,dpi in enumerate(dps):
if ma < dpi+i:
ma = dpi+i
return ma+1
print(calc_dp(1))
|
s708988017 | p03822 | u488401358 | 1588040902 | Python | PyPy3 (2.4.0) | py | Runtime Error | 782 | 111460 | 582 | N=int(input())
edge={i:[] for i in range(1,N+1)}
parent={i:0 for i in range(1,N+1)}
for i in range(0,N-1):
a=int(input())
edge[a].append(i+2)
edge[i+2].append(a)
parent[i+2]=a
memo={i:-1 for i in range(1,N+1)}
def dp(num):
if memo[num]!=-1:
return memo[num]
if edge[num]==[parent[num]]:
memo[num]=0
return memo[num]
test=[]
for i in edge[num]:
if i!=parent[num]:
test.append(dp(i))
test.sort(reverse=True)
memo[num]=max(i+1+test[i] for i in range(0,len(test)))
return memo[num]
print(dp(1)) |
s817809152 | p03822 | u145600939 | 1587319284 | Python | Python (3.4.3) | py | Runtime Error | 461 | 38884 | 511 | n = int(input())
win = {}
# key:勝者 value:keyに負けた人(set)
for i in range(n - 1):
a = int(input())
a -= 1
if a not in win.keys():win[a] = set()
win[a].add(i+1)
def dfs(winner):
if winner not in win.keys():return 0
losers = win[winner]
storong = len(losers)
deeps = []
for loser in losers:
deeps.append(dfs(loser))
deeps.sort(reverse = True)
deep = 1
for i,d in enumerate(deeps):
deep = max(deep, i+1 + d)
return deep
print(dfs(0))
|
s530506363 | p03822 | u693716675 | 1585691064 | Python | Python (3.4.3) | py | Runtime Error | 351 | 19000 | 451 | #b
n = int(input())
edges = [[] for _ in range(n)]
for i in range(n-1):
b = int(input())
edges[b-1].append(i+1)
def dfs(u):
e = edges[u]
if e==[]:
return 0
#else
#return maximum
length = []
for v in e:
length.append(dfs(v))
l = len(length)
length.sort()
maxi = 0
for v in length:
cal = v + l
maxi= max(maxi, cal)
l -= 1
return maxi
print(dfs(0)) |
s560975678 | p03822 | u065446124 | 1584324327 | Python | Python (3.4.3) | py | Runtime Error | 373 | 28856 | 368 | import sys
input=sys.stdin.readline
n=int(input())
l=[int(input()) for i in range(n-1)]
import collections
d=collections.defaultdict(list)
for i,j in enumerate(l,2):
d[j].append(i)
def f(a):
if len(d[a])==0:
return 0
else:
l=[f(i) for i in d[a]]
l.sort(reverse=True)
return max([i+l[i]+1 for i in range(len(l))])
print(f(1)) |
s464292420 | p03822 | u476604182 | 1583544952 | Python | Python (3.4.3) | py | Runtime Error | 498 | 31824 | 349 | from functools import lru_cache
N, *A = map(int, open(0).read().split())
dic = [[] for i in range(N+1)]
for i in range(N-1):
dic[A[i]] += [i+2]
@lru_cache(None)
def solve(n):
if len(dic[n])==0:
return 0
ls = []
for e in dic[n]:
ls += [solve(e)]
ls.sort(reverse=True)
return max(ls[i]+i+1 for i in range(len(ls)))
print(solve(1)) |
s863980969 | p03822 | u547167033 | 1582682894 | Python | PyPy3 (2.4.0) | py | Runtime Error | 848 | 141360 | 302 | import sys
input=sys.stdin.readline
n=int(input())
t=[[] for i in range(n)]
for i in range(1,n):
a=int(input())
t[a-1].append(i)
def dfs(v):
if not t[v]:
return 0
l=[dfs(i)+1 for i in t[v]]
l.sort()
for i in range(len(t[v])-1):
l[i+1]=max(l[i+1],l[i]+1)
return l[-1]
print(dfs(0)) |
s300355491 | p03822 | u712993629 | 1582675278 | Python | PyPy3 (2.4.0) | py | Runtime Error | 837 | 142796 | 351 | n = int(input())
g = [[] * 1 for i in range(n)]
for i in range(n-1):
a = int(input())
g[a-1].append(i+1)
def dfs(v):
max = 0
max_i = 0
for u in g[v]:
next = dfs(u)
if next == max:
max_i += 1
elif next > max:
max = next
max_i = 1
return max + max_i
print(dfs(0))
|
s420077118 | p03822 | u712993629 | 1582675192 | Python | Python (3.4.3) | py | Runtime Error | 325 | 18912 | 351 | n = int(input())
g = [[] * 1 for i in range(n)]
for i in range(n-1):
a = int(input())
g[a-1].append(i+1)
def dfs(v):
max = 0
max_i = 0
for u in g[v]:
next = dfs(u)
if next == max:
max_i += 1
elif next > max:
max = next
max_i = 1
return max + max_i
print(dfs(0))
|
s475568412 | p03822 | u754022296 | 1581814254 | Python | Python (3.4.3) | py | Runtime Error | 1431 | 594968 | 476 | import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
import heapq
def main():
n = int(input())
T = [[] for _ in range(n+1)]
for i in range(1, n):
T[int(input())].append(i)
def dfs(v):
if not T[v]:
return 0
L = []
for i in T[v]:
heapq.heappush(L, dfs(i)+1)
temp = heapq.heappop(L)
while L:
temp = max(temp+1, heapq.heappop(L))
return temp
ans = dfs(1)
print(ans)
if __name__ == "__main__":
main() |
s467743144 | p03822 | u141610915 | 1580955738 | Python | PyPy3 (2.4.0) | py | Runtime Error | 702 | 182528 | 488 | import sys
input = sys.stdin.readline
N = int(input())
a = [0]
reva = [[] for _ in range(N + 1)]
for _ in range(N - 1): a.append(int(input()))
for i in range(N):
if a[i] == 0: continue
reva[a[i]].append(i + 1)
dp = [-1] * (N + 1)
def dfs(x):
global dp
if len(reva[x]) == 0:
dp[x] = 1
return 1
t = []
for y in reva[x]:
dfs(y)
t.append(dp[y])
t.sort(reverse = True)
for i in range(len(t)):
t[i] += i + 1
dp[x] = max(t)
return dp[x]
print(dfs(1) - 1) |
s439285460 | p03822 | u141610915 | 1580955449 | Python | PyPy3 (2.4.0) | py | Runtime Error | 533 | 137136 | 494 | import sys
input = sys.stdin.readline
N = int(input())
a = [0]
reva = [[] for _ in range(N + 1)]
for _ in range(N - 1): a.append(int(input()))
for i in range(N):
if a[i] == 0: continue
reva[a[i]].append(i + 1)
dp = [-1] * (N + 1)
def dfs(x):
global dp
if len(reva[x]) == 0:
dp[x] = 1
return 1
t = []
for y in reva[x]:
t.append(dfs(y))
t.sort(reverse = True)
return dp[x]
for i in range(len(t)):
t[i] += i + 1
dp[x] = max(t)
return dp[x]
print(dfs(1) - 1) |
s011461622 | p03822 | u141610915 | 1580955388 | Python | PyPy3 (2.4.0) | py | Runtime Error | 688 | 182784 | 479 | import sys
input = sys.stdin.readline
N = int(input())
a = [0]
reva = [[] for _ in range(N + 1)]
for _ in range(N - 1): a.append(int(input()))
for i in range(N):
if a[i] == 0: continue
reva[a[i]].append(i + 1)
dp = [-1] * (N + 1)
def dfs(x):
global dp
if len(reva[x]) == 0:
dp[x] = 1
return 1
t = []
for y in reva[x]:
t.append(dfs(y))
t.sort(reverse = True)
for i in range(len(t)):
t[i] += i + 1
dp[x] = max(t)
return dp[x]
print(dfs(1) - 1) |
s906832780 | p03822 | u141610915 | 1580955134 | Python | PyPy3 (2.4.0) | py | Runtime Error | 720 | 185216 | 510 | import sys
input = sys.stdin.readline
N = int(input())
a = [0]
reva = [[] for _ in range(N + 1)]
for _ in range(N - 1): a.append(int(input()))
for i in range(N):
if a[i] == 0: continue
reva[a[i]].append(i + 1)
dp = [-1] * (N + 1)
def dfs(x):
global dp
if len(reva[x]) == 0:
dp[x] = 1
return 1
t = []
for i in range(len(reva[x])):
y = reva[x][i]
t.append(dfs(y))
t.sort(reverse = True)
for i in range(len(t)):
t[i] += i + 1
dp[x] = max(t)
return dp[x]
print(dfs(1) - 1) |
s118270517 | p03822 | u141610915 | 1580954172 | Python | PyPy3 (2.4.0) | py | Runtime Error | 723 | 185344 | 515 | import sys
input = sys.stdin.readline
N = int(input())
a = [0]
reva = [[] for _ in range(N + 1)]
for _ in range(N - 1): a.append(int(input()))
for i in range(N):
if a[i] == 0: continue
reva[a[i]].append(i + 1)
dp = [-1] * (N + 1)
def dfs(x):
global dp
if len(reva[x]) == 0:
dp[x] = 1
return 1
t = []
for i in range(len(reva[x])):
y = reva[x][i]
t.append(dfs(y))
t.sort(reverse = True)
for i in range(len(reva[x])):
t[i] += i + 1
dp[x] = max(t)
return dp[x]
print(dfs(1) - 1) |
s921198196 | p03822 | u334712262 | 1577354140 | Python | Python (3.4.3) | py | Runtime Error | 590 | 122872 | 1648 | # -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(input())
def read_int_n():
return list(map(int, input().split()))
def read_float():
return float(input())
def read_float_n():
return list(map(float, input().split()))
def read_str():
return input().strip()
def read_str_n():
return list(map(str, input().split()))
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, 'sec')
return ret
return wrap
@mt
def slv(N, A):
g = defaultdict(list)
for i, a in enumerate(A):
i += 2
g[a].append(i)
@lru_cache(maxsize=None)
def f(u):
d = []
for v in g[u]:
d.append(f(v))
if not d:
return 0
heapq.heapify(d)
while len(d) > 1:
l = heapq.heappop(d)
r = heapq.heappop(d)
heapq.heappush(d, max((l + 1, r)))
return heapq.heappop(d) + 1
return f(1)
def main():
N = read_int()
A = [read_int() for _ in range(N-1)]
print(slv(N, A))
if __name__ == '__main__':
main()
|
s806306472 | p03822 | u606045429 | 1576188402 | Python | Python (3.4.3) | py | Runtime Error | 313 | 23112 | 253 | N, *A = map(int, open(0).read().split())
E = [[] for _ in range(N + 1)]
for i, v in enumerate(A, 1):
E[v - 1].append(i)
def dfs(v):
D = sorted(dfs(u) for u in E[v])
return max([0] + [len(D) - i + d for i, d in enumerate(D)])
print(dfs(0)) |
s362420161 | p03822 | u532966492 | 1574919304 | Python | Python (3.4.3) | py | Runtime Error | 358 | 35696 | 440 | def main():
n=int(input())
a=[int(input()) for _ in [0]*(n-1)]
a=[[i+2,a[i]] for i in range(n-1)]
l=[[] for _ in [0]*n]
for i,j in a:
l[j-1].append(i-1)
def dps(i):
if l[i]==[]:
return 1
_max=0
_min=10**6
for j in l[i]:
temp=dps(j)
_max=max(temp,_max)
_min=min(temp,_min)
return max(_max,_min+1)
print(dps(0))
main() |
s286965951 | p03822 | u844789719 | 1573180713 | Python | Python (3.4.3) | py | Runtime Error | 671 | 159972 | 514 | import collections, sys
sys.setrecursionlimit(10**5 + 5)
N, *A = [int(_) for _ in open(0).read().split()]
win_lose = {i: set() for i in range(1, N + 1)}
for win, lose in zip(A, range(2, N + 1)):
win_lose[win].add(lose)
memo = [-1] * (N + 1)
def rec(x):
if memo[x] == -1:
if win_lose[x] == set():
memo[x] = 0
else:
memo[x] = max(a + b for a, b in zip(
sorted(rec(y) for y in win_lose[x])[::-1], range(1, N + 1)))
return memo[x]
print(rec(1))
|
s759457794 | p03822 | u844789719 | 1573180647 | Python | Python (3.4.3) | py | Runtime Error | 651 | 159976 | 526 | import collections, sys
sys.setrecursionlimit(10**5 + 5)
N, *A = [int(_) for _ in open(0).read().split()]
win_lose = {i: set() for i in range(1, N + 1)}
for win, lose in zip(A, range(2, N + 1)):
win_lose[win].add(lose)
memo = [-1] * (N + 1)
def rec(x):
if memo[x] == -1:
if win_lose[x] == set():
memo[x] = 0
else:
memo[x] = max(a + b for a, b in zip(
sorted(rec(y) for y in win_lose[x])[::-1], range(1, N + 1)))
return memo[x]
print(rec(1))
print(memo)
|
s171694968 | p03822 | u844789719 | 1573179305 | Python | Python (3.4.3) | py | Runtime Error | 557 | 143736 | 556 | import collections, sys
sys.setrecursionlimit(10**5 + 5)
N, *A = [int(_) for _ in open(0).read().split()]
win_lose = {i: set() for i in range(1, N + 1)}
for win, lose in zip(A, range(2, N + 1)):
win_lose[win].add(lose)
memo = [-1] * (N + 1)
def rec(x):
if memo[x] == -1:
if win_lose[x] == set():
memo[x] = 0
else:
memo[x] = max(rec(y) for y in win_lose[x]) + len(win_lose[x])
return memo[x]
print(
max(a + b for a, b in zip(
sorted(rec(x) for x in win_lose[1])[::-1], range(1, N + 1))))
|
s931165288 | p03822 | u844789719 | 1573179085 | Python | Python (3.4.3) | py | Runtime Error | 308 | 52028 | 468 | import collections
N, *A = [int(_) for _ in open(0).read().split()]
win_lose = {i: set() for i in range(1, N + 1)}
for win, lose in zip(A, range(2, N + 1)):
win_lose[win].add(lose)
memo = [-1] * (N + 1)
def rec(x):
if memo[x] == -1:
if win_lose[x] == set():
memo[x] = 0
else:
memo[x] = max(rec(y) for y in win_lose[x]) + len(win_lose[x])
return memo[x]
for x in win_lose[1]:
rec(x)
print(1 + max(memo[2:]))
|
s343292117 | p03822 | u543954314 | 1571418794 | Python | Python (3.4.3) | py | Runtime Error | 435 | 20004 | 379 | n = int(input())
tree = [list() for _ in range(n+1)]
for i in range(2,n+1):
a = int(input())
tree[a].append(i)
win = [0]*(n+1)
def dfs(v,win):
if tree[v] == []:
win[v] = 0
return win[v]
l = list()
for x in tree[v]:
dfs(x,win)
l.append(win[x])
l.sort(reverse=True)
win[v] = max(i+1+l[i] for i in range(len(l)))
return win[v]
print(dfs(1,win)) |
s222932462 | p03822 | u729133443 | 1570597812 | Python | Python (3.4.3) | py | Runtime Error | 302 | 22804 | 187 | def dfs(v):
d=sorted(map(dfs,t[v]))
l=len(d)
return max([0]+[l-i+d for i,d in e(d)])
e=enumerate
n,*a=map(int,open(0))
t=[[]for _ in[0]+a]
for i,v in e(a,1):t[v-1]+=i,
print(dfs(0)) |
s690158283 | p03822 | u509368316 | 1570064516 | Python | Python (3.4.3) | py | Runtime Error | 524 | 135828 | 374 | import sys
def input(): return sys.stdin.readline().strip()
N=int(input())
A=[int(input()) for i in range(N-1)]
c=[[] for i in range(N)]
for i,a in enumerate(A):
c[a-1].append(i+1)
import sys
sys.setrecursionlimit(10**5+9)
def f(i):
if len(c[i])==0:
return 0
return max([e+i+1 for i,e in enumerate(sorted([f(j) for j in c[i]],reverse=True))])
print(f(0)) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.