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 = [int(a) for a in input().split()]
d = [0] * (N - 1)

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

money = 1000
stock = 0
state = 0
for i in range(N - 1):
    if d[i] == 1 and state == 0:
        stock += money // A[i]
        money %= A[i]
        state = 1
    elif d[i] == -1 and state == 1:
        money += A[i] * stock
        stock = 0
        state = 0

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
dp = [0] * (N + 1)
dpm = [0] * (N + 1)
dpm[0] = 1000
for i in range(N):
  dp[i + 1] = max(dp[i + 1], dp[i], dpm[i] // a[i] * 1000 + dpm[i] % a[i])
  dpm[i + 1] = max(dpm[i + 1], dpm[i], a[i] * (dp[i] // 1000) + dp[i] % 1000)
print(dpm[-1])

Submission status: Accepted

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

money=1000
kabu=0

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

print(money)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n=int(input())
x=input()
x0=x.split()
a=[int(n) for n in x0]
m=1000
for i in range(n-1):
    if a[i]<a[i+1]:
        m1=int(math.floor(m/a[i]))
        m=m+m1*(a[i+1]-a[i])
print(m)

Submission status: Accepted

Solution code:

n = int(input())
A = list(int(x) for x in input().split())
m = 1000
k = 0

last = n-1
tmp = A[n-1]
for i in reversed(range(n)):
    if tmp > A[i]:
        last = i + 1
        break
    tmp = A[i]

for i in range(n):
    if i == last:
        m += A[i] * k
        break
    elif i + 1 <= last and A[i] < A[i + 1]:
        k += m // A[i]
        m -= A[i] * (m // A[i])
    elif i + 1 <= last and A[i] > A[i + 1]:
        m += A[i] * k
        k = 0


print(m)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[]
pre=-1
for x in a:
  if x!=pre:
    b.append(x)
  pre=x
n=len(b)
if n==1:
  print(1000)
  exit()
# はじめに売ることはないので初期値はFalse
up=False
money=1000
kabu=0
for i in range(n-1):
  # 上がり調子だったのに、次下がるという場合、売る。
  if b[i]>b[i+1]:
    if up:
      money+=kabu*b[i]
      kabu=0
    up=False
  elif b[i]<b[i+1]:
    # 下がり調子だったのに、次上がるという場合、買う。
    if not up:
      kabu+=money//b[i]
      money%=b[i]
    up=True
if b[n-2]<b[n-1]:
  money+=kabu*b[-1]
  kabu=0
print(money)

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


# 処理内容
def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    money = 1000
    tmp = A[0]
    for i in range(1, N):
        tmp = min(tmp, A[i])
        if tmp < A[i]:
            m = money // tmp
            money -= m * tmp
            money += m * A[i]
            tmp = A[i]
    
    print(money)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(n-1):
  #print(i,ans,kabu)
  if a[i+1] > a[i] and direct <= 0:
    kabu = ans // a[i]
    ans %= a[i]
    direct = 1
    # 次が下→上の切り替え時に買う
  elif a[i+1] < a[i] and direct >= 0:
    # 上→下の切り替え時に売る
    ans += kabu*a[i]
    kabu = 0
    direct = -1
ans += kabu*a[-1]
print(ans)

Submission status: Accepted

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

ans = 1000
for i in range(N):
  if i >= 1:
    if A[i] >= A[i-1]:
      q, mod = divmod(ans, A[i-1])
      ans = q*A[i] + mod

print(ans)

Submission status: Accepted

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

mon=1000
kabu=0

for i in range(N-1):
    if A[i] < A[i+1]:
        mon += kabu*A[i]
        kabu = mon//A[i]
        mon = mon - (kabu*A[i])
    else:
        mon += kabu*A[i]
        kabu = 0
    #print(mon)
    #print(kabu)
    #print("----")

mon += kabu*A[N-1]

print(mon)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1000
stock = 0
for i, num in enumerate(a):
  if i == n-1:
    ans += stock*num
  else:  
    if num > a[i+1]:
      ans += stock*num
      stock = 0
    elif num < a[i+1]:
      stock += ans//num
      ans %= num
    else:
      continue
print(ans)

Submission status: Accepted

Solution code:
(N, ) = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]

M = 1000
S = 0

i = 0
while True:
    while A[i] >= A[i+1]:
        i += 1
        if i == N - 1:
            print(M)
            exit()
    # A[i] < A[i+1]
    C = M // A[i]
    M -= A[i] * C
    S += C

    while A[i] <= A[i+1]:
        i += 1
        if i == N - 1:
            M += S * A[N-1]
            print(M)
            exit()
    # A[i] > A[i+1]
    M += A[i] * S
    S = 0

Submission status: Accepted

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

# 金
ans=1000
# 株
cnt=0
for i in range(1, n):
    if a[i] > a[i - 1]:
        b[i] -= 1
        b[i-1]+=1

for i in range(n):
    if b[i]==1:
        cnt+=ans//a[i]
        ans-=cnt*a[i]
    elif b[i]==-1:
        ans+=cnt*a[i]
        cnt=0

print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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

anow=0
anxt=0

m=1000
s=0

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

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

Submission status: Accepted

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

cash = 1000
stock = 0

for a, b in zip(A[:-1], A[1:]):
    if a <= b:
        n = int(cash / a)
        cash -= n * a
        stock += n
    else:
        cash += stock * a
        stock = 0

cash += stock * A[-1]
print(cash)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
kane = 1000
kabu = 0
for i in range(1, n):
  if a[i] > a[i-1]:
    kabu = kane // a[i-1]
    kane = kane - kabu*a[i-1]
    kane = kane + kabu*a[i]
    kabu = 0
print(kane)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=1000
dp=[0]
for i in range(N-1):
  if A[i]<A[i+1]:
    dp.append(1)
  else:
    dp.append(0)
o=len(dp)
dp.append(0)
for i in range(o):
  if dp[i]==1 and dp[i+1]==0:
    dp[i]=2
  if dp[i]==0 and dp[i+1]==1:
    dp[i]=-1
p=0
for i in range(N):
  if dp[i]==-1:
    p=ans//A[i]
    ans=ans%A[i]
  if dp[i]==2:
    ans+=A[i]*p
    p=0
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
l=list(map(int,input().split()))
l2=[]
for i in range(N-1):
   if l[i+1]-l[i]>0:
      l2.append("p")
   elif l[i+1]-l[i]==0:
      l2.append("n")
   else:
      l2.append("m")
money=1000
kabu=0
for i in range(N-1):
   if kabu==0 and l2[i]=="p":
      kabu+=money//l[i]
      money-=(money//l[i])*l[i]
   elif kabu!=0 and l2[i]=="m":
      money+=l[i]*kabu
      kabu=0
if kabu>0:
   money+=kabu*l[-1]
print(money)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


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

    for i in range(1, len(a)):
        add_stock = 0
        # print(money, stock)
        if a[i - 1] < a[i]:
            add_stock = money // a[i - 1]
            money -= add_stock * a[i - 1]
            stock += add_stock
        elif a[i - 1] > a[i]:
            money += a[i - 1] * stock
            stock = 0

    print(money + stock * a[-1])


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """7
100 130 130 130 115 115 150"""
        output = """1685"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """6
200 180 160 140 120 100"""
        output = """1000"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """2
157 193"""
        output = """1216"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy


def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 1000
    for i in range(N - 1):
        if A[i] < A[i + 1]:
            ans += (A[i + 1] - A[i]) * (ans // A[i])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = input().split()
for i in range(N):
  A[i] = int(A[i])
money = 1000
stock = 0

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
po="n"
pr=A[0]
T=[]
for i in range(1,N):
    if po=="n":
        if pr<A[i]:
            T.append(["Kai",A[0]])
            po="UP"
            pr=A[i]
        else:
            po="DWN"
            pr=A[i]
    elif po=="UP":
        if pr<=A[i]:
            pr=A[i]
        else:
            T.append(["Uri",pr])
            po="DWN"
            pr=A[i]+0
    elif po=="DWN":
        if pr>=A[i]:
            pr=A[i]
        else:
            T.append(["Kai",pr])
            po="UP"
            pr=A[i]
if po=="UP":
    T.append(["Uri",pr])

#print(T)
Money=1000
Kabu=0
for i in range(len(T)):
    if T[i][0]=="Kai":
        Kabu+=Money//T[i][1]
        Money%=T[i][1]
    else:
        Money+=Kabu*T[i][1]
        Kabu=0
print(Money)

Submission status: Accepted

Solution code:
import math
import sys
from itertools import permutations
input = sys.stdin.readline

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

flag=True
cur=1000
st=0
for i in range(N-1):
    if flag and arr[i+1]>arr[i]:
        st=cur//arr[i]
        cur-=st*arr[i]
        flag=False
    elif (not flag) and arr[i+1]<arr[i]:
        cur+=st*arr[i]
        st=0
        flag=True

cur+=st*arr[N-1]
print(cur)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
ans=1000
for i,j in zip(a,a[1:]):
    if i<j:
        ans+=ans//i*abs(i-j)
print(ans)

Submission status: Accepted

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

money += sp_list[N-1] * stock

print(money)

Submission status: Accepted

Solution code:
LI = lambda: list(map(int, input().split()))

N = int(input())
A = LI()


def main():
    ans = 1000
    k = 0
    for i in range(N - 1):
        if A[i] == A[i + 1]:
            continue
        if A[i] > A[i + 1]:
            ans += k * A[i]
            k = 0
        else:
            x = ans // A[i]
            ans -= x * A[i]
            k += x
    ans += k * A[-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int, input().split()))
kabu = 0
money = 1000
buy = True
prev = 1 << 50
for i, a in enumerate(As):
    if buy: #買いtrend
        if prev < a:
            #買う
            # print("buy",i, prev)
            kabu += money // prev
            money -= kabu * prev
            buy = False
    else:
        if prev > a:
            #売る
            # print("sell",i,prev)
            buy = True
            money += kabu * prev
            kabu = 0
    prev = a

print(money+kabu*a)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
A = LI()

tmp = 1000
stocks = 0
for i in range(N - 1):
    if A[i] < A[i + 1]: # 明日の方が高い　買う
        new_stocks = tmp // A[i]
        stocks += new_stocks
        tmp -= new_stocks * A[i]
    else:# 明日の方が安い　うる
        tmp += stocks * A[i]
        stocks = 0
tmp += stocks * A[-1]
print(tmp)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
slst = []
llst = []
stmp = 201
ltmp = 0
for i in a:
    if stmp >= i:
        stmp = i
    else:
        slst.append(stmp)
        stmp = i

for i in reversed(a):
    if ltmp <= i:
        ltmp = i
    else:
        llst.insert(0, ltmp)
        ltmp = i

total = 1000
for i in range(len(slst)):
    cnt = total // slst[i]
    total -= (slst[i] * cnt)
    total += (llst[i] * cnt)

print(total)

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int, input().split())) + [0]

total = 1000
stock = 0

if A[0] < A[1]:
    stock = total // A[0]
    total -= stock * A[0]

for i in range(1, N):
    if A[i] > A[i+1] and stock != 0:
        total += stock*A[i]
        stock = 0
    if A[i] < A[i+1] and stock == 0:
        stock = total//A[i]
        total -= stock*A[i]

print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input(""))
aa=input("").split(" ")
lista=[]
for i in aa:
    lista+=[int(i)]
s=1000
a=0
for i in range(n-1):
    tt=lista[i]
    t=lista[i+1]-tt
    
    if(t>0):
        a+=s//tt
        s%=tt
    elif(t<0):
        
        s+=tt*a
        a=0
s+=a*lista[n-1]
print(s)

Submission status: Accepted

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

p = 1000
s = 0

for i, j in zip(A, A[1:]):
  if i < j:
    d, m = divmod(p, i)
    s += d
    p = m
  elif i > j:
    p += s * i
    s = 0

if s != 0:
  p += s * A[-1]

print(p)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
money = 1000
stock = 0
mi = min(A)
last = -1
for i, (a, b) in enumerate(zip(A, A[1:])):
    if a > b:
        if stock > 0:
            money += stock * a
            stock = 0
            last = money
    elif a < b:
        stock += money // a
        money %= a

now = money + stock * A[-1]


print(max(last, now))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(money)

Submission status: Accepted

Solution code:
#m-solutions2020 d
import sys,math,collections,itertools
input = sys.stdin.readline

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

cashe = 1000
stake = 0

a =[]
for i in range(N-1):
    a.append(A[i+1]-A[i])

for i in range(N-1):
    if a[i]>0:
        stake += cashe//A[i]
        cashe = cashe%A[i]
    elif a[i]==0:
        continue
    else:
        cashe += stake*A[i]
        stake = 0
cashe += stake*A[-1]
print(cashe)

Submission status: Accepted

Solution code:
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]):
        cash += stock * a[i]
        stock = 0
    elif(a[i] < a[i+1]):
        stock += cash // a[i]
        cash %= a[i]
