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):
n = int(input())
pricies = list(map(int, input().split()))
m = 1000
stocks = 0
ask = 0

for i in range(n):
    price = pricies[i]
    isHigher = False
    isLower = False

    if stocks > 0:
        if price >= ask:
            m += price * stocks
            stocks = 0

    for j in range(i+1, n):
        if pricies[j] >= price:
            isHigher = True
            break
        else:
            isLower = True

    if isHigher and not isLower:
        stocks, m = stocks + m // price, m % price
        ask = price

print(m)

Solution (Python):
class Assets:
    def __init__(self):
        self.money = 1000;self.stock = 0       
    
    def transaction(self, p_rate, n_rate = 0):

        if p_rate < n_rate and p_rate <= self.money:
            num = self.money // p_rate 
            self.stock += num
            self.money -= (num * p_rate)
        elif p_rate > n_rate :
            self.money += (p_rate * self.stock)
            self.stock = 0

    def show(self):
        print(self.money)

day = int(input())
rate_of_change = list(map(int, input().split()))

ans = Assets()

for j, i in enumerate(range(day) , 1):

    if j == day:
        Assets.transaction(ans, rate_of_change[i])
    else:        
        Assets.transaction(ans, rate_of_change[i], rate_of_change[j])

Assets.show(ans)

Solution (Python):
import sys
import  math
from heapq import *
import fractions
import bisect
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
A=nl()
A.append(0)
m=1000
kabu=0
for i in range(N):
    if(A[i]<A[i+1]):
        tmp=m//A[i]
        m=m-(A[i]*tmp)
        kabu+=tmp
    elif(A[i-1]<=A[i] and A[i+1]<A[i]):
        m+=kabu*A[i]
        kabu=0

print(m)

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

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

kabu = 0
en = 1000

for i in range(N):
    a, b = divmod(en, A[i])
    kabu += a
    en = b
    if A[i+1] < A[i]:
        en += kabu*A[i]
        kabu = 0

print(en)

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
mon=1000
stk=0
for i in range(n-1):
  if A[i]<=A[i+1]:
    stk+=mon//A[i]
    mon%=A[i]
  else:
    mon+=stk*A[i]
    stk=0
mon+=stk*A[-1]
print(mon)

Solution (Python):
def main():
    N = int(input())
    A = list(map(int,input().split()))
    i = 0
    j = 1
    money = 1000
    num_of_stock = 0

    while (i<N-1):
        
        if A[i] < A[i+1]:
            num_of_stock += money // A[i]
            money = money % A[i]
        elif A[i] > A[i+1]:
            money += A[i]*num_of_stock
            num_of_stock = 0
        else:
            i += 1
            continue
        i += 1

    money += A[N-1]*num_of_stock

    return money

print(main())

Solution (Python):




def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 1000

    for i in range(1, N):
        if A[i - 1] < A[i]:
            stock = ans // A[i - 1]
            ans += stock * (A[i] - A[i - 1])
    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
N = int(input())
m = 1000
k = 0
A = list(map(int,input().split()))
M = A[0]
for a in A:
  if M > a:
    if k > 0:
      m += k * M
      k = 0
  elif M < a:
    if k == 0:
      k,m = divmod(m,M)
  M = a
m += k * M
print(m)

Solution (Python):

def sell(asset, price):
  return price * asset[1] + asset[0]


def buy(money, price):
  stock = money // price
  remainder = money % price
  
  return (remainder, stock)


from collections import deque
import copy

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


assets = deque()
assets.append((1000, 0))

for i in range(N - 1):
  new_assets = deque()
  max_money = 0
  
  for a in assets:
    max_money = max(max_money, sell(a, A[i]))  
  
  assets.append((max_money, 0))
  assets.append(buy(max_money, A[i]))

ans = 0

for a in assets:
  ans = max(ans, sell(a, A[N - 1]))

print(ans)

Solution (Python):
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    a.append(0)
    pre = 10**9
    kabu = 0
    ans = 1000
    flag1,flag2 = False,False
    if a[0]<a[1]:
        flag2 = True
    else:
        flag1 = True
    for i in range(n+1):
        if flag1==True and a[i]> pre:
            kabu += ans//pre
            ans -= kabu*pre
            flag1,flag2 = False,True
        elif flag2==True and a[i]<pre:
            ans += kabu*pre
            kabu = 0
            flag1,flag2 = True,False
        pre = a[i]
        
    print(ans)

