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



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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








def solve_slow(N, As):
    nums = [0, 1, 2]

    ans = 0
    for comb in product(nums, repeat=N):
        money = 1000
        stock = 0
        for c, a in zip(comb, As):
            
            if c == 0:
                money += a * stock
                stock = 0
            
            elif c == 1:
                stock += money // a
                money = money % a
            else:
                pass
            
        
        ans = max(ans, money)

    return ans
                

def solve_fast(N, As):
    def uniq(As):
        ret = []
        for a in As:
            if not ret:
                ret.append(a)
            else:
                if ret[-1] != a:
                    ret.append(a)
        return ret
    
    As = uniq(As)
    N = len(As)
    
    ans = 0
    money = 1000
    stock = 0
    for n in range(N):
        ans = max(ans, money + stock * As[n])
    
        if n == N - 1:
            break
    
        if As[n] < As[n + 1]:
            stock_diff, money = divmod(money, As[n])
            stock += stock_diff
            
        elif As[n] > As[n + 1]:
            money += stock * As[n]
            stock = 0
        
    
    return ans

print(solve_fast(N, As))

Solution (Python):
import io
import os

from collections import Counter, defaultdict, deque


from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 5)


def solve(N, A):
    @lru_cache(maxsize=None)
    def maxProfitAtEnd(i, money, stocks):
        if i == N - 1:
            
            return money + stocks * A[i]

        
        best = maxProfitAtEnd(i + 1, money, stocks)
        
        best = max(best, maxProfitAtEnd(i + 1, money + stocks * A[i], 0))
        
        numStock = money // A[i]
        best = max(
            best, maxProfitAtEnd(i + 1, money - numStock * A[i], stocks + numStock)
        )

        return best

    return maxProfitAtEnd(0, 1000, 0)


def solve(N, A):
    money = 1000
    stock = 0
    for i in range(N - 1):
        if A[i] < A[i + 1]:
            
            numStock = money // A[i]
            money -= numStock * A[i]
            stock += numStock
            
            money += stock * A[i + 1]
            stock = 0
    assert stock == 0
    return money


if __name__ == "__main__":
    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    (N,) = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    ans = solve(N, A)
    print(ans)

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

upFlag = False
B = []
for i in range(1, N):
    if A[i-1] < A[i]:
        if not upFlag:
            B.append(A[i-1])
            upFlag = True
    elif A[i-1] > A[i]:
        if upFlag:
            B.append(A[i-1])
            upFlag = False

if upFlag:
    B.append(A[-1])


X = 1000
Y = 0

for i, b in enumerate(B):
    if i%2 == 0:
        Y = X//b
        X = X%b
    else:
        X += Y*b
        Y = 0

print(X)

Solution (Python):

import math
import bisect
import copy
import time
from collections import Counter


n=int(input())
l=[int(x) for x in input().split()]
k=[]
def stockBuySell(price, n):  
    if (n == 1): 
        return

    i = 0
    while (i < (n - 1)): 
        while ((i < (n - 1)) and 
                (price[i + 1] <= price[i])): 
            i += 1
          
        if (i == n - 1): 
            break

        buy = i 
        i += 1
           
        while ((i < n) and (price[i] >= price[i - 1])): 
            i += 1
 
        sell = i - 1
        k.append(buy)
        k.append(sell)

stockBuySell(l,n)
cur=1000

for i in range(len(k)):
	if i%2==0:
		x=cur//l[k[i]]
		cur-=int(x)*l[k[i]]
	else:
		cur+=int(x)*l[k[i]]

print(cur)

Solution (Python):
from collections import deque, Counter, defaultdict
from itertools import chain, combinations
import json

import bisect
import sys
import math
import bisect
from functools import lru_cache
import itertools

sys.setrecursionlimit(10 ** 8)

M = 10 ** 9 + 7
INF = 10 ** 17


def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    
    
    

    aa = []
    t = A[0]
    start = A[0]
    for ai in A[1:]:
        if t <= ai:
            t = ai
            if start is None:
                start = ai
            continue
        aa.append(
            (start, t)
        )
        t = ai
        start = ai
    if start is not None:
        aa.append((start, A[-1]))

    m = 1000
    for a, b in aa:
        if a is None:
            continue
        c = m // a
        m += (b - a) * c
    print(m)


