Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
a_n = [0] * n
b_n = [0] * n
for i in range(n):
    a_n[i] = bisect.bisect_right(b, a[i])
for i in range(n):
    b_n[i] = n - bisect.bisect_right(c, b[i])
ans = 0
for i in a_n:
    ans += sum(b_n[i:])
print(ans)

Fixed submission:
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
a_n = [0] * n
b_n = [0] * n
for i in range(n):
    a_n[i] = bisect.bisect_right(b, a[i])
for i in range(n):
    b_n[i] = n - bisect.bisect_right(c, b[i])
ans = 0
b_n_t = [0]
for i in range(n):
    b_n_t.append(b_n[i] + b_n_t[i])
for i in a_n:
    ans += b_n_t[-1] - b_n_t[i]
print(ans)
Buggy python submission (verdict: Runtime Error):
sd = input()
t = input()
n = len(sd)
m = len(t)
ss = []
for i in range(n - m + 1):
    flag = True
    s = ""
    for j in range(m):
        if sd[i + j] == "?" or sd[i + j] == t[j]:
            continue
        else:
            flag = False
    if flag:
        k = 0
        for j in range(n):
            if j < i or j > i + m:
                if sd[j] == "?":
                    s += "a"
                else:
                    s += sd[j]
            else:
                if sd[j] == "?":
                    s += t[k]
                else:
                    s += sd[j]
                k += 1
        ss.append(s)
if ss:
    print(sorted(ss)[0])
else:
    print("UNRESTORABLE")

Fixed submission:
sd = input()
t = input()
for i in range(len(sd) - len(t), -1, -1):
    t_kamo = sd[i:i + len(t)]
    for j in range(len(t) + 1):
        if j == len(t):
            s = sd[:i].replace("?", "a") + t + sd[i + len(t):].replace("?", "a")
            print(s)
            exit()
        if t_kamo[j] == "?":
            continue
        elif t_kamo[j] != t[j]:
            break
print("UNRESTORABLE")
Buggy python submission (verdict: Runtime Error):
s = input()

i = 0
j = len(s) - 1
ans = 0
while i != j:
    if s[i] != s[j]:
        if s[i] != "x" and s[j] != "x":
            print(-1)
            exit()
        elif s[i] != "x":
            j -= 1
        else:
            i += 1
        ans += 1
    else:
        i += 1
        j -= 1

print(ans)

Fixed submission:
s = input()

i = 0
j = len(s) - 1
ans = 0
while i != j:
    if abs(i - j) == 1:
        if s[i] == s[j]:
            break
    if s[i] != s[j]:
        if s[i] != "x" and s[j] != "x":
            print(-1)
            exit()
        elif s[i] != "x":
            j -= 1
        else:
            i += 1
        ans += 1
    else:
        i += 1
        j -= 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
d = [[float("inf")] * n for i in range(n)]
edge = []
for i in range(m):
    a, b = map(int, input().split())
    edge.append((a, b))
    d[a - 1][b - 1] = 1
    d[b - 1][a - 1] = 1
ans = 0
for a in range(m):
    d_s = [x[:] for x in d]
    x, y = edge[a]
    d_s[x - 1][y - 1] = float("inf")
    d_s[y - 1][x - 1] = float("inf")
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d_s[i][j] = min(d_s[i][j], d_s[i][k] + d_s[k][j])
    flag = 0
    for i in range(n):
        if flag:
            break
        for j in range(n):
            if flag:
                break
            if d_s[i][j] == float("inf"):
                ans += 1
                flag = 1
print(ans)

Fixed submission:
n, m = map(int, input().split())
d = [[float("inf")] * n for i in range(n)]
edge = []
for i in range(m):
    a, b = map(int, input().split())
    edge.append((a, b))
    d[a - 1][b - 1] = 1
    d[b - 1][a - 1] = 1
ans = 0
for a in range(m):
    d_s = [x[:] for x in d]
    x, y = edge[a]
    d_s[x - 1][y - 1] = float("inf")
    d_s[y - 1][x - 1] = float("inf")
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d_s[i][j] = min(d_s[i][j], d_s[i][k] + d_s[k][j])
    flag = 0
    for i in range(n):
        if flag:
            break
        for j in range(n):
            if flag:
                break
            if d_s[i][j] == float("inf"):
                ans += 1
                flag = 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
