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.



Sample input:
7
100 130 130 130 115 115 150


Sample output:
1685

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
ls=list(map(int,input().split()))
c=sorted(ls)
mini=c[0]
maxi=c[-1]
lb=0
k=1000
st=0
pmin=c.index(mini)
pmax=c.index(maxi)
for i in range(n):
  if i<n-1:
    if ls[i]>ls[i+1]:
      k+=ls[i]*st
      st=0
    elif ls[i]<ls[i+1]:
      st+=k//ls[i]
      k=k-ls[i]*(k//ls[i])
      lb=ls[i]
  else:     #最終日に売り抜くパターン！
    if lb<ls[i]:
      k+=ls[i]*st
      st=0
k+=lb*st
print(k)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
money = 1000
have = 0
buy = 0
c = []
check = a[0]
start = False
check2 = -1
for i in range(1,n):
    if check2 > a[i] and start:
        c.append((check,check2))
        check = a[i]
        check2 = -1
        #print(i)
    elif check >= a[i] and start:
        check = a[i]
    elif check2 <= a[i] and start:
        check2 = a[i]
    elif check < a[i] and not start:
        check2 = a[i]
        start = True
    elif check >= a[i] and not start:
        check = a[i]
    #print(check,check2)
if check2 != -1 and check != check2:
    c.append((check,check2))
#print(c)
for i in c:
    have = money//i[0]
    money -= i[0]*have
    money += have * i[1]
print(money)

Submission status: Accepted

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

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

print(mon)

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

ans = [1000, 0]

for i in range(n-1):
    if a_list[i] < a_list[i+1]:
        # 買っておいて
        ans[1] = ans[1] + ans[0] // a_list[i]
        ans[0] = ans[0] - (ans[0] // a_list[i]) * a_list[i]
        # 売る
        ans[0] = ans[0] + ans[1] * a_list[i+1]
        ans[1] = 0
    elif a_list[i] > a_list[i+1]:
        # 売っておいて
        ans[0] = ans[0] + ans[1] * a_list[i]
        ans[1] = 0
        # 買う
        ans[1] = ans[0] // a_list[i+1]
        ans[0] = ans[0] - (ans[0] // a_list[i+1]) * a_list[i+1]

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

Submission status: Accepted

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

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


def tot(ud):
    tmp = 1000
    kab = 0
    for i in range(n):
        if ud[i] ==-1: #谷　buy kab
            if kab==0:
                kab,tmp = divmod(tmp,A[i])
        elif ud[i] ==1: #　山　sell kab
            tmp +=kab*A[i]
            kab = 0
#        print(tmp,kab)
#    print(ud)
    return tmp
n = ni()
A = list(ma())
ans = 0
ud = pe(1000)
tmp = tot(ud)
ans = max(ans,tmp)
ud = pe(0)
tmp = tot(ud)
ans = max(ans,tmp)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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


print(mon)

Submission status: Accepted

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

money = 1000
stock = 0

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

Submission status: Accepted

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

kai = [False] * N
uri = [False] * N
# 買いポイント探す
i = 0


# すべて同じ株価or単調減少
tmp = A[0]
flag = True
for i in range(1, N):
    if A[i-1] < A[i]:
        flag = False

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

start = 10000
stop = 10000

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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


print(max(1000,start))

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

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

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

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

            i=nxt
        maxyen=max(maxyen,yen)
    print(maxyen)
    #ss=readline().rstrip()
    #yn(1)

    return

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

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

mon = 1000
stock = 0

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

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


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

Submission status: Accepted

Solution code:
import sys
import math
sys.setrecursionlimit(int(1e6))
#dprint = print
def dprint(*args):
  pass

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

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

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

print(money)

Submission status: Accepted

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

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

money = 1000
stock = 0

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

print(money)

Submission status: Accepted

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

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

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


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# D

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

cash = 1000
stock = 0

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

Submission status: Accepted

Solution code:
import sys

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

# print(a_list)
for i in range(len(a_list) - 1):
    a_today = a_list[i]
    for a in a_list[i+1:]:
        a_next = a
        if a != a_today:
            break
    # これからずっと値段が変わらない場合は全部売ってあと放置
    if a_next == a_today:
        possesion += num_stock * a_list[-1]
        print(possesion)
        sys.exit()
    
    if a_today < a_next:
        # 買えるだけ買う
        possesion_yesterday = possesion
        possesion = possesion % a_today
        num_stock += possesion_yesterday // a_today
    if a_today > a_next:
        # うれるだけうる
        possesion += num_stock * a_today
        num_stock = 0
#     print(a_today, a_next, possesion, num_stock)

# 最終日は買えるだけ買う
possesion += num_stock * a_list[-1]
print(possesion)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
PriceList = list(map(int, input().split()))
poss = 1000
stock = 0
basis = PriceList[0]
for price in PriceList:
    if basis > price:
        basis = price
    if basis < price:
        # 株の購入
        stock = stock + poss // basis
        poss = poss % basis
        basis = price
        # 株の売却
        poss = poss + stock * price
        stock = 0
print(poss)

Submission status: Accepted

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

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

Submission status: Accepted

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

def fun(N, As):
    stocks = 0
    money = 1000
    prev = 201
    for n in range(N):
        curr = As[n]
        if n < N - 1:
            nex = As[n + 1]
        else:
            nex = 99
        if curr >= prev and curr >= nex:
            # 局所最大なら全部売る
            money += curr * stocks
            stocks = 0
        elif curr <= prev and curr <= nex:
            # 局所最小なら全部買う
            stocks += int(money / curr)
            money -= curr * int(money / curr)
        prev = curr
    return money

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

Submission status: Accepted

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

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

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

print(money)

Submission status: Accepted

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

# 100 90 80 70 60 200
current_assets = 1000
current_price = 0
min_price = 999
for i in range(n-1):
    if min_price > data[i]:
        min_price = data[i]

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

        current_assets += buy_counts * data[i+1]

        # print(data[i], data[i+1])
        # print("MIN", min_price)
        min_price = 999

print(current_assets)

Submission status: Accepted

Solution code:
import math

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

    # dp=[1]*202020
    # for i in range(K):
    #     dp[K]*=A[i]

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

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

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

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




    print(Yen)

resolve()

Submission status: Accepted

Solution code:
import sys

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

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

print(money)

Submission status: Accepted

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

print(m+s*a[len(a)-1])

Submission status: Accepted

Solution code:
N = int(input())
money = 1000
M = N//2
prices = list(map(int, input().split()))
prices.append(0)
dp = [[0 for j in range(M+1)]for i in range(N+1)]
dp[0][0] = money
max_p = money

for i in range(1, N+1):
    for j in range(M+1):
        dp[i][j] = dp[i-1][j]
        if j > 0:
            for l in range(i):
                x = dp[l][j-1] % prices[l] + dp[l][j-1]//prices[l]*prices[i]
                if dp[i][j] < x:
                    dp[i][j] = x
            if max_p < dp[i][j]:
                max_p = dp[i][j]
print(max_p)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
money = 1000
cab = 0
lowprice = A[0]
check = True
for i in range(1,N):
    if(lowprice>A[i]):
        lowprice = A[i]
        check= True
    elif((lowprice<A[i]) and (check == True)):
        high_price = A[i]
        if(i<N-1):
            if(high_price > A[i+1]):
                cab=money//lowprice
                money-=(lowprice*cab)
                money+=(high_price*cab)
                cab = 0
                check = False
                lowprice = high_price
        else:
            cab=money//lowprice
            money-=(lowprice*cab)
            money+=(high_price*cab)
            cab = 0
            check = False
            lowprice = high_price
print(money)

Submission status: Accepted

Solution code:
N = int(input())
L = [0 for i in range(N)]
L = list(map(int,input().split()))


T = 0
M = 1000

I = 0
J = 0
while I < N-1:
    
    if I+J+1 > N or L[I+J] >= L[I+1+J]:
        M += (M//L[I]) * (L[I+J]-L[I])
        I += J+1
        J = 0
    else:
        if I+J+1 == N-1:
          M += (M//L[I]) * (L[I+J+1]-L[I])
          I += J+1
          J = 0
        else:J += 1

print(M)

Submission status: Accepted

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

fp = A[0]
tmp = 0
c = 1000
stock = 0
IsSearchTop = False
first = True
for i in range(1,N):
#
        if IsSearchTop:
            if fp <= A[i]:
                fp = A[i]
                if i == N-1:
                    c += stock * fp
            elif fp > A[i]:
                c += stock * fp
                stock = 0
                fp = A[i]
                IsSearchTop = False
        else:
            if fp >= A[i]:
                fp = A[i]
            elif fp < A[i]:
                stock += c // fp
                c -= stock * fp
                IsSearchTop = True
                fp = A[i]
                if i == N - 1:
                    c += stock * fp
print(c)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
B=[]
B.append(A[0])
for i in range(1,N):
    if A[i]!=B[-1]: B.append(A[i])
i=1
length=len(B)
while i<length-1:
    if B[i]>B[i-1] and B[i]<B[i+1] or B[i]<B[i-1] and B[i]>B[i+1]:
        B.pop(i)
        length-=1
    else:
        i+=1
yen=1000
kabu=0
for i in range(len(B)):
    if len(B)>=2 and B[0]<B[1]:
        if i%2==0 and i<len(B)-1:
            kabu=yen//B[i]
            yen=yen%B[i]
        elif i%2==1:
            yen=yen+kabu*B[i]
    else:
        if i%2==1 and i<len(B)-1:
            kabu=yen//B[i]
            yen=yen%B[i]
        elif i%2==0:
            yen=yen+kabu*B[i]
print(yen)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=[1000]
m=1000
k=0
for i,num in enumerate(a):
	m=k*num+m
	k=0
	ans.append(m)
	if i<n-1:
		if num<a[i+1]:
			k=m//num
			m=m-k*num
print(max(ans))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cash = 1000
for i in range(N-1):
    if A[i] < A[i+1]:
        cash += (A[i+1]-A[i])*(cash//A[i])
print(cash)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_max = max(A)

is_start_increase = [0]*N
is_end_increase = [0]*N
is_increasing = [0]*N

# for i in range(1, N):
#     if A[i-1] <= A[i]:
#         is_increasing[i] = 1
# next_is_end = 0
# is_total_start = 0
# for i in range(0, N-1):
#     if next_is_end:
#         if is_increasing[i] == 1 and is_increasing[i+1] == 0:
#             is_end_increase[i] = 1
#             next_is_end = 0

#     if not is_total_start:
#         if is_increasing[i+1] == 1:
#             is_start_increase[i] = 1
#             is_total_start = 1
#             next_is_end = 1
#     # if i == 0 and is_increasing[i+1] == 1:
#     #     is_start_increase[i] = 1
#     #     continue
#     # if i == 1 and is_start_increase[i-1]==1:
#     #     continue
#     elif next_is_end == 0:
#         if is_increasing[i] == 1 and is_increasing[i-1] == 0:
#             is_start_increase[i] = 1
#             next_is_end = 1

for i in range(N-1):
    if A[i] <= A[i+1]:
        is_increasing[i] = 1
if is_increasing[0] == 1:
    is_start_increase[0] = 1
for i in range(1, N):
    if is_increasing[i] == 1 and is_increasing[i-1] == 0:
        is_start_increase[i] = 1
    if is_increasing[i] == 0 and is_increasing[i-1] == 1:
        is_end_increase[i] = 1

money = 1000
stock = 0
stocks = []
for i in range(N):
    if i==N-1:
        # print("last", i)
        money += stock * A[i]
        break
    if is_start_increase[i]:
        # print("start", i)
        stock += money // A[i]
        money -= A[i] * stock
    if is_end_increase[i]:
        # print("end", i)
        money += stock * A[i]
        stock = 0
    stocks.append(stock)

print(money)

Submission status: Accepted

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

A.append(0)

money = 1000
kau = 0

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

Submission status: Accepted

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

if a[0] < a[1]:
    botom.append(0)
if a[n-1] >= a[n-2]:
    peak.append(n-1)
for i in range(1, n-1):
    if a[i] >= a[i-1] and a[i] > a[i+1]:
        peak.append(i)
    if a[i] <= a[i-1] and a[i] < a[i+1]:
        botom.append(i)

for i in range(n):
    if i in botom:
        kabu += money//a[i]
        money %= a[i]
    if i in peak:
        money += kabu*a[i]
        kabu = 0

print(money)

Submission status: Accepted

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

money = 1000
kabu = 0
while A[-1] <= A[-2]:
    A.pop()
    if len(A) == 1:
        break
t = [0] * len(A)
for i in range(1, len(A)):
    if A[i] > A[i-1]:
        t[i] = 1
    elif A[i] < A[i-1]:
        t[i] = -1
    else:
        t[i] = 0

n_move = 1

for i in range(len(A) - 1):
    if n_move == 1 and t[i] != 1 and t[i+1] == 1:
        kabu += money // A[i]
        money -= A[i] * (money // A[i])
        
        n_move = 1 - n_move
    elif n_move == 0 and t[i] != -1 and t[i+1] == -1:
        money += A[i] * kabu
        kabu = 0
        n_move = 1 - n_move
print(money + kabu * A[-1])

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
value = 1000
current = 0
stocks = 0
for i in range(N-1):
    if A[i] < A[i+1] :
        h = value//A[i]
        stocks+=h
        value = value - h*A[i]
    if A[i]==max(A[i:]) :
        value = value + stocks*A[i]
        stocks = 0
    elif A[i] > A[i+1]:
        value = value + stocks*A[i]
        stocks = 0
value = value + stocks*A[N-1]
print(value)

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(i) for i in input().split()]

stock = 0
money = 1000
for i in range(n):
  if i < n - 1:
    if lst[i] <= lst[i + 1]:
      stock += money // lst[i]
      money = money % lst[i]
    if lst[i] > lst[i + 1]:
      if stock > 0:
        money += stock * lst[i]
        stock = 0
  if i == n - 1:
    if stock > 0:
      money += stock * lst[i]
      stock = 0
  #print(money)
print(money)