if __name__ == "__main__":
    main()

Solution (Python):
n = map(int,input().split())
a = list(map(int,input().split()))
newa=[a[0]]
for i in range(1,len(a)):
    if a[i]!=a[i-1]:
        newa.append(a[i])
check=[]
for i in range(1,len(newa)):
    if newa[i]>newa[i-1]:
        check.append(i)
        
stock=0
money=1000
if len(check)>=1:
    kau1=min(newa[0:check[0]])
    stock=money//kau1
    money=money%kau1
    money+=newa[check[0]]*stock
    
    

    for i in range(len(check)-1):
        kau=min(newa[check[i]:check[i+1]])
        stock=money//kau
        money=money%kau
        money+=newa[check[i+1]]*stock
    
print(money)

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

for i in range(n-1):
  if stock == 0:
    if line[i] < line[i+1]:
      buy_stock = money // line[i]
      stock += buy_stock
      money -= buy_stock * line[i]
  else:
    if line[i] > line[i+1]:
      get_money = stock * line[i]
      money += get_money
      stock = 0

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

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

inc = False
money = 1000
kabu = 0
for i in range(N - 1):
    if (not inc) and A[i] < A[i + 1]:
        
        kabu = money // A[i]
        money -= kabu * A[i]
        inc = True
    elif inc and A[i] > A[i + 1]:
        money += kabu * A[i]
        kabu = 0
        inc = False

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

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

n_kabu = 0
money = 1000
for d in range(N-1):
    kabuka_today = A[d]
    kabuka_tomorrow = A[d+1]
    if kabuka_today < kabuka_tomorrow:
        can_buy = money//kabuka_today
        n_kabu += can_buy
        money -= can_buy*kabuka_today
    elif kabuka_today > kabuka_tomorrow:
        money += n_kabu*kabuka_today
        n_kabu = 0

money += n_kabu*A[N-1]

print(money)

Solution (Python):
N = int(input())
price = list(map(int,input().split()))
money = 1000
stack = 0

state = None
for i in range(1,N):
    if state == None:
        if price[i] < price[i-1]:
            money += price[i-1]*stack
            stack = 0
            state = False
        elif price[i] > price[i-1]:
            stack = money//price[i-1]
            money = money%price[i-1]
            state = True
    elif state == True:
        if price[i] < price[i-1]:
            money += price[i-1]*stack
            stack = 0
            state = False
    elif state == False:
        if price[i] > price[i-1]:
            stack = money//price[i-1]
            money = money%price[i-1]
            state = True

if stack != 0:
    money += price[-1]*stack
    stack = 0

print(money)

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

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()

turn = []
t = [a[0]]
for i in range(1,len(a)):
	if t[-1]<=a[i]:
		t.append(a[i])
	else:
		turn.append(t)
		t = [a[i]]
turn.append(t)


money = 1000
kabu = 0
for i in turn:
	if len(i)>=2:
		t = money//min(i)
		money-=t*min(i)
		money+=t*max(i)

print(money)

Solution (Python):
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter

from functools import lru_cache

sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


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

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

money = 1000
stock = 0
buy_turn = True
ans = 0

for i, a in enumerate(A[:N]):
    ans = max(ans, money)
    if buy_turn:
        if A[i + 1] < A[i]:
            continue
        buy_turn = False
        stock += money // A[i]
        money = money % A[i]

    else:
        if A[i + 1] > A[i]:
            continue
        buy_turn = True
        money += A[i] * stock
        stock = 0
    ans = max(ans, money)

print(ans)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    shojikin = 1000
    mochikabu = 0

    result = []
    for idx, a in enumerate(A[:-1]):
        if A[idx] <= A[idx+1]:
            available = shojikin // A[idx]
            mochikabu += available
            shojikin -= available * A[idx]

        elif A[idx] > A[idx+1]:
            shojikin += mochikabu * A[idx]
            mochikabu = 0

    shojikin += mochikabu * A[-1]
    print(shojikin)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

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

