Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import itertools

n, m = map(int, input().split(' '))
x = map(int, input().split(' '))
if n >= m:
    print(0)
else:
    x = sorted(x)
    seq = [i for i in range(1,m)]
    locations = itertools.combinations(seq,n-1)
    best_sc = None
    for location in locations:
        sc = 0
        for i in range(0,n):
            g = None
            if i == 0:
                g = x[:location[i]]
            elif i == n-1:
                g = x[location[i-1]:]
            else:
                g = x[location[i-1]:location[i]]
            sc += g[-1] - g[0]
        if (best_sc is None) or (best_sc > sc):
            best_sc = sc
    print(best_sc)


Fixed submission:
n, m = map(int, input().split(' '))
x = list(map(int, input().split(' ')))
if n >= m:
    print(0)
else:
    x.sort()
    d = [x[i+1] - x[i] for i in range(m-1)]
    d.sort()
    #print(d)
    print(sum(d[:m-n]))

Buggy python submission (verdict: Runtime Error):
import itertools

n, m = map(int, input().split(' '))
x = map(int, input().split(' '))
if n >= m:
    print(0)
else:
    x = sorted(x)
    seq = [i for i in range(1,m)]
    locations = list(itertools.combinations(seq,n-1))
    best_sc = None
    for location in locations:
        sc = 0
        for i in range(0,n):
            g = None
            if i == 0:
                g = x[:location[i]]
            elif i == n-1:
                g = x[location[i-1]:]
            else:
                g = x[location[i-1]:location[i]]
            sc += g[-1] - g[0]
        if (best_sc is None) or (best_sc > sc):
            best_sc = sc
    print(best_sc)


Fixed submission:
n, m = map(int, input().split(' '))
x = list(map(int, input().split(' ')))
if n >= m:
    print(0)
else:
    x.sort()
    d = [x[i+1] - x[i] for i in range(m-1)]
    d.sort()
    #print(d)
    print(sum(d[:m-n]))

Buggy python submission (verdict: Runtime Error):
import itertools

n, m = map(int, input().split(' '))
x = map(int, input().split(' '))
if n >= m:
    print(0)
else:
    x = sorted(x)
    seq = [i for i in range(1,m)]
    locations = list(itertools.combinations(seq,n-1))
    best_sc = None
    for location in locations:
        sc = 0
        for i in range(0,n):
            g = None
            if i == 0:
                g = x[:location[i]]
            elif i == n-1:
                g = x[location[i-1]:]
            else:
                g = x[location[i-1]:location[i]]
            sc += max(g) - min(g)
        if (best_sc is None) or (best_sc > sc):
            best_sc = sc
    print(best_sc)


Fixed submission:
n, m = map(int, input().split(' '))
x = list(map(int, input().split(' ')))
if n >= m:
    print(0)
else:
    x.sort()
    d = [x[i+1] - x[i] for i in range(m-1)]
    d.sort()
    #print(d)
    print(sum(d[:m-n]))
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

bits_k = len(bin(k)) - 2
forma = f'0{bits_k}b'
data = [0 for i in range(bits_k)]

for an in a:
    anb = format(an, forma)
    #print(anb)
    for i in range(bits_k):
        if anb[-i-1] == '0':
            data[i] += 1
        else:
            data[i] -= 1

#print(data)
ans = 0
for i in range(bits_k - 1, -1, -1):
    if data[i] > 0:
        ans |= (1 << i)
        if ans > k:
            ans &= ~(1 << i)

#print(ans)
b = sum([ans ^ an for an in a])
print(b)


Fixed submission:
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

bits_k = len(bin(k)) - 2
forma = '0%db' % bits_k
data = [0 for i in range(bits_k)]

for an in a:
    anb = format(an, forma)
    #print(anb)
    for i in range(bits_k):
        if anb[-i-1] == '0':
            data[i] += 1
        else:
            data[i] -= 1

#print(data)
ans = 0
for i in range(bits_k - 1, -1, -1):
    if data[i] > 0:
        ans |= (1 << i)
        if ans > k:
            ans &= ~(1 << i)

#print(ans)
b = sum([ans ^ an for an in a])
print(b)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
b = [sum([x ^ an for an in a]) for x in range(0, k+1)]
print(max(b))


Fixed submission:
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

bits_k = len(bin(k)) - 2
forma = '0%db' % bits_k
data = [0 for i in range(bits_k)]

