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 sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().split())) + [0]
    
    B = []
    left, right = 0, 1
    while right != N + 2:
        if A[left:right] == list(sorted(A[left:right])):
            right += 1
        else:
            B.append(A[left:right - 1])
            left = right - 1
    
    score = 1000
    for a_lr in B:
        if len(a_lr) == 1:
            continue
        stock, score = divmod(score, a_lr[0])
        score += stock * a_lr[-1]
    print(score)


if __name__ == '__main__':
    solve()

Solution (Python):


import sys
import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        for line in sys.stdin:
            lines.append(line.strip())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    

    n = int(lines[0])
    a = [int(e) for e in lines[1].split(' ')]

    return (n, a)


def solve(n, a):

    b = []
    for i in range(1, n):
        b.append(a[i] - a[i-1])
    
    current_money = 1000
    current_stock = 0
    for i in range(0, n):
        if i < n - 1 and b[i] >= 0:
            if current_stock == 0:
                current_stock = current_money // a[i]
                current_money = current_money - current_stock * a[i]
        else:
            if current_stock > 0:
                current_money = current_money + current_stock * a[i]
                current_stock = 0

        if debug:
            log("i=%d, current_money=%d, current_stock=%d" % (i, current_money, current_stock))

    if current_stock > 0:
        current_money = current_money + current_stock * a[n-1]


    return current_money

def main():
    
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':
  
    main()

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

stack = 0
ans = 1000

Flag = False
i,j  = 0, 0

while i < n-1:
    if a[i] < a[i+1]:
        stack = ans // a[i]
        ans -= stack*a[i]
        j = i
        while j < n-1:
            if a[j] >= a[j+1]:
                ans += stack*a[j]
                stack = 0
                i = j
                Flag = True
                break
            j += 1

    if Flag:
        Flag = False
        continue

    i += 1

if a[-2] < a[-1]:
    ans += stack*a[-1]

print(ans)

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

money = 1000
min_val = 10000

for i in range(N):
    if (A[i] < min_val):
        min_val = A[i]

    if (A[i] > min_val):
        money += (money // min_val) * (A[i] - min_val)
        min_val = A[i]

print(money)

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

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

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

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

money = 1000
kabu = 0
toku = 0
min_val = 10000

for i in range(N):
    if (A[i] < min_val):
        min_val = A[i]

    if (A[i] > min_val):
        money += (money // min_val) * (A[i] - min_val)
        min_val = A[i]

print(money)

Solution (Python):

import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
A=list(map(int,input().split()))
edge=[[] for _ in range(N)]
for i in range(N-1):
    edge[i].append((i+1,1,1))
for i in range(N):
    for j in range(i+1,N):
        if A[i]<A[j]:
            edge[i].append((j,A[i],A[j]))
que=deque([0])
d=[0]*N
d[0]=1000
while que:
    v=que.popleft()
    for nv,be,af in edge[v]:
        tmp=d[v]//be
        if d[nv]<d[v]-tmp*be+tmp*af:
            d[nv]=d[v]-tmp*be+tmp*af
            que.append(nv)
print(d[-1])

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

 

def maxProfit(prices) -> int:
    if len(prices) < 2:
        return 0
    ans = 1000
    for i in range(1,len(prices)):
        if prices[i] > prices[i - 1]:
            stock = ans // prices[i - 1]
            ans += stock * (prices[i] - prices[i - 1]) 
    return ans

def main():
    n = int(input().strip())
    A = [int(i) for i in input().strip().split()]

    ans = maxProfit(A)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from math import sqrt, gcd, ceil, log
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))

def main():
	n = int(input()); arr = read()
	stocks = 0; money = 1000
	j = n-1
	while j > 0 and arr[j] <= arr[j-1]:
		j -= 1
	for i in range(j):
		if arr[i] < arr[i+1] and stocks == 0:
			stocks += money//arr[i]
			money = money%arr[i]
		elif arr[i] > arr[i+1] and stocks:
			money += stocks*arr[i]
			stocks = 0

	
	if stocks:
		money += arr[j]*stocks

	print(money)