cash += stock * a[n-1]

print(cash)

Submission status: Accepted

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

kane=1000
kabu=0

for i in range(n-1):
    if alist[i] > alist[i+1]:
        kane += kabu * alist[i]
        kabu = 0
    else:
        kaerukabu = kane // alist[i]
        kabu += kaerukabu
        kane -= kaerukabu * alist[i]

print(kane + kabu * alist[n-1])

Submission status: Accepted

Solution code:
n=int(input())
*a,=map(int, input().split())
upd=[1 if a[i+1]-a[i]>0 else (0 if a[i+1]-a[i]==0 else -1) for i in range(n-1)]
#print(upd)
lastbuy=-1
lastsell=-1
now=1000
stock=0
for i in range(n-1):
    #print(now,stock,a[i])
    if upd[i]==1 and stock==0:
        lastbuy=i
        stock+=(now//a[i])
        now-=(now//a[i])*a[i]
    elif upd[i]==-1 and stock>0:
        lastsell=i
        now+=stock*a[i]
        stock=0

if stock>0:
    now+=max(a[lastbuy:])*stock
print(now)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
n_money = 1000
n_stock = 0
for i in range(N-1):
    if A[i+1] > A[i]:
        # buy
        n_buy_stock = n_money // A[i]
        n_stock += n_buy_stock
        n_money = n_money - n_buy_stock * A[i]
    else:
        # sell
        n_money += n_stock * A[i]
        n_stock = 0
n_money += n_stock * A[N-1]
print(n_money)

Submission status: Accepted

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = LMIIS()
    money = 1000
    stock = 0
    for i in range(N):
        if i +1 < N and A[i] < A[i+1]:
            stock += money // A[i]
            money %= A[i]
        elif i+1 < N and A[i] > A[i+1] or i == N-1:
            money += stock * A[i]
            stock = 0
    print(money)



    
main()

Submission status: Accepted

Solution code:
import math

n=int(input())
arr=list(map(int,input().split(" ")))

cap= 1000
stocks=0
for i in range(n-1):
    if arr[i]<arr[i+1]:
        k= math.floor(cap/arr[i])
        stocks+=k
        cap-=k*arr[i]
    elif arr[i]>arr[i+1]:
        cap += stocks * arr[i]
        stocks =0
cap+=stocks*arr[-1]
print(cap)

Submission status: Accepted

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

ch = 0
buy = []
sell = []
for i in range(1,N):
    if ch == 0:
        if A[i-1] < A[i]:
            buy.append(A[i-1])
            ch = 1
    else:
        if A[i-1] > A[i]:
            sell.append(A[i-1])
            ch = 0
    if ch == 1 and i == N-1:
        sell.append(A[N-1])
    
ans = 1000
for i in range(len(sell)):
    kabu = ans // buy[i]
    ans %= buy[i]
    ans += kabu * sell[i]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
m = 1000
b = []
s = []
flag = True
count = 0
while count < n - 1:
    if flag == True:
        if a[count] >= a[count + 1]:
            count += 1
        else:
            b.append(a[count])
            flag = False
    else:
        if a[count] <= a[count + 1]:
            count += 1
        else:
            s.append(a[count])
            flag = True
if len(b) > len(s):
    s.append(a[-1])
k = 0
for i in range(len(b)):
    k += m // b[i]
    m %= b[i]
    m += s[i] * k
    k = 0
print(m)

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    A = LI()

    s = 0
    f = 1000
    for i in range(N-1):
        f += s * A[i]
        if A[i+1] > A[i]:
            s = f // A[i]
            f -= s * A[i]
        else:
            s = 0
    print(f+s*A[-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

m=1000
n=0
flag=0
for i in range(N-1):
  if flag==1:
    m += n*A[i]
    n = 0
    flag = 0
  
  if A[i+1]>A[i]:
    buy = m//A[i]
    n += buy
    m -= buy*A[i]
    flag=1

if flag == 1:
  m += n*A[-1]
  n = 0
  flag = 0
  
print(m)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
 
def main():
    n=int(input())
    A=list(map(int, input().split()))
    yen=1000
    kabu=0
    for i in range(n-1):
        if A[i] < A[i+1]:
            kabu+=yen//A[i]
            yen=yen%A[i]
           
        elif A[i] > A[i+1]:
            yen+= kabu*A[i]
            kabu=0
    print(yen+kabu*A[n-1])


 
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
now=1000
n=int(input())
l=list(map(int,input().split()))+[0]
m=l[0]
i=1
while i<n:
    if l[i]<m:m=l[i]
    else:
        while i<n and l[i]<l[i+1]:
            i+=1
        k=now//m
        now=now%m + k*l[i]
        m=l[i]
    i+=1
print(now)

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
    
def do():
    n=INT()
    A=LIST()
    ct=1000
    st=0
    flg=0 #0=stock0
    for i in range(n-1):
        if flg==0:
            if A[i]<A[i+1]:
                st=ct//A[i]
                ct=ct%A[i]
                flg=1
            else:
                pass
        else:
            if A[i]>A[i+1]:
                ct=ct+st*A[i]
                st=0
                flg=0
            else:
                pass
    ct=ct+st*A[n-1]
    print(ct)

if __name__ == '__main__':
    do()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int, input().split()))
ans = 1000
kabu = 0
flg = True
for i in range(N-1):
    if flg and arr[i] < arr[i+1]:
        tmp = ans//arr[i]
        ans -= tmp*arr[i]
        kabu += tmp
        flg = False
    if not flg and arr[i] > arr[i+1]:
        ans += kabu*arr[i]
        kabu = 0
        flg = True
print(ans+kabu*arr[-1])

Submission status: Accepted

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

money = 1000
kabu = 0

for i in range(0, len(a)-1):
  if a[i] > a[i+1]:
    money += kabu*a[i]
    kabu = 0
  else:
    kabu += money//a[i]
    money %= a[i]
    
money += kabu*a[-1]
print(money)

Submission status: Accepted

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

ny =1000
nk =0

for i in range(n-1):
    if al[i] > al[i+1]:
        if nk >0:
            ny += nk*al[i]
            nk = 0
    elif al[i] < al[i+1]:
        bk = ny // al[i]
        nk += bk
        ny -= bk*al[i]
    else:
        pass

ny += nk*al[n-1]
print(ny)

Submission status: Accepted

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

buy = [0] * n
sell = [0] * n

prev = 1000
for i in range(n - 1):
    if a[i+1] > a[i]:
        buy[i] += 1

    if a[i+1] < a[i]:
        sell[i] += 1

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

    if a[i-1] > a[i]:
        sell[i-1] += 1

kabu = 0
money = 1000
for i, e in enumerate(a):
    if buy[i] == 2:
        get = money // e
        kabu += get
        money -= get * e

    if sell[i] == 2:
        money += kabu * e
        kabu = 0

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

print(money)

Submission status: Accepted

Solution code:
#
#    ------------------------------------------------
#           ____          _     Generatered using
#          / ___|        | |
#         | |    __ _  __| | ___ _ __  ______ _
#         | |   / _` |/ _` |/ _ \ '_ \|_  / _` |
#         | |__| (_| | (_| |  __/ | | |/ / (_| |
#          \____\____|\____|\___|_| |_/___\____|
#
#      GNU Affero General Public License v3.0
#    ------------------------------------------------
#    Author   : prophet
#    Created  : 2020-07-25 08:23:15.745703
#    UUID     : K8TO3VDPFHYsy1xb
#    ------------------------------------------------
#
production = True

import sys, math, collections

def input(f = 0, m = 0):

    if m > 0: return [input(f) for i in range(m)]
    else:
        l = sys.stdin.readline()[:-1]

        if f >= 10:
            u = False
            f = int(str(f)[-1])
        else: u = True

        if f == 0: p = [l]
        elif f == 1: p = list(map(int, l.split()))
        elif f == 2: p = list(map(float, l.split()))
        elif f == 3: p = list(l)
        elif f == 4: p = list(map(int, list(l)))
        elif f == 5: p = l.split()

        return p if u else p[0]

def out(l, f = 0, n = True):

    if f == 0: p = str(l)
    elif f == 1: p = " ".join(map(str, l))
    elif f == 2: p = "\n".join(map(str, l))
    elif f == 3: p = "".join(map(str, l))

    print(p, end = "\n" if n else "")

def log(*args):
    if not production:
        print("$$$", end = "")
        print(*args)

enu = enumerate
ter = lambda a, b, c: b if a else c
ceil = lambda a, b: -(-a // b)
flip = lambda a: (a + 1) & 1

def mapl(i, f = 0):

    if f == 0: return list(map(int, i))
    elif f == 1: return list(map(str, i))
    elif f == 2: return list(map(list, i))

#
#   >>>>>>>>>>>>>>> START OF SOLUTION <<<<<<<<<<<<<<
#


def solve():

    n = input(11)
    a = input(1)


    y = 1000

    p = 10 ** 10

    for i in a: 
        if i > p:
            m = y // p
            y -= m * p
            y += m * i
        p = i

    out(y)

    return


solve()

#
#   >>>>>>>>>>>>>>>> END OF SOLUTION <<<<<<<<<<<<<<<
#

Submission status: Accepted

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

money = 1000

buy = A[0]
sell = A[0]

for i in range(N - 1):
    if A[i] > A[i + 1]:
        if sell != 0:
            money += (money // buy) * (sell - buy)
        buy = A[i + 1]
        sell = 0
    else:
        sell = A[i + 1]

if sell != 0:
    money += (money // buy) * (sell - buy)

print(money)