for an in a:
    anb = format(an, forma)
    #print(anb)
    for i in range(bits_k):
        if anb[-i-1] == '0':
            data[i] += 1
        else:
            data[i] -= 1

#print(data)
ans = 0
for i in range(bits_k - 1, -1, -1):
    if data[i] > 0:
        ans |= (1 << i)
        if ans > k:
            ans &= ~(1 << i)

#print(ans)
b = sum([ans ^ an for an in a])
print(b)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = [0]
h[1:] = list(map(int, input().split()))
inf = 1 << 60
dp = [inf] * (N + 1)
dp[0] = 0
dp[1] = 0
for i in range(2, N + 1):
    for k in range(1, min(i, K + 1)):
        dp[i] = min(dp[i], dp[i - k] + abs(h[i - k] - h[i]))

print(dp[N])


Fixed submission:
N, K = map(int, input().split())
h = [0]
h[1:] = list(map(int, input().split()))
inf = 1 << 60
dp = [inf] * (N + 1)
dp[0] = 0
dp[1] = 0
for i in range(2, N + 1):
    for k in range(1, min(i, K + 1)):
        dp[i] = min(dp[i], dp[i - k] + abs(h[i - k] - h[i]))

print(dp[N])
Buggy python submission (verdict: Time Limit Exceeded):
import re

N = int(input())

ans = 0
for i in range(1, N+1):
    n = i
    flags = 0
    while True:
        flags |= 1 << (n % 10)
        n //= 10
        if n == 0:
            break
    if flags == 0xA8:
        ans += 1

print(ans)


Fixed submission:
def keta(n, elements):
    results = elements
    for i in range(n - 1):
        results = [r * 10 + e for e in elements for r in results]
    return results

N = int(input())

num753 = set()
for i in range(3,10):
    n1 = keta(i, set([3, 5, 7]))
    n2 = keta(i, set([3, 5]))
    n3 = keta(i, set([5, 7]))
    n4 = keta(i, set([3, 7]))
    num753 |= set(n1) - set(n2) - set(n3) - set(n4)
num753 = sorted(num753)

ans = 0
for i in num753:
    if i <= N:
        ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math

def prime_division(n):
    ans = []
    m = int(n ** 0.5)
    i = 2
    while i <= m:
        if n % i == 0:
            cnt = 0
            while n % i == 0:
                n //= i
                cnt += 1
            ans.append((i, cnt))
            m = int(n ** 0.5)
        i += 1
    if n != 1:
        ans.append((n, 1))
    return ans

#  aabaaba  count+(N-1) C (N-1)

N,M=map(int,input().split())
pms=prime_division(M)
ans = 1
for pm in pms:
    #print(pm)
    ans *= math.factorial(pm[1]+(N-1)) // (math.factorial(pm[1]) * math.factorial(N-1))

print(ans % (10 ** 9 + 7))


Fixed submission:
import math

def prime_division(n):
    ans = []
    m = int(n ** 0.5)
    i = 2
    while i <= m:
        if n % i == 0:
            cnt = 0
            while n % i == 0:
                n //= i
                cnt += 1
            ans.append((i, cnt))
            m = int(n ** 0.5)
        i += 1
    if n != 1:
        ans.append((n, 1))
    return ans

#  aabaaba  count+(N-1) C (N-1)

N,M=map(int,input().split())
pms=prime_division(M)
ans = 1
for pm in pms:
    #print(pm)
    ans *= math.factorial(pm[1]+(N-1)) // (math.factorial(pm[1]) * math.factorial(N-1))

print(ans % (10 ** 9 + 7))

Buggy python submission (verdict: Time Limit Exceeded):
import math

def prime_division(n):
    ans = []
    m = int(n ** 0.5)
    i = 2
    while i <= m:
        if n % i == 0:
            cnt = 0
            while n % i == 0:
                n //= i
                cnt += 1
            ans.append((i, cnt))
            m = int(n ** 0.5)
        i += 1
    if n != 1:
        ans.append((n, 1))
    return ans

#  aabaaba  count+(N-1) C (N-1)

N,M=map(int,input().split())
pms=prime_division(M)
ans = 1
for pm in pms:
    #print(pm)
    ans *= math.factorial(pm[1]+(N-1)) // (math.factorial(pm[1]) * math.factorial(N-1))

print(ans % (10 ** 9 + 7))


Fixed submission:
import math

