Problem:
To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.  He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:    * A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.  In the i-th day, M-kun can make the following trade **any number of times** (possibly zero), **within the amount of money and stocks that he has at the time**.    * Buy stock: Pay A_i yen and receive one stock.   * Sell stock: Sell one stock for A_i yen.  What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Incorrect submission (verdict: Wrong Answer):

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline


def array(size, init=0):
    return [[init for j in range(size[1])] for i in range(size[0])]


def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())


directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


def permutate(x, A):
    n = len(x)
    res = [-1]*n
    for i in range(n):

        res[i] = x[A[i]]
    return res


def generate_bin(digit):
    for b in range(2**digit):
        b = bin(b)
        tmp = b[2:].zfill(digit)
        yield list(map(int, tmp))
        
        # yield b


def search(x, count):
    #print("top", x, count)

    for d in directions:
        nx = d+x
        # print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)

    return count


N = II()
A = acinput()

A.insert(0, A[0]+1)
A.append(A[-1]-1)

tmp = []
for i in range(len(A)):
    if A[i] != A[i-1]:
        tmp.append(A[i])

A = tmp

x = 1000
k = 0

for i in range(len(A)-1):
    print(i, x, k)
    if A[i-1] > A[i] and A[i] < A[i+1]:
        k += x//A[i]
        x = x % A[i]
    elif A[i-1] < A[i] and A[i] > A[i+1]:
        #print(i, x, k)
        x += A[i]*k
        k = 0


print(x)

# print(A)


Fixed, accepted submission:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline


def array(size, init=0):
    return [[init for j in range(size[1])] for i in range(size[0])]


def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())


directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


def permutate(x, A):
    n = len(x)
    res = [-1]*n
    for i in range(n):

        res[i] = x[A[i]]
    return res


def generate_bin(digit):
    for b in range(2**digit):
        b = bin(b)
        tmp = b[2:].zfill(digit)
        yield list(map(int, tmp))
        
        # yield b


def search(x, count):
    #print("top", x, count)

    for d in directions:
        nx = d+x
        # print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)

    return count


N = II()
A = acinput()

A.insert(0, A[0]+1)
A.append(A[-1]-1)

tmp = []
for i in range(len(A)):
    if A[i] != A[i-1]:
        tmp.append(A[i])

A = tmp

x = 1000
k = 0

for i in range(len(A)-1):
    #print(i, x, k)
    if A[i-1] > A[i] and A[i] < A[i+1]:
        k += x//A[i]
        x = x % A[i]
    elif A[i-1] < A[i] and A[i] > A[i+1]:
        #print(i, x, k)
        x += A[i]*k
        k = 0


print(x)

# print(A)