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


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


def std_in():
    return sys.stdin.readline().strip()


def main():
    n = int(std_in())
    a = list(get_ints())
    a.append(0)
    m = 1000
    k = 0

    for i in range(n):
        if i == n:
            break
        if a[i] > a[i+1]:
            m += k * a[i]
            k = 0
        elif a[i] < a[i+1]:
            q, mod = divmod(m, a[i])
            k += q
            m = mod
    print(m)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
total=1000
z=[]
t=float("infinity")
for i in range(n):
	if len(z)==0:
		z.append(l[i])
	else:
		if z[-1]<l[i]:
			z.append(l[i])
		elif z[-1]>l[i]:
			if len(z)>1:
				f=total//z[0]
				total=total%z[0]
				total+=f*z[-1]
			z=[l[i]]
if len(z)>1:
	f=total//z[0]
	total=total%z[0]
	total+=f*z[-1]
print(total)

Solution (Python):
N = int(input())
A = [int(X) for X in input().split()]
Stock = 0
Money = 1000
for T in range(0,N-1):
    if A[T+1]>A[T]:
        Purchase = Money//A[T]
        Stock += Purchase
        Money -= Purchase*A[T]  
    elif A[T+1]<A[T]:
        Money += Stock*A[T]
        Stock = 0
if Stock>0:
    Money += Stock*A[N-1]
    Stock = 0
print(Money)

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

now=1000
dp_poss=[0 for i in range(n+1)]
dp_stck=[0 for i in range(n+1)]

buy_choice=[False]*(n+1)
tmp=a[1]
for i in range(1,n):
    if tmp==a[i+1]:continue
    if tmp<a[i+1]:
        buy_choice[i]=True
    tmp=a[i+1]
buy_choice[-1]=False



dp_poss[0]=1000


for i in range(1,n+1):
    if buy_choice[i]:
        afford=dp_poss[i-1]//a[i]
        dp_poss[i]=dp_poss[i-1]-afford*a[i]
        dp_stck[i]=dp_stck[i-1]+afford
    else:
        dp_poss[i]=dp_poss[i-1]+dp_stck[i-1]*a[i]

print(dp_poss[n])

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
money = 1000
sell = 0
num = 0
for i in range(N-1):
    if A[i] < A[i+1]:
        num += int(money / A[i])
        money %= A[i]
        sell = 1
    elif sell == 1:
        money += num * A[i]
        num = 0
        sell = 0
if num > 0:
    money += num*A[N-1]
print(money)

Solution (Python):



import sys


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


def std_in():
    return sys.stdin.readline().strip()


def main():
    n = int(std_in())
    a = list(get_ints())
    a.append(0)
    m = 1000
    k = 0

    for i in range(n):
        if i == n:
            break
        if a[i] > a[i+1]:
            m += k * a[i]
            k = 0
        elif a[i] < a[i+1]:
            q, mod = divmod(m, a[i])
            k += q
            m = mod
    print(m)


if __name__ == "__main__":
    main()

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

num_max = 0
num_min = 0
d_flg = False
u_flg = False

if n == 2 and a[1] > a[0]:
    b = a
else:
    for i in range(1, n-1):
        if a[i-1] <= a[i] and a[i] > a[i+1]:
            b.append(a[i])
            d_flg = False
            u_flg = True
        elif a[i-1] >= a[i] and a[i] < a[i+1]:
            b.append(a[i])
            u_flg = False
            d_flg = True

    if u_flg is True and b[-1] < a[-1]:
        b[-1] = a[-1]
    elif u_flg is True and b[-1] > a[-1]:
        b.append(a[-1])
    elif d_flg is True and b[-1] < a[-1]:
        b.append(a[-1])
    elif u_flg is True and b[-1] > a[-1]:
        b[-1] = a[-1]


if len(b) == 1:
    print(1000)
    exit()

for i in range(len(b)-1):
    if b[i] > b[i+1] and kabu > 0:
        money += kabu * b[i]
        kabu = 0

    elif b[i] < b[i+1] and money >= b[i]:
        kabu = money // b[i]
        money -= kabu * b[i]

