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()))

mon = 1000


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

print(mon)

Submission status: Accepted

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

money = 1000
kabu = 0

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

Submission status: Accepted

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

money = 1000
stock = 0

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

print(money)

Submission status: Accepted

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

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

print(money)

Submission status: Accepted

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

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

len_AA=len(AA)

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

print money

Submission status: Accepted

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

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 A[i] < A[i+1]:
        s+=m//A[i]
        m=m%A[i]
    if A[i] > A[i+1]:
        m += A[i]*s
        s=0

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

Submission status: Accepted

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

money = 1000
stok = 0

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

Submission status: Accepted

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

b = []

#1：購入、-1：売却、0：何もしない

before = 1

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

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

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

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

print(money)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

def main():
    import sys
    import math

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

    money = 1000

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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


money=1000

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

Submission status: Accepted

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

    money = 1000
    stock = 0

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

    money += stock * A[-1]

    print(money)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

    N_stock = 0


    while (True):

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

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

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

        index_cheap = index_expen


    print(current_money)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

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

downTrend = True

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

print(cash)

Submission status: Accepted

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


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

money = 1000
stock = 0

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

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

print(money)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
A.append(-1)
money = 1000
stocks = 0
down = True
for i in range(n):
    # 下がってるけど次上がる→買う
    if down and A[i+1] > A[i]:
        down = False
        stocks = money // A[i]
        money %= A[i]
    # 上がってるけど次下がる→売る
    elif not(down) and A[i+1] < A[i]:
        down = True
        money += A[i] * stocks
        stoks = 0
print(money)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

real = 1000
unreal = 0

for i in range(n-1):

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

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

    else:
        continue

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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




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

Submission status: Accepted

Solution code:

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

    money = 1000
    trading = []
    buy = True

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

print(Deposit)

Submission status: Accepted

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


m = 1000
k = 0

for i in range(N-1):
    if A[i] < A[i+1]:
        p = int(m//A[i])
        k += p
        m -= A[i]*p
    elif A[i] > A[i+1]:
        m += A[i]*k
        k = 0

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


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

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


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

    print(m)

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

best = 0

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

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

print(money)

Submission status: Accepted

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

for d in range(n-1):
    if stock ==0:
        if alist[d+1] > alist[d]:
            # 明日上がるなら買い
            stock=int(money/alist[d])
            money-=stock*alist[d]
        else:
            # 明日下がる場合は何もしない
            pass
    else:
        if alist[d+1] > alist[d]:
            # 明日上がるなら全部売ってから買い
            money+=stock*alist[d]
            stock=int(money/alist[d])
            money-=stock*alist[d]
        else:
            # 明日下がる場合で株を持っていたら売り
            money+=stock*alist[d]
            stock=0

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#D
N=int(input())
A=list(map(int,input().split()))
B=[]

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

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

Submission status: Accepted

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

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

#  print(i,'money:',money,'stock:',stock)
print(money)

Submission status: Accepted

Solution code:
import sys

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

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

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

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

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

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

Submission status: Accepted

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


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

Submission status: Accepted

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

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

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

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

print(money)

Submission status: Accepted

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

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

Submission status: Accepted

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




    zouka=0


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

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

    print(temo)
resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n = int(input())
a_list = list(map(int, input().split()))
trade_list = []
prev = "+"
money = 1000
kabu = 0

while a_list[len(a_list) - 1] <= a_list[len(a_list) - 2]:
    a_list.pop()
    if len(a_list) == 1:
        print(1000)
        sys.exit()

while a_list[0] >= a_list[1]:
    a_list.pop(0)
    if len(a_list) == 1:
        print(1000)
        sys.exit()

trade_list.append(a_list[0])

#この時点で少なくとも2つは要素ある状態
#少なくとも最初の要素 < 最後の要素

for i in range(len(a_list) - 1):
    if a_list[i] > a_list[i + 1]:
        now = "-"
    elif a_list[i] < a_list[i + 1]:
        now = "+"
    else:
        now = prev

    if prev != now:
        trade_list.append(a_list[i])
    prev = now

trade_list.append(a_list[-1])

for i in range(len(trade_list)):
    #買い
    if i % 2 == 0:
        kabu = money // trade_list[i]
        money -= kabu * trade_list[i]
    else:
        money += kabu * trade_list[i]

print(money)

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 first:
        if fp > A[i]:
            fp = A[i]
        elif fp < A[i]:
            stock += c // fp
            c -= stock * fp
            IsSearchTop = True
            first = False
            fp = A[i]
            if i == N - 1:
                c += stock * fp
    else:
        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()))
# 単純な話
money = 1000
stock = 0
for i in range(N-1):
    if A[i]<A[i+1]:
        stock += money//A[i]
        money %= A[i]
        money += stock*A[i+1]
        stock = 0
print(money)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
At_list = [A_list[0]]
D_list = list()
Dt_list = list()
mone = 1000
stoc = 0
for i in range(1,len(A_list)):
    D_list.append(A_list[i]-A_list[i-1])
for h in range(len(D_list)):
    if D_list[h] != 0:
        Dt_list.append(D_list[h])
        At_list.append(A_list[h + 1])
for j in range(len(Dt_list)):
    if Dt_list[j] > 0:#買う
        stoc = stoc + mone // At_list[j]
        mone = mone % At_list[j]
    elif Dt_list[j] < 0:
        mone = mone + stoc * At_list[j]
        stoc = 0
        last_sell = j
if len(Dt_list) != 0:
    if At_list[-1] >= At_list[-2]:
        mone = mone + stoc * At_list[-1]
    else:
        mone = mone + stoc * At_list[last_sell]
else:
    mone = 1000
print(mone)

Submission status: Accepted

Solution code:
def INT():
    return int(input())

def MI():
    return map(int, input().split())

def LI():
    return list(map(int, input().split()))

N = INT()
A = LI()
ans = 1000

for i in range(1, N):
    tmp = ans
    if A[i] > A[i - 1]:
        purchase = tmp // A[i - 1]
        ans = max(ans, tmp + (A[i] - A[i - 1]) * purchase)
        
print(ans)

Submission status: Accepted

Solution code:
# @uthor : Kaleab Asfaw
import sys

input = sys.stdin.readline

def main(n, lst):
    peaks = []
    minn = float("inf")
    maxx = float("-inf")
    money = 1000
    for i in lst:
        if i > maxx:
            if maxx != float("-inf"):
                times, rem = divmod(money, maxx)
                money = i * times + rem
                maxx = i
            else:
                maxx = i
        else:
            maxx = i
    return money
            
n = int(input())
lst = list(map(int, input().split()))
print(main(n, lst))

Submission status: Accepted

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

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