Problem:
 Score: 400 points    Problem Statement  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?      Constraints   2 \leq N \leq 80  100 \leq A_i \leq 200  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  \cdots  A_N       Output  Print the maximum possible amount of money that M-kun can have in the end, as an integer.        Sample Input 1 7
100 130 130 130 115 115 150      Sample Output 1 1685  In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:   Initially, he has 1000 yen and no stocks.  Day 1 : Buy 10 stocks for 1000 yen. Now he has 0 yen.  Day 2 : Sell 7 stocks for 910 yen. Now he has 910 yen.  Day 3 : Sell 3 stocks for 390 yen. Now he has 1300 yen.  Day 4 : Do nothing.  Day 5 : Buy 1 stock for 115 yen. Now he has 1185 yen.  Day 6 : Buy 10 stocks for 1150 yen. Now he has 35 yen.  Day 7 : Sell 11 stocks for 1650 yen. Now he has 1685 yen.   There is no way to have 1686 yen or more in the end, so the answer is 1685 .       Sample Input 2 6
200 180 160 140 120 100      Sample Output 2 1000  In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.       Sample Input 3 2
157 193      Sample Output 3 1216  In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2 , after which we will have 1216 yen.

Solution (Python):
FAST_IO = 1
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()

    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
rrmm = lambda n: [rrm() for _ in xrange(n)]
N = rri()
arr = rrm()
def solve(N, prices):
    ans = 1000
    for i in range(1, len(prices)):
        if prices[i]>prices[i-1]:
            ans += (prices[i]-prices[i-1])*(ans//prices[i-1])
    return ans
print(solve(N, arr))

Solution (Python):
def main():
    import sys
    N=int(sys.stdin.readline())
    A=list(map(int,sys.stdin.readline().split()))
    B=[0]*N

    money=1000
    kabu=0
    for i in range(N-1):
        if A[i]<A[i+1]:
            kabu+=money//A[i]
            money%=A[i]

        elif A[i]>A[i+1]:
            money+=kabu*A[i]
            kabu=0

    money+=kabu*A[-1]
    print(money)
    
main()

Solution (Python):

n=int(input())
A=list(map(int,input().split()))
x=1000
k=0
flag=False
def buy(x,k,cost):
    k+=x//cost
    x-=(x//cost)*cost
    return x,k

def sell(x,k,cost):
    x+=k*cost
    k=0
    return x,k

for i in range(n-1):
    today=A[i]
    tomorrow=A[i+1]
    if flag:
        if today<=tomorrow:
            pass
        else:
            x,k=sell(x,k,today)
            flag=False
    else:
        if today<=tomorrow:
            x,k=buy(x,k,today)
            flag=True
        else:
            pass
        
print(x+k*A[n-1])

Solution (Python):
def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]
    X=list(map(int,input().split()))
    A=list(map(int,input().split()))
    N=X[0]
    syojikin=1000
    kabu=0
    A.append(-1)
    for i in range(N):
        if A[i]<A[i+1]:
            kabu+=syojikin//A[i]
            syojikin=syojikin%A[i]
        elif A[i]>A[i+1]:
            syojikin+=kabu*A[i]
            kabu=0
    print(syojikin)






if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
c = 1000
s = 0

for i in range(N - 1):
    if A[i] < A[i + 1]:
        s += c // A[i]
        c %= A[i]
    else:
        c += s * A[i]
        s = 0

if A[N - 2] < A[N - 1]:
    c += s * A[N - 1]

print(c)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

sign = -1
temp = []
temp2 = []
for i in range(N-1):
    if sign == -1:
        if A[i] >= A[i+1]:
            continue
        else:
            temp.append(A[i])
            sign = 1
    else:
        if A[i] <= A[i+1]:
            continue
        else:
            temp2.append(A[i])
            sign = -1

if sign == 1:
    if A[i] <= A[i+1]:
        temp2.append(A[i+1])
    else:
        temp2.append(A[i])


S = 1000
for a, b in zip(temp, temp2):
    cnt = S//a
    S += cnt*(b-a)

print(S)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

L = [(1000, 0)] 

for i in range(N):
  L2 = []
  for x in L:
    
    L2.append(x)
    
    if x[0] >= A[i]:
      L2.append( (x[0] % A[i], x[1] + x[0] // A[i] ) )
    
    if x[1] > 0:
      L2.append( (x[0] + x[1] * A[i], 0) )
  
  
  
  L = []
  for x in range(len(L2)):
    for y in range(len(L2)):
      if y == x:
        continue
      ok = True
      if (L2[x][0] < L2[y][0] and L2[x][1] < L2[y][1] or 
          L2[x][0] == L2[y][0] and L2[x][1] < L2[y][1] or 
          L2[x][0] < L2[y][0] and L2[x][1] == L2[y][1] or 
          L2[x][0] == L2[y][0] and L2[x][1] == L2[y][1] and x > y):
        ok = False
        break
    if ok:
      L.append(L2[x])
  
  
  
ans = 0
for i in L:
  ans = max(ans, i[0])
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
default = True
for i in range(N-1):
    if A[i] < A[i+1]:
        default = False
        break
if default:
    print(1000)
    exit()
updown = [(0, A[0])]
for i in range(N-1):
    if A[i] < A[i+1]:
        updown.append((1, A[i+1]))
    elif A[i] > A[i+1]:
        updown.append((-1, A[i+1]))
stock = 0
money = 1000
for i in range(len(updown)-1):
    if updown[i][0] == 0 and updown[i+1][0] == 1:
        buy = money // updown[i][1]
        stock += buy
        money -= buy * updown[i][1]
    if updown[i][0] == 0 and updown[i+1][0] == -1:
        pass
    if updown[i][0] == -1 and updown[i+1][0] == 1:
        buy = money // updown[i][1]
        stock += buy
        money -= buy * updown[i][1]
    if updown[i][0] == -1 and updown[i+1][0] == -1:
        pass
    if updown[i][0] == 1 and updown[i+1][0] == 1:
        pass
    if updown[i][0] == 1 and updown[i+1][0] == -1:
        money += stock * updown[i][1]
        stock = 0
if stock:
    money += stock * updown[-1][1]
    stock = 0
print(money)

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue



sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))




def main():
    n = ni()
    a = na()
    
    a.append(-INF)

    kabu = 0
    kane = 1000

    turn = []
    for i in range(n):
        if a[i + 1] > a[i]:
            turn.append(1)
        elif a[i + 1] < a[i]:
            turn.append(-1)
        else:
            turn.append(0)

    katteruflg = False
    for i in range(len(turn)):
        if turn[i] == 1:
            if katteruflg:
                continue
            kabu = kane // a[i]
            kane -= a[i] * kabu
            katteruflg = True
        elif turn[i] == -1:
            kane += a[i] * kabu
            kabu = 0
            katteruflg = False

    print(kane)


if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
A_list = list(map(int, input().split()))

money = 1000
stock = 0

for i in range(N-1):
    tmp_stock = 0
    if(A_list[i] < A_list[i+1]):
        
        tmp_stock = money // A_list[i]
        money = money - A_list[i] * tmp_stock
        stock += tmp_stock
    elif(A_list[i] > A_list[i+1]):
        
        tmp_stock = stock
        money = money + A_list[i] * tmp_stock
        stock -= tmp_stock
    else:
        pass
        


tmp_stock = stock
money = money + A_list[N-1] * tmp_stock
stock -= tmp_stock

print(money)

Solution (Python):

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))
        
        