money += kabu * b[i+1]

print(money)

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

flag = False
up,down = [],[]
for i in range(n):
    if L[i] < L[i+1] and flag == False:
        flag =True
        up.append(i)
    elif L[i] > L[i+1] and flag == True:
        flag = False
        down.append(i)

sisan = 1000
div,buy,sell = 0,0,0
for i in range(n):
    if i in up:
        div = sisan//L[i]
        
        buy = div*L[i]
        sisan -= buy
        
    elif i in down:
        sell = div*L[i]
        sisan +=sell
        
print(sisan)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cash = 1000
bill = 0
table = []

for i in range(n-1):
    if a[i] > a[i+1]:
        table.append(False)
    elif a[i] == a[i+1]:
        table.append("-")
    else:
        table.append(True)

if table[0] == True:
    bill, cash = divmod(cash, a[0])

i = 0
while i < n-1:
    if table[i] == True:
        while not table[i] == False:
            i += 1
            if i == n-1:
                break
        cash += bill * a[i]
        bill = 0
        
    else:
        while not table[i] == True:
            i += 1
            if i == n-1:
                break
        bill, cash = divmod(cash, a[i])
        
cash += bill * a[-1]
print(cash)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
M=1000
K=0
S=False
for i in range(N-1):
    
    if S==False:
        if A[i]<A[i+1]:
            temp=0
            temp=M//A[i]
            M-=temp*A[i]
            K+=temp
            S=True
            
    else:
        if A[i]>A[i+1]:
            M+=K*A[i]
            K=0
            S=False
M+=K*A[i+1]
        
print(M)

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

