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
n=int(input())
a=list(map(int,input().split()))
money=1000
kabu=0
flg=0
first=True
if a[1]-a[0]>=0:
    flg="+"
else:
    flg="-"
x=a[1]

if len(a)==2:
    if a[1]>a[0]:
        kabu+=money//a[0]
        money-=a[0]*(money//a[0])
        money+=kabu*a[1]
    print(money)
    sys.exit()

for i , y in enumerate(a[2:],2):
    if flg=="+"  and y-x>=0:
        x=y
    elif flg=="-" and y-x<=0:
        x=y
    elif flg=="+" and y-x<0:
        x=y
        if first:
            kabu+=money//a[0]
            money-=a[0]*(money//a[0])
            first=False
        flg="-"
        money+=kabu*a[i-1]
        kabu=0
    elif flg=="-" and y-x>0:
        x=y
        if first:
            first=False
        flg="+"
        kabu+=money//a[i-1]
        money-=a[i-1]*(money//a[i-1])
    

if kabu!=0:
    if flg=="+":
        money+=kabu*a[-1]
    elif flg=="-":
        X=a[-1]
        for Y in a.reverse()[1:]:
            if X<=Y:
                X=Y
            else:
                money+=kabu*Y
                break

if first:
    if a[-1]>a[0]:
        kabu+=money//a[0]
        money-=a[0]*(money//a[0])
        money+=kabu*a[-1]

print(money)

Solution (Python):


def main():
    N = int(input())
    A = list(map(int, input().split()))
    cnt = 0
    money = 1000
    for i in range(N - 1):
        if cnt > 0:
            money += cnt * A[i]
            cnt = 0
        if A[i] < A[i + 1]:
            cnt, money = divmod(money, A[i])
    if cnt > 0:
        money += cnt * A[N - 1]
        cnt = 0
    print(money)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
m = 1000
c = 0
v = 0
for i in range(n-1):
	if c == 0:
		if p[i+1] > p[i]:
			c = m // p[i]
			v = p[i]
			m -= c * v
	else:
		if p[i+1] < p[i]:
			m += p[i] * c
			c = 0
if c > 0:
     m += p[n-1] * c
print(m)

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

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

Solution (Python):
N = int(input())
line  = list(map(int, input().split()))
money = 1000
k = line[0]
ln = [line[0]]
for i in range(1,N):
    if line[i] != ln[-1]:
        ln.append(line[i])

while len(ln) > 1:
    if ln[0] >  ln[1]:
        ln = ln[1:]
        
    else:
        break

while len(ln) > 1:
    if ln[-1] < ln[-2]:
        ln = ln[:-1]
        
    else:
        break


ln1= [ ln[0]]
st = True
for i in range(1,len(ln)-1):
    if (ln1[-1] < ln[i] <ln[i +1]) or (ln1[-1] > ln[i] > ln[i +1]):
        pass
    else:
        ln1.append(ln[i])
            
ln1.append(ln[-1])        


for i in range(int(len(ln1)/2)):
    money = ln1[2*i +1] * int(money / ln1[2*i]) + (money %ln1[2*i])
    
print(money)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cash = 1000
kabu = 0
up=False
down=False
buysell = [0]*n
                          
buyable=True
for index,(i,j) in enumerate(zip(a[:-1], a[1:])):
  if i <= j:
    if buyable:
      buysell[index] = 1
      buyable=False
  else:
    if not buyable:
      buysell[index] = -1
      buyable=True
else:
  if not buyable:
    buysell[-1] = -1

for index,i in enumerate(buysell):
  if i == 0:
    continue
  if i> 0:
    kabu = cash // a[index]
    cash -= kabu * a[index]
  else:
    cash += kabu * a[index]
    kabu = 0
print(cash)

Solution (Python):
def main():

    n = int(input())
    A = list(map(int, input().split()))
    INF = 19**18

    X = []
    Y = []
    A = [INF] + A
    for i in range(1, n):
        if len(X) == len(Y):
            if A[i-1] >= A[i] and A[i] < A[i+1]:
                X.append(i)
        if len(X) > len(Y):
            if  A[i-1] <= A[i] and A[i] > A[i+1]:
                Y.append(i)
    else:
        if len(X) > len(Y):
            Y.append(i+1)
    
    

    cur = 1000
    for i, j in zip(X, Y):
        q, cur = divmod(cur, A[i])
        cur += q*A[j]
    print(cur)



if __name__ == '__main__':
    main()

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


money = 1000

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

print(money)

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

mon = 1000


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

print(mon)

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

money = 1000
kabu = 0

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

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

money = 1000
stock = 0

day = 0
for i in range(N):
    if i < day:
        continue
    for j in range(i+1, N):
        if stock == 0:
            if A[i] < A[j]:
                stock += money // A[i]
                money -= stock * A[i]
                break
            else:
                break
        else:
            if A[j-1] > A[j]:
                money += stock * A[j-1]
                stock = 0
                day = j
                break
if stock > 0:
    money += stock * A[-1]
    stock = 0

print(money)

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

for i in range(n-1):
    if A[i] < A[i+1]:
        stack = int(money/A[i])
        money = stack*A[i+1] - stack*A[i] + money

print(money)

Solution (Python):

import sys
N=int(sys.stdin.readline().strip())
A=[float("inf")]+map(int, sys.stdin.readline().split())+[0]
AA=[]
for i,a in enumerate(A):
    if i==0:
        AA.append(a)
    else:
        if prev==a:
            pass
        else:
            AA.append(a)
    prev=a

KATAMUKI=[]
for i,a in enumerate(A):
    if i==0:
        pass
    else:
        if prev<a:
            KATAMUKI.append("+")
        elif prev>a:
            KATAMUKI.append("-")
    prev=a

len_AA=len(AA)

money=1000
kabu=0
for i in range(1,len_AA-1):
    kabuka=AA[i]
    
    if KATAMUKI[i-1]=="-" and KATAMUKI[i]=="+":   
        kabu+=money/kabuka
        money-=(money/kabuka)*kabuka
    elif KATAMUKI[i-1]=="+" and KATAMUKI[i]=="-":   
        money+=kabu*kabuka
        kabu=0

print money

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
money=1000;kabu=0
i=0
while(i<N):
    j=i
    money,kabu=money-(money//A[i])*A[i],kabu+(money//A[i])
    while(j<N-1 and A[j]<=A[j+1]):
        j+=1
    money=money+kabu*A[j]
    kabu=0
    i=j+1
print(money)

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

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

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

money = 1000
stok = 0

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

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

b = []



before = 1

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

for i in b:
  if i == 1:
    b.insert(0, 0)
    break
  elif i == -1:
    b.insert(0, 1)
    break

if len(b) != n:
  b.insert(0, 0)

a.reverse()
money = 1000
kabu = 0
    
for i in range(n):
  if b[i] == 1:
    kabu, money = divmod(money, a[i])
  if b[i] == -1:
    money = money + kabu * a[i]
    kabu = 0

print(money)

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

def kabu(N, A_list, money):
    for i in range(0, N-1):
        kounyuu = money // A_list[i]
        if A_list[i+1] > A_list[i]:
            money += (A_list[i+1]-A_list[i]) * kounyuu
    return money
        
print(kabu(N, A, 1000))

Solution (Python):


def main():
    import sys
    import math

    N = list(map(int, sys.stdin.readline().split()))
    A_list = list(map(int, sys.stdin.readline().split()))

    money = 1000

    for i in range(1,len(A_list)):
        if A_list[i] > A_list[i-1]:
            stock = money // A_list[i-1]
            money -= (stock * A_list[i-1])

            money += (stock * A_list[i])
    
    
    print(money)


if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
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 =  kabu + int((m - (m % a[i])) / a[i])
    m = m % a[i]
  elif a[i] > a[ i +1 ]:
    m = m + (a[i] * kabu)
    kabu = 0
if kabu > 0:
  m = m + kabu * a[-1]
print(m)

Solution (Python):

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


money=1000

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

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

    money = 1000
    stock = 0

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

    money += stock * A[-1]

    print(money)


if __name__ == "__main__":
    main()

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

Solution (Python):

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

    current_money = 1000
    cheap = Prices[0]
    index_cheap = 1
    index_expen = 1

    N_stock = 0


    while (True):

        if (index_expen >= N or index_cheap >= N):
            break
        
        while (index_cheap < N):
            if (cheap >= Prices[index_cheap]):
                cheap = Prices[index_cheap]
                index_cheap += 1
            
            else:
                cheap = Prices[index_cheap]
                index_expen = index_cheap
                index_cheap -= 1
                break

        if (index_expen >= N or index_cheap >= N):
            break

        N_toBuy = current_money // Prices[index_cheap]
        current_money += (Prices[index_expen] - Prices[index_cheap]) * N_toBuy
        

        index_cheap = index_expen


    print(current_money)



if __name__ == '__main__':
    main()

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

A = list(map(int,input().split()))
reverseA = sorted(A,reverse = True)
money = 1000
stock = 0

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

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

downTrend = True

prev = l[0]
for i in range(1, N):
    A = l[i]
    if downTrend == True:
        if A < prev:
            prev = A
        else:
            stock, cash = divmod(cash, prev)
            prev = A
            downTrend = False
    else:
        if A > prev:
            prev = A
        else:
            cash += stock * prev
            stock = 0
            downTrend = True
            prev = A
cash += stock * prev

print(cash)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


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


if __name__ == '__main__':
    main()

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

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

money = 1000
stock = 0

for i in range(1, N):
    
    if cng[i+1] == 1:
        ds = money // A[i]
        dm = ds * A[i]
        stock += ds
        money -= dm
    
    elif cng[i+1] == -1:
        ds = stock
        dm = ds * A[i]
        stock -= ds
        money += dm

if stock != 0:
    money += stock * A[N]

print(money)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
money = [[1000, 0]]
ans = 1000
B = [1 for _ in range(N)] 
for k in range(N-1):
  if A[k] < A[k+1]:
    B[k] = -1
  elif A[k] == A[k+1]:
    B[k] = 0

for k in range(N):
  if B[k] == -1:
    new = []
    for j in range(len(money)):
      if A[k] <= money[j][0]:
        new.append([money[j][0]%A[k], money[j][1] + money[j][0]//A[k]])
    money += new
  
  if B[k] == 1:
    ans = 0
    for j in range(len(money)):
      ans = max(money[j][0] + money[j][1]*A[k], ans)
    money[0] = [ans, 0]

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
left=a[0]
kyokushou=[]
kyokudai=[]
if min(a)==max(a):
  print(1000)
else:
  for i in range(n):
    if left<a[i]:
      f=0
      saki=0
      kyokushou.append(0)
      break
    elif left>a[i]:
      f=1
      saki=1
      break

  for i in range(n-1):
    if f==0 and a[i]>a[i+1]:
      kyokudai.append(i)
      f=1
    elif f==1 and a[i]<a[i+1]:
      kyokushou.append(i)
      f=0
      if i==n-2:
        kyokudai.append(n-1)
  if saki==0 and len(kyokudai)==0:
    kabu=1000//min(a)
    shoji=1000-kabu*min(a)
    shoji+=kabu*max(a)
    print(shoji)
  elif saki==1 and len(kyokudai)==0:
    print(1000)
  else:
    kabu=0
    shoji=1000
    for i in range(len(kyokudai)):
      kabu=shoji//a[kyokushou[i]]
      shoji-=kabu*a[kyokushou[i]]
      shoji+=kabu*a[kyokudai[i]]
      kabu=0
    print(shoji)

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

real = 1000
unreal = 0

for i in range(n-1):

    if a[i] < a[i+1]:
        get_unreal, real = divmod(real, a[i])
        unreal += get_unreal

    elif a[i] > a[i+1]:
        real += (unreal * a[i])
        unreal = 0

    else:
        continue

real += unreal * a[n-1]
print(real)

Solution (Python):
n=int(input())
a = list(map(int,input().split()))
saifu = 1000
kaine = None
urine = None
for i in range(n):
  if kaine == None:
    kaine = a[i]
    continue
  if urine == None:
    if i < n - 1 and kaine < a[i]:
      urine = a[i]
      continue
    elif i < n - 1:
      kaine = a[i]
      continue
  if i < n - 1 and urine != None and urine <= a[i]:
    urine = a[i]
    continue
  if i == n - 1 and kaine < a[i]:
    urine = a[i]
  if kaine != None and urine != None:
    kabu = int(saifu / kaine)
    saifu += kabu * (urine - kaine)
    kaine = a[i]
    urine = None
print(saifu)

Solution (Python):
def main(N, A):
    
    lst = A[:2]
    if A[0] <= A[1]:
        first_mode = 'up'
    else:
        first_mode = 'down'
    mode = first_mode

    for a in A[2:]:
        if mode == 'up':
            if lst[-1] <= a:
                lst[-1] = a
            else:
                lst.append(a)
                mode = 'down'
        else:   
            if lst[-1] >= a:
                lst[-1] = a
            else:
                lst.append(a)
                mode = 'up'

    money = 1000
    if first_mode == 'down' and len(lst) == 2:
        print(money)
        exit()
    elif first_mode == 'down':
        del lst[0]
    

    for n in range(len(lst) // 2):
        shou = money // lst[2*n]
        amari = money % lst[2*n]
        money = shou * lst[2*n+1] + amari
        
    print(money)




if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    main(N, A)

Solution (Python):

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

    money = 1000
    trading = []
    buy = True

    for i in range(1, len(A)):
        if buy:
            if A[i] <= A[i-1]:
                continue
            else:
                trading.append(A[i-1])
                if i == len(A)-1:
                    trading.append(A[i])
                buy = False
        else:
            if A[i] >= A[i-1]:
                if i == len(A)-1:
                    trading.append(A[i])
                continue
            else:
                trading.append(A[i-1])
                buy = True
    
    if len(trading) % 2 == 1:
        trading.pop(-1)
    

    stock = 0
    
    for i in range(len(trading)):
        if i % 2 == 0:
            stock = money // trading[i]
            money -= stock * trading[i]
        else:
            money += stock * trading[i]
            stock = 0
    
    print(money)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
money = 1000
kab = 0
flag = 0

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

if flag == 1:
  money += A[N-1] * kab
  
print(money)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Deposit=1000
Stock=0
A.insert(0,201)

j=0
while j<len(A)-1:
    if A[j]==A[j+1]:
        A.pop(j)
    else:
        j+=1

if N==2:
    if A[1]<A[2]:
        Deposit=1000-1000//A[1]*A[1]+1000//A[1]*A[2]        
else:
    for i in range(len(A)-2): 
        if A[i]>A[i+1] and A[i+1]<A[i+2]: 
            Stock=Deposit//A[i+1]
            Deposit=Deposit-Stock*A[i+1]
        elif A[i]<A[i+1] and A[i+1]>A[i+2]: 
            Deposit=Deposit+Stock*A[i+1]
            Stock=0
        i+=1
        
Deposit=Deposit+Stock*A[-1]

print(Deposit)

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]:
        p = int(m//A[i])
        k += p
        m -= A[i]*p
    elif A[i] > A[i+1]:
        m += A[i]*k
        k = 0

m += A[N-1]*k
print(m)

Solution (Python):

import sys


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

    dif = [t1 - t0 for t0,t1 in zip(A[:-1], A[1:])]
    for d in dif:
        if d!=0: break
    pre = d
    for d in range(N-1):
        if dif[d] == 0:
            dif[d] = pre
        pre = dif[d]


    if dif[0] > 0:
        c = m // A[0]
        m -= A[0] * c
    
    for i in range(1,N-1):
        if (dif[i-1] < 0)and(dif[i]>0):
            c = m // A[i]
            m -= A[i] * c
            
        elif (dif[i-1] > 0)and(dif[i]<0):
            m += A[i] * c
            c = 0
            
        
    
    m += A[N-1] * c
    c = 0

    print(m)

    return



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

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
ar=list(map(int,input().split()))
c=1000
for i in range(n-1):
    if ar[i+1]>ar[i]:
        cur=c//ar[i]
        c%=ar[i]
        c+=ar[i+1]*cur

print(c)

Solution (Python):
from collections import deque
N = int(input())
alist = list(map(int,input().split()))
money = 1000
kabu = 0
zogen = []
flag = 0

def buy(i):
    global alist
    global money
    global kabu
    kabu = money//alist[i]
    money -= kabu*alist[i]

def sell(i):
    global alist
    global money
    global kabu
    money += kabu*alist[i]
    kabu = 0

best = 0

for i in range(N):
    if i < N-1:
        if flag == 0:
            if alist[i] < alist[i+1] :
                buy(i)
                flag = True
                continue
            elif alist[i] > alist[i+1]:
                flag == False
                continue
            else:
                continue

        if flag == True:
            if alist[i] > alist[i+1]:
                sell(i)
                flag = False
            else:
                continue
        elif flag == False:
            if alist[i] < alist[i+1]:
                buy(i)
                flag = True
            else:
                continue
    else:
        if flag == True:
            sell(i)
            flag = False

print(money)

Solution (Python):

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

    current_money = 1000
    cheap = Prices[0]
    index_cheap = 1
    index_expen = 1

    N_stock = 0


    while (True):

        if (index_expen >= N or index_cheap >= N):
            break
        
        while (index_cheap < N):
            if (cheap >= Prices[index_cheap]):
                cheap = Prices[index_cheap]
                index_cheap += 1
            
            else:
                cheap = Prices[index_cheap]
                index_expen = index_cheap
                index_cheap -= 1
                break

        if (index_expen >= N or index_cheap >= N):
            break

        N_toBuy = current_money // Prices[index_cheap]
        current_money += (Prices[index_expen] - Prices[index_cheap]) * N_toBuy
        

        index_cheap = index_expen


    print(current_money)



if __name__ == '__main__':
    main()

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

for d in range(n-1):
    if stock ==0:
        if alist[d+1] > alist[d]:
            
            stock=int(money/alist[d])
            money-=stock*alist[d]
        else:
            
            pass
    else:
        if alist[d+1] > alist[d]:
            
            money+=stock*alist[d]
            stock=int(money/alist[d])
            money-=stock*alist[d]
        else:
            
            money+=stock*alist[d]
            stock=0

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

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

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

print(max(dp[-1])[0])

Solution (Python):

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

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

while True:
    if C==[]:
        break
    if C[-1][0]=="down":
        C.pop(-1)
    else:
        break
        
kabu=0
money=1000
if C!=[]:
    if C[0][0]=="up":
        kabu+=money//A[0]
        money%=A[0]
for act in C:
    if act[0]=="up":
        money+=A[act[1]+1]*kabu
        kabu=0
    else:
        kabu+=money//A[act[1]+1]
        money%=A[act[1]+1]
print(money)

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

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


print(money)

Solution (Python):
import sys

def f_buy(m, s, a):
  tmp = m//a
  return m-tmp*a, s+tmp

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

n = int(input())
la = list(map(int, input().split()))
lb = [0]*(n-1)
for i in range(n-1):
  if la[i+1]-la[i] > 0:
    lb[i] = 1
  if la[i+1]-la[i] < 0:
    lb[i] = -1

if lb.count(1) == 0:
  print(1000)
  sys.exit()

m = 1000
s = 0
frg = 0
for i in range(n-1):
  if lb[i] == 1:
    if frg != 1:
      m, s = f_buy(m, s, la[i])
    frg = 1
  if lb[i] == -1:
    if frg != -1:
      m, s = f_sell(m, s, la[i])
    frg = -1

m, s = f_sell(m, s, la[n-1])
print(m)

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


joutai = 0
kabusu = 0
money = 1000
for i in range(N):
    if i == N-1:
        if joutai == 1 and A[i] >= A[i-1]:
            money = money + A[i] * kabusu
            joutai = 0
            kabusu = 0
    else:
        if A[i] < A[i+1]:
            if joutai == 0:
                kabusu = money // A[i]
                joutai = 1
                money = money - A[i] * kabusu
        elif A[i] > A[i+1]:
            if joutai == 1:
                money = money + A[i] * kabusu
                joutai = 0
                kabusu = 0
                
print(money)

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

num = temp[0]
A = [num]
for i in range(1, N):
    if temp[i] != num:
        A.append(temp[i])
        num = temp[i]
A += [0]

ls = []
bottom = A[0]
for i in range(1, len(A)-1):
    if A[i-1] < A[i] and A[i] > A[i+1]:
        ls += [bottom, A[i]]
    if A[i-1] > A[i] and A[i] < A[i+1]:
        bottom = A[i]

stucks = 0
money = 1000
for i in range(len(ls)):
    if i % 2 == 0:
        stucks = money // ls[i]
        money -= stucks * ls[i]
    else:
        money += stucks * ls[i]
        stucks = 0

print(money)

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

Y = 1000
buy, sell = [], []
for i in range(N):
  if not buy:
    buy.append(A[i])
  elif not sell:
    if A[i] <= buy[-1]:
      buy[-1] = A[i]
    else:
      sell.append(A[i])
  else:
    if A[i] >= sell[-1]:
      sell[-1] = A[i]
    else:
      div, mod = divmod(Y, buy.pop())
      Y = sell.pop() * div + mod
      buy.append(A[i])
if buy and sell and buy[-1] < sell[-1]:
    div, mod = divmod(Y, buy.pop())
    Y = sell.pop() * div + mod
print(Y)

Solution (Python):
def resolve():
    n=int(input())
    a = list(map(int, input().split()))
    b=[0]*n




    zouka=0


    for i in range(1,n):
        if a[i-1]<a[i]:
            if zouka!=1:
                zouka=1
                b[i-1]=1
        if a[i-1]>a[i]:
            if zouka!=-1:
                zouka=-1
                b[i-1]=-1
        if i==n-1 and zouka!=0:
            b[n-1]=zouka*(-1)

    kab=0
    temo=1000
    for i in range(n):
        if b[i]==1:
            kab += temo // a[i]
            temo -= a[i] * kab
        elif b[i]==-1:
            temo += kab * a[i]
            kab = 0
        if i==n-1 and kab!=0:
            temo += kab * a[i]
            kab = 0

    print(temo)
resolve()

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

for i in range(1,N): 
  if(A[i-1] > A[i]):
    buy=A[i]
  elif(buy < A[i]):
    max = A[i]
    kabu = int(kane/buy)
    kane = kane + kabu*(max-buy)  
    kabu = 0
    buy = A[i]
print(kane)

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

l = []
tmp = []
for i in range(n-1):
    if a[i] < a[i+1] and len(tmp) == 0:
        tmp.append(a[i])
    elif a[i] > a[i+1] and len(tmp) == 1:
        tmp.append(a[i])
        l.append(tmp)
        tmp = []
if len(tmp) == 1 and tmp[0] < a[-1]:
    tmp.append(a[-1])
    l.append(tmp)

res = 1000
for a, b in l:
    res += (res // a) * (b - a)

print(res)

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