def search(x, count):
    

    for d in directions:
        nx = d+x
        
        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):
    
    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]:
        
        x += A[i]*k
        k = 0


print(x)

Solution (Python):
def read():
    N = int(input())
    A = list(map(int, input().split()))
    return N, A

def solve(N, A):
    x = 1000
    y = 0
    op = "buy"
    for i in range(N):
        if i == N-1:
            x += y * A[i]
        elif op == "buy":
            if A[i] >= A[i+1]:
                continue
            else:
                c = x // A[i]
                x -= c * A[i]
                y += c
                op = "sell"
        elif op == "sell":
            if A[i] <= A[i+1]:
                continue
            else:
                c = y
                x += c * A[i]
                y -= c
                op = "buy"
    return max(x, 1000)


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
money = 1000
stock = 0

for i in range(n-1):
  if li[i] < li[i+1]:
    stock = money // li[i]
    money += stock * (li[i+1]-li[i])
  
print(money)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')




import numpy as np    


def solve():
    N = II()
    A = LI()

    dp = [[0, 0] for _ in range(N+1)]
    
    dp[0][0] = (1000, 0)
    dp[0][1] = (0, 0)
    

    def buy(lst, a):
        mon, stock = lst
        n = mon // a
        mon = mon - n * a
        stock += n
        return mon, stock

    def sell(lst, a):
        mon, stock = lst
        mon += stock * a
        stock = 0
        return mon, stock

    for i in range(1, N+1):
        a = A[i-1]

        
        bb = buy(dp[i-1][0], a)
        sb = buy(dp[i-1][1], a)

        if bb[1] == sb[1]:
            if bb[0] > sb[0]:
                dp[i][0] = bb
            else:
                dp[i][0] = sb
        elif bb[1] > sb[1]:
            dp[i][0] = bb
        else:
            dp[i][0] = sb

        
        bs = sell(dp[i-1][0], a)
        ss = sell(dp[i-1][1], a)
        if bs[0] > ss[0]:
            dp[i][1] = bs
        else:
            dp[i][1] = ss

        

    print(max(dp[N][0][0], dp[N][1][0]))

