Problem p02603 (python)

Fixed submission:
N = int(input())
As = list(map(int, input().split()))

rlt = 1000
for i in range(N-1):
  if As[i+1] > As[i]:
    rlt = (As[i+1]-As[i])*(rlt//As[i]) + rlt
    
print(rlt)

Buggy submission (verdict: Runtime Error):
N = int(input())
As = list(map(int, input().split()))

hs = []
ls = []

i = 0
if As[1] > As[0]:
  ls.append((As[0],0))
  f = 1
elif As[1] < As[0]:
  hs.append((As[0],0))
  f = 0
else:
  fl = 1
  for j in range(2,N):
    if As[j] > As[0]:
      ls.append((As[0],0))
      f = 1
      i = j
      fl = 0
      break
    elif As[j] < As[0]:
      hs.append((As[0],0))
      f = 0
      i = j
      fl = 0
      break
  if fl == 1:
    print(1000)
    exit()
  
while i < N-1:
  if f == 1 and As[i] > As[i+1]:
    hs.append((As[i],i))
    f = 0
  elif f == 0 and As[i] < As[i+1]:
    ls.append((As[i],i))
    f = 1
  i += 1
    
if As[N-2] < As[N-1]:
  hs.append((As[N-1],N-1))
elif As[N-2] > As[N-1]:
  ls.append((As[N-1],N-1))

if len(ls) == 1 and len(hs) == 1 and ls[0][1] > hs[0][1]:
  print(1000)
  exit()
if ls[0][1] > hs[0][1]:
  del hs[0]
if hs[-1][1] < ls[-1][1]:
  del ls[-1]

Buggy submission (verdict: Runtime Error):
N = int(input())
As = list(map(int, input().split()))

hs = []
ls = []

i = 0
if As[1] > As[0]:
  ls.append((As[0],0))
  f = 1
elif As[1] < As[0]:
  hs.append((As[0],0))
  f = 0
else:
  fl = 1
  for j in range(2,N):
    if As[j] > As[0]:
      ls.append((As[0],0))
      f = 1
      i = j
      fl = 0
      break
    elif As[j] < As[0]:
      hs.append((As[0],0))
      f = 0
      i = j
      fl = 0
      break
  if fl == 1:
    print(1000)
    exit()
  
while i < N-1:
  if f == 1 and As[i] > As[i+1]:
    hs.append((As[i],i))
    f = 0
  elif f == 0 and As[i] < As[i+1]:
    ls.append((As[i],i))
    f = 1
  i += 1
    
if As[N-2] < As[N-1]:
  hs.append((As[N-1],N-1))
elif As[N-2] > As[N-1]:
  ls.append((As[N-1],N-1))

if len(ls) == 1 and len(hs) == 1 and ls[0][1] > hs[0][1]:
  print(1000)
  exit()
if ls[0][1] > hs[0][1]:
  del hs[0]
if hs[-1][1] < ls[-1][1]:
  del ls[-1]
  
memo = [[[0]*2 for _ in range(len(ls)+1)]  for _ in range(2)]
memo[0][0][0] = 1000
memo[0][0][1] = 0
memo[1][0][0] = 1000
memo[1][0][1] = 0

for i in range(1,len(ls)+1):
  a = memo[1][i-1][0] // ls[i-1][0]
  if a > memo[0][i-1][1]:
    memo[0][i][0] = memo[1][i-1][0]-a*ls[i-1][0]
    memo[0][i][1] = a
  else:
    memo[0][i][0] = memo[1][i-1][0]
    memo[0][i][1] = 0
  b = memo[0][i][1]*hs[i-1][0] + memo[0][i][0]
  if b > memo[1][i-1][0]:
    memo[1][i][0] = b
    memo[1][i][1] = 0
  else:
    memo[1][i][0] = memo[0][i][0]
    memo[1][i][1] = memo[0][i][1]
    
print(memo[1][-1][0])

Buggy submission (verdict: Runtime Error):
N = int(input())
As = list(map(int, input().split()))

hs = []
ls = []

i = 0
if As[1] > As[0]:
  ls.append((As[0],0))
  f = 1
elif As[1] < As[0]:
  hs.append((As[0],0))
  f = 0
else:
  for j in range(2,N):
    if As[j] > As[0]:
      ls.append((As[0],0))
      f = 1
      i = j
      break
    elif As[j] < As[0]:
      hs.append((As[0],0))
      f = 0
      i = j
      break
  
while i < N-1:
  if f == 1 and As[i] > As[i+1]:
    hs.append((As[i],i))
    f = 0
  elif f == 0 and As[i] < As[i+1]:
    ls.append((As[i],i))
    f = 1
  i += 1
    
if As[N-2] < As[N-1]:
  hs.append((As[N-1],N-1))
elif As[N-2] > As[N-1]:
  ls.append((As[N-1],N-1))

if len(ls) == 1 and len(hs) == 1 and ls[0][1] > hs[0][1]:
  print(1000)
  exit()
if ls[0][1] > hs[0][1]:
  del hs[0]
if hs[-1][1] < ls[-1][1]:
  del ls[-1]
  
memo = [[[0]*2 for _ in range(len(ls)+1)]  for _ in range(2)]
memo[0][0][0] = 1000
memo[0][0][1] = 0
memo[1][0][0] = 1000
memo[1][0][1] = 0

for i in range(1,len(ls)+1):
  a = memo[1][i-1][0] // ls[i-1][0]
  if a > memo[0][i-1][1]:
    memo[0][i][0] = memo[1][i-1][0]-a*ls[i-1][0]
    memo[0][i][1] = a
  else:
    memo[0][i][0] = memo[1][i-1][0]
    memo[0][i][1] = 0
  b = memo[0][i][1]*hs[i-1][0] + memo[0][i][0]
  if b > memo[1][i-1][0]:
    memo[1][i][0] = b
    memo[1][i][1] = 0
  else:
    memo[1][i][0] = memo[0][i][0]
    memo[1][i][1] = memo[0][i][1]
    
print(memo[1][-1][0])