def main(n,a):
    genkin=1000
    kabu=0
    for i in range(n-1):
        if a[i]>a[i+1]:
            
            genkin+=kabu*a[i]
            kabu=0
        elif a[i]<a[i+1]:
            
            kabu+=genkin//a[i]
            genkin=genkin%a[i]
    if kabu>0:
        genkin+=kabu*a[n-1]
    return genkin

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)

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

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

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())

memo = {}
s = set()

ans = 0
def dfs(idx, money, stocks):
    global ans
    if idx==N:
        ans = max(ans, money)
        return
    a = As[idx]
    if (idx, stocks) in s:
        if money<=memo[(idx, stocks)]:
            return
    s.add((idx, stocks))
    memo[(idx, stocks)] = money
    dfs(idx+1, money+stocks*a, 0)
    dfs(idx+1, money, stocks)
    cnt = money//a
    dfs(idx+1, money-cnt*a, stocks+cnt)

dfs(0, 1000, 0)
print(ans)

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

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools









N= int(input())
A = [int(i) for i in input().split()]
ans = 1000
stock = 0

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

if(stock != 0):
  ans += stock*A[N-1]


print(ans)

Solution (Python):

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



flag=0
start=a[0]
b=[]

for i in range(1,n):
    
    if flag==0:
        if start>a[i]:
            start=a[i]
        elif start<a[i]:
            end=a[i]
            flag=1
    if flag==1:
        if end<=a[i]:
            end=a[i]
            if i==n-1:
                b.append([start,end])
        elif end>a[i]:
            b.append([start,end])
            start=a[i]
            flag=0

saifu=1000