if __name__ == '__main__':
    solve()

Solution (Python):
import sys, math
from collections import defaultdict
from itertools import product
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    A = list(mi())

    stock = 0
    money = 1000

    u = []
    d = []

    i = 0
    while i < N-1:
        if A[i] <= A[i+1]:
            tmp = i
            while i+1 < N and A[i] <= A[i+1]:
                i += 1
            u.append((tmp, i))
        else:
            i += 1

    for v in u:
        stock += money//A[v[0]]
        money -= A[v[0]] * (money//A[v[0]])

        money += A[v[1]] * stock
        stock = 0

    print(money)

if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
A_list = list(map(int, input().split()))

low_num = 200
high_num = 1000
have_flag = False
money = 1000
stock = 0
last_money = 1000
for i in range(N):
    if have_flag:
        if high_num <= A_list[i]:
            high_num = A_list[i]
        else:
            have_flag = False
            money = money + stock * high_num
            stock = 0
            low_num = A_list[i]
            last_money = money
    else:
        if low_num >= A_list[i]:
            low_num = A_list[i]
        else:
            have_flag = True
            stock = money // low_num
            money = money % low_num
            high_num = A_list[i]

if have_flag:
    if last_money < money + stock * A_list[N - 1]:
        last_money = money + stock * A_list[N - 1]

print(last_money)

Solution (Python):
n = int(input())
money = 1000
maisuu = 0
value = list(map(int, input().split()))
for i in range(0, n-1):
    if value[i] < value[i+1]:
        maisuu += money//value[i]
        money %= value[i]
    else:
        money += maisuu * value[i]
        maisuu = 0
money += maisuu * value[n-1]
print(money)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
flag=1
k=0
c=1000
for i in range(n-1):
  if flag and a[i]<a[i+1]:
    k=c//a[i]
    c-=a[i]*k
    flag=0
  elif not flag and a[i]>a[i+1]:
    c+=k*a[i]
    k=0
    flag=1
c+=k*a[-1]
print(c)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

ans = 1000
ans_list1 = []
ans_list2 = []
l = [0,0]
d = [0,0]

for i in range(1,n):
    if a[i-1] <= a[i]:
        l[1] = i
        ans_list2.append(d)
        d = [i,i]
    else:
        d[1] = i
        ans_list1.append(l)
        l = [i,i]
ans_list1.append(l)
ans_list2.append(d)

for i in range(len(ans_list1)):
    k = ans // a[ans_list1[i][0]]
    ans += (a[ans_list1[i][1]] - a[ans_list1[i][0]]) * k

print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())
m=1000
for i in range(1,n):
    if a[i]>a[i-1]:
        m=m//a[i-1]*a[i]+m%a[i-1]
print(m)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

money = 1000
stock = 0
for day in range(n-1):
    if a[day+1] > a[day]:
        buyStock = money // a[day]
        if buyStock > 0:
            stock += buyStock
            money -= buyStock * a[day]
    elif a[day+1] < a[day]:
        money += stock * a[day]
        stock = 0
money += stock * a[-1]
print(money)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

score=1000

for i in range(N-1):
    if A[i] < A[i+1]:
        num=score//A[i]
        score+=num*(A[i+1]-A[i])

print(score)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))

p = 1000
kabu = 0

uri = False

for i in range(n):
    if i == n-1 or (uri and a[i+1] < a[i]):
        
        p += kabu * a[i]
        kabu = 0
        uri = False
    elif (not uri) and a[i+1] > a[i]:
        
        kabu = p // a[i]
        p -= a[i] * kabu
        uri = True

    

