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())
A = list(map(int,input().split()))
money = 1000
kabu = 0
for i in range(N-1):
    if A[i+1] < A[i]:
        money += A[i] * kabu
        kabu = 0
    else:
        kabu += money//A[i]
        money -= A[i] * (money//A[i])
    
money += A[-1] * kabu


print(money)

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

stock = 0
money = 1000
for i in range(N-1):
    if A[i+1] >= A[i]:

        stock += money // A[i]
        money = money % A[i]
    else:

        money += stock*A[i]
        stock = 0
        


            

money += stock*A[N-1]
stock = 0 

print(money)

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

ans = 1000
k = 0
for i in range(len(A)-1):
    if A[i] < A[i+1] and (ans//A[i]) != 0:
        k = ans // A[i]
        ans = ans % A[i]
        ans += k * A[i+1]
        k = 0
    else:
        continue

print(ans)

Solution (Python):
import numpy as np

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

A_diff = np.diff(A)

wollet = 1000
stock = 0
for i, rate in enumerate(A_diff):
    if rate > 0:
        stock_num = wollet // A[i]
        wollet -= stock_num*A[i]
        stock += stock_num
    elif rate < 0:
        wollet += stock*A[i]
        stock = 0
        
wollet += A[N-1]*stock

print(wollet)

Solution (Python):


from math import floor
N = int(input())
A = list(map(int, input().split()))

total = 1000
for i in range(0, N - 1):
    if A[i] < A[i+1]:
        total += floor(total / A[i]) * ( A[i + 1] - A[i] )

print(total)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



N = INT()
A = LIST()

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

print(money)

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

up = True
points = []

for a0, a1 in zip(A, A[1:]):
    if up:
        if a0 < a1:
            up = False
            points.append(a0)
    else:
        if a0 > a1:
            up = True
            points.append(a0)

money = 1000
tickets = 0

for i in range(len(points)):
    if i % 2 == 1:
        money += points[i] * tickets
        tickets = 0
    if i % 2 == 0 and i + 1 < len(points):
        tickets = money // points[i]
        money -= tickets * points[i]

print(money)

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

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

total = 1000

for i in range(n-1):
    if a[i+1] > a[i]:
        num = total//a[i]
        total -= a[i] * num
        total += num * a[i+1]

print(total)

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

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

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

    else:
        M += kabu * A[i+1]
    print(M)


if __name__ == '__main__':
    main()

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

n = int(input())
a = list(map(int, input().split()))
a = [[i,0] for i in a]

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

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

kane += kabu * a[i][0]

print(kane)

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

money=1000
kabu=0

uru_t=[0]*n
kai_t=[0]*n

kai_flag=False
uri_flag=True

for i in range(n-1):
    tmp_v=a[i]
    for j in range(i+1,n):
        if tmp_v<a[j]:
            kai_flag=True
            break
    if a[i]<=a[i+1]:
        uri_flag=False

    if kai_flag:
        num_kabu=money//a[i]
        kabu+=num_kabu
        money-=num_kabu*a[i]

    if uri_flag:
        money+=kabu*a[i]
        kabu=0

    
    kai_flag=False
    uri_flag=True

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

Solution (Python):


import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy
import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputintlist(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloatlist(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

def to10(A):
    ret=0
    for a in range(len(A)-1,-1,-1):
        ret+=X[a]*2**(N-1-a)
    return ret


N=int(input())
A=inputintlist()
A.append(-inf)
num=A[0]
l=0
kane=1000
kabu=0
for a in range(0,N):
    if A[a]<=A[a+1]:
        pass
    else:
        kabu=kane//A[l]
        kane=kane%A[l]
        kane+=kabu*A[a]
        kabu=0
        l=a+1
    

print(kane)

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

print(money)

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

for i in range(len(a)-1):
    if a[i] >= a[i+1]:
        wallet += stock*a[i]
        stock = 0
    else:
        stock += wallet//a[i]
        wallet %= a[i]

wallet += stock*a[n-1]

print(wallet)

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

Solution (Python):
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
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()))

money = 1000
stock = 0

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

print(money)

Solution (Python):


n = int(input())
a_list = list(map(int, input().split()))
x = 1000
b_list = []
start_a = a_list[n - 1]
a = start_a
flag = 0
for i in range(2, len(a_list) + 1):
    if a_list[n - i] <= a:
        flag = 1
        a = a_list[n - i]
        if n - i == 0:
            b_list.append(start_a)
            b_list.append(a)
    else:
        if flag == 0:
            a = a_list[n - i]
            start_a = a
        else:
            b_list.append(start_a)
            b_list.append(a)
            a = a_list[n - i]
            start_a = a
b_list.reverse()
for i in range(len(b_list) // 2):
    c = x // b_list[2 * i]
    x -= b_list[2 * i] * c
    x += b_list[2 * i + 1] * c

print(x)

Solution (Python):
N = int(input())
A = [int(hoge) for hoge in input().split()]
Money = 1000
Kabu = 0
for n,a in enumerate(A):
    if n>0:
        if a>=A[n]:
            Money += a*Kabu
            Kabu = 0
        else:
            Kabu += Money//a
            Money %= a
    if n<N-1:
        if a>=A[n+1]:
            Money += a*Kabu
            Kabu = 0
        else:
            Kabu += Money//a
            Money %= a
Money += A[-1] * Kabu
print(Money)

Solution (Python):
def main():
    
    N=int(input())
    A=list(map(int,input().split()))
    t=[A[0]]
    bef=A[0]
    flag=1
    for i in range(1,N):
        if flag==1:
            if bef > A[i]:
                t.append(bef)
                bef=A[i]
                flag=-1
            else:
                bef=A[i]
        elif flag==-1:
            if bef < A[i]:
                t.append(bef)
                bef=A[i]
                flag=1
            else:
                bef=A[i]
    if flag==1:
        t.append(bef)
    money=1000
    while t:
        b=t.pop(0)
        a=t.pop(0)
        cn=money//b
        money=money%b
        money+=cn*a
    print(money)

main()

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

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

bs = []
b = True
for i in range(n-1):
    if b and a[i] < a[i+1]:
        bs.append(i)
        b = False
    elif not b and a[i] > a[i+1]:
        bs.append(i)
        b = True

if not b:
    bs.append(n-1)

m = 1000
for i in range(0, len(bs), 2):
    s = m // a[bs[i]]
    m += s * (a[bs[i+1]]-a[bs[i]])

print(m)

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

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

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

lst = []
small, large = 0, 0
for i in range(N):
    if i==0:
        small = A[i]
        large = A[i]
    else:
        if A[i] < large:
            if small < large:
                lst.append((small,large))
            small = A[i]
            large = A[i]
        else:
            large = A[i]
else:
    if small < large:
        lst.append((small,large))

ans = 1000
for s, l in lst:
    stock, ans = divmod(ans, s)
    ans += stock * l
print(ans)

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

for n in range(N-1):
  if A[n]<A[n+1]:
    M+=S*A[n]
    S=0
    S+=M//A[n]
    M-=S*A[n]
  elif A[n+1]<A[n]:
    M+=S*A[n]
    S=0

if S!=0:
  M+=S*A[-1]

print(M)

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

arr = [tmp[0]]

for i in range(1, n):
    if arr[-1] != tmp[i]:
        arr.append(tmp[i])

m, s = 1000, 0
for i in range(1, len(arr)):
    if arr[i - 1] < arr[i]:
        s += m // arr[i - 1]
        m -= m // arr[i - 1] * arr[i - 1]
        m += s * arr[i]
        s = 0

print(m)

Solution (Python):
n=int(input())
a=[float('Inf')]
a+=list(map(int,input().split()))
i=0
m=1000
while i<n:
    if a[i]>a[i+1]:
        s=a[i+1]
    if a[i]<a[i+1]:
        m=m//s*a[i+1]+m%s
        s=a[i+1]
    i+=1
print(m)

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

money = 1000
max_money = money

open_price = a[0]
close_price = a[0]

for i in range(1, n):
	if a[i] >= a[i - 1]:
		close_price = a[i]
	else:			
		
		
		if close_price > open_price:
			num = money // open_price
			left = money - num * open_price
			money = left + num * close_price
			max_money = max(money, max_money)
		open_price = a[i]
		close_price = 0
		

if close_price > open_price:
	num = money // open_price
	left = money - num * open_price
	money = left + num * close_price
	max_money = max(money, max_money)

print(max_money)

Solution (Python):
import sys
 
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))

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

print(m)

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

import bisect
import sys

sys.setrecursionlimit(10**6)


def bfs(s, n, node):
    
    check = [False for _ in range(n)]
    check[s] = True
    
    queue = deque([s])
    visited_num = 1

    
    
    color = [-1 for _ in range(n)]
    color[s] = 0

    while visited_num < n:
        
        if len(queue) == 0:
            
            
            return color

        now_vertex = queue.popleft()
        
        for next_vertex in node[now_vertex]:

            if check[next_vertex] == True:
                continue

            queue.append(next_vertex)
            check[next_vertex] = True

            
            color[next_vertex] = color[now_vertex]+1
            visited_num += 1

    return color


mod = 10**9+7



def ceil(x, y): return (x+y-1)//y
def input_list(): return list(map(int, input().split()))


def buy(buy_cost, sell_cost, money):
    return money % buy_cost+(money//buy_cost)*sell_cost


n = int(input())
a = input_list()

mon = 1000
i = 0
while i < n:
    
    for j in range(i+1, n):
        if a[j] > a[i]:
            mon = buy(a[i], a[j], mon)
            
            i = j-1
            break
        if a[j] <= a[i]:
            break
    i += 1


print(mon)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): 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()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n=k()
a =l()
first = 1000
for i in range(n-1):
    if a[i] > a[i+1]:
        stuck = a[cnt:i+1]
        b = first//min(stuck)
        first += (max(stuck)-min(stuck))*b
        cnt = i+1

stuck=a[cnt:]
b = first//min(stuck)
first += (max(stuck)-min(stuck))*b

print(first)

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]:
        tmp = m // a[i]
        k += tmp
        m -= tmp * a[i]
    else:
        m += k * a[i]
        k = 0