def prime_division(n):
    ans = []
    m = int(n ** 0.5)
    i = 2
    while i <= m:
        if n % i == 0:
            cnt = 0
            while n % i == 0:
                n //= i
                cnt += 1
            ans.append((i, cnt))
            m = int(n ** 0.5)
        i += 1
    if n != 1:
        ans.append((n, 1))
    return ans

#  aabaaba  count+(N-1) C (N-1)

N,M=map(int,input().split())
pms=prime_division(M)
ans = 1
for pm in pms:
    #print(pm)
    ans *= math.factorial(pm[1]+(N-1)) // (math.factorial(pm[1]) * math.factorial(N-1))

print(ans % (10 ** 9 + 7))
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 0
    for l in range(0, N):
        s = 0
        for r in range(l, N):
            s += A[r]
            if s % M == 0:
                ans += 1
    return ans

print(solve())


Fixed submission:
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 0
    c = {}
    s = [0 for i in range(N+1)]
    s[0] = 0
    for i in range(0, N):
        s[i + 1] = (s[i] + A[i]) % M
    #print(s)
    for i in range(0, N+1):
        if s[i] not in c.keys():
            c[s[i]] = 0
        ans += c[s[i]]
        c[s[i]] += 1
    return ans

print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 0
    for l in range(0, N):
        s = 0
        for r in range(l, N):
            s += A[r]
            if s % M == 0:
                ans += 1
    return ans

print(solve())


Fixed submission:
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 0
    c = {}
    s = [0 for i in range(N+1)]
    s[0] = 0
    for i in range(0, N):
        s[i + 1] = (s[i] + A[i]) % M
    #print(s)
    for i in range(0, N+1):
        if s[i] not in c.keys():
            c[s[i]] = 0
        ans += c[s[i]]
        c[s[i]] += 1
    return ans

print(solve())
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    s=input()
    K=int(input())
    words = set()
    ls = len(s)
    for i in range(ls):
        for j in range(i+1,ls+1):
            words.add(s[i:j])
    words = sorted(list(words))
    return words[K-1]

print(solve())


Fixed submission:
def solve():
    s=input()
    K=int(input())
    words = set()
    ls = len(s)
    for i in range(ls):
        for j in range(i+1,min(i+6,ls+1)):
            words.add(s[i:j])
    words = sorted(list(words))
    return words[K-1]

print(solve())
Buggy python submission (verdict: Runtime Error):
def solve():
    n = list(input())
    n = [ord(c) - ord('0') for c in n]
    sign = ['-', '+']

    for i in range(2):
        for j in range(2):
            for k in range(2):
                t = n[0]
                t += n[1] * (i * 2 - 1)
                t += n[2] * (j * 2 - 1)
                t += n[3] * (k * 2 - 1)
                if t == 7:
                    print(f'{n[0]}{sign[i]}{n[1]}{sign[j]}{n[2]}{sign[k]}{n[3]}=7')
                    return


if __name__ == "__main__":
    solve()


Fixed submission:
def solve():
    S=list(input())
    for i in range(8):
        ans=int(S[0])
        for j in range(3):
            if (i & (1 << j)) != 0:
                ans+=int(S[j+1])
            else:
                ans-=int(S[j+1])
        if ans==7:
            ans=S[0]
            for j in range(3):
                if (i & (1 << j)) != 0:
                    ans+='+'
                else:
                    ans+='-'
                ans+=S[j+1]
            return ans+'=7'

print(solve())
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort()
B.sort()
C.sort()

a2=[]
for i1 in range(N):
    a2.append(bisect.bisect_left(C,B[i1]+1))
ans=0
for i0 in range(N):
    a1 = bisect.bisect_left(B,A[i0]+1)
    for i1 in range(a1,N):
        ans += N - a2[i1]
print(ans)


Fixed submission:
import bisect

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort()
B.sort()
C.sort()

a2=[]
for i1 in range(N):
    a2.append(bisect.bisect_left(C,B[i1]+1))

a2c=[0]*(N+1)
for i1 in range(N-1,-1,-1):
    a2c[i1] = a2c[i1+1] + N - a2[i1]