print(p)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
kabu = 0
money = 1000
for i in range(n-1):
    if a[i] < a[i+1]:
        kabu += money//a[i]
        money -= a[i] * (money//a[i])
    else:
        money += kabu * a[i]
        kabu = 0
money += kabu * a[n-1]
print(money)

Solution (Python):

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

money = 1000
kab = 0

for i in range(len(As)-1):
    if As[i+1] >= As[i]:
        kounyu = money // As[i]
        kab += kounyu
        money -= kounyu * As[i]
    elif As[i+1] < As[i]:
        money += kab * As[i]
        kab = 0
    

money += kab * As[-1]


print(money)

Solution (Python):
def main():
    n = int(input())
    lst_a = [int(v) for v in input().split(' ')]
    s = 0
    c = int(1000)
    max_c = int(1000)

    prev_tlt = '-'
    for i in range(len(lst_a) - 1):
        if lst_a[i] < lst_a[i + 1]:
            tlt = '+'
        elif lst_a[i] > lst_a[i + 1]:
            tlt = '-'
        else:
            continue

        if tlt != prev_tlt:
            if tlt == '-':
                c += s * lst_a[i]
                s = 0
            else:
                s = int(c // lst_a[i])
                c -= lst_a[i] * s
            max_c = max(c, max_c)
        prev_tlt = tlt
    c += s * lst_a[-1]
    max_c = max(c, max_c)
    print(max_c)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
asset = 1000
for i in range(n-1):
    if a[i] < a[i+1]:
        store = asset // a[i]
        amari = asset % a[i]
        asset = store * a[i+1] + amari

print(asset)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

P = []
m = A[0]
for i in range(1, N):
    if A[i] < A[i - 1]:
        if m != A[i - 1]:
            P.append((m, A[i - 1]))
        m = A[i]
if m < A[N - 1]:
    P.append((m, A[N - 1]))

ans = 1000
for p in P:
    ans += (p[1] - p[0]) * (ans // p[0])
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split())) + [0]
money = 1000
stock = 0
prev = 201

for i,price in enumerate(A):
    if price > prev and money >= prev:
        stock += money // prev
        money -= stock * prev

    elif price < prev and stock:
        money += prev * stock
        stock = 0
    prev = price

print(money)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
t,m=0,1000
for i in range(n-1):
    if a[i]<a[i+1]:
        b=m//a[i]
        t+=b
        m-=b*a[i]
    if a[i]>a[i+1]:
        m+=t*a[i]
        t=0
if t==0:
    print(m)
else:
    print(m+t*a[n-1])

Solution (Python):
def solve(N, As):
    money = 1000
    stock = 0
    for i in range(len(As)):
        if i == N - 1:
            money += stock * As[i]
            stock = 0
            continue
        if As[i] > As[i + 1]:
            money += stock * As[i]
            stock = 0
        if As[i] < As[i + 1]:
            stock += money // As[i]
            money = money % As[i]
    print(money)


if __name__ == '__main__':
    
    N = int(input())
    
    As = [int(i) for i in input().split()]
    
    solve(N, As)

Solution (Python):
import math
N = int(input())
A = list(map(int, input().split()))
now = 1000
stack = 0
for i in range(0, N-1):
    if stack != 0: 
        now += stack*A[i]
        stack = 0
    if A[i] < A[i+1]: 
        stack = math.floor(now/A[i])
        now -= stack*A[i]
    
now += stack*A[N-1] 
stack = 0
print(now)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
money=1000
stock=0

for i in range(n-1):
    if A[i]<A[i+1]:
        stock+=money//A[i]
        money=money%A[i]
    elif A[i]>A[i+1]:
        money+=stock*A[i]
        stock=0
print(money+stock*A[n-1])

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
arr=list(map(int,input().split()))
have=0
ans=1000
rate=0
for i in range(n-1):
    
    if(have!=0):
        if(arr[i+1]<arr[i]):
            ans+=have*(arr[i]-rate)
            have=0
    else:
        if(arr[i+1]>arr[i]):
            have=ans//arr[i]
            rate=arr[i]
    

if(have!=0):
    ans+=have*(arr[-1]-rate)
    have=0
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int,readline().split()))
k = 0
ss = sorted(a)
qq = ss[::-1]

if len(set(a)) == 1 or qq == a:
  print(1000)
  exit()
elif ss == a:
  k = 1000//a[0]
  print(k*a[-1]+1000-a[0]*k)
  exit()

