Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
def resolve():
    t = input()
    l = len(t)
    que = []
    ans = [""]
    ma = [0]

    def dfs(s, idx):
        if idx == l:
            a = s.count("D") + s.count("PD")
            if ma[0] < a:
                ans[0] = s
                ma[0] = a
            return

        if t[idx] == "?":
            dfs(s + "D", idx + 1)
        else:
            dfs(s + t[idx], idx + 1)

    dfs("", 0)
    print(ans[0])

resolve()

Fixed submission:
def resolve():
    t = input()
    s = ""
    for c in t:
        if c == "?":
            c = "D"

        s += c
    print(s)

resolve()

Buggy python submission (verdict: Runtime Error):
def resolve():
    t = input()
    l = len(t)
    que = []
    ans = [""]
    ma = [0]

    def dfs(s, idx):
        if idx == l:
            a = s.count("D") + s.count("PD")
            if ma[0] < a:
                ans[0] = s
                ma[0] = a
            return

        if t[idx] == "?":
            dfs(s + "P", idx + 1)
            dfs(s + "D", idx + 1)
        else:
            dfs(s + t[idx], idx + 1)

    dfs("", 0)
    print(ans[0])

resolve()

Fixed submission:
def resolve():
    t = input()
    s = ""
    for c in t:
        if c == "?":
            c = "D"

        s += c
    print(s)

resolve()

Buggy python submission (verdict: Runtime Error):
def resolve():
    t = input()
    l = len(t)
    que = []

    def dfs(s, idx):
        if idx == l:
            que.append(s)
            return

        if t[idx] == "?":
            dfs(s + "P", idx + 1)
            dfs(s + "D", idx + 1)
        else:
            dfs(s + t[idx], idx + 1)

    dfs("", 0)
    ans = ""
    ma = 0
    for q in que:
        a = q.count("D") + q.count("PD")
        if ma < a:
            ma = a
            ans = q

    print(ans)

resolve()

Fixed submission:
def resolve():
    t = input()
    s = ""
    for c in t:
        if c == "?":
            c = "D"

        s += c
    print(s)

resolve()
Buggy python submission (verdict: Time Limit Exceeded):
def resolve():
    from collections import deque

    n, m = map(int, input().split())
    d = {}
    par = [[] for i in range(n)]
    que = deque()
    for i in range(m):
        a, b = map(int, input().split())
        par[a - 1].append(b)
        par[b - 1].append(a)

    que.append(1)
    while(len(que) > 0):
        label = que.popleft()
        for i, p in enumerate(par):
            if label in p and i + 1 not in d.keys():
                que.append(i + 1)
                d[i + 1] = label

    if len(d) < n:
        print("No")
        return

    print("Yes")
    for i in range(2, n + 1):
        print(d[i])

resolve()

Fixed submission:
def resolve():
    from collections import deque

    n, m = map(int, input().split())
    d = {}
    par = [[] for i in range(n)]
    que = deque()
    for i in range(m):
        a, b = map(int, input().split())
        par[a - 1].append(b)
        par[b - 1].append(a)

    que.append(1)
    while(len(que) > 0):
        label = que.popleft()
        for i in par[label - 1]:
            if i not in d.keys():
                que.append(i)
                d[i] = label

    if len(d) < n:
        print("No")
        return

    print("Yes")
    for i in range(2, n + 1):
        print(d[i])

resolve()
Buggy python submission (verdict: Runtime Error):
def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    path = {}
    c = 1
    i = 1
    while c < k:
        if not i in path.keys():
            path[i] = c
            i = A[i - 1]
            c += 1
        elif i in path.keys():
            start = path[i]
            roop = c - path[i]
            break
    roop += start - 1
    idx = 1
    for r in range(roop):
        idx = A[idx - 1]
    print(idx)

resolve()

Fixed submission:
def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    s = []
    ord = [-1 for i in range(n + 1)]
    rep = 1
    pos = 1
    while ord[pos] == -1:
        ord[pos] = len(s)
        s.append(pos)
        pos = A[pos - 1]
        rep += 1

    rep = len(s) - ord[pos]
    # l: ループが始まる点
    l = ord[pos]
    if k < l:
        print(s[k])
    else:
        # ループが始まるまでずらす
        k -= l
        k %= rep
        print(s[l + k])
resolve()