m += k * a[-1]
print(m)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
s = 1000
i = 0
j = 1
while True:
  if i+1 < n and a[i] < a[i+1]:
    if j+1 < n and a[j] < a[j+1]:
      j += 1
    elif j+1 < n and a[j] >= a[j+1]:
      s = (s//a[i])*a[j]+s%a[i]
      if j+2 == n:
        break
      else:
        i = j+1
        j += 2
    else:
      if a[i] < a[j]:
        s = (s//a[i])*a[j]+s%a[i]
        break
  elif i+1 < n and a[i] >= a[i+1]:
    i += 1
    j += 1
  else:
    break
print(s)

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

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

money += kanum * li[N-1]
print(money)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()
    A = in_nl() + [0]

    money = 1000
    kabu = 0

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

        

    print(money)


if __name__ == '__main__':
    main()

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

money = 1000
stack = 0

check = [0]*n

for i in range(n-1):
    if li[i] < li[i+1]:
        check[i] = 'up'
    elif li[i] == li[i+1]:
        check[i] = '-'
    else:
        check[i] = 'down'

for i in range(n):
    if check[i] == 'up':

        get_stack = money//li[i]
        money -= get_stack * li[i]
        stack += get_stack
        
    elif check[i] == '-':
        continue
    
    else:
        
        money += stack * li[i]
        stack = 0
        
print(money)

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


before = A[0]
raising = False
buy = []
sell = []
for i in range(1, len(A)):
  v = A[i]
  if before < v:
    if not raising:
      buy.append(i - 1)
      raising = True
  if v < before:
    if raising:
      sell.append(i - 1)
    raising = False
  before = v

m = 1000
k = 0
for i in range(N):
  v = A[i]
  if i in buy:
    k = m // v
    m = m - k * v
  if i in sell:
    m += k * v
    k = 0
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 + 1] > a[i]:
        buy = money // a[i]
        stock += buy
        money -= buy * a[i]
    if a[i + 1] < a[i]:
        money += stock * a[i]
        stock = 0
print(money + stock * a[n - 1])

Solution (Python):




def main():
    n = int(input())
    al = list(map(int, input().split()))
    
    buy_is = []
    sell_is = []

    if n == 2:
        if al[0] < al[1]:
            money = 1000
            buy_kabu = 1000//al[0]
            money -= (buy_kabu*al[0])
            money += al[1]*buy_kabu
            print(money)
        else:
            print(1000)
        exit()


    if al[0] < al[1]:
        buy_is.append(0)

    
    for i, (a0,a1,a2) in enumerate( zip(al[0:-2], al[1:-1], al[2:]) ):
        if a0 >= a1 and a1 < a2:
            buy_is.append(i+1)
        elif a0 <= a1 and a1 > a2:
            sell_is.append(i+1)


    money = 1000
    kabu = 0
    for i in range(n):
        if i in buy_is:
            buy_kabu = money//al[i]
            kabu += buy_kabu
            money -= (buy_kabu*al[i])
        elif i in sell_is or i == n-1:
            money += al[i]*kabu
            kabu = 0
    
    print(money)
    
    

if __name__ == "__main__":
    main()

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


if __name__ == '__main__':
    main()

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

cur_m = 1000
cur_s = 0

state = -1 
prev = 0
for ai in a:
  if state == -1:
    prev = ai
    state = 0
  elif state == 1:
    if prev > ai: 
      
      cur_m += (prev * cur_s)
      cur_s = 0
      state = 0
  else:
    if prev < ai:
      
      buy_num = cur_m // prev
      cur_s += buy_num
      cur_m -= prev * buy_num
      state = 1
  prev = ai

if cur_s > 0:
  cur_m += (a[-1] * cur_s)

print(cur_m)

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

money = 1000


def search_V(st):
    for j in range(st, n):
        if a[j] > a[j-1]:
            return j-1
    return -1

def search_A(st):
    for j in range(st, n):
        if a[j] < a[j-1]:
            return j-1
    return -1

i = 0
stock = 0
while i < n:
    
    V = search_V(i+1)
    if V == -1:
        break
    else:
        
        stock, money = divmod(money, a[V])
        
    
    A = search_A(V+1)
    if A == -1:
        
        money += stock * a[A]
        break
    else:
        money += stock * a[A]
    
    i = A

print(money)

Solution (Python):


import sys
input=sys.stdin.readline

def main():
    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]:
            m+=s*A[i]
            s=0
        else:
            n=int(m//A[i])
            m-=n*A[i]
            s+=n
    m+=s*A[-1]
    print(m)
    
if __name__=="__main__":
    main()

Solution (Python):

def main():
  n = int(input())
  A = list(map(int, input().split()))
  A.append(0)

  money = 1000
  kabu = 0
  for i in range(n):
    a = A[i]
    nxt = A[i + 1]

    if a < nxt:
      cnt = money // a
      money -= cnt * a
      kabu += cnt
    elif a > nxt:
      money += kabu * a
      kabu = 0

  print(money)


main()

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

l = []
flag = False
for i in range(N-1):
  if flag:
    if A[i] > A[i+1]:
      l.append(A[i])
      flag = False
  else:
    if A[i] < A[i+1]:
      l.append(A[i])
      flag = True
if flag:
  l.append(A[-1])

flag = False
ans = 1000
stock = 0
for n in l:
  if flag:
    ans += stock * n
    stock = 0
    flag = False
  else:
    stock = ans // n
    ans -= stock * n
    flag = True
print(ans)