m = 1000
if a[0] < a[1]:
  pp = 1000//a[0]
  m -= pp*a[0]
  k += pp

for i in range(n-2):
  if a[i] <= a[i+1] and a[i+1] > a[i+2]:
    m += k*a[i+1]
    k = 0
  if a[i] >= a[i+1] and a[i+1] < a[i+2]:
    pp = m//a[i+1]
    m -= pp*a[i+1]
    k += pp

m += k*a[-1]
print(m)

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def S(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

alphabets = "abcdefghijklmnopqrstuvwxyz"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    N = I()
    A = LI()

    money = 1000

    partial = []
    tmp = [A[0]]
    last = A[0]

    for i in range(1, N):
        if A[i] >= last:
            tmp.append(A[i])
            last = A[i]
        else:
            partial.append(tmp)
            tmp = [A[i]]
            last = A[i]

    partial.append(tmp)

    for part in partial:
        if len(part) <= 1:
            continue
        last = part[-1]
        start = part[0]
        stock = money // start
        money = money % start + stock * last
    print(money)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
price = [int(x) for x in input().split()]

custom_price = []
i = 0
while True:
    
    
    while i+1 < n and price[i+1] <= price[i]:
        i += 1
    custom_price.append(price[i])
    i += 1
    if i == n:
        break
    while i+1 < n and price[i+1] >= price[i]:
        i += 1
    custom_price.append(price[i])
    i += 1
    if i == n:
        break
if len(custom_price) % 2 == 1:
    custom_price.pop(-1)

i = 0
cur = 1000
while i < len(custom_price):
    stocks = cur//custom_price[i]
    cur += (stocks * (custom_price[i+1] - custom_price[i]))
    i += 2
print(cur)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
wallet = 1000
stock = 0
price = 0
for i in range(n - 1):
    if a[i] < a[i + 1]:
        price = a[i]
        buy = wallet // price
        stock += buy
        wallet -= (price * buy)
    elif a[i] > a[i + 1]:
        price = a[i]
        wallet += (price * stock)
        stock = 0
if stock != 0:
    wallet += (a[- 1] * stock)
print(wallet)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

dp = [1000] * N
M = 1000
for i in range(N) :
  for j in range(i + 1, N) :
    if A[j] > A[i] :
      kabu = max(dp[:i + 1]) // A[i]
      kane = max(dp[:i + 1]) % A[i]
      dp[j] = max(dp[j], kane + kabu * A[j])

print(max(dp))

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]

dp = [[[0,0] for _ in range(2)] for _ in range(N+1)]
dp[0][0] = [1000,0]

for i in range(N):
    for j in range(i+1):
        if(dp[j][0][0] > dp[i+1][0][0]):
            dp[i+1][0][0] = dp[j][0][0]
        x = dp[j][1][0] + dp[j][1][1] * A[i]
        if(x > dp[i+1][0][0]):
            dp[i+1][0][0] = x
        
        y = dp[j][0][0] // A[i]
        x = dp[j][0][0] % A[i]
        if((y > dp[i+1][1][1] ) or (y == dp[i+1][1][1] and x > dp[i+1][1][0])):
            dp[i+1][1][1] = y
            dp[i+1][1][0] = x
        
        y = dp[j][1][1] + dp[j][1][0] // A[i]
        x = dp[j][1][0] % A[i]
        if((y > dp[i+1][1][1] ) or (y == dp[i+1][1][1] and x > dp[i+1][1][0])):
            dp[i+1][1][1] = y
            dp[i+1][1][0] = x

print(dp[N][0][0])

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

s = [[0]*2 for i in range(n)]
m = [[0]*2 for i in range(n)]


s[0][0] = 0
m[0][0] = 1000

s[0][1] = 1000//a[0]
m[0][1] = 1000 - s[0][1] * a[0]