main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
money = 1000
kabu = 0
if a[0] < a[1]:
    flg = 'kau'
elif a[0] > a[1]:
    flg = 'uru'
else:
    flg = 'f'
for i in range(1,n):
    if a[i-1] < a[i]:
        if flg != 'uru':
            kabu = money // a[i-1]
            money -= kabu * a[i-1]
            flg = 'uru'
    elif a[i-1] > a[i]:
        if flg != 'kau':
            money += kabu * a[i-1]
            kabu = 0
            flg = 'kau'
    else:
        pass
if kabu > 0:
    money += kabu * a[-1]
print(money)

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

m = 1000
stock = 0

for i in range(n-1):
  if s[i+1] >= s[i]:
    
    stock = stock + m // s[i]
    m = m - s[i]*(m//s[i])
  else:
    
    m = m + stock*s[i]
    stock = 0

an  = m + stock*s[n-1]
print(an)

Solution (Python):
N = int(input())
A = list(int(a) for a in input().split())
A.extend([0])

money = 1000
stock = 0

for i in range(N):
    if A[i] < A[i+1] and money >= A[i]:
        stock += money // A[i]
        money %= A[i]
    if A[i] > A[i+1]:
        money += stock * A[i]
        stock = 0

print(money)

Solution (Python):
n = int(input())
*A, = map(int, input().split())
ans = 1000
for i in range(n - 1):
    ans += (ans // A[i]) * max(A[i + 1] - A[i], 0)
print(ans)

Solution (Python):
n=int(input())

al=list(map(int,input().split()))

stock = 0
money = 1000
for i in range(n-1):
    sell = stock*al[i]
    stock = 0
    money += sell
    if al[i] < al[i+1]:
        buynum = money//al[i]
        stock= buynum
        money -= buynum*al[i]

print(money + stock*al[n-1])

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

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    current = 1000
    prev = A[0]
    for a in A[1:]:
        if prev < a:
            n = current // prev
            current = a * n + (current % prev)
        prev = a
    print(current)


if __name__ == "__main__":
    main()

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

    
    
    
    

    
    
    
    
    

    for i in range(n - 1):
        today = a[i]
        tomorrow = a[i + 1]

        if tomorrow > today:
            count = money // today
            money %= today
            money += tomorrow * count

    print(money)


if __name__ == '__main__':
    main()

Solution (Python):

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

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

print(money)

Solution (Python):
n = int(input())
A = [*map(int, input().split())]
yen = 1000
for i in range(n-1): yen += max(0, yen//A[i] * (A[i+1] - A[i]))
print(yen)

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

for i in range(n-1):
    if a[i] < a[i+1]:
        money = money//a[i] * a[i+1] + money%a[i]

print(money)

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

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

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

Solution (Python):
n = int(input())
A = [*map(int, input().split())] + [0]
dp_yen = [1000]*(n+1)
for day in range(n):
    dp_yen[day+1] = max(dp_yen[day], dp_yen[day] + dp_yen[day]//A[day] * (A[day+1] - A[day]))
print(dp_yen[n])

Solution (Python):
n = int(input())
A = [*map(int, input().split())] + [0]
dp_yen = [0]*(n+1)
dp_yen[0] = 1000
for day in range(n):
    dp_yen[day+1] = max(dp_yen[day], dp_yen[day] + (A[day+1] - A[day]) * (dp_yen[day] // A[day]))
print(dp_yen[n])

Solution (Python):
n = int(input())
A = [0] + [*map(int, input().split())] + [0]
dp_yen = [0]*(n+1)
dp_yen[1] = 1000
for day in range(1, n):
    stock = 0
    if A[day] > 0: stock = dp_yen[day] // A[day]
    remain = dp_yen[day] - stock*A[day]
    dp_yen[day+1] = max(dp_yen[day], remain + stock * A[day+1])
print(dp_yen[n])

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


def main():
    N = int(input())
    A = list(map(int,input().split()))
    zougen = [-1 if A[i] > A[i + 1] else 1 for i in range(N - 1)]
    money = 1000
    kabu = 0
    for i in range(N-1):
        if zougen[i] == 1:
            kabu += money // A[i]
            money = money % A[i]
        else:
            money += kabu * A[i]
            kabu = 0
    print(A[-1] * kabu + money)



if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
A = [0] + [*map(int, input().split())]
dp_stock = [0]*(n+1)
dp_yen = [0]*(n+1)
dp_yen[0] = 1000
for day in range(1, n+1):
    dp_stock[day] = max(dp_stock[day-1], dp_yen[day-1] // A[day])
    remain = dp_yen[day-1] - dp_stock[day-1]*A[day-1]
    dp_yen[day] = max(dp_yen[day-1], remain + dp_stock[day-1] * A[day])
print(dp_yen[n])

Solution (Python):
n = int(input())

a = [0 for _ in range(n)]
a = [int(s) for s in input().split()]


st_num = 0
mny = 1000

for i in range(n-1):
    now_mn = a[i]
    next_mn = a[i+1]
    
    
    if(next_mn > now_mn):
        st_num += mny//now_mn
        mny = mny%now_mn
    else:
        mny += st_num*now_mn
        st_num = 0

if(a[n-1] > a[n-2] and st_num > 0):
    mny += st_num*a[n-1]
    st_num = 0

print(mny)

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))
cache = 1000
numStock = 0
for i in range(1,n):
    if arr[i]>arr[i-1]:
        available = cache//arr[i-1]
        cache -= arr[i-1]*available
        numStock += available
    else:
        cache += arr[i-1]*numStock
        numStock = 0

if numStock>0:
    cache += arr[-1]*numStock

print(cache)

Solution (Python):
def sell(rate,kabu):
    money = rate * kabu
    return money
    
def buy(rate,money):
    kabu = money//rate
    otsuri = money % rate
    return kabu,otsuri
    
N,*A = map(int,open(0).read().split())
max_money = 1000
have_money = 1000
have_kabu = 0
for i in range(N-1):
    if A[i] < A[i+1] and have_money > A[i]: 
        have_kabu,have_money = buy(A[i],have_money)
    if A[i] > A[i+1]: 
        have_money += sell(A[i],have_kabu)
        have_kabu = 0
        max_money = max(max_money,have_money)
have_money += sell(A[-1],have_kabu)    
max_money = max(max_money,have_money)
print(max_money)

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

i = 0

yen = 1000
while i < N:
    while i < N - 1 and A[i] >= A[i + 1]:
        i += 1

    if i == N - 1:
        break

    x = i
    while i < N - 1 and A[i] <= A[i + 1]:
        i += 1

    y = i

    cnt = yen // A[x]
    yen = yen - A[x] * cnt + A[y] * cnt

print(yen)

Solution (Python):
def sell(rate, kabu):
    return rate * kabu
    
def buy(rate, money):
    kabu = money//rate
    otsuri = money % rate
    return kabu, otsuri
    
N,*A = map(int, open(0).read().split())
money = 1000
have_kabu = 0
for i in range(N):
    
    money += sell(A[i], have_kabu)
    have_kabu = 0
    
    if i != N-1 and A[i] < A[i+1]: 
        have_kabu, money = buy(A[i], money)
print(money)

Solution (Python):

def sell(price):
  global money
  global stock
  
  money += price * stock
  stock = 0


def buy(price):
  global money
  global stock

  stock += money // price
  money %= price


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]:
    sell(A[i])
    buy(A[i])
  elif A[i + 1] < A[i]:
    sell(A[i])

sell(A[N - 1])

print(money)

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

kab=0
mon=1000

for i in range(n-1):
    if a[i]<a[i+1]:
        x=mon//a[i]
        kab+=x
        mon-=x*a[i]
    else:
        mon+=kab*a[i]
        kab=0

mon+=a[-1]*kab

print(mon)

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

money = 1000
kabu = 0

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

print(money)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a = a + [0]
state = a[0]
money = 1000
stock = 0
for i in range(1, n+1):
  if state < a[i] and stock == 0:
    stock += (money // state)
    money %= state
  elif state > a[i] and stock > 0:
    money += (stock * state)
    stock = 0
  state = a[i]
print(money)

Solution (Python):
N = int(input())
An = list(map(int, input().split()))
dp = [0 for x in range(N+10)]
An.insert(0, 0)
dp[1] = 1000
for i in range(2, N+1):
    stock = dp[i-1] // An[i-1]

    money = dp[i-1]
    money -= stock * An[i-1]
    money += stock * An[i]

    dp[i] = max(dp[i-1], money)
print(dp[N])

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

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

print(m+s*A[-1])

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

money = 1000
for x, y in zip(a[:-1], a[1:]):
    if x < y:
        stocks = money // x
    else:
        stocks = 0
    money += (y - x) * stocks

print(money)

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

Solution (Python):
def function(N, A):
    money = 1000
    kabu = 0
    for i in range(N-1):
        if A[i] < A[i+1]:
            kabu = int(money/A[i])
            money += (A[i+1]-A[i])*kabu
    print(money)


if __name__ == '__main__':
    N = int(input())
    A = input().split()
    A = [int(a) for a in A]
    function(N, A)

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

m = 1000
k = 0
for i in range(N-1):
    if A[i] > A[i+1]:
        m += k * A[i]
        k = 0
    elif A[i] < A[i+1]:
        m += k * A[i]
        k = m // A[i]
        m -= k * A[i]
    else:
        pass
m += (k * A[-1])
print(m)

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

g=1000

for s1,s2 in zip(A[:-1],A[1:]):
    if s1<s2:
        stockNum=g//s1
        g+=stockNum*(s2-s1)

print(g)

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

print(kane+kabu*a[n-1])

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

ans = 0

lst = [1000] * (N)

for i in range(N): 
    a = A[i]
    for j in range(i + 1, N):
        if A[j] > a:
            tmp = lst[i] // a * A[j] + lst[i] % a
            lst[j] = max(max(lst[j], tmp), lst[j - 1])
        else:
            lst[j] = max(lst[j], lst[j - 1])

print (lst[-1])

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

buy = A[0]
ans = 1000
for i in range(1, N):
  if A[i] > A[i+1] and buy < A[i]:
    ans = ans % buy + ans // buy * A[i]
    buy = 500
  elif buy > A[i]:
    buy = A[i]
print(ans)

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


cMoney = 1000
stock = 0
A.append(0)
for i in range(N):
    stock = 0

    if A[i] < A[i+1]:
        stock = cMoney // A[i]
        cMoney -= stock * A[i]
    cMoney += A[i+1] * stock



    

print(cMoney)

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

for p in range(N):
    
    if A[p] < A[p+1]:
        kabu += money//A[p]
        money -= (money//A[p])*A[p]
    
    
    if A[p] > A[p+1]:
        money += kabu*A[p]
        kabu = 0 
    

print(money)

Solution (Python):
N = int(input())
A = [0] * (N + 1)
A = list(map(int,input().split()))


cMoney = 1000
stock = 0
A.append(0)

















for i in range(N):
    stock = 0
    if A[i] < A[i+1]:
        stock = cMoney // A[i]

    cMoney += (A[i+1] - A[i]) * stock
print(cMoney)

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

amount = 1000
MX = A[0]
MN = A[0]

for i in range(1, N):
	a = A[i]
	if a < MX:
		if MN < MX:
			s = amount // MN
			t = amount % MN
			amount = MX * s + t
		MN = a
		MX = a
	else:
		if a > MX:
			MX = a
		if a < MN:
			MN = a

	if i == N - 1 and MN < MX:
		s = amount // MN
		t = amount % MN
		amount = MX * s + t

print(amount)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = 1000
for i in range(1, N):
    if A[i] > A[i-1]:
        total += (total // A[i-1]) * (A[i]-A[i-1])
print(total)

Solution (Python):
n=int(input())
I=list(map(int,input().split()))
num=1000
for i in range(n-1):
    if I[i]<I[i+1]:
        p=num//I[i]
        num-=p*I[i]
        num+=p*I[i+1]

print(num)

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

for i in range(1, n):
    if a[i] > a[i - 1]:
        cnt = money // a[i - 1]
        money = cnt * a[i] + money % a[i - 1]
print(money)

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

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 += stock * (A[i+1] - A[i])
        
        
print(money)

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


money = 1000

kabu_c = 0

l = []

for i in range(n-1):
    if A[i+1] - A[i] > 0:
         l.append(1)
    else:
         l.append(0)



for idx, i in enumerate(l):
    if i == 1 and money - A[idx] >= 0:
        kabu_c = money // A[idx]
        money = money - kabu_c * A[idx]
    if i == 0:
        money = money + kabu_c * A[idx]
        kabu_c = 0

if kabu_c > 0:
    money = money + kabu_c * A[n-1]
print(money)

Solution (Python):
n=int(input())
ar=list(map(int,input().split()))
li=[]
bo=True
for i in range(n-1):
	if bo and ar[i+1]>=ar[i]:
		li.append(ar[i])
		bo=False
	elif not bo and ar[i]>ar[i+1]:
		li.append(ar[i])
		bo=True
if len(li)==0:
	print(1000)
	exit()
if bo and li[-1]==ar[n-2]:
	if ar[n-1]>ar[n-2]:
		li.pop()
		li.append(ar[n-1])
if not bo:
	li.append(ar[-1])
s=1000
l=len(li)
for i in range(0,l,2):
		a=li[i]
		b=li[i+1]
		x=s//a
		r=s%a
		s=b*x+r
print(s)

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

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

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

money=1000
kabukosuu=0

for i in range(n-1):
    money+=kabukosuu*a[i]
    kabukosuu=0
    if a[i]<a[i+1]:
        kabukosuu=money//a[i]
        money=money%a[i]
    
money+=kabukosuu*a[n-1]

print(money)

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

x = 1000
y = 0

for i in range(n):
    if a[i] < a[i+1]:
        y += x // a[i]
        x %= a[i]
    elif a[i] > a[i+1]:
        x += y * a[i]
        y = 0

print(x)

Solution (Python):
n = int(input())

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

memo = [0]*n

memo[0] = 1000

for i in range(1,n):
    memo[i] = max((memo[i-1]%a[i-1]+a[i]*(memo[i-1]//a[i-1])),memo[i-1])

print(memo[-1])

Solution (Python):
n = int(input())

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

money = 1000

for i in range(1,n):
    if a[i-1] < a[i]:
        money = money%a[i-1]+a[i]*(money//a[i-1])

print(money)

Solution (Python):
M = int(input())
A = [int(_) for _ in input().split()]
N = 1000
kabu = 0
if A[0] < A[1]:
    kabu, N = divmod(N, A[0])
for a, b, c in zip(A, A[1:], A[2:]):
    if a <= b >= c:
        N += kabu * b
        kabu = 0
    elif a >= b <= c:
        dkabu, N = divmod(N, b)
        kabu += dkabu
N += kabu * A[-1]
print(N)

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

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    m = 1000
    sell = False
    buyp = A[0]
    sellp = None
    for i in range(1, N):
        if (not sell) and (buyp > A[i]):
            buyp = A[i]
        elif (not sell):
            sell = True
            sellp = A[i]
        if sell and (sellp < A[i]):
            sellp = A[i]
        elif sell:
            s = m // buyp
            m = s * sellp + (m % buyp)
            buyp = A[i]
            sell = False
    return m
print(main())

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

from collections import deque


cash = 1000
stock = 0

peak_min = deque()
peak_max = deque()

d_prev = 0
for i in range(1, n):
    a = A[i]
    if A[i] > A[i-1]:
        d = 1
    elif A[i] == A[i-1]:
        d = d_prev
    else:
        d = -1
    if d_prev*d == -1:
        if d == 1:
            peak_min.append(i-1)
        elif d == -1:
            peak_max.append(i-1)
    d_prev = d


if len(peak_min) == 0 and len(peak_max) == 0:
    if A[0] < A[-1]:
        ans = cash%A[0] + cash//A[0]*A[-1]
    else:
        ans = cash
elif len(peak_min) == 0:
    ans = cash%A[0] + cash//A[0]*A[peak_max[0]]
elif len(peak_max) == 0:
    ans = cash%A[peak_min[0]] + cash//A[peak_min[0]]*A[-1]
else:
    if peak_max[0] < peak_min[0]:
        peak_min.appendleft(0)
    if peak_max[-1] < peak_min[-1]:
        peak_max.append(n-1)

    
    while len(peak_min) != 0:
        i = peak_min.popleft()
        stock += cash//A[i]
        cash = cash%A[i]

        
        i = peak_max.popleft()
        cash += stock*A[i]
        stock = 0

    ans = cash
print(ans)

Solution (Python):
M = int(input())
A = [int(_) for _ in input().split()]
N = 1000
if all(a >= b for a, b in zip(A, A[1:])):
    print(N)
else:
    kabu = 0
    if A[0] < A[1]:
        kabu, N = divmod(N, A[0])
    for a, b, c in zip(A, A[1:], A[2:]):
        if a <= b >= c:
            N += kabu * b
            kabu = 0
        elif a >= b <= c:
            dkabu, N = divmod(N, b)
            kabu += dkabu
    N += kabu * A[-1]
    print(N)

Solution (Python):
N = int(input())
arr = list(map(int, input().split()))
dp = [1000]*N
for i in range(1,N):
    dp[i] = max((dp[i-1]//arr[i-1])*arr[i]+dp[i-1]%arr[i-1],dp[i-1])
print(dp[-1])

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
a = nl()

dp=[0]*(n+1)
dp[0] = 1000

for i in range(1,n):
  dp[i]=dp[i-1]
  for j in range(i):
    v = dp[j] // a[j]
    w = dp[j] + (a[i]-a[j])*v
    dp[i] = max(dp[i],w)
print(dp[n-1])

Solution (Python):

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


dp = [0]*N
dp[0] = 1000

for i in range(1,N):
    for j in range(0,i):
        dp[i] = max(dp[i], dp[i-1],(dp[j]//A[j])*A[i]+dp[j]%A[j])

print(dp[-1])

Solution (Python):




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


res=1000

for i in range(1,n):

    if a[i]>a[i-1]:
        t=(res//a[i-1])*a[i]+res%a[i-1]
        res=t


print(res)

Solution (Python):
N= int(input())
A = list(map(int,input().split()))
A.insert(0,A[0])
A.append(0)
 
money = 1000
previous_tendency = "remain"
current_tendency = "remain"
count = 1


 
for n in range(1,len(A)-1):
    if A[n] < A[n+1]:
        current_tendency = "up"
    elif A[n] > A[n+1]:
        current_tendency = "down"
    else:
        current_tendency = "remain"
        
    if previous_tendency != "down" and current_tendency != "up":
        
        money += (money//min(A[count:n+1]))*(A[n]-min(A[count:n+1]))
        
        count = n + 1
        previous_tendency = "remain"
    
    
    if current_tendency != "remain":
      previous_tendency = current_tendency
print(money)

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

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

for x in range(n-1):
    if l.pop(-1)==1:
        l.append(1)
        l.append(-1)
        break

m=1000
p=0
for i,x in enumerate(l):
    if x==1:
        t=m//a[i]
        m-=t*a[i]
        p+=t
    if x==-1:
        m+=p*a[i]
        p=0
print(m)

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

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

amount = 1000

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

print(amount)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
x = []
flag = True
for i in reversed(range(n)):
    if i == 0:
        if not flag:
            x.append(a[i])
        break
    flag2 = flag
    if flag:
        if a[i - 1] < a[i]:
            x.append(a[i])
            flag2 = not flag2
    else:
        if a[i - 1] > a[i]:
            x.append(a[i])
            flag2 = not flag2
    flag = flag2
x = x[::-1]
m = len(x)
if m % 2 == 1:
    x = x[1:]

cur_money = 1000
cur_stock = 0
for i in range(m):
    if i % 2 == 0:
        cur_stock += cur_money // x[i]
        cur_money %= x[i]
    else:
        cur_money += x[i] * cur_stock
        cur_stock = 0
print(cur_money)

Solution (Python):



def million(n, a):
    wallet = 1000
    i = 0
    while i < n - 1:
        highest_price = a[i]
        cheapest_price = a[i]
        
        for j in range(i + 1, n):
            if highest_price > a[j]:
                break
            if highest_price < a[j]:
                highest_price = a[j]
            if cheapest_price > a[j]:
                cheapest_price = a[j]
        if highest_price > cheapest_price:
            
            stock = wallet // cheapest_price
            wallet = wallet - stock * cheapest_price
            wallet = wallet + stock * highest_price
            i = j
        else:
            i += 1
    return wallet


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    print(million(n, a))

Solution (Python):
N = int(input())
stocks = list(map(int, input().split()))
curr = 1000
for i in range(N - 1):
  cnt = 0
  if stocks[i] < stocks[i + 1]:
    cnt = curr // stocks[i]
  curr += cnt * (stocks[i + 1] - stocks[i])
print(curr)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 1000
for i in range(N-1):
    if A[i] < A[i+1]:
        temp = ans // A[i]
        ans -= temp*A[i]
        ans += temp*A[i+1]
print(ans)

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

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



dp = [0] * n
dp[0] = 1000




for i in range(n):
    if i == 0:
        dp[i] = 1000
        continue
    
    dp[i] = dp[i-1]
    for j in range(i):
        
        dp[i] = max(dp[i], (dp[j] // a[j]) * a[i] + dp[j] % a[j])
    
print(dp[n-1])

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

money = 1000
for i in range(n-1):
    stocks = 0
    if a[i] < a[i + 1]:
        stocks = money // a[i]
    money += (a[i+1] - a[i]) * stocks
print(money)