for h in range(1, 3501):
    for n in range(h, 3501):
        bunshi = N * h * n
        bunbo = 4 * h * n - N * n - N * h
        if bunbo > 0 and bunshi % bunbo == 0:
            print("{} {} {}".format(h, n, bunshi // bunbo))
            exit()

Fixed submission:
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        waru = 4 * h * n - N * n - N * h
        if waru != 0:
            w = (N * h * n) / waru
            if w > 0 and int(w) / w == 1:
                print("{} {} {}".format(h, n, int(w)))
                exit()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        bunbo = 4 * h * n - N * n - N * h
        if bunbo > 0 and N * h * n % bunbo == 0:
            print("{} {} {}".format(h, n, N * h * n // bunbo))
            exit()

Fixed submission:
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        waru = 4 * h * n - N * n - N * h
        if waru != 0:
            w = (N * h * n) / waru
            if w > 0 and int(w) / w == 1:
                print("{} {} {}".format(h, n, int(w)))
                exit()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        bunshi = N * h * n
        bunbo = 4 * h * n - N * n - N * h
        if bunbo > 0 and bunshi % bunbo == 0:
            print("{} {} {}".format(h, n, bunshi // bunbo))
            exit()

Fixed submission:
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        waru = 4 * h * n - N * n - N * h
        if waru != 0:
            w = (N * h * n) / waru
            if w > 0 and int(w) / w == 1:
                print("{} {} {}".format(h, n, int(w)))
                exit()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        waru = 4 * h * n - N * n - N * h
        if waru > 0 and (N * h * n) % waru == 0:
                print("{} {} {}".format(h, n, (N * h * n) // waru))
                exit()

Fixed submission:
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        waru = 4 * h * n - N * n - N * h
        if waru != 0:
            w = (N * h * n) / waru
            if w > 0 and int(w) / w == 1:
                print("{} {} {}".format(h, n, int(w)))
                exit()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        waru = 4 * h * n - N * n - N * h
        if waru != 0:
            w = (N * h * n) / waru
            if w > 0 and int(w) / w == 1:
                print("{} {} {}".format(h, n, int(w)))
                exit()

Fixed submission:
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        waru = 4 * h * n - N * n - N * h
        if waru != 0:
            w = (N * h * n) / waru
            if w > 0 and int(w) / w == 1:
                print("{} {} {}".format(h, n, int(w)))
                exit()
Buggy python submission (verdict: Runtime Error):
a, b, c, d, e, f = map(int, input().split())
x = set()
y = set()
for A in range(0, f + 1, 100 * a):
    for B in range(0, f + 1, 100 * b):
        if A + B > f:
            break
        if A + B:
            x.add(A + B)
for C in range(0, f + 1, c):
    for D in range(0, f + 1, d):
        if C + D > f:
            break
        y.add(C + D)
ans = 0
for X in x:
    for Y in y:
        if X + Y <= f and 100 * Y / X <= e:
                if ans < Y / (X + Y):
                    ans = Y / (X + Y)
                    w = X
                    s = Y
print(w + s, s)

Fixed submission:
a, b, c, d, e, f = map(int, input().split())
x = set()
y = set()
for A in range(0, f + 1, 100 * a):
    for B in range(0, f + 1, 100 * b):
        if A + B > f:
            break
        if A + B:
            x.add(A + B)
for C in range(0, f + 1, c):
    for D in range(0, f + 1, d):
        if C + D > f:
            break
        y.add(C + D)
ans = 0
w = 100 * a
s = 0
for X in x:
    for Y in y:
        if X + Y <= f and 100 * Y / X <= e:
                if ans < Y / (X + Y):
                    ans = Y / (X + Y)
                    w = X
                    s = Y
print(w + s, s)

Buggy python submission (verdict: Runtime Error):
a, b, c, d, e, f = map(int, input().split())
x = set()
y = set()
for A in range(0, f + 1, 100 * a):
    for B in range(0, f + 1, 100 * b):
        if A + B:
            x.add(A + B)
for C in range(0, f + 1, c):
    for D in range(0, f + 1, d):
        y.add(C + D)
ans = 0
for X in x:
    for Y in y:
        if X + Y <= f and 100 * Y / X <= e:
                if ans < Y / (X + Y):
                    ans = Y / (X + Y)
                    w = X
                    s = Y
print(w + s, s)

Fixed submission:
a, b, c, d, e, f = map(int, input().split())
x = set()
y = set()
for A in range(0, f + 1, 100 * a):
    for B in range(0, f + 1, 100 * b):
        if A + B > f:
            break
        if A + B:
            x.add(A + B)
for C in range(0, f + 1, c):
    for D in range(0, f + 1, d):
        if C + D > f:
            break
        y.add(C + D)
ans = 0
w = 100 * a
s = 0
for X in x:
    for Y in y:
        if X + Y <= f and 100 * Y / X <= e:
                if ans < Y / (X + Y):
                    ans = Y / (X + Y)
                    w = X
                    s = Y
print(w + s, s)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, [input() for _ in range(n)]))
ans = 0
for i in set(a):
  if a.count(i) % 2 == 1:
    ans += 1
print(ans)


Fixed submission:
n = int(input())
s = set()
for i in range(n):
  a = input()
  if a in s:
    s.remove(a)
  else:
    s.add(a)
print(len(s))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, [input() for _ in range(n)]))
ans = 0
for i in set(a):
  if a.count(i) % 2 == 1:
    ans += 1
print(ans)

Fixed submission:
n = int(input())
s = set()
for i in range(n):
  a = input()
  if a in s:
    s.remove(a)
  else:
    s.add(a)
print(len(s))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = []
for i in range(n):
  a = input()
  if a in l:
    l.remove(a)
  else:
    l.append(a)
print(len(l))

Fixed submission:
n = int(input())
s = set()
for i in range(n):
  a = input()
  if a in s:
    s.remove(a)
  else:
    s.add(a)
print(len(s))
Buggy python submission (verdict: Time Limit Exceeded):
import collections

n = int(input())
a = list(map(int, input().split()))
ac = collections.Counter(a)
ans = 0
for i in range(min(a) - 1, max(a) + 2):
    total = 0
    for j in ac:
        if j == i or j + 1 == i or j - 1 == i:
            total += ac[j]
    ans = max(ans, total)
print(ans)

Fixed submission:
import collections

n = int(input())
a = list(map(int, input().split()))
ac = collections.Counter(a)
ans = 0
for i in range(min(a) - 1, max(a) + 2):
    ans = max(ans, ac[i - 1] + ac[i] + ac[i + 1])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(min(a) - 1, max(a) + 2):
    total = 0
    for j in a:
        if j == i or j + 1 == i or j - 1 == i:
            total += 1
    ans = max(ans, total)
print(ans)

Fixed submission:
import collections

n = int(input())
a = list(map(int, input().split()))
ac = collections.Counter(a)
ans = 0
for i in range(min(a) - 1, max(a) + 2):
    ans = max(ans, ac[i - 1] + ac[i] + ac[i + 1])
print(ans)
Buggy python submission (verdict: Runtime Error):
s = set(input())
a = "abcdefghijklmnopqrstuvwxyz"
for i in s:
  a = a.replace(i, "")
print(a[0])

Fixed submission:
s = set(input())
a = "abcdefghijklmnopqrstuvwxyz"
for i in s:
  a = a.replace(i, "")
if a:
  print(a[0])
else:
  print("None")
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ac = sorted(list(set(a)))
ans = []
for i in ac[::-1]:
    c = a.count(i)
    if len(ans) == 2:
        break
    if not ans and c >= 4:
        print(i ** 2)
        exit()
    if c >= 2:
        ans.append(i)
if len(ans) == 2:
    print(ans[0] * ans[1])
else:
    print(0)

Fixed submission:
import collections

n = int(input())
a = list(map(int, input().split()))
ac = collections.Counter(a)
quad = [i for i in ac if ac[i] >= 4]
if quad:
    ans = max(quad) ** 2
else:
    ans = 0
twice = sorted([i for i in ac if ac[i] >= 2])
if len(twice) >= 2:
    ans = max(ans, twice[-1] * twice[-2])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq

def dijkstra(graph, node, start):
    dist = [float("inf") for _ in range(node)]

    dist[start] = 0
    q = []
    heapq.heappush(q, (0, start))

    while q:
        cost, cur_node = heapq.heappop(q)

        if dist[cur_node] < cost:
            continue

        for nex_cost, nex_node in graph[cur_node]:
            dist_cand = dist[cur_node] + nex_cost
            if dist_cand < dist[nex_node]:
                dist[nex_node] = dist_cand
                heapq.heappush(q, (dist[nex_node], nex_node))

    return dist


n = int(input())
adj = [[] for _ in range(n)]
for i in range(n - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    adj[a].append((c, b))
    adj[b].append((c, a))
q, k = map(int, input().split())
k -= 1
shortest = []
for i in range(n):
    shortest.append(dijkstra(adj, n, i))
for i in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    print(shortest[x][k] + shortest[y][k])

Fixed submission:
import heapq

def dijkstra(graph, node, start):
    dist = [float("inf") for _ in range(node)]

    dist[start] = 0
    q = []
    heapq.heappush(q, (0, start))

    while q:
        cost, cur_node = heapq.heappop(q)

        if dist[cur_node] < cost:
            continue

        for nex_cost, nex_node in graph[cur_node]:
            dist_cand = dist[cur_node] + nex_cost
            if dist_cand < dist[nex_node]:
                dist[nex_node] = dist_cand
                heapq.heappush(q, (dist[nex_node], nex_node))

    return dist


n = int(input())
adj = [[] for _ in range(n)]
for i in range(n - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    adj[a].append((c, b))
    adj[b].append((c, a))
q, k = map(int, input().split())
shortest = dijkstra(adj, n, k - 1)
for i in range(q):
    x, y = map(int, input().split())
    print(shortest[x - 1] + shortest[y - 1])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = input().split()
b = []
if n % 2:
    for i in range(n):
        if i % 2 == 0:
            b = [a[i]] + b
        else:
            b.append(a[i])
    print(" ".join(b))
else:
    for i in range(n):
        if i % 2:
            b = [a[i]] + b
        else:
            b.append(a[i])
    print(" ".join(b))

Fixed submission:
n = int(input())
a = input().split()
b = []
for i in range(n - 1, -1, -2):
  b.extend([a[i]])
for j in range(n % 2, n, 2):
  b.extend([a[j]])
print(" ".join(b))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = input().split()
b = []
for i in range(n):
  if i % 2 == 0:
    b = b + a[i:i+1]
  else:
    b = a[i:i+1] + b
if n % 2 == 1:
  b = b[::-1]
print(" ".join(b))


Fixed submission:
n = int(input())
a = input().split()
b = []
for i in range(n - 1, -1, -2):
  b.extend([a[i]])
for j in range(n % 2, n, 2):
  b.extend([a[j]])
print(" ".join(b))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = input().split()
b = []
for i in range(n):
  b.append(a[i])
  b = b[::-1]
print(" ".join(b))

Fixed submission:
n = int(input())
a = input().split()
b = []
for i in range(n - 1, -1, -2):
  b.extend([a[i]])
for j in range(n % 2, n, 2):
  b.extend([a[j]])
print(" ".join(b))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
  b.append(a[i])
  b = b[::-1]
print(" ".join(map(str, b)))

Fixed submission:
n = int(input())
a = input().split()
b = []
for i in range(n - 1, -1, -2):
  b.extend([a[i]])
for j in range(n % 2, n, 2):
  b.extend([a[j]])
print(" ".join(b))
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10 ** 9 + 7

def kaijo(n):
    k = 1
    for i in range(1, n + 1):
        k *= i % MOD
    return k

n, m = map(int, input().split())
if n == m:
    print((kaijo(n) ** 2 * 2) % MOD)
elif abs(n - m) > 1:
    print(0)
else:
    if n < m:
        print((kaijo(n) ** 2 * m) % MOD)
    else:
        print((kaijo(m) ** 2 * n) % MOD)

Fixed submission:
MOD = 10 ** 9 + 7

def kaijo(n):
    k = 1
    for i in range(1, n + 1):
        k = k * i % MOD
    return k

n, m = map(int, input().split())
if n == m:
    print((kaijo(n) ** 2 * 2) % MOD)
elif abs(n - m) > 1:
    print(0)
else:
    if n < m:
        print((kaijo(n) ** 2 * m) % MOD)
    else:
        print((kaijo(m) ** 2 * n) % MOD)
Buggy python submission (verdict: Runtime Error):
import collections

s = input()
c = collections.Counter(s).most_common()
m = c[0][1]
maxi = 0
if m == 1:
    print(len(s) // 2)
else:
    for k in c:
        if k[1] == m and k[0] != s[0]:
            index = [i for i, x in enumerate(s) if x == k[0]]
            sa = [index[i + 1] - index[i] for i in range(m - 1)]
            if maxi < min(sa):
                use = k
                maxi = min(sa)
    ans = 0
    while len(set(s)) != 1:
        a = ""
        for i in range(len(s) - 1):
            if s[i] == use[0] or s[i + 1] == use[0]:
                a += use[0]
            else:
                a += s[i]
        ans += 1
        s = a
    print(ans)

Fixed submission:
s_ = input()
answ = float("inf")
for i in s_:
    s = s_
    ans = 0
    while len(set(s)) != 1:
        a = ""
        for j in range(len(s) - 1):
            if s[j] == i or s[j + 1] == i:
                a += i
            else:
                a += s[j]
        ans += 1
        s = a
    answ = min(answ, ans)
print(answ)
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().splt())
if a + b >= 10:
  print("error")
else:
  print(a + b)

Fixed submission:
a, b = map(int, input().split())
if a + b >= 10:
  print("error")
else:
  print(a + b)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n = int(input())
s = list(map(int, [input() for _ in range(n)]))
ans = []
for i in range(1, n + 1):
  for j in itertools.combinations(s, i):
    sum_ = sum(j)
    if sum_ % 10 == 0:
      sum_ = 0
    ans.append(sum_)
print(max(ans))

Fixed submission:
n = int(input())
s = list(map(int, [input() for _ in range(n)]))
if sum(s) % 10 == 0:
  while sum(s) % 10 == 0 and len(s) > 0:
    s = sorted(s)
    i = 0
    while s[i] % 10 == 0 and i + 1 < len(s):
      i += 1
    del s[i]
  print(sum(s))
else:
  print(sum(s))
Buggy python submission (verdict: Runtime Error):
import itertools

n, a, b = map(int, input().split())
if n < 2 and a != b:
    print(0)
elif n == 1:
    print(1)
else:
    c = set()
    for i in itertools.combinations_with_replacement(range(a, b + 1), n - 2):
        c.add(sum(i))
    print(len(c))

Fixed submission:
n, a, b = map(int, input().split())
print(max(b - a + 1 + (b - a) * (n - 3), 0))
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
ans = [a[i][0] for i in range(n) for _ in range(a[i][1])]
print(sorted(ans)[k-1])

Fixed submission:
n, k = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
ans = 0
i = 0
a = sorted(a)
while True:
  ans += a[i][1]
  if ans >= k:
    break
  i += 1
print(a[i][0])
Buggy python submission (verdict: Runtime Error):
import collections

n = int(input())
s = [input() for i in range(n)]
c = []
for i in s:
    c.append(collections.Counter(i))
a = c[0] & c[1]
for i in range(2, n):
    a &= c[i]
ans = []
a = a.most_common()
for i in a:
    for j in range(i[1]):
        ans.append(i[0])
print("".join(sorted(ans)))

Fixed submission:
import collections

n = int(input())
s = [input() for i in range(n)]
if n == 1:
    print("".join(sorted(s[0])))
else:
    c = []
    for i in s:
        c.append(collections.Counter(i))
    a = c[0] & c[1]
    for i in range(2, n):
        a &= c[i]
    ans = []
    a = a.most_common()
    for i in a:
        for j in range(i[1]):
            ans.append(i[0])
    print("".join(sorted(ans)))
Buggy python submission (verdict: Runtime Error):
x = int(input())
a = 0
for i in range(1, 10 ** 9):
    if a >= x:
        print(i - 1)
        break
    a += i

Fixed submission:
a, b = input().split()
if a == "H":
  if b == "H":
    print("H")
  else:
    print("D")
else:
  if b == "H":
    print("D")
  else:
    print("H")
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 1
mod = 10 ** 9 + 7
for i in range(1, n + 1):
    ans *= i % mod
print(ans % mod)

Fixed submission:
n = int(input())
ans = 1
for i in range(1, n + 1):
  ans = i * ans % 1000000007
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 1
for i in range(1, n + 1):
  ans *= i
print(ans % 1000000007)

Fixed submission:
n = int(input())
ans = 1
for i in range(1, n + 1):
  ans = i * ans % 1000000007
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = []
b = []
for i in range(n):
    A, B = map(int, input().split())
    a.append(A)
    b.append(B)
ans = 0
a = a[::-1]
b = b[::-1]
for i in range(n):
    if a[i] % b[i] == 0:
        continue
    p = (a[i] // b[i] + 1) * b[i] - a[i]
    ans += p
    for j in range(i, n):
        a[j] += p
print(ans)

Fixed submission:
n = int(input())
a = []
b = []
for i in range(n):
    A, B = map(int, input().split())
    a.append(A)
    b.append(B)
ans = 0
a = a[::-1]
b = b[::-1]
for i in range(n):
    a[i] += ans
    if a[i] % b[i] == 0:
        continue
    ans += (a[i] // b[i] + 1) * b[i] - a[i]
print(ans)