if __name__ == "__main__":
	main()

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
cnt=0
w=1000
for i in range(len(A)):
    if i!=len(A)-1:
        if A[i]<A[i+1]:
            cnt+=w//A[i]
            w=w%A[i]
        if A[i]>A[i+1]:
            w+=cnt*A[i]
            cnt=0
    else:
        w+=cnt*A[i]
print(max(n,w))

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

Solution (Python):

N= int(input())
A= list(map(int,input().split()))
A_s = [0]
B = 1000
C = 0
B_1 = 0
C_1 = 0
for j in range(N-1):
    A_s.append(A[j+1]-A[j])


for i in range(N-1):
    if A_s[i+1] > 0:
        B_1 = B - (B // A[i])*A[i]
        C_1 = C + B // A[i]
        B = B_1
        C = C_1
    elif A_s[i+1] <= 0:
        if C > 0:
            B_1 = B + C * A[i ]
            C = 0
            B = B_1
        else:
            pass
    else:
        pass

if C > 0:
    B = B +C*A[N-1]

print(B)

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

B = []

def buy(m, s, a):
    t = m//a
    return m-t*a, s+t

def sell(m, s, a):
    return m+s*a, 0

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

B.append(1)

flag = True

MAX = 1000

for i in range(n-1):
    if flag and B[i] == 1:
        money, stock = buy(money, stock, A[i])
        flag = False
        MAX = max(money, MAX)
        
    elif not flag and B[i] == -1:
        money, stock = sell(money, stock, A[i])
        flag = True
        MAX = max(money, MAX)
        

money, stock = sell(money, stock, A[n-1])
MAX = max(money, MAX)


print(MAX)

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

Solution (Python):

import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
A=list(map(int,input().split()))
edge=[[] for _ in range(N)]
for i in range(N-1):
    edge[i].append((i+1,1,1))
for i in range(N):
    for j in range(i+1,N):
        if A[i]<A[j]:
            edge[i].append((j,A[i],A[j]))
que=deque([0])
d=[0]*N
d[0]=1000
while que:
    v=que.popleft()
    for nv,be,af in edge[v]:
        tmp=d[v]//be
        if d[nv]<d[v]-tmp*be+tmp*af:
            d[nv]=d[v]-tmp*be+tmp*af
            que.append(nv)
print(d[-1])

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 += int(money/A[i])
    money -= A[i]*int(money/A[i])
  if A[i]>A[i+1]:
    money += A[i]*stock
    stock = 0
money += A[n-1]*stock
print(money)

Solution (Python):
import sys

input = sys.stdin.readline

def main():
    ans = 1000
    s = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = [""]*n
    for i in reversed(range(n)):
        if a[i]>a[i-1]:
            b[i]="sell"
        elif a[i]<=a[i-1]:
            b[i]="buy"
    if a[0] <= a[1]:
      b[0] = "buy"
    elif a[0] > a[1]:
      b[0] = "sell"
    for i in range(n):
        if b[i] == "sell" and (i==n-1 or b[i+1] != "sell"):
            ans += s*a[i]
            s = 0
        elif b[i] == "buy" and i != n-1 and b[i+1] != "buy":
            s += ans//a[i]
            ans -= ans//a[i]*a[i]
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
money=1000
tip=0
buy=[]
k=0
flag=0
while N-1>k:
    if flag==0:
        for i in range(k,N-1):
            if a[i]<a[i+1]:
                tip=money//a[i]
                money-=a[i]*tip
                buy.append(a[i])
                k=i
                flag=1
                break
            k=i+1
    elif flag==1:
        for i in range(k,N-1):
            if a[i]>a[i+1]:
                money+=tip*a[i]
                tip=0
                buy.pop(0)
                k=i
                flag=0
                break
            k=i+1
print(money+tip*a[N-1])

Solution (Python):
n=int(input())
ls=list(map(int,input().split()))
c=sorted(ls)
mini=c[0]
maxi=c[-1]
lb=0
k=1000
st=0
pmin=c.index(mini)
pmax=c.index(maxi)
for i in range(n):
  if i<n-1:
    if ls[i]>ls[i+1]:
      k+=ls[i]*st
      st=0
    elif ls[i]<ls[i+1]:
      st+=k//ls[i]
      k=k-ls[i]*(k//ls[i])
      lb=ls[i]
  else:     
    if lb<ls[i]:
      k+=ls[i]*st
      st=0
k+=lb*st
print(k)

Solution (Python):


n = int(input())
a = list(map(int,input().split()))
money = 1000
have = 0
buy = 0
c = []
check = a[0]
start = False
check2 = -1
for i in range(1,n):
    if check2 > a[i] and start:
        c.append((check,check2))
        check = a[i]
        check2 = -1
        
    elif check >= a[i] and start:
        check = a[i]
    elif check2 <= a[i] and start:
        check2 = a[i]
    elif check < a[i] and not start:
        check2 = a[i]
        start = True
    elif check >= a[i] and not start:
        check = a[i]
    
if check2 != -1 and check != check2:
    c.append((check,check2))

for i in c:
    have = money//i[0]
    money -= i[0]*have
    money += have * i[1]
print(money)

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

mon=1000
for i in range(1,N):
    if A[i]>A[i-1]:
        mon=mon//A[i-1]*A[i]+mon%A[i-1]

print(mon)

Solution (Python):
N = int(input())
A = list(map(int,input().split( )))
def f(tail,head):
    if tail < head:
        return 1
    elif tail > head:
        return -1
    else:
        return 0

actions = ['stay' for _ in range(N)]

if N > 2:
    i = 0
    while f(A[i],A[i+1]) == 0:
        i += 1
        if i+1 == N-1:
            break
    if f(A[i],A[i+1]) == 1:
        actions[i] = 'buy'
    else:
        actions[i] = 'stay'
    before = f(A[i],A[i+1])
    for j,(tail,head) in enumerate(zip(A[i+1:],A[i+2:])):
        now = f(tail,head)
        if now == before:
            before = now
        elif now == 0:
            before = before
        else:
            if before == -1:
                actions[j+i+1] = 'buy'
            else:
                actions[j+i+1] = 'sell'
            before = now

else:
    if f(A[0],A[1]) == 1:
        actions[0] = 'buy'
actions[-1] = 'sell'

def action(stock,money,a,price):
    if a == 'stay':
        return stock,money
    elif a == 'buy':
        stock += money//price
        money = money%price
        return stock,money
    else:
        money += stock*price
        stock = 0
        return stock,money
    
stock = 0
money = 1000
for a,price in zip(actions,A):
    stock,money = action(stock,money,a,price)
print(money)

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

if a == sorted(a,reverse=True):
    print(x)

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

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

ans = [1000, 0]

for i in range(n-1):
    if a_list[i] < a_list[i+1]:
        
        ans[1] = ans[1] + ans[0] // a_list[i]
        ans[0] = ans[0] - (ans[0] // a_list[i]) * a_list[i]
        
        ans[0] = ans[0] + ans[1] * a_list[i+1]
        ans[1] = 0
    elif a_list[i] > a_list[i+1]:
        
        ans[0] = ans[0] + ans[1] * a_list[i]
        ans[1] = 0
        
        ans[1] = ans[0] // a_list[i+1]
        ans[0] = ans[0] - (ans[0] // a_list[i+1]) * a_list[i+1]

print(ans[0] + ans[1]*a_list[-1])

Solution (Python):
ma = lambda :map(int,input().split())
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
def tsune(pal,R,pre):
    c = 0
    ud = [0 for i in range(n)]
    for i in range(n):
        r = R[i]
        if pal*r > pal*pre:
            pal*=-1
            ud[i] = -pal
        pre =r
    return  ud

def pe(pre):
    ud = [0 for i in range(n)]
    for i in range(n-1):
        if pre >= A[i] and A[i] <= A[i+1]:
            ud[i] = -1
        if pre <= A[i] and A[i] >= A[i+1]:
            ud[i] = 1
        pre = A[i]
    if A[-1] < A[-2]:
        ud[-1] = 0
    if A[-1] > A[-2]:
        ud[-1] = 1
    return ud


def tot(ud):
    tmp = 1000
    kab = 0
    for i in range(n):
        if ud[i] ==-1: 
            if kab==0:
                kab,tmp = divmod(tmp,A[i])
        elif ud[i] ==1: 
            tmp +=kab*A[i]
            kab = 0


    return tmp
n = ni()
A = list(ma())
ans = 0
ud = pe(1000)
tmp = tot(ud)
ans = max(ans,tmp)
ud = pe(0)
tmp = tot(ud)
ans = max(ans,tmp)
print(ans)

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

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
    else:
        pass
money += stock * a[-1]
print(money)

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

up = True
b = []
for i in range(n):
    if up and a[i] < a[i+1] or not up and a[i] > a[i+1]:
        b.append(a[i])
        up = not up

m = 1000
for i in range(0, len(b), 2):
    v = m//b[i]
    m += (b[i+1]-b[i])*v
print(m)

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

for i in range(n):
    top_nxt = -1
    bot_nxt = -1
    for j in range(i+1, n):
        if a[i] < a[j]:
            top_nxt = j
            break
    for j in range(i+1, n):
        if a[i] > a[j]:
            bot_nxt = j
            break
    if top_nxt == -1 and bot_nxt == -1:
        yen += kabu*a[i]
        kabu = 0
    elif top_nxt == -1:
        yen += kabu*a[i]
        kabu = 0
    elif bot_nxt == -1:
        tmp = yen//a[i]
        kabu += tmp
        yen -= tmp*a[i]
    elif top_nxt < bot_nxt:
        tmp = yen//a[i]
        kabu += tmp
        yen -= tmp*a[i]
    elif bot_nxt < top_nxt:
        yen += kabu*a[i]
        kabu = 0
if kabu:
    yen += kabu*a[-1]
print(yen)

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

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

    elif i == n:
        if kab != 0:
            mon += kab * a[n-1]
            kab = 0
    else:
        if a[i-2] < a[i-1]:
            mon += kab * a[i-1]
            kab = 0
        if a[i-1] < a[i]:
            kab = int(mon/a[i-1])
            mon = mon%a[i-1]


print(mon)

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

money = 1000
stock = 0

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

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

kai = [False] * N
uri = [False] * N

i = 0



tmp = A[0]
flag = True
for i in range(1, N):
    if A[i-1] < A[i]:
        flag = False

if flag:
    print(1000)
    sys.exit(0)

start = 10000
stop = 10000

for i in range(1, N):
    if A[i-1] < A[i] and stop != i-1:
        start = i-1
        stop = i
    elif A[i-1] < A[i] and stop == i-1:
        stop = i
    elif A[i-1] >= A[i] and start != 10000 and stop != 10000:
        kai[start] = True
        uri[stop] = True
        start = 10000
        stop = 10000
    if i == N-1 and start != 10000 and stop != 10000:
        kai[start] = True
        uri[stop] = True
cnt = 0
kingaku = 1000
for i in range(N):
    if kai[i]:
        cnt = kingaku // A[i]
        kingaku -= A[i] * cnt
    if uri[i]:
        kingaku += A[i] * cnt
        cnt = 0
    if i == N-1:
        kingaku += A[i] * cnt
print(kingaku)

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

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

money = 1000
kabu = 0
for i in range(a):
    if i == a - 1:
        if b[i] >= b[i-1]:
            money += b[i] * kabu
            kabu = 0              
    elif b[i] < b[i + 1]:
        kabu += int(money / b[i])
        money %= b[i]
    elif b[i] > b[i + 1]:
        money += b[i] * kabu
        kabu = 0
        
print(str(money))

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


print(max(1000,start))

Solution (Python):




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    
    
    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    maxyen=0
    for sss in range(n):
        i=sss
        yen=1000
        kab=0
        while i < n:
            now=arr[i]
            nxt=999
            minxxx=99999
            for j in range(i+1,n):
                if arr[j]>now:
                    nxt=j
                    break
                if arr[j]==now:
                    continue
                
                now=arr[j]
                minxxx=99999
            if nxt < 999 and minxxx==99999:
                kabu = yen // now
                yen = yen % now
                yen += kabu * arr[nxt]
                kabu=0
            if nxt < 999 and minxxx<99999:
                kabu = yen // minxxx
                yen = yen % minxxx
                yen += kabu * arr[nxt]
                kabu=0

            i=nxt
        maxyen=max(maxyen,yen)
    print(maxyen)
    
    

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
N = int(input())
prices = [ int(v) for v in input().split(" ") ]

money = 1000
stock = 0
p_price = float("inf")

for i in range(N):
  c_price = prices[i]
  
  
  if c_price > p_price:
    money += int(c_price * stock)
    stock = 0

  
  for j in range(i+1, N):
    if prices[j] > c_price:
      stock += int(money / c_price)
      money -= int(c_price * stock)
      p_price = c_price
      break
    elif prices[j] <= c_price:
      break 
      
  
print(money)

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

mon = 1000
stock = 0

lim_a = [a_list[0]]
for i in range(1, N-1):
    if lim_a[-1] == a_list[i]:
        continue
    if a_list[i] <= a_list[i+1] and a_list[i] <= a_list[i-1]:
        lim_a.append(a_list[i])
    elif a_list[i] >= a_list[i+1] and a_list[i] >= a_list[i-1]:
        lim_a.append(a_list[i])

if len(lim_a) >= 2:
    if lim_a[-2] <= lim_a[-1] <= a_list[-1]:
        lim_a[-1] = a_list[-1]
    elif lim_a[-2] >= lim_a[-1] >= a_list[-1]:
        lim_a[-1] = a_list[-1]
    else:
        lim_a.append(a_list[-1])
else:
    lim_a.append(a_list[-1])


for i in range(1, len(lim_a)):
    if lim_a[i-1] <= lim_a[i]:
        stock = mon // lim_a[i-1]
        mon += stock*(lim_a[i] - lim_a[i-1])
    
print(max(mon, 1000))

Solution (Python):
import sys
import math
sys.setrecursionlimit(int(1e6))

def dprint(*args):
  pass

money = 1000
stump = 0
n = list(map(int, input().split()))[0]
A = list(map(int, input().split()))
dprint('n = ', n)
dprint('A = ', A)

btm = A[0]
top = A[0]
for i in range(1, n):
  dprint('i = ', i)
  a_i = A[i]
  if (stump==0):
    
    if a_i <= btm:
      btm = a_i
    else:
      p = money // btm
      stump = stump + p
      money = money - (p*btm)
      top = a_i
      btm = a_i
      dprint('[buy] stump, money', stump, money)
  else:
    
    if a_i >= top:
      top = a_i
    else:
      money = money + (top*stump)
      stump = 0
      top = a_i
      btm = a_i
      dprint('[sell] stump, money', stump, money)

if (stump>0):
  money = money + (top*stump)
  dprint('[sell] stump, money', stump, money)

print(money)

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

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

money = 1000
stock = 0

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

print(money)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
money = 1000
stock = 0
tmp = [0]*(N-1)

for i in range(N-1):
    if A[i] == A[i+1]:
        tmp[i] = 0
    else:
        if A[i] < A[i+1]:
            tmp[i] = 1
        else:
            tmp[i] = -1

for i in range(N-1):
    if tmp[i] == 1:
        money += A[i]*stock
        stock = 0
        stock += money//A[i]
        money -= A[i]*stock
    if tmp[i] == -1:
        money += A[i]*stock
        stock = 0
    if tmp[i] == 0:
        continue
    


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

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

count = [1000,0]
x = a[0]
for i in range(1,n):
    if a[i] > x:
        d = count[0]//x
        count = [count[0]-d*x, d+count[1]]
    else:
        count = [count[0]+count[1]*x,0]
    x = a[i]
if count[1]:
    count[0] += count[1]*x
print(count[0])

Solution (Python):


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

cash = 1000
stock = 0

for i in range(N - 1):
    if (A[i] < A[i + 1]):
        num = cash // A[i]
        stock += num
        cash -= num * A[i]
    elif (A[i] > A[i + 1]):
        cash += stock * A[i]
        stock = 0
            
print(cash + A[-1] * stock)

Solution (Python):
import sys

n = int(input())
a_list = list(map(lambda x: int(x), input().split()))
possesion = 1000
num_stock = 0


for i in range(len(a_list) - 1):
    a_today = a_list[i]
    for a in a_list[i+1:]:
        a_next = a
        if a != a_today:
            break
    
    if a_next == a_today:
        possesion += num_stock * a_list[-1]
        print(possesion)
        sys.exit()
    
    if a_today < a_next:
        
        possesion_yesterday = possesion
        possesion = possesion % a_today
        num_stock += possesion_yesterday // a_today
    if a_today > a_next:
        
        possesion += num_stock * a_today
        num_stock = 0



possesion += num_stock * a_list[-1]
print(possesion)

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

Solution (Python):
n = int(input())
PriceList = list(map(int, input().split()))
poss = 1000
stock = 0
basis = PriceList[0]
for price in PriceList:
    if basis > price:
        basis = price
    if basis < price:
        
        stock = stock + poss // basis
        poss = poss % basis
        basis = price
        
        poss = poss + stock * price
        stock = 0
print(poss)

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

money = 1000
for i,a in enumerate(A):
  if i>0:
    a_current = A[i-1]
    if a_current < a:
      money = a*(money//a_current) + (money%a_current)
print(money)

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

def fun(N, As):
    stocks = 0
    money = 1000
    prev = 201
    for n in range(N):
        curr = As[n]
        if n < N - 1:
            nex = As[n + 1]
        else:
            nex = 99
        if curr >= prev and curr >= nex:
            
            money += curr * stocks
            stocks = 0
        elif curr <= prev and curr <= nex:
            
            stocks += int(money / curr)
            money -= curr * int(money / curr)
        prev = curr
    return money

if __name__ == '__main__':
    N, As = read()
    print(fun(N, As))

Solution (Python):
N = int(input())
lst = list(map(int, input().split()))
bs = [0]*N
stock = 0
money = 1000

for i in range(1, N):
    if lst[i-1] < lst[i]:
        bs[i-1] = 1
        if i > 1 and bs[i-2] != 0:
            bs[i-1] = -1

for i in range(N):
    if bs[i] == 1:
        stock = money // lst[i]
        money = money % lst[i]
    elif bs[i] == 0 and stock > 0:
        money += stock * lst[i]
        stock = 0
    elif bs[i] == -1 and i == N-1 and stock > 0:
        money += stock * lst[i]
        stock = 0

print(money)

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


current_assets = 1000
current_price = 0
min_price = 999
for i in range(n-1):
    if min_price > data[i]:
        min_price = data[i]

    if data[i] < data[i+1]:
        buy_counts = current_assets // min_price
        current_assets -= buy_counts * min_price

        current_assets += buy_counts * data[i+1]

        
        
        min_price = 999

print(current_assets)

Solution (Python):
import math

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

    
    
    

    List = [[0 for j in range(N)] for i in range(N)]

    for i in range(N):
        for j in range(i,N):
            List[i][j]=A[j]-A[i]

    buy=False
    Kabu=0
    Yen=1000
    for i in range(N):
        isUp=True
        UpCnt=0
        DownStart=0
        for j in range(i+1,N):
            if(A[j]>A[i]):
                UpCnt+=1
            else:
                if(UpCnt==0):
                    DownStart=j
                    isUp=False
                    break
        if(i+1==N):
            isUp=False
        if(isUp and i!=DownStart-1):
            if(buy==False):
                buy=True
                Kabu+=Yen//A[i]
                Yen-=A[i]*(Yen//A[i])
            
            
            
            

        else:
            if(buy):
                buy=False
                Yen+=A[i]*Kabu
                Kabu=0




    print(Yen)

resolve()

Solution (Python):
import sys

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]
stocks = [0] * N
money = 1000

for i in range(0, N):
    if i == N - 1:
        for j in range(0, i):
            money += stocks[j] * A[i]
            stocks[j] = 0
    elif A[i+1] < A[i]:
        for j in range(0, i):
            money += stocks[j] * A[i]
            stocks[j] = 0
    elif A[i+1] > A[i]:
        stocks[i], money = divmod(money, A[i])

print(money)