Buggy python submission (verdict: Runtime Error):
def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    path = {}
    c = 1
    i = 1
    while c < k:
        if not i in path.keys():
            path[i] = c
            i = A[i - 1]
            c += 1
        elif i in path.keys():
            start = path[i]
            roop = c - path[i]
            break
    roop = k % roop
    idx = 1
    for r in range(roop):
        idx = A[idx - 1]
    print(idx)

resolve()

Fixed submission:
def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    s = []
    ord = [-1 for i in range(n + 1)]
    rep = 1
    pos = 1
    while ord[pos] == -1:
        ord[pos] = len(s)
        s.append(pos)
        pos = A[pos - 1]
        rep += 1

    rep = len(s) - ord[pos]
    # l: ループが始まる点
    l = ord[pos]
    if k < l:
        print(s[k])
    else:
        # ループが始まるまでずらす
        k -= l
        k %= rep
        print(s[l + k])
resolve()
Buggy python submission (verdict: Runtime Error):
x = int(input())
for i in range(-118, 120):
  for j in range(-119, 119):
    if i**5 - j**5 = x:
      print(i, j)
      exit(0)


Fixed submission:
s = input()
if s == "ABC":
    print("ARC")
else:
    print("ABC")
Buggy python submission (verdict: Runtime Error):
x = int(input())
for i in range(-118, 120):
  for j in range(-119, 119):
    if i**5 - j** = x:
      print(i, j)
      exit(0)

Fixed submission:
x = int(input())
for i in range(-118, 120):
  for j in range(-119, 119):
    if i**5 - j**5 == x:
      print(i, j)
      exit(0)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n = int(input())
K = list(range(1, n + 1))
A = list(map(int, input().split()))
K_A = list(zip(K, A))
C = list(itertools.combinations(K_A, 2))
ans = 0
for c in C:
    if abs(c[0][0] - c[1][0]) == c[0][1] + c[1][1]:
        ans += 1
print(ans)


Fixed submission:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
B = [i + A[i - 1] for i in range(1, n + 1)]
C = Counter(B)
ans = 0
for i in range(1, n + 1):
    x = i - A[i - 1]
    if x in C.keys():
        ans += C[x]
print(ans)
Buggy python submission (verdict: Runtime Error):
import itertools

n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations(I, n)))
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d
print(max(ans))

Fixed submission:
import itertools

n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations_with_replacement(I, n)))
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d
print(max(ans))

Buggy python submission (verdict: Runtime Error):
n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations(I, n)))
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d
print(max(ans))

Fixed submission:
import itertools

n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations_with_replacement(I, n)))
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d
print(max(ans))

Buggy python submission (verdict: Runtime Error):
import itertools
n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations(I, n)))
l = len(A)
for i in range(l):
    if A[0][0] == 1:
        A.append(A[0])
    A.pop(0)
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d

print(max(ans))

Fixed submission:
import itertools

n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations_with_replacement(I, n)))
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d
print(max(ans))
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
if n % 2 != 0:
    for i in range(1, m + 1):
        print(i, n + 1 - i)
else:
    for i, j in range(1, m + 1), range(1, m + 1):
        if i == n / 2:
            i += 1
        print(i, n - j)
        i += 1

Fixed submission:
n,m=map(int,input().split())
if n%2==1:
    [print(i+1,n-i) for i in range(m)]
else:
    [print(i+1,n-i) if i<m/2 else print(i+1,n-i-1) for i in range(m)]
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
l = len(s)
res = []

def dfs(s, i, j):
    if j - i <= 3:
        return 0
    if int(s[i:j]) % 2019 == 0 and (i, j) not in res:
        res.append((i, j))
        return 1
    return dfs(s, i + 1, j) + dfs(s, i, j - 1)

print(dfs(s, 0, len(s)))

Fixed submission:
s = input()
l = len(s)
dp = [0 for i in range(2019)]
dp[0] = 1
r = 0
for i in range(1, l + 1):
    r += int(s[-i]) * pow(10, i - 1, 2019)
    r %= 2019
    dp[r] += 1
    