ans=0
for i0 in range(N):
    a1 = bisect.bisect_left(B,A[i0]+1)
    ans += a2c[a1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort()
B.sort()
C.sort()

ans=0
for i0 in range(N):
    a1 = bisect.bisect_left(B,A[i0]+1)
    for i1 in range(a1,N):
        a2 = bisect.bisect_left(C,B[i1]+1)
        ans += N - a2
print(ans)


Fixed submission:
import bisect

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort()
B.sort()
C.sort()

a2=[]
for i1 in range(N):
    a2.append(bisect.bisect_left(C,B[i1]+1))

a2c=[0]*(N+1)
for i1 in range(N-1,-1,-1):
    a2c[i1] = a2c[i1+1] + N - a2[i1]

ans=0
for i0 in range(N):
    a1 = bisect.bisect_left(B,A[i0]+1)
    ans += a2c[a1]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n, m = map(int, input().split())
uf = UnionFind(n)
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    uf.unite(a, b)
D = uf.all_group_members()
ans = 0
for i in D.values():
    ans = max(ans, len(i))
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, m = map(int, input().split())
G = [set() for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].add(b)
    G[b].add(a)
ans = 0
seen = [0]*n
def dfs(v):
    res = 0
    stack = [v]
    seen[v] = True
    while stack:
        res += 1
        v = stack.pop()
        for nv in G[v]:
            if seen[nv]:
                continue
            seen[nv] = True
            stack.append(nv)
    return res
for i in range(n):
    if not seen[i]:
        ans = max(ans, dfs(i))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h, w = map(int, input().split())
cy, cx = map(lambda x:int(x)-1, input().split())
gy, gx = map(lambda x:int(x)-1, input().split())
S = [input() for _ in range(h)]
seen = [[-1]*w for _ in range(h)]
def f(sy, sx, cnt):
    que = []
    que.append((sy, sx))
    seen[sy][sx] = cnt
    while que:
        y, x = que.pop()
        for dy, dx in ((-1,0),(0,-1),(1,0),(0,1)):
            ny, nx = y+dy, x+dx
            if 0 <= ny < h and 0 <= nx < w:
                if seen[ny][nx] != -1:
                    continue
                if S[ny][nx] == "#":
                    continue
                seen[ny][nx] = cnt
                que.append((ny, nx))
cnt = 0
for i in range(h):
  for j in range(w):
    if S[i][j] == "." and seen[i][j] == -1:
      f(i, j, cnt)
      cnt += 1
G = [set() for _ in range(cnt+1)]
for i in range(h):
  for j in range(w):
    for dy in range(-2, 3):
      for dx in range(-2, 3):
        y, x = i+dy, j+dx
        if 0 <= y < h and 0 <= x < w:
          if S[i][j] == "#":
            continue
          if S[y][x] == "#":
            continue
          G[seen[i][j]].add(seen[y][x])
          G[seen[y][x]].add(seen[i][j])
s = seen[cy][cx]
g = seen[gy][gx]
dist = [-1]*(cnt+1)
dist[s] = 0
que = deque()
que.append((s, 0))
while que:
  v, d = que.popleft()
  dist[v] = d
  if g == v:
    print(dist[g])
    exit()
  for i in G[v]:
    if dist[i] != -1:
      continue
    que.append((i, d+1))
print(-1)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
cy, cx = map(lambda x:int(x)-1, input().split())
gy, gx = map(lambda x:int(x)-1, input().split())
S = [input() for _ in range(h)]
seen = [[-1]*w for _ in range(h)]
def f(sy, sx, cnt):
    que = []
    que.append((sy, sx))
    seen[sy][sx] = cnt
    while que:
        y, x = que.pop()
        for dy, dx in ((-1,0),(0,-1),(1,0),(0,1)):
            ny, nx = y+dy, x+dx
            if 0 <= ny < h and 0 <= nx < w:
                if seen[ny][nx] != -1:
                    continue
                if S[ny][nx] == "#":
                    continue
                seen[ny][nx] = cnt
                que.append((ny, nx))
cnt = 0
for i in range(h):
  for j in range(w):
    if S[i][j] == "." and seen[i][j] == -1:
      f(i, j, cnt)
      cnt += 1
G = [set() for _ in range(cnt+1)]
for i in range(h):
  for j in range(w):
    for dy in range(-2, 3):
      for dx in range(-2, 3):
        y, x = i+dy, j+dx
        if 0 <= y < h and 0 <= x < w:
          if S[i][j] == "#":
            continue
          if S[y][x] == "#":
            continue
          G[seen[i][j]].add(seen[y][x])
          G[seen[y][x]].add(seen[i][j])
s = seen[cy][cx]
g = seen[gy][gx]
dist = [-1]*(cnt+1)
dist[s] = 0
que = deque()
que.append(s)
while que:
  v = que.popleft()
  if g == v:
    print(dist[g])
    exit()
  for i in G[v]:
    if dist[i] != -1:
      continue
    dist[i] = dist[v]+1
    que.append(i)
print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
K = []
T = []
F = []
for _ in range(n):
  s = input()
  if "." not in s:
    s += "."
  a, b = s.split(".")
  k = len(b)
  c = int(a+b)
  t = 0
  while c%2 == 0:
    c //= 2
    t += 1
  T.append(t-k)
  f = 0
  while c%5 == 0:
    c //= 5
    f += 1
  F.append(f-k)
ans = 0
for i in range(n-1):
  t1, f1 = T[i], F[i]
  for j in range(i+1, n):
    t2, f2 = T[j], F[j]
    if 0 <= min(t1+t2, f1+f2):
      ans += 1
print(ans)

Fixed submission:
class BIT():
 
    __slots__ = ["n", "data"]
 
    def __init__(self, li):
        self.n = len(li) + 1
        self.data = [0] + li
        for i in range(1, self.n):
            if i + (i & -i) < self.n:
                self.data[i + (i & -i)] += self.data[i]
 
    def add(self, i, a):
        i += 1
        while i < self.n:
            self.data[i] += a
            i += i & -i
        
    def acc(self, i):
        res = 0
        while i > 0:
            res += self.data[i]
            i -= i & -i
        return res
    
    def fold(self, l, r):
        res = 0
        while l < r:
            res += self.data[r]
            r -= r & -r
        while r < l:
            res -= self.data[l]
            l -= l & -l
        return res

import bisect
n = int(input())
TF = []
for _ in range(n):
  s = input()
  if "." not in s:
    s += "."
  a, b = s.split(".")
  k = len(b)
  c = int(a+b)
  t = 0
  while c%2 == 0:
    c //= 2
    t += 1
  f = 0
  while c%5 == 0:
    c //= 5
    f += 1
  TF.append((t-k, f-k))
TF.sort()
ans = 0
U = 50
bit = BIT([0]*100)
temp = n
for t, f in TF:
  r = bisect.bisect_left(TF, (-t, -U))
  while temp > r:
    temp -= 1
    nt, nf = TF[temp]
    bit.add(nf+U, 1)
  ans += n-temp - bit.acc(-f+U)
for t, f in TF:
  if min(t, f) >= 0:
    ans -= 1
ans //= 2
print(ans)
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))
if k == 0:
  print(max(A))
  exit()