for i in range(1,n):
    s[i][0] = 0
    m[i][0] = max(m[i-1][0], m[i-1][1]+s[i-1][1]*a[i])
    s[i][1] = max(m[i-1][0]//a[i], s[i-1][1])
    if m[i-1][0]//a[i] > s[i-1][1]:
        m[i][1] = m[i-1][0]-s[i][1]*a[i]
    elif m[i-1][0]//a[i] < s[i-1][1]:
        m[i][1] = m[i-1][1]
    else:
        m[i][1] = max(m[i-1][0]-s[i][1]*a[i], m[i-1][1])

print(m[n-1][0])

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
chk = [0]*(n-1)
for i in range(n-1):
    if A[i + 1] > A[i]:
        chk[i] = 2
    elif A[i + 1] < A[i]:
        chk[i] = 0
    else:
        chk[i] = 1
c = 0
money = 1000
stock = 0
for i in range(n-1):
    if chk[i] == 2 and c == 0:
        stock = money//A[i]
        money -= stock*A[i]
        c = 1
    elif chk[i] == 0 and c == 1:
        money += stock*A[i]
        stock = 0
        c = 0
    if 2 not in chk[i:]:
        money += stock*A[i]
        stock = 0
        break
money += stock*A[n-1]
print(money)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
c=0
L=[]
Benef=[]
for i in range(N-1):
    if A[i]>A[i+1]:
        L.append((c,i))
        Benef.append(A[i]-A[c])
        c=i+1
    elif i==N-2:
        L.append((c,N-1))
        Benef.append(A[N-1]-A[c])
l=len(L)
maney=1000
for j in range(l):
    maney+=(maney//A[L[j][0]])*Benef[j]
print(maney)

Solution (Python):
n = int(input())
a = [0] + [int(_) for _ in input().split()]

dp = [1000] * (n+1)
for i in range(1, n+1):

    stock = dp[i] // a[i]
    amari = dp[i] % a[i]
    max_price = 0
    for j in range(min(n+1, i+1), n+1):
        price = stock * a[j] + amari
        if price > max_price:
            max_price = price
        dp[j] = max(max_price, dp[j])

print(dp[n])

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
ans=[1000,0]
for i in range(1,n):
    if a[i-1]<a[i]:
        ans[0]+=ans[1]*a[i-1]
        ans[1]=0
        ans[1]=ans[0]//a[i-1]
        ans[0]=ans[0]%a[i-1]
    else:
        ans[0]+=ans[1]*a[i-1]
        ans[1]=0
print(ans[0]+ans[1]*a[-1])

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
m = 1000
ind_st = 0
ind = 1
while ind_st < n:
    if ind < n:
        if a[ind]>=a[ind-1]:
            ind+=1
            continue
    
    kb = m//a[ind_st]
    m += kb*(a[ind-1]-a[ind_st])
    ind_st = ind
    ind = ind_st+1

print(m)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
maney = 1000
stock = 0

for i in range(N):
    if i == N-1:
        maney += stock * A[i]
        break
    if A[i] < A[i+1]:
        tmp_stock = maney // A[i]
        stock += tmp_stock
        maney -= tmp_stock * A[i]
    elif A[i] > A[i+1]:
        maney += stock * A[i]
        stock = 0
    else:
        pass

print(maney)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

max_money = 1000
kabu = 0
money = 1000
for i in range(N):
    if i == 0:
        if A[i]<A[i+1]:
            kabu = int(1000/A[i])
            money -= kabu*A[i]
        continue
    if i == N-1:
        if A[i-1]<A[i]:
            money += kabu * A[i]
            kabu = 0
            max_money = max(max_money,money)
        break

    if A[i-1]<A[i]:
        money += kabu * A[i]
        kabu = 0
        max_money = max(max_money,money)
    if A[i]<A[i+1]:
        kabu += int(money/A[i])
        money -= A[i]*int(money/A[i])

print(max_money)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

dA=list()
for i in range(N-1):
    dA.append(A[i+1]-A[i])


m=N-2
while m>0 and dA[m]<=0:
    m-=1


money=1000
stock=0
for i in range(m+1):
    if dA[i]>0:
        a = int(money / A[i])
        money -= (a*A[i])
        stock += a
    else:
        if dA[i]<0:
            money += (stock*A[i])
            stock = 0
    

money += stock*A[m+1]
stock=0


print(money)

Solution (Python):



N = int(input())
A = list(map(int, input().split()))
mn = 1000
st = 0
for i in range(N-1):
    if A[i+1]>A[i]:
        by = mn//A[i]
        st += by
        mn -= by*A[i]
    if A[i+1]<=A[i]:
        mn += st*A[i]
        st = 0

print(st*A[-1]+mn)

Solution (Python):
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    kabu = 0
    money = 1000
    for i in range(N-1):
        if A[i] < A[i+1]:
            kabu += money//A[i]
            money -= A[i]*(money//A[i])
        elif A[i] > A[i+1]:
            money += kabu*A[i]
            kabu = 0
    if kabu != 0:
        money += kabu*A[-1]
    print(money)
resolve()