c_amount = 1000
for i in range(N-1):
    if(A[i]>=A[i+1]):
        pass
    else:
        c_amount+=(c_amount//A[i])*(A[i+1]-A[i])

print(c_amount)

Solution (Python):
N = int(input())
sum = 1000
A = list(map(int, input().split()))
pre = A[0]
greater = False
less = False
for a in A[1:]:
  if greater:
    if pre > a:
      less = True
      greater = False
      sum = sum % i + (sum // i) * pre
  elif less:
    if pre < a:
      greater = True
      less = False
      i = pre
  else:
    if pre > a:
      less = True
    elif pre < a:
      greater = True
      i = pre
  pre = a
if greater:
  sum = sum % i + (sum // i) * pre
print(sum)

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

    has_stock = False
    money = 1000
    stock = 0
    for day_i in range(N-1):
        if not has_stock:
            if A[day_i] < A[day_i + 1]:
                has_stock = True
                num = math.floor(money / A[day_i])
                stock += num
                money -= num * A[day_i]
                
        else:
            if A[day_i] > A[day_i + 1]:
                has_stock = False

                money += stock * A[day_i]
                stock = 0
                
    
    if has_stock:
        money += stock * A[N-1]
    print(money)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A.append(-1)
up = []
pre = False
sw = False
for i in range(1,N+1):
    if A[i] > A[i-1]:
        sw = True
    elif A[i] < A[i-1]:
        sw = False
    if sw != pre:
        up.append(i-1)
    pre = sw
if up == []:
    print(1000)
    exit()
money = 1000
p = False
for j in up:
    if p == False:
        stock = money//A[j]
        money -= stock*A[j]
    else:
        money += stock*A[j]
        stock = 0
    p = not p
print(money)

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

money = 1000
stock = 0

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

print(money + stock * A[i+1])

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

stock=0
money=1000

graph=[0]*n
if a[0]<a[1]:
    graph[0]=-1
    increase=1
elif a[0]==a[1]:
    increase=0
else:
    increase=-1

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

if a[n-2]<a[n-1]:
    graph[n-1]=1

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

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

money = 1000
stock = 0

diff = ['-'] * n
for i in range(1, n):
    if a[i] < a[i - 1]:
        diff[i] = '-'
    elif a[i] > a[i - 1]:
        diff[i] = '+'
    elif a[i] == a[i - 1]:
        diff[i] = diff[i - 1]

for i in range(n - 1):
    if diff[i] == '-':
        if diff[i + 1] == '+':
            stock += money // a[i]
            money = money % a[i]
    elif diff[i] == '+':
        if diff[i + 1] == '-':
            money += stock * a[i]
            stock = 0
if stock > 0:
    money += stock * a[n - 1]

print(money)

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

up_down_list = []
init = True
for i in range(len(l)):
    
    ai = l[i] 
    
    if init:
        up_down_list.append(-2)
        tmp_ai = ai
        init = False
        continue
        

    
    if ai > tmp_ai:
        up_down_list.append(1)
        
    elif ai == tmp_ai:
        up_down_list.append(0)
        
    else:
        up_down_list.append(-1)
    
    
    tmp_ai = ai
    
uri = []
kai = []

s_flag = False
for n in range(len(up_down_list)):
    
    inv_n = len(up_down_list) - (n + 1)
    
    if s_flag == False:
        if up_down_list[inv_n] == 1:
            s_flag = True
            uri.append(inv_n)
            continue
    else:
        
        if up_down_list[inv_n] < 0:
            
            kai.append(inv_n)
            s_flag = False
        
        
        

s = 0
yen = 1000

for k in range(len(kai)):
    
    inv_k = len(kai) - (k+1)
    
    kaine = l[kai[inv_k]]
    urine = l[uri[inv_k]]
    
    s=int(yen / kaine)
    yen = yen - kaine * s
    
    yen = yen + urine * s

print(yen)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
A.append(0)
f = [0] * N
for i in range(N):
    if A[i] < A[i+1]:
        f[i] = 1
    elif A[i] > A[i+1]:
        f[i] = 2
cnt = 1000
stock = 0
prev = 0
for i in range(N):
    if stock == 0 and prev != 1 and f[i] == 1:
        stock = cnt // A[i]
        cnt %= A[i]
    elif prev != 2 and f[i] == 2:
        cnt += stock * A[i]
        stock = 0
    prev = f[i]
print(cnt)

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

Solution (Python):
N = int(input())
rate = list(map(lambda x:int(x), input().split(' ')))
isGrowing = False
money = 1000
upstart = None
for i in range(N-1):
    if not isGrowing and rate[i] < rate[i+1]:
        isGrowing = True
        upstart = i
    elif isGrowing and rate[i] > rate[i+1] and upstart is not None:
        money = (money//rate[upstart])*rate[i] + money%rate[upstart]
        upstart = i+1
        isGrowing = False
if isGrowing:
    money = (money//rate[upstart])*rate[N-1] + money%rate[upstart]
print(money)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    kai = set()
    uri = set()
    i = 1
    while i < n:
        if A[i - 1] < A[i]:
            kai.add(i - 1)
            while i < n and A[i - 1] <= A[i]:
                i += 1
            uri.add(i - 1)
            while i < n and A[i - 1] >= A[i]:
                i += 1
        else:
            i += 1

    if len(kai) == 0:
        print(1000)
        exit()

    money = 1000
    kabu = 0
    for i in range(n):
        if i in kai:
            if i == n - 1:
                continue
            kabu, money = divmod(money, A[i])
        elif i in uri:
            money += kabu * A[i]
            kabu = 0
    print(money)


if __name__ == '__main__':
    resolve()

Solution (Python):
import sys
k=1000
N=int(input())
L=list(map(int,input().split()))
i=0
while i<N-1:
  if L[i]<L[i+1]:
    n=L[i]
    break
  i+=1
  if i==N-1:
    print(1000)
    sys.exit()
n=0
for j in range(i,N):
  if j==N-1 and n!=0:
    m=max(L[s:])
    c=k%n
    k=((k//n)*m)+c
    n=0
    break
  if j==N-1 and n==0:
    break
  if L[j]<L[j+1] and n==0:
    n=L[j]
    s=j
  if L[j]>L[j+1] and n!=0:
    m=L[j]
    c=k%n
    k=((k//n)*m)+c
    n=0
print(k)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
start = n-1
for i in range(n-1):
  if a[i]<a[i+1]:
    start = i
    break

money = 1000
kabu = 0
l = [a[start]]
high = 1
for i in range(start+1,n-1):
  if high==1:
    if a[i]<a[i+1]:
      continue
    else:
      l.append(a[i])
      high = 0
  else:
    if a[i]>a[i+1]:
      continue
    else:
      l.append(a[i])
      high = 1
if high==1 and a[-2]<a[-1]:
  l.append(a[-1])
if high==0 and a[-2]>a[-1]:
  l.append(a[-1])

if len(l)%2==0:
  for i in range(len(l)):
    if i%2==0:
      kabu += money//l[i]
      money -= (money//l[i])*l[i]
    else:
      money += kabu*l[i]
      kabu = 0
   
  print(money)
else:
  l.pop()
  for i in range(len(l)):
    if i%2==0:
      kabu += money//l[i]
      money -= (money//l[i])*l[i]
    else:
      money += kabu*l[i]
      kabu = 0
    
  print(money)

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

for i in range(n):
    if i == n-1:
        if s:
            m += A[i] * s
            s = 0
            break
        else:
            break

    if A[i] < A[i+1] and m > A[i]:
        s = m // A[i]
        m -= A[i] * s
    elif A[i] > A[i+1]:
        if s:
            m += A[i] * s
            s = 0
        else:
            continue

print(m)

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

Solution (Python):
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    c = 1000
    s = 0

    for i in range(0, n-1):
        j = i + 1  
        diff = A[j] - A[i]
        
        if diff > 0:
            s += c // A[i]
            c = c % A[i]
        
        elif diff < 0:
            c += s * A[i]
            s = 0
        

    
    c += s * A[n-1]

    print(c)


if __name__ == "__main__":
    resolve()

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):
        dp[i] = max(dp[i], dp[j] // a[j] * a[i] + dp[j] % a[j])
print(dp[-1])

Solution (Python):
def main():
    N = int(input())
    A = list(map(int,input().split()))
    money = 1000
    kabu = 0
    
    flag = ''
    for n in range(N):
        if A[0] > A[n]:
            flag = 'down'
            break
        elif A[0] < A[n]:
            flag = 'up'
            kabu = money//A[0]
            money -= A[0]*kabu
            break
        
    if flag == '':
        print(money)
    else:
        limit = 0
        for n in reversed(range(1,N)):
            if A[n-1] < A[n]:
                limit = n
                break
                
        if limit == 0:
            print(money)
        else:
            for n in range(1,N+1):
                if n == limit+1:
                    money += A[n-1]*kabu
                    break
                else:
                    if flag == 'down':
                        if A[n-1] < A[n]:
                            kabu = money//A[n-1]
                            money -= A[n-1]*kabu
                            flag = 'up'
                    elif flag == 'up':
                        if A[n-1] > A[n]:
                            money += A[n-1]*kabu
                            flag = 'down'
            print(money)
        
if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
import itertools



n = int(input())
ajs = input().split()
ajs = [int(aj) for aj in ajs]
ajs = np.array(ajs)




ajs_delta = ajs[1:] - ajs[:-1]

for i in range(1, ajs_delta.shape[0]):
  if ajs_delta[i] == 0:
    ajs_delta[i] = ajs_delta[i-1]
    

  
kyokuchi = {'lower':[], 'upper':[]}


first_value = 0
for aj_delta in ajs_delta:
  if aj_delta != 0:
    first_value = aj_delta
    break

if ajs_delta[0] > 0:
  kyokuchi['lower'].append(0)
elif ajs_delta[0] < 0:
  
  pass
elif ajs_delta[0]  == 0 and first_value > 0:
  kyokuchi['lower'].append(0)

for i in range(0, ajs_delta.shape[0]-1):
  if ajs_delta[i] * ajs_delta[i+1] < 0:
    if ajs_delta[i] > 0:
      kyokuchi['upper'].append(i+1)
    else:
      kyokuchi['lower'].append(i+1)

if ajs_delta[-1] > 0:
  kyokuchi['upper'].append(n-1)
else:
  
  pass


kyokuchi_s = sorted(kyokuchi['upper'] + kyokuchi['lower'])

current_money = 1000
pos = 0

for idx in kyokuchi_s:
  if pos >= 0:
    stock = int(current_money / ajs[idx])
    current_money = current_money % ajs[idx]
    pos = -1
  else:
    current_money += stock * ajs[idx]
    stock = 0
    pos = 1
    
print(current_money)

Solution (Python):
n=int(input())
m=list(map(int,input().split()))
a=[]
if m[0]<m[1]:
    a.append([0,-1])
for i in range(1,n-1):
    if m[i]>m[i-1] and m[i]>=m[i+1]:
        a.append([i,1])
    elif m[i]<=m[i-1] and m[i]<m[i+1]:
        a.append([i,-1])
    elif m[i]<m[i-1] and m[i]<=m[i+1]:
        a.append([i,-1])
    elif m[i]>=m[i-1] and m[i]>m[i+1]:
        a.append([i,1])
if m[n-1]>m[n-2]:
    a.append([n-1,1])

k=0
r=1000
t=0
for i in range(len(a)):
    if a[i][1]==-1 and t==0:
        k=r//m[a[i][0]]
        r-=k*m[a[i][0]]
        t=1
    elif a[i][1]==1 and t==1:
        
        
        r+=k*m[a[i][0]]
        k=0
        t=0
        
    
print(r)

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

future = []

for i in range(n-1):
  if a[i] < a[i+1]:
    future.append("u")
  elif a[i] == a[i+1]:
    future.append("e")
  else:
    future.append("d")

try:
  last_u = future[::-1].index("u")
except ValueError:
  print(1000)
else:
  last_u = n - last_u - 1
  for i in range(last_u):
    if future[i] == "u":
      if kabu != 0:
        ans += kabu * a[i]
        kabu = 0
      kabu = ans // a[i]
      ans -= kabu * a[i]
    if future[i] == "d":
      if kabu != 0:
        ans += kabu * a[i]
        kabu = 0
  if kabu != 0:
    ans += kabu * a[last_u]
  print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
original_money=1000
money=1000
stock=0
buy_price=l[0]
for i in range(n-1):
    if buy_price<l[i]:
        money+=stock*l[i]
        stock=0
    if l[i+1]>l[i]:
        if stock==0:
            buy_price=l[i]
            stock=money//l[i]
            money-=stock*buy_price
    else:
        continue

if l[n-1]>buy_price and stock:
    money+=stock*l[n-1]

print(money)

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

Solution (Python):

"""
@author: H_Hoshigi
"""
def main():
    N = int(input())
    A_lsit = list(map(int, input().split()))

    DP_list = [1000]*N

    for i in range(N):
        bought_stock_number = DP_list[i]//A_lsit[i]
        remained_monye = DP_list[i]%A_lsit[i]
        max_i = DP_list[i]
        for j in range(i+1, N):
            DP_list[j] = max(
                max_i,
                DP_list[j],
                bought_stock_number*A_lsit[j] + remained_monye
            )
    print(DP_list[-1])

if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=1000
for i in range(1,N):
  if A[i]>A[i-1]:
    Key=ans//A[i-1]
    key=ans%A[i-1]
    Ans=Key*A[i]+key
    ans=Ans
print(ans)

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

from collections import defaultdict
sys.setrecursionlimit(10**7)
import math







def run():
    N, *A = map(int, read().split())
    buy = True
    money = 1000
    stock = 0
    for i, a in enumerate(A[:-1]):
        if buy:
            if a < A[i+1]:
                stock += money // a
                money %= a
                buy = False
        else:
            if a > A[i+1]:
                money += stock * a
                stock = 0
                buy = True
    money += stock * A[-1]
    print(money)



if __name__ == "__main__":
    run()

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

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

print(money)

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

money = 1000
kabu, comp = 0, 0
for cnt, i in enumerate(a):
    comp = a[cnt+1]-a[cnt] if cnt+1<len(a) else -1
    if kabu == 0 and comp > 0:
        kabu = money // i
        money -= kabu*i
    elif kabu > 0 and comp < 0:
        money += kabu*i
        kabu = 0

print(money)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
c = 1000
s = 0
l = len(A)
for i, a in enumerate(A):
    if i < l - 1:
        if a == A[i+1]:
            continue
        elif a < A[i+1]:
            if c >= a:
                ss = c // a
                s += ss
                c -= ss * a
        else:
            if s > 0:
                c += s * a
                s = 0
    else:
        if s > 0:
            c += s * a
            s = 0
print(c)

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

ans = []

now = a[0]
flag = 'u'

for i in range(n-1):
    if flag == 'u':
        if now < a[i+1]:
            ans.append(a[i])
            if i == n-2:
                
                ans.append(a[i+1])
                break
            
            flag = 'd'
        now = a[i+1]
        
    else:
        if now > a[i+1]:
            ans.append(now)

            
            flag = 'u'
        now = a[i+1]

if flag == 'd':
    ans.append(a[n-1])

okane = 1000

for i in range(len(ans)//2):
    okane += (okane // ans[2*i])*(ans[2*i+1]-ans[2*i])
    
    
print(okane)

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

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


s=[0]*n
m=[0]*n

if a[1]>a[0]:
  s[0]=1000//a[0]
  m[0]=1000-a[0]*s[0]
else:
  s[0]=0
  m[0]=1000

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

m[n-1]=m[n-2]+a[n-1]*s[n-2]
print(m[n-1])

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
c = [0]*n
cash = 1000
stock = 0


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

if b[1]=='+':
    c[0]=1
elif b[1]=='-':
    c[0]=2

for i in range(1,n-1):
    if b[i]=='+' and b[i+1]=='-':
        c[i]=2
    elif b[i]=='-' and b[i+1]=='+':
        c[i]=1

if b[n-1]=='+':
    c[n-1]=2
elif b[n-1]=='-':
    c[n-1]=1
last = n
ch = 0
for i in range(n):
    if c[n-1-i]==1:
        ch = 1
    elif c[n-1-i]==2 and ch==0:
        break
    elif c[n-1-i]==2 and ch==1:
        last = n-i
        break
    elif ch == 1 and i==n-1:
        last=0
        break

if c[0]==0:
    for i in range(1,n):
        if c[i]==2:
            c[0]=1
            break

for i in range(last):
    if c[i]==1:
        stock += cash//a[i]
        cash -= cash//a[i]*a[i]
    if c[i]==2:
        cash += stock*a[i]
        stock =0
print(cash)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
P = ['n'] * N
s = 1
if A[N-1] > A[N-2]:
    P[N-1] = 'sell'
    s = 0
for i in range(N-2):
    if s == 1:
        if A[N-i-2] > A[N-i-3]:
            P[N-i-2] = 'sell'
            s = 0
    else:
        if A[N-i-2] < A[N-i-3]:
            s = 1
            P[N-i-2] = 'buy'
if s == 0:
    P[0] = 'buy'
    s = 1
else:
    P[0] = 'n'

m = 1000
stock = 0
    
for i in range(N):
    if s == 1:
        if P[i] == 'buy':
            stock = m // A[i]
            m = m % A[i]
            s = 0
    else:
        if P[i] == 'sell':
            m += stock*A[i]
            stock = 0
            s = 1
print(m)

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

for i in range(N-1):
    B[i] = A[i+1] -  A[i]



yen = 1000
i = 0
while i < N - 1:
    if B[i] > 0:
        j = i
        while j < N - 1 and B[j] >= 0:
            j += 1
        j -= 1
        
        kabu = yen // A[i]
        yen += kabu * (A[j+1] - A[i])
        i = j + 1
        continue
    if B[i] < 0:
        j = i
        while j < N - 1 and B[j] <= 0:
            j += 1
        j -= 1
        
        i = j + 1
        continue
    i += 1

print(yen)

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

money = 1000
kabu = 0
pos = 'no'
p = 0
for i, t in enumerate(al):
    if pos != 'no':
        if p < t:
            money += (kabu*t)
            kabu = 0
            pos = 'no'

    if pos == 'no' and money // t >= 1:
        f = False
        for j, a in enumerate(al[i:]):
            if a > t:
                if t == min(al[i:i+j+1]):
                    f = True
                    break
        if f:
            kabu += (money // t)
            money -= kabu*t
            pos = 'b'
            p = t
print(money)

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

money = 1000
kabu = 0
tmp = a[0]
ans = 0
for i in a:
    if i > tmp:
        kabu += money//tmp
        money -= (money//tmp)*tmp
    elif i < tmp:
        money += kabu*tmp
        kabu = 0
        ans = max(money,ans)
    tmp = i

if kabu > 0:
    money += kabu*a[-1]

print(max(money,ans))

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

money = 1000
stock = 0
price = float("INF")
for i in range(N-1):
    if price < A[i] and stock != 0:
        money += stock * A[i]
        stock = 0
        price = float("INF")

    if A[i] < A[i+1]:
        stock += money // A[i]
        money -= A[i] * stock
        price = A[i]

if price < A[N-1] and stock != 0:
    money += stock * A[N-1]
    
print(money)

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

stair = []
startDate = 0
lastDate = N - 1
firstFlag = True
for i in range(1, N):
    if (A[i] == A[i - 1]):
        stair.append(0)
        continue
    if (A[i] < A[i - 1]):
        stair.append(-1)
    else:
        if (firstFlag):
            firstFlag = False
            startDate = i - 1
        stair.append(1)
        lastDate = i

upflag = False
base = 1000
ticket = 0
for i in range(startDate, lastDate):
    if (stair[i] == 0):
        continue
    if (upflag):
        if (stair[i] == -1):
            base += ticket * A[i]
            upflag = False
    else:
        if (stair[i] == 1):
            ticket = int(base / A[i])
            base -= ticket * A[i]
            upflag = True

if (ticket != 0):
    base += ticket * A[lastDate]

print(base)

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

dp = [0 for j in range(n)]
dp[0] = 1000
for i in range(n):
    ai = a[i]
    m = dp[i]
    s = m // ai

    for j in range(i + 1, n):
        dp[j] = max(dp[j], m + s * (a[j] - ai))

    for j in range(n - 1):
        dp[j + 1] = max(dp[j + 1], dp[j])
print(dp[-1])

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

ans=1000
i=0
while i<n:
    j=i
    mi=a[i]
    while j<n and mi>=a[j]:
        mi=min(mi,a[j])
        j+=1
    
    if j==n:
        break

    stock=ans//mi
    ans-=stock*mi
    ans+=a[j]*stock
    i=j

print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))

    now = 1000
    stock_num = 0

    for i in range(len(a)-1):
        
        if stock_num == 0:
            if a[i] < a[i + 1]:
                stock_num = int(now / a[i])
                now -= a[i] * stock_num

        
        else:
            if a[i] > a[i + 1]:
                now += a[i] * stock_num
                stock_num = 0

    if not stock_num == 0:
        now += a[-1] * stock_num
    print(now)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
old = A[0]
a = 0 
b = 1000 
for i in range(1, N):
    Ai = A[i]
    if old < Ai:
        a += b // old
        b = b % old
    elif old > Ai:
        b += old * a
        a = 0
    old = Ai
b += old * a
print(b)

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

M = 1000
X = 0
i = 0

while i<N-1:
  if A[i]<A[i+1]:
    M = M + X * A[i]
    X = M // A[i]
    M = M - X * A[i]
    i += 1
  elif A[i]>A[i+1]:
    M = M + X * A[i]
    X = 0
    i += 1
  else:
    i += 1

print(A[N-1] * X + M)