ng = -1
ok = max(A) + 1
def test(x):
    cnt = 0
    for a in A:
        if a > x:
            cnt += -(-a//x) - 1
    return cnt <= k

while ok - ng > 1:
    mid = (ok+ng) // 2
    if test(mid):
        ok = mid
    else:
        ng = mid
        
print(ok)

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
ng = -1
ok = max(A) + 1
def test(x):
    if x == 0:
      return False
    cnt = 0
    for a in A:
        if a > x:
            cnt += -(-a//x) - 1
    return cnt <= k

while ok - ng > 1:
    mid = (ok+ng) // 2
    if test(mid):
        ok = mid
    else:
        ng = mid
        
print(ok)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))
ng = -1
ok = max(A) + 1
def test(x):
    cnt = 0
    for a in A:
        if a > x:
            cnt += -(-a//x) - 1
    return cnt <= k

while ok - ng > 1:
    mid = (ok+ng) // 2
    if test(mid):
        ok = mid
    else:
        ng = mid
print(ok)


Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
ng = -1
ok = max(A) + 1
def test(x):
    if x == 0:
      return False
    cnt = 0
    for a in A:
        if a > x:
            cnt += -(-a//x) - 1
    return cnt <= k

while ok - ng > 1:
    mid = (ok+ng) // 2
    if test(mid):
        ok = mid
    else:
        ng = mid
        
print(ok)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))
M = 1000
for i in range(n-1):
    dp = list(range(M+1))
    a, b = A[i], A[i+1]
    for j in range(M+1):
        if j >= a:
            dp[j] = max(dp[j-a]+b, dp[j])
    M = dp[M]
ans = M
print(ans)

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
x, y, z = 1000, 0, 1000
for a in A:
  s1, t1 = divmod(x, a)
  s2, t2 = divmod(z, a)
  s2 += y
  if s1 > s2:
    y, z = s1, t1
  elif s2 > s1:
    y, z = s2, t2
  else:
    y, z = s1, max(t1, t2)
  x = max(x, y*a+z)
ans = x
print(ans)