print(sum([(i * (i - 1)) // 2 for i in dp]))

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
l = len(s)
A = [i for i in range(2019, int(s) + 2019, 2019)]
ans = 0
for a in A:
    di = len(str(a))
    for i in range(l - di + 1):
        # print(s[i:i + di], a)
        if s[i:i + di] == str(a):
            ans +=1
print(ans)

Fixed submission:
s = input()
l = len(s)
dp = [0 for i in range(2019)]
dp[0] = 1
r = 0
for i in range(1, l + 1):
    r += int(s[-i]) * pow(10, i - 1, 2019)
    r %= 2019
    dp[r] += 1
    
print(sum([(i * (i - 1)) // 2 for i in dp]))

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
ans = 0
for i in range(len(s) - 3):
    for j in range(i + 4, len(s) + 1):
        n = int(s[i:j])
        if n % 2019 == 0:
            ans += 1
print(ans)

Fixed submission:
s = input()
l = len(s)
dp = [0 for i in range(2019)]
dp[0] = 1
r = 0
for i in range(1, l + 1):
    r += int(s[-i]) * pow(10, i - 1, 2019)
    r %= 2019
    dp[r] += 1
    
print(sum([(i * (i - 1)) // 2 for i in dp]))
Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
q = []
for i in range(1, 10):
    q.append(i)
for i in range(k):
    x = q.pop(0)
    if x % 10 != 0:
        q.append(10 * x + x % 10 - 1)
    q.append(10 * x + x % 10)
    if x % 10 != 9:
        q.append(10 * x + x % 10 + 1)

print(x)


Fixed submission:
from collections import deque


k = int(input())
q = deque(range(1, 10))

for i in range(k):
    x = q.popleft()
    if x % 10 != 0:
        q.append(10 * x + x % 10 - 1)
    q.append(10 * x + x % 10)
    if x % 10 != 9:
        q.append(10 * x + x % 10 + 1)

print(x)

Buggy python submission (verdict: Time Limit Exceeded):
def check(a):
    if len(str(a)) == 1:
        return True
    a = list(str(a))
    b = [abs(int(a[i]) - int(a[i + 1])) for i in range(len(a) - 1)]
    if max(b) > 1:
        return False
    else:
        return True

k = int(input())
a = 0
while True:
    a += 1
    if check(a):
        k -= 1
    if k == 0:
        print(a)
        exit()

Fixed submission:
from collections import deque


k = int(input())
q = deque(range(1, 10))

for i in range(k):
    x = q.popleft()
    if x % 10 != 0:
        q.append(10 * x + x % 10 - 1)
    q.append(10 * x + x % 10)
    if x % 10 != 9:
        q.append(10 * x + x % 10 + 1)

print(x)
Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import shortest_path
import numpy as np
n, x, y = map(int, input().split())
ans = [0] * (n - 1)
table = []
for i in range(n):
    l = [0] * n
    if i + 1 < n:
        l[i + 1] = 1
    if i - 1 > -1:
        l[i - 1] = 1
    if i == x - 1:
        l[y - 1] = 1
    if i == y - 1:
        l[x - 1] = 1
    table.append(l)
a = shortest_path(np.array(table))
a = a.flatten().tolist()
for i in range(1, n):
    print(a.count(i) // 2)


Fixed submission:
n, x, y = map(int, input().split())
ans = [0] * n
for i in range(1, n + 1):
  for j in range(i + 1, n + 1):
    d = min(j - i, abs(i - x) + abs(j - y) + 1)
    ans[d] += 1
    
for i in range(1, n):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import shortest_path
import numpy as np
n, x, y = map(int, input().split())
ans = [0] * (n - 1)
table = []
for i in range(n):
    l = [0] * n
    if i + 1 < n:
        l[i + 1] = 1
    if i - 1 > -1:
        l[i - 1] = 1
    if i == x - 1:
        l[y - 1] = 1
    if i == y - 1:
        l[x - 1] = 1
    table.append(l)
a = shortest_path(np.array(table))
a = a.flatten().tolist()
for i in range(n - 1):
    ans[i] += a.count(i + 1)
ans = list(map(lambda x: x // 2, ans))
ans[-1] = 0
for i in range(n - 1):
    print(ans[i])


Fixed submission:
n, x, y = map(int, input().split())
ans = [0] * n
for i in range(1, n + 1):
  for j in range(i + 1, n + 1):
    d = min(j - i, abs(i - x) + abs(j - y) + 1)
    ans[d] += 1
    
for i in range(1, n):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import shortest_path
import numpy as np


n, x, y = map(int, input().split())
ans = [0] * (n - 1)

table = []
for i in range(n):
  l = [0] * n
  if i + 1 < n:
    l[i + 1] = 1
  if i - 1 > -1:
    l[i - 1] = 1
  if i == x - 1:
    l[y - 1] = 1
  if i == y - 1:
    l[x - 1] = 1
  table.append(l)

a = shortest_path(np.array(table))
for i in a:
  for j in i:
    ans[int(j) - 1] += 1

ans = list(map(lambda x: x // 2, ans))
ans[-1] = 0
for i in range(n - 1):
  print(ans[i])


Fixed submission:
n, x, y = map(int, input().split())
ans = [0] * n
for i in range(1, n + 1):
  for j in range(i + 1, n + 1):
    d = min(j - i, abs(i - x) + abs(j - y) + 1)
    ans[d] += 1
    
for i in range(1, n):
    print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
import itertools


A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = {}
for _ in range(M):
    x, y, z = map(int, input().split())
    key = str(x) + "&" + str(y)
    if key in m.keys():
        if m[key] < z:
            m[key] = z
    else:
        m[key] = z
A_B = list(itertools.product(range(1, A + 1), range(1, B + 1)))
A_B_sum = list(map(lambda x: x[0] + x[1], (itertools.product(a, b))))
for i in range(len(A_B)):
    # print(str(A_B[i][0]) + "&" + str(A_B[i][1]))
    if str(A_B[i][0]) + "&" + str(A_B[i][1]) in m.keys():
        # print(str(A_B[i][0]) + "&" + str(A_B[i][1]))
        A_B_sum[i] -= m[str(A_B[i][0]) + "&" + str(A_B[i][1])]
print(min(A_B_sum))

Fixed submission:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
min_a = min(a)
min_b = min(b)
ans = min_a + min_b
for _ in range(M):
    i, j, c = map(int, input().split())
    ans = min(ans, a[i - 1] + b[j - 1] - c)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, a, b = map(int, input().split())
l = []
while(True):
    for i in range(a):
        l.append(1)
    for i in range(b):
        l.append(0)
    if len(l) >= n:
        break

print(sum(l[:n]))


Fixed submission:
n, a, b = map(int, input().split())
k = n // (a + b)
m = n % (a + b)
print(k * a + min(a, m))
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
S = []
C = []
for _ in range(N):
  s, c = map(int, input().split())
  S.append(s - 1)
  C.append(c)

num = ['*'] * N

for s, c in zip(S, C):
  if num[s] == '*':
    num[s] = str(c)
  elif num[s] != str(c):
    print(-1)
    exit()

if num[0] == '*':
  num[0] = '1'
elif num[0] == '0':
  print(-1)
  exit()
print(''.join(num).replace('*', '0'))

Fixed submission:
N, M = map(int, input().split())
S = []
C = []
for _ in range(M):
  s, c = map(int, input().split())
  S.append(s - 1)
  C.append(c)

num = ['*'] * N

for s, c in zip(S, C):
  if s == 0 and c == 0 and N != 1:
    print(-1)
    exit()
  if num[s] == '*':
    num[s] = str(c)
  elif num[s] != str(c):
    print(-1)
    exit()

if num[0] == '*' and N != 1:
  num[0] = '1'
print(''.join(num).replace('*', '0'))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
S = []
C = []
for _ in range(N):
  s, c = map(int, input().split())
  S.append(s - 1)
  C.append(c)

num = ['*'] * N

for s, c in zip(S, C):
  if num[s] == '*':
    num[s] = str(c)
  elif num[s] != str(c):
    print(-1)
    exit()

print(''.join(num).replace('*', '0'))

Fixed submission:
N, M = map(int, input().split())
S = []
C = []
for _ in range(M):
  s, c = map(int, input().split())
  S.append(s - 1)
  C.append(c)

num = ['*'] * N

for s, c in zip(S, C):
  if s == 0 and c == 0 and N != 1:
    print(-1)
    exit()
  if num[s] == '*':
    num[s] = str(c)
  elif num[s] != str(c):
    print(-1)
    exit()

if num[0] == '*' and N != 1:
  num[0] = '1'
print(''.join(num).replace('*', '0'))
Buggy python submission (verdict: Runtime Error):
H, N = map(int, input().split())
A = sorted(list(map(int, input().split())))

if H < A[-1] + A[-2]:
    print('Yes')
else:
    print('No')



Fixed submission:
H, N = map(int, input().split())
A = sum(list(map(int, input().split())))

if H <= A:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
if N <= K:
    print('0')
    return
H = sorted(list(map(int, input().split())))
if K == 0:
    print(sum(H))
else:
    print(sum(H[:K + 1]))


Fixed submission:
N, K = map(int, input().split())
H = sorted(list(map(int, input().split())))
if N <= K:
    print('0')
elif K == 0:
    print(sum(H))
else:
    print(sum(H[:N - K]))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, m = map(int,input().split())

edge = [[] for _ in range(n)]
for i in range(m):
    a, b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    
visited = [0]*n
visited[0] = 1
q = deque([0])

ans = 0

while sum(visited) < n:
    while q:
        now = q.popleft()
        for to in edge[now]:
            if visited[to] == 0:
                q.append(to)
                visited[to] = 1
    #print(visited)
    if sum(visited) < n:
        ans += 1
        for i,v in enumerate(visited):
            if v == 0:
                q.append(i)
                visited[i] = 1
                break

print(ans)

Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
 
    def find(self, x):#要素xが属するグループの根を返す
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
 
    def union(self, x, y):#要素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):#要素xが属するグループのサイズ（要素数）を返す
        return -self.parents[self.find(x)]
 
    def same(self, x, y):#要素x, yが同じグループに属するかどうかを返す
        return self.find(x) == self.find(y)
 
    def members(self, x):#要素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):#print()での表示用 ルート要素: [そのグループに含まれる要素のリスト]を文字列で返す
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n, m = map(int,input().split())
uf = UnionFind(n)

edge = []
for i in range(m):
    a,b = map(int,input().split())
    edge.append((a-1,b-1))
    
for a,b in edge:
    uf.union(a, b)
    
print(uf.group_count()-1)
Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
ans = 1
num = 0
if not k%2:
    print(-1)
else:
    while True:
        num = (num*10+7)%k
        if num == 0:
            print(ans)
            break
        ans += 1

Fixed submission:
k = int(input())
num = 0
if not k%2:
    print(-1)
else:
    for i in range(k):
        num = (num*10+7)%k
        if num == 0:
            print(i+1)
            break
    else:
        print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
ans = 1
num = 0
if not k%2:
    print(-1)
else:
    while True:
        num = num*10%k + 7%k
        if num%k == 0:
            print(ans)
            break
        ans += 1

Fixed submission:
k = int(input())
num = 0
if not k%2:
    print(-1)
else:
    for i in range(k):
        num = (num*10+7)%k
        if num == 0:
            print(i+1)
            break
    else:
        print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
ans = 1
num = 0
if not k%2:
    print(-1)
else:
    while True:
        num = num*10%k + 7%k
        if num%k == 0:
            print(ans)
            break
        ans += 1

Fixed submission:
k = int(input())
num = 0
if not k%2:
    print(-1)
else:
    for i in range(k):
        num = (num*10+7)%k
        if num == 0:
            print(i+1)
            break
    else:
        print(-1)
Buggy python submission (verdict: Runtime Error):
n = int(input())
A = list(map(int,input().split()))

groups = []
prev = A[0]
for i in range(1,n):
    if A[i] != prev:
        groups.append(prev)
        prev = A[i]
groups.append(prev)

money = 1000
stock = 0
if groups[0] < groups[1]:
    buy = money//groups[0]
    stock += buy
    money -= groups[0]*buy
    
for i in range(1,len(groups)-1):
    if groups[i-1] < groups[i] and groups[i] > groups[i+1]:# 周辺の最高値
        money += stock*groups[i]
        stock = 0
    elif groups[i-1] > groups[i] and groups[i] < groups[i+1]:# 周辺の最安値
        buy = money//groups[i]
        stock += buy
        money -= groups[i]*buy

money += stock*groups[-1]

print(money)

Fixed submission:
n = int(input())
A = list(map(int,input().split()))

groups = []
prev = A[0]
for i in range(1,n):
    if A[i] != prev:
        groups.append(prev)
        prev = A[i]
groups.append(prev)

if len(groups) == 1:
    print(1000)
    exit()

money = 1000
stock = 0
if groups[0] < groups[1]:
    buy = money//groups[0]
    stock += buy
    money -= groups[0]*buy
    
for i in range(1,len(groups)-1):
    if groups[i-1] < groups[i] and groups[i] > groups[i+1]:# 周辺の最高値
        money += stock*groups[i]
        stock = 0
    elif groups[i-1] > groups[i] and groups[i] < groups[i+1]:# 周辺の最安値
        buy = money//groups[i]
        stock += buy
        money -= groups[i]*buy

money += stock*groups[-1]

print(money)