for (s,e) in b:
    saifu=saifu+(e-s)*(saifu//s)
    

print(saifu)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    shojikin = 1000
    mochikabu = 0

    result = []
    for idx, a in enumerate(A):
        if idx == N-1:
            break
        if A[idx] <= A[idx+1]:
            
            available = shojikin // A[idx]
            mochikabu += available
            shojikin -= available * A[idx]

        elif A[idx] > A[idx+1]:
            
            shojikin += mochikabu * A[idx]
            mochikabu = 0

    shojikin += mochikabu * A[-1]
    print(shojikin)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

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


a = 1000
k = 0

for i in range(n-1):
    if L[i]<L[i+1] and k == 0:
        k = a//L[i]
        a = a-L[i]*k
    elif L[i]>L[i+1] and k > 0:
        a = a + L[i]*k
        k = 0
else:
    if k > 0:
        a = a + L[n-1]*k

print(a)

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
money = 1000
stock = 0
i = 0
while i < N - 1:
    if A[i + 1] > A[i]:
        
        stock += money // A[i]
        money -= (money // A[i]) * A[i]
        
        
    if A[i + 1] < A[i]:
        money += stock * A[i]
        stock = 0
    if i == N - 2 and stock > 0:
        money += stock * A[-1]
        stock = 0

    
    i += 1
print(money)

Solution (Python):
n = int(input())
a = [300]
a += list(map(int,input().split()))
a += [0]


d = []
for i in range(1,len(a)-1):
  if a[i-1] >= a[i] < a[i+1]:
    d.append((a[i],-1))
  elif a[i-1] <= a[i] > a[i+1]:
    d.append((a[i],1))

m = 1000
k = 0
for x,y in d:
  c = m//x
  if y == -1:
    k += c
    m -= x*c
  else:
    m += k*x
    k = 0
  
print(max(m,1000))

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

cnt = 0
money = 1000
stock = 0
for i in range(N-1):
    if A[i] == A[i+1]:
        pass
    elif A[i] < A[i+1]:
        tmp = money // A[i]
        stock += tmp
        money -= tmp * A[i]
    else:
        money += stock * A[i]
        stock = 0

money += stock * A[-1]

print(money)

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007




def main():
	N = inputInt()
	A = inputList()

	katamuki = []
	for i,val in enumerate(A):
		if i == 0:
			continue
		if val < A[i-1]:
			katamuki.append("down")
		elif val > A[i-1]:
			katamuki.append("up")
		else:
			katamuki.append("to")

	ans = 1000
	for i,val in enumerate(A):
		if i == len(A)-1:
			break
		if val <= ans:
			if katamuki[i] == "up":
				tmp = ans // val
				ans = ans - (val * tmp)
				ans = ans + (tmp * A[i+1])

	print(ans)





if __name__ == "__main__":
	main()

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

    up = False
    money = 1000
    has = 0  
    if A[0] < A[1]:
        v = money//A[0]
        has += v
        money -= v*A[0]
        up = True
    pre = A[0]
    for a in A[1:]:
        if not up:
            if pre >= a:
                pass
            else:
                
                v = money//pre
                has += v
                money -= v*pre
                up = True
        else:
            if pre <= a:
                pass
            else:
                
                money += has*pre
                has = 0
                up = False
        pre = a
    money += has*A[-1]
    print(money)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
lst=tuple(map(int,input().split()))

sm=1000
stock=0


token=0
for i in range(n-1):
    if token==0:
        if lst[i+1]<=lst[i] : continue 
        stock=sm//lst[i]       
        sm=sm%lst[i]
        token=1
    else :
        if lst[i+1]>=lst[i] : continue
        sm+=stock*lst[i]
        stock=0
        token=0

if token==1 :
    sm+=stock*lst[n-1]

print(sm)

Solution (Python):

import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()

    kabu_price = LI()

    cash = 1000
    kabu = 0

    for i in range(N - 1):
        ratio = kabu_price[i + 1] / kabu_price[i]

        if ratio == 1:
            continue

        if ratio < 1:
            if kabu > 0:
                cash += kabu * kabu_price[i]
                kabu = 0

        if ratio > 1:
            if cash >= kabu_price[i]:
                num_parcha = cash // kabu_price[i]
                kabu += num_parcha
                cash = cash % kabu_price[i]

    cash += kabu * kabu_price[-1]

    print(cash)


main()

Solution (Python):
"""
同じ日に購入オペレーションと売却オペレーションを行う意味がない。
買うときは、買えるだけ買う方がよい。
売る時は売れるだけ売る方がよい。

まず１日目、株を買うかどうか判断する。
翌日以降の連続する日付の中で、
"""

N = int(input())
A = list(map(int,input().split()))
M = 1000
stock = 0
buyPrice = 0
for i in range(N):
    if stock and A[i] >= buyPrice:
        
        if i==N-1 or A[i] >= A[i+1]:
            M += stock*A[i]
            stock = 0
    else:
        
        if i!=N-1 and A[i] < A[i+1]:
            stock += M//A[i]
            M %= A[i]
            buyPrice = A[i]
print(M)

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

if __name__ == '__main__':
    main()

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

m = 1000
s = 0

mx = [0] * n
for i in range(1, n):
    if a[i-1] < a[i]:
        mx[i] = 1
        mx[i-1] = 0
        
mn = [0] * n
for i in range(1, n):
    if a[i-1] >= a[i]:
        mn[i] = 1
        mn[i-1] = 0
        
if a[0] < a[1]:
    mn[0] = 1
    
mn[-1] = 0

for i in range(n):
    if mx[i] == 1:
        m += s * a[i]
        s = 0
    if mn[i] == 1:
        s += m//a[i]
        m -= m//a[i] * a[i]

print(m)

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

die = True
for i in range(N-1):
    if A[i] < A[i+1]:
        die = False
        break
if die:
    print(1000)
    exit()

ud = [(0, A[0])]
for i in range(N-1):
    if A[i] < A[i+1]:
        ud.append((1, A[i+1]))
    elif A[i] > A[i+1]:
        ud.append((-1, A[i+1]))

hold = 0
hand = 1000
for i in range(len(ud)-1):
    if ud[i][0] == 0 and ud[i+1][0] == 1:
        buy = hand // ud[i][1]
        hold += buy
        hand -= buy * ud[i][1]
    if ud[i][0] == 0 and ud[i+1][0] == -1:
        pass
    if ud[i][0] == -1 and ud[i+1][0] == 1:
        buy = hand // ud[i][1]
        hold += buy
        hand -= buy * ud[i][1]
    if ud[i][0] == -1 and ud[i+1][0] == -1:
        pass
    if ud[i][0] == 1 and ud[i+1][0] == 1:
        pass
    if ud[i][0] == 1 and ud[i+1][0] == -1:
        hand += hold * ud[i][1]
        hold = 0
if hold:
    hand += hold * ud[-1][1]
    hold = 0
print(hand)

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007




def main():
	N = inputInt()
	A = inputList()

	katamuki = []
	for i,val in enumerate(A):
		if i == 0:
			continue
		if val < A[i-1]:
			katamuki.append("down")
		elif val > A[i-1]:
			katamuki.append("up")
		else:
			katamuki.append("to")

	ans = 1000
	for i,val in enumerate(A):
		if i == len(A)-1:
			break
		if val <= ans:
			if katamuki[i] == "up":
				tmp = ans // val
				ans = ans - (val * tmp)
				ans = ans + (tmp * A[i+1])

	print(ans)





if __name__ == "__main__":
	main()

Solution (Python):
n, *A = map(int, open(0).read().split())
s = 1000
t = 0
for a0, a1 in zip(A, A[1:]):
    s += t*a0
    t = 0
    if a0 < a1:
        t = s // a0
        s -= t*a0
if t:
    s += t*a1
print(s)

Solution (Python):
import math

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

    stock_n=0
    money_n=1000

    for i in range(N-1):
        if A[i+1]>A[i] and stock_n==0:
            stock_n=money_n//A[i]
            money_n=money_n%A[i]
        elif A[i+1]<A[i] and stock_n!=0:
            money_n+=stock_n*A[i]
            stock_n=0

    if stock_n!=0:
        money_n+=stock_n*A[N-1]

    print(money_n)



if __name__=="__main__":
    main()

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

ret1 = []
ret2 = []

idx = 0
c = 1
while idx < n-1:
    if c and a[idx] < a[idx + 1]:
        ret1.append(a[idx])
        c = not(c)
    if not(c) and a[idx] > a[idx+1]:
        ret2.append(a[idx])
        c = not(c)
    idx += 1
if not(c):
    ret2.append(a[idx])
ans = 1000
for i in range(len(ret1)):
    ans += (ret2[i]-ret1[i]) * (ans//ret1[i])

print(ans)

Solution (Python):
N = int(input())
l = list(map(int,input().split(" ")))
DP = [1000]*N
for i in range(N):
  for j in range(i+1,N):
    t = DP[i]//l[i]
    DP[j] = max(DP[j-1],(DP[i]-l[i]*t)+l[j]*t)
print(DP[-1])

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

die = True
for i in range(N-1):
    if A[i] < A[i+1]:
        die = False
        break
if die:
    print(1000)
    exit()

ud = [(0, A[0])]
for i in range(N-1):
    if A[i] < A[i+1]:
        ud.append((1, A[i+1]))
    elif A[i] > A[i+1]:
        ud.append((-1, A[i+1]))

hold = 0
hand = 1000
for i in range(len(ud)-1):
    if ud[i][0] == 0 and ud[i+1][0] == 1:
        buy = hand // ud[i][1]
        hold += buy
        hand -= buy * ud[i][1]
    if ud[i][0] == 0 and ud[i+1][0] == -1:
        pass
    if ud[i][0] == -1 and ud[i+1][0] == 1:
        buy = hand // ud[i][1]
        hold += buy
        hand -= buy * ud[i][1]
    if ud[i][0] == -1 and ud[i+1][0] == -1:
        pass
    if ud[i][0] == 1 and ud[i+1][0] == 1:
        pass
    if ud[i][0] == 1 and ud[i+1][0] == -1:
        hand += hold * ud[i][1]
        hold = 0

if hold:
    hand += hold * ud[-1][1]
    hold = 0

print(hand)

Solution (Python):
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
count = 0
ans = "No"

N = i()
A = l()

M = 1000
S = 0
P = 0

for i in range(1,N):
    
    
    if A[i-1] < A[i]:
        S += M // A[i-1]
        P = A[i-1]
        if M - S*P >= 0:
            M -= S*P
    elif A[i-1] > A[i]:
        M += S*A[i-1]
        S = 0
    
if S > 0:
    M += S*A[N-1]
print(M)

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]:
        continue
    elif A[i] > A[i + 1]:
        money += A[i] * stock
        stock = 0
    elif A[i] < A[i + 1]:
        buy = money // A[i]
        money -= A[i] * buy
        stock += buy
money += A[N - 1] * stock
stock = 0

print(money)

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

updown = [0] * (N-1)
for i in range(N-1):
  if A[i] > A[i+1]:
    updown[i] = 0
  elif A[i] == A[i+1]:
    updown[i] = 1
  else:
    updown[i] = 2
    
down = 0
up = -1
for i in range(N-1):
  if updown[i]==0:
    if up!=-1:
      ans += (ans // A[down]) * (A[up] - A[down])
      up = -1
    down = i + 1
    
  elif updown[i]==2:
    up = i + 1

if up!=-1:
  ans += (ans // A[down]) * (A[up] - A[down])
print(ans)

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

A1 = []
for i in range(1, N):
    a = A[i] - A[i-1]
    A1.append(a)
A1.append(A1[N-2])

for i in range(N):
    if A1[i] > 0:
        b = total // A[i]
        total -= A[i] * b
        kabu += b
    elif A1[i] < 0:
        total += kabu * A[i]
        kabu = 0
    else:
        continue
    
total += kabu * A[N-1]
print(total)

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

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
D=[]
for i in range(N-1):
  if A[i]<A[i+1]:
    D.append((A[i],A[i+1]))
if len(D)==0:
  print(1000)
  exit()
money=1000
for a,b in D:
  k=money//a
  nokori=money-a*k
  money=nokori+k*b
print(money)

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

t = [-1] * (N + 1)
t[0] = 1000
for i in range(N):
    t[i + 1] = max(t[i + 1], t[i])
    k = t[i] // A[i]
    y = t[i] % A[i]
    for j in range(i + 1, N):
        t[j + 1] = max(t[j + 1], k * A[j] + y)
print(t[N])

Solution (Python):
import sys
import numpy as np

def Ss():
  return sys.stdin.readline().rstrip().split(' ')

def Is():
  ss = Ss()
  return map(int, ss) if len(ss) > 1 else int(ss[0])

money = 1000
n = Is()
prices = [*Is()]

for today, tomorrow in zip(prices, prices[1:] + [0]):
  buyable_stock_count = int(money / today)
  new_money = money + buyable_stock_count * (tomorrow - today)
  if new_money > money:
    money = new_money

print(money)

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

money = 1000
kabu = 0
p = [1]*N

for i in range(N-1):
  if A[i] < A[i+1]:
    p[i] = 0
  elif A[i] > A[i+1]:
    p[i] = 2

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

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

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

import itertools
a = [k for k, v in itertools.groupby(a)]

a.insert(0,1000)
a.append(1)

now_y=1000
now_k=0

for i in range(1,len(a)-1):
    if a[i-1] < a[i] < a[i+1]:
        continue
    if a[i-1] > a[i] > a[i+1]:
        continue

    if a[i-1] < a[i] > a[i+1]: 
        now_y += now_k * a[i]
        now_k=0
        ans=max(ans,now_y)

    if a[i-1] > a[i] < a[i+1]: 
        kabu = now_y // a[i]
        now_k += kabu
        now_y-=kabu*a[i]

print(ans)

Solution (Python):
from math import sqrt,ceil,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())




n=ii()
a=li()
i=0
ans=1000
while(i<n-1):

    while(i<n-1 and a[i]>=a[i+1]):
        i+=1
    if(i==n-1):
        break
    j=i
    i+=1
    while(i<n and a[i]>=a[i-1]):
        i+=1
    x1=ans//a[j]
    ans%=a[j]
    ans+=(x1*a[i-1])
print(ans)

Solution (Python):

import sys
rl = sys.stdin.buffer.readline

N = int(rl())
AS = tuple(map(int, rl().split()))

posession = 1000
for i in range(N-1):
  if AS[i] < AS[i+1]:
    quantity = posession // AS[i]
    posession = (posession - quantity * AS[i]) + quantity * AS[i+1]

print(posession)