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 math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
a = lr()
aa = []
pre = 0
for num in a:
    if num != pre:
        aa.append(num)
        pre = num
if len(aa) == 1:
    print(1000)
    sys.exit()

aaa = [aa[0]]
pre = aa[0]


spflag = False
if aa[0] < aa[1]:
    spflag = True

epflag = False

pflag = spflag
nnn = len(aa[1:])
for i,num in enumerate(aa[1:]):
    
    if pflag:
        if num < pre:
            aaa.append(pre)
            pflag = not pflag
            pre = num
        else:
            pre = num
    else:
        if num > pre:
            aaa.append(pre)
            pflag = not pflag
            pre = num
        else:
            pre = num

    if i == nnn-1:
        if pflag == True:
            epflag = True
        else:
            epflag = False
        aaa.append(num)
if not spflag:
    aaa = aaa[1:]
if not epflag:
    aaa.pop()

ret = 1000
kabu = 0
for i,num in enumerate(aaa):
    if i%2 == 0:
        kabu+=(ret//num)
        ret=ret%num
    else:
        ret+=kabu*num
        kabu=0
print(ret)

Solution (Python):
import sys
sys.setrecursionlimit(4100000)
import math
import itertools
INF = float('inf')
from heapq import heapify, heappop, heappush

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

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


if __name__ == '__main__':
    main()

Solution (Python):

def main():
    _ = int(input())
    A = list(int(c) for c in input().split(' '))

    tmp = []
    for i in range(len(A)-1):
        if A[i] != A[i+1]:
            tmp.append(A[i])
    if len(tmp) == 0:
        return 1000
    if A[-1] != tmp[-1]:
        tmp.append(A[-1])
    A = tmp
    

    money = 1000
    if len(A) < 2:
        return 1000

    if len(A) == 2:
        if A[0] < A[1]:
            kabu = money // A[0]
            money -= kabu*A[0]
            money += kabu*A[1]
            return money
        else:
            return 1000

    l = []
    kabu = 0
    if A[1] > A[0]:
        ascending = True
        kabu = money // A[0]
        money -= kabu*A[0]
    else:
        ascending = False
    for i in range(len(A)-1):
        
        if ascending:  
            if A[i] > A[i+1]:
                
                if kabu > 0:
                    money += kabu*A[i]
                    kabu = 0
                    ascending = False
                else:
                    pass
            else:
                continue
        else:  
            if A[i] < A[i+1]:
                
                assert kabu == 0
                kabu = money // A[i]
                money -= kabu*A[i]
                ascending = True
            else:
                continue
    if kabu > 0:
        money += kabu*A[-1]
        kabu = 0
    return money
if __name__ == "__main__":
    ret = main()
    print(ret)

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

now = 1000
kabu = 0
temp = 0


for i in range(n-1):
    if A[i+1] > A[i]:
        temp = now//A[i]
        kabu += now//A[i]
        now -= temp*A[i]
    elif A[i+1] == A[i]:
        continue
    else:
        if kabu == 0:
            continue
        else:
            now += A[i]*kabu
            kabu = 0

now += kabu*A[-1]

print(now)

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

flag = [0]*n
t = 1
for i in range(n-1):
  if t == 1:
    if a[i] >= a[i+1]: continue
    else:
      flag[i] = 1
      t = 2
  elif t == 2:
    if a[i] <= a[i+1]: continue
    else:
      flag[i] = 2
      t = 1

if t == 2:
  for i in range(n-1, -1, -1):
    if flag[i] == 1:
      flag[i] = 0
      break
    if a[i] < a[i-1]: continue
    flag[i] = 2
    break

ans = 1000
s = 0
for i in range(n):
  if flag[i] == 1:
    t = ans//a[i]
    ans%=a[i]
  if flag[i] == 2:
    ans += t*a[i]
    t = 0
print(ans)

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

kabu = 0
money = 1000

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

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


def prepare(A):
  B=[10000,A[0]]
  for i in range(1,len(A)):
    if B[-2]<B[-1] and B[-1]<=A[i]:
      B[-1]=A[i]
    elif B[-2]>B[-1] and B[-1]>=A[i]:
      B[-1]=A[i]
    else:
      B.append(A[i])
  return B

A=prepare(A)
money=1000
kabu=0
A.pop(0)

def buy(money,kabuka):
  kabu=money//kabuka
  money=kabu*kabuka
  return money,kabu

def sell(money,kabuka,kabu):
  money+=kabuka*kabu
  return money

while True:
  if len(A)==1:
    money=sell(money,A[0],kabu)
    break
  elif A[0]<A[1]:
    diffmoney,diffkabu=buy(money,A[0])
    money-=diffmoney
    kabu+=diffkabu
  elif A[0]>A[1]:
    money=sell(money,A[0],kabu)
    kabu=0
  A.pop(0)
print(money)

Solution (Python):
N=int(input())
A= list(map(int,input().split()))
maxi=max(A)
mini=min(A)
p = 1000
q = 0

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

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
N=int(input())
T=list(map(int,input().split()))
S=[]
S.append("")
for i in range(N-1):
    if T[i]==T[i+1]:
        S.append("E")
    elif T[i]>T[i+1]:
        S.append("D")
    else:
        S.append("U")
    



pos=1
ans=1000
stock=0

buyflag=False

while pos<N:
    if buyflag==False:
        if S[pos]=="U":
            stock=ans//T[pos-1]
            ans=ans-T[pos-1]*stock
            buyflag=True
    elif buyflag==True:
        if S[pos]=="D":
            ans=ans+T[pos-1]*stock
            stock=0
            buyflag=False
    
    pos+=1
ans+= stock*T[pos-1]

print(ans)

Solution (Python):



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

    splits = []
    j = 0
    i = 2
    while i < N:
        if A[i-2] <= A[i-1] and A[i-1] > A[i]:
            splits.append(A[j:i])
            j = i
            i += 2
        else:
            i += 1

    splits.append(A[j:])
    S = len(splits)

    if S == 0:
        if A[0] >= A[-1]:
            return 1000
        else:
            n = 1000 // A[0]
            return n * A[-1]

    money = 1000
    for xs in splits:
        n = money // min(xs)
        money = money % min(xs)
        money += n * xs[-1]

    return money

    
    
    
    
    
    
    


print(main())

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] and money >= A[i]:
        stock += (money // A[i])
        money %= A[i]
    elif A[i] > A[i + 1]:
        money += stock * A[i]
        stock = 0
money += (stock * A[N - 1])
print(money)

Solution (Python):
N = int(input())
prices = [int(n) for n in input().split()]
money = 1000
stock = 0
p_today = prices[0]
for n in range(N-1):
    p_tomm = prices[n+1]
    if p_tomm >= p_today:
        n_stock = money // p_today
        stock += n_stock
        money = money - p_today * n_stock
        
        
        
    else:
        money += p_today * stock
        stock = 0
    p_today = p_tomm

if stock > 0:
    money += prices[-1] * stock

print(money)

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

def solve(N, A):
    S = 0
    C = [A[0]]
    for i in range(1,N):
        if A[i] == C[-1]:
            continue
        C.append(A[i])
    A = C
    if len(C) == 1:
        print(1000)
        return
    C = [(A[0], '-' if A[0] <A[1] else '+')]

    for i in range(1,len(A)-1):
        if C[-1] ==  A[i]:
            continue
        if A[i-1] > A[i] <= A[i+1]:
            C.append((A[i], "-"))
        elif A[i-1] < A[i] >= A[i+1]:
            C.append((A[i], "+"))
    if C[-1][0] != A[-1]:
        C.append((A[-1], '+' if A[-1] > C[-1][0] else '-'))

    S = 0
    m = 1000
    if C and C[0][1] == '+':
        C = C[1:]
    if C and C[-1][1] == '-':
        C = C[:-1]
    

    for p, s in C:
        m_ = m
        S_ = S
        if s == '+':
            m += p * S 
            S = 0
        else:
            
            m -= (m_ // p) * p
            S = (m_ // p)
        
    print(m)

solve(N, A)

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


A_compress = []
val = 0
for a in A:
    if val != a:
        A_compress.append(a)
        val = a


A = A_compress
status = [None] * (len(A))



for i in range(1, len(A_compress) - 1):
    if A[i-1] < A[i] and A[i] < A[i+1]:
        status[i] = "up"
    elif A[i-1] < A[i] and A[i] > A[i+1]:
        status[i] = "top"
    elif A[i-1] > A[i] and A[i] < A[i+1]:
        status[i] = "bottom"
    elif A[i-1] > A[i] and A[i] > A[i+1]:
        status[i] = "down"


money = 1000
kabu = 0
for a, s in zip(A, status):
    if s == "bottom":
        kabu += money // a
        money %= a
    if s == "top":
        money += a * kabu
        kabu = 0
print(money)

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

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

money += A[-1] * ticket

print(money)

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]
    if a[i] > a[i + 1]:
        money += kabu * a[i]
        kabu = 0
money += kabu * a[n - 1]
print(money)

Solution (Python):
def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    stock = 0
    MONEY = 1000
    for i in range(N - 1):
        if A[i] < A[i + 1]:
            
            MONEY += stock * A[i]
            stock = 0
            
            stock = MONEY // A[i]
            MONEY -= A[i] * stock
        else:
            
            MONEY += stock * A[i]
            stock = 0
    if stock > 0:
        
        MONEY += stock * A[-1]
        stock = 0
    print(MONEY)

resolve()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
inc = False
dec = False
new = [a[0]]
for i in a[1:]:
  if i < new[-1]:
    if not dec:
      new.append(i)
      dec = True
      inc = False
    else:
      new[-1] = i
  elif i > new[-1]:
    if not inc:
      new.append(i)
      inc = True
      dec = False
    else:
      new[-1] = i
new = [0]+new+[0]
n = len(new)
money = 1000
stocks = 0
for i in range(1,n-1):
  if new[i-1] < new[i] > new[i+1]:
    money += stocks * new[i]
    stocks = 0
  else:
    has_peek_up = False
    for j in range(i+1,n):
      if new[j-1] < new[j] > new[j+1]:
        has_peek_up = True
        break
    if has_peek_up:
      stocks = money//new[i]
      money = money%new[i]
print(money)

Solution (Python):

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

price=1000 
stock=0 
for n in range(N-1):
    next_ = n+1
    
    if A[n] < A[next_]: 
        if price >= A[n]:
            stock = price//A[n]
            price = price - A[n]*stock
        
    elif A[n] > A[next_]: 
        price = price + A[n]*stock
        stock = 0
        
price = price + A[-1]*stock
stock = 0
print(price)

Solution (Python):
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    a = list(map(int, input().split()))
    dp = [0]*n
    dp[0] = 1000
    for i in range(n):
        for j in range(i):
            dp[i] = max([dp[j]+dp[j]//a[j]*(a[i]-a[j]), dp[j], dp[i]])
                    
    print(dp[-1])
        
    
if __name__ == '__main__':
    main()

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

Solution (Python):
n=int(input())
c=list(map(int, input().split()))
d = c[1:]
k = 1000

for i in range(n-1):
    if c[i] >= d[i]:
        pass
    if c[i] < d[i]:
        l = k//c[i]
        k -= c[i]*l
        k += l*d[i]

print(k)

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

Solution (Python):


import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from collections import deque

def resolve():
    N, = iim()
    *A, = iim()

    m = 1000

    m1 = A[0]
    i = 1
    k1 = 0
    while i < N:
        for i in range(i, N):
            ai = A[i]
            diff = ai - m1
            if diff > 0:
                k1, m = divmod(m, m1)
                break
            elif diff < 0:
                m1 = ai

        m1 = A[i]
        for i in range(i, N):
            ai = A[i]
            diff = ai - m1
            if diff > 0:
                m1 = ai
            else:
                k1, m = 0, m + m1 * k1
                i += 1
                break
        else:
            break
        m1 = ai

    print(max(1000, m))



if __name__ == "__main__":
    resolve()

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

now=1000
amari=0
stock=0
i=0
while i<N-1:
    if A[i]<A[i+1]:
        if stock==0:
            stock=now//A[i]
            amari=now%A[i]
            now=0
    elif A[i]>A[i+1]:
        if stock>0:
            now=stock*A[i]+amari
            stock=0
            amari=0
    i+=1

now=max(now,stock*A[i]+amari)
print(now)

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

baibai = [] 

for i in range(N):
    sa = A[i+1] -A[i]
    if sa == 0:
        baibai.append(0)
    elif sa != abs(sa):
        baibai.append(-1)
    else:
        baibai.append(1)


if 1 in baibai:
    uri = N - (baibai[::-1].index(1))
else:
    uri = -1


for j in range(N):
    if j == uri:
        kane += A[j] * kabu
        print(kane)
        exit()
    elif baibai[j] > 0:
        kabu += (kane//A[j])
        kane -= A[j] * (kane//A[j])
    elif baibai[j] < 0:
        kane += A[j] * kabu
        kabu = 0

print(kane)

Solution (Python):
N = int(input())
L_A=list(map(int,input().split()))
m = 1000
L = []
for i in range(N-1):
    if L_A[i] < L_A[i+1]:
        L.append(1)
    elif  L_A[i] > L_A[i+1]:
        L.append(-1)
    else:
        L.append(0)
k = 0
a = 0
while k < N-1:
    if L[k] == 1 and a == 0:
        a = L_A[k]
    elif  L[k] == -1 and a != 0:
        m += (m//a)*(L_A[k]-a)
        a = 0
    k += 1
if a != 0:
    m += (m//a)*(L_A[-1]-a)
print(m)

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
strategy = ["stay"] * len(A)

trend = "down"
for i in range(len(A)):
    if i == len(A) - 1:
        new_trend = "down"
        strategy[i] = "sell"
    else:
        if A[i] < A[i+1]:
            new_trend = "up"
            if trend != new_trend:
                strategy[i] = "buy"
        elif A[i] > A[i+1]:
            new_trend = "down"
            if trend != new_trend:
                strategy[i] = "sell"
        else:
            strategy[i] = "stay"
            new_trend = trend
    trend = new_trend


money = 1000
stock = 0
for s in range(len(strategy)):
    if strategy[s] == "sell":
        money = money + stock * A[s]
        stock = 0
    elif strategy[s] == "buy":
        stock = money // A[s]
        money = money - A[s] * stock
    else:
        continue
print(money)

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

for i in range(n-1):
  if a[i] < a[i+1]:
    tmp = ans // a[i]
    ans += (a[i+1] - a[i]) * tmp
    
print(ans)

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

def buy(N, A):
    m = N[0]
    n = N[1]
    
    num = m//A
    N[0] = m%A
    N[1] = n + num
    return N

def sell(N, A):
    m = N[0]
    n = N[1]
    N[1] = 0
    N[0] = m + A * n
    return N


for i in range(n-1):
    if Alist[i] == Alist[i+1]:
        up = None
        continue
    if Alist[i] > Alist[i+1]:
        up = True
        break
    if Alist[i] < Alist[i+1]:
        up = False
        break

if up == None:
    print(1000)
else:
    poplist = []
    for i in range(1, n):
        if up:
            if Alist[i] >= Alist[i-1]:
                poplist.append(i-1)
            else:
                up = False
        
        else:
            if Alist[i] <= Alist[i-1]:
                poplist.append(i-1)
            else:
                up = True

    for idx in poplist[::-1]:
        Alist.pop(idx)

    if Alist[0] > Alist[1]:
        Alist.pop(0)
    
    if len(Alist) == 1:
        print(1000)
    else:
        if Alist[-1] < Alist[-2]:
            Alist.pop(-1)
        
        money = 1000
        nums = len(Alist)//2
        for i in range(nums):
            kabu = money // Alist[2*i]
            money = money%Alist[2*i] + kabu * Alist[2*i+1]
        print(money)

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

b=[0]

for i in range(1,n):
    if a[i]>a[i-1]:
        b.append(1)
    else:
        b.append(0)

ans=1000
for j in range(n-1):
    if b[j]==0:
        if b[j+1]==1:
            kabu=ans//a[j]

            ans+=(a[j+1]-a[j])*kabu
    else:
        if b[j+1]==1:
            kabu=ans//a[j]
            ans+=(a[j+1]-a[j])*kabu
print(ans)

Solution (Python):
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext

def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n = i_input()
    num_list = i_list()

    money = 1000
    stock = 0
    buy_flag = True
    sale_flag = False

    for day in range(n):
        today_info = num_list[day]
        if buy_flag:
            buy_flag = False
            for feature in range(day+1,n):
                feature_info = num_list[feature]
                if today_info < feature_info:
                    for min_day in range(day+1, feature):
                        if today_info > num_list[min_day]:
                            break
                    else:
                        buy_flag = True
                    break
            if buy_flag:
                buy_num = money // today_info
                money -= buy_num * today_info
                stock += buy_num
                buy_flag = False
                sale_flag = True
            else:
                buy_flag = True
        elif sale_flag:
            for feature in range(day+1,n):
                feature_info = num_list[feature]
                if feature_info > today_info:
                    for min_day in range(day+1, feature):
                        if today_info > num_list[min_day]:
                            break
                    else:
                        sale_flag = False
                    break
            if sale_flag:
                money += today_info * stock
                stock = 0
                buy_flag = True
                sale_flag = False
            else:
                sale_flag = True

        

    print(money)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
_A = list(map(int, input().split()))
ans = 1000
A = [_A[0]]
B = []

for i in range(n-1):
  if (_A[i] != _A[i + 1]):
    A.append(_A[i+1])

flag = 0
if(len(A) < 2):
  print(ans)
  exit()
for i in range(len(A)-1):
  if (flag == 0 and A[i] < A[i + 1]):
    flag = 1
    B.append(A[i])
  elif (flag == 1 and A[i] > A[i + 1]):
    flag = 0
    B.append(A[i])
if((A[-1] > A[-2] and flag == 1) or (A[-1] < A[-2] and flag == 0)):
  B.append(A[-1])
if(len(B) < 2):
  print(ans)
  exit()
if(B[-1] < B[-2]):
  B.pop(-1)

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

print(ans)

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

money=1000
kabu=0
number=0

answers=[1000]

for n in range(N-1):
    if A[n]<A[n+1]:
        suuji=money//A[n]
        money-=A[n]*suuji
        kabu+=suuji
    elif A[n]>A[n+1]:
        money+=kabu*A[n]
        kabu=0
    answers.append(money)

money+=kabu*A[-1]

answers.append(money)

print(max(answers))

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

A = []
for i in range(N):
  if B[i] != B[i+1]:
    A.append(B[i])

T = []
for i in range(len(A)-1):
  if A[i] < A[i+1]:
    T.append(1)
  else:
    T.append(-1)
T.append(0)
T2 = [(A[0], T[0])]
for i in range(len(A)-1):
  if T[i] != T[i+1]:
    T2.append((A[i+1], T[i+1]))

money = 1000
kabu = 0
for m, s in T2:
  if kabu > 0 and s == -1:
    money += m * kabu
    kabu = 0
  if s == 1:
    buy = money // m
    kabu += buy
    money -= buy * m
if kabu > 0:
  money += A[-1] * kabu
print(money)

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

Solution (Python):
import sys

n, *aaa = map(int, sys.stdin.buffer.read().split())
stock = 0
cash = 1000
aaa.append(0)
for a_today, a_tomorrow in zip(aaa, aaa[1:]):
    cash += stock * a_today
    stock = 0

    if a_today < a_tomorrow:
        stock = cash // a_today
        cash -= stock * a_today

print(cash)

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

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

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

last_stock_val = 0
for i in range(N -1):
    if list_A[-1-i] < list_A[-2-i]:
        continue
    else:
        last_stock_val = list_A[-1-i]
        break
money += stock * last_stock_val
print(money)

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

d = []
for i in range(1, n):
  diff = a[i] - a[i-1]
  if diff > 0:
    d.append('up')
  elif diff < 0:
    d.append('down')
  else:
    d.append('stay')

def buy_all(money, stock, price):
  num = money // price
  money -= price * num
  stock += num
  return money, stock

def sell_all(money, stock, price):
  money += price * stock
  stock = 0
  return money, stock

flag = 0
for i in range(n-1):
  if flag == 0 and d[i] == 'up':
    money, stock = buy_all(money, stock, a[i])
    flag = 1
  if flag == 1 and d[i] == 'down':
    money, stock = sell_all(money, stock, a[i])
  if flag == 1 and d[i] == 'up':
    money, stock = buy_all(money, stock, a[i])
if stock > 0:
  money, stock = sell_all(money, stock, a[n-1])
print(money)

Solution (Python):
def solve(A,N):
    A.append(0)
    money,kabu = 1000,0
    ans = money
    pt = 0
    while pt < N:
        if A[pt] < A[pt+1]:
            ans = max(ans,money)
            kabu += (money // A[pt])
            money %= A[pt]
            pt += 1
            while pt < N:
                if A[pt] > A[pt+1]:
                    money += (A[pt] * kabu)
                    kabu = 0
                    ans = money
                    pt += 1
                    break
                else:
                    pt += 1
        else:
            pt += 1

    return ans

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

    print(solve(A,N))

if __name__ == "__main__":
    main()

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

y = 1000
k = 0


B = []
for i in range(len(A)-1):
  if A[i] == A[i+1]: continue
  B.append(A[i])



B = A
for i in range(len(B)-1):
  b = B[i]

  if B[i] == B[i+1]: continue

  if B[i] < B[i+1]:
    
    kabusu = y // b
    kakaku = kabusu * b
    y -= kakaku
    k += kabusu

    

  else:
    
    urine = k * b
    y += urine
    k = 0

    


if k>0:
  b = B[-1]
  urine = k * b
  y += urine

print(y)

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

while i < N:
    
    while i < (N-1) and A[i] > A[i+1]:
        i += 1
    if i == (N-1):
        break
    s = i
    
    while i < (N-1) and A[i] <= A[i+1]:
        i += 1
    if (i-s) > 0:
        c += (A[i]-A[s]) * (c//A[s])
        i += 1
print(c)

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

amt = 1000
for i in range(1, n):
    if nums[i] > nums[i-1]:
        q = amt//nums[i-1]
        amt -= q*nums[i-1]
        amt += q*nums[i]
print(amt)

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

stock = 0
prop = 1000
grad = -1

for i in range(n - 1):
    diff = a[i + 1] - a[i]
    if grad == -1 and diff > 0:
        trade_num = prop // a[i]
        stock = trade_num
        prop -= (trade_num * a[i])
        grad = 1
    elif grad == 1 and diff < 0:
        prop += stock * a[i]
        stock = 0
        grad = -1
if grad == 1:
    prop += stock * a[n - 1]

print(prop)