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:
# print('input >>')
N = input()
As = list(map(int,(input().split())))

money = 1000
kab = 0

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

money += kab * As[-1]

# print('-----output-----')
print(money)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    lst_a = [int(v) for v in input().split(' ')]
    s = 0
    c = int(1000)
    max_c = int(1000)

    prev_tlt = '-'
    for i in range(len(lst_a) - 1):
        if lst_a[i] < lst_a[i + 1]:
            tlt = '+'
        elif lst_a[i] > lst_a[i + 1]:
            tlt = '-'
        else:
            continue

        if tlt != prev_tlt:
            if tlt == '-':
                c += s * lst_a[i]
                s = 0
            else:
                s = int(c // lst_a[i])
                c -= lst_a[i] * s
            max_c = max(c, max_c)
        prev_tlt = tlt
    c += s * lst_a[-1]
    max_c = max(c, max_c)
    print(max_c)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(asset)

Submission status: Accepted

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

P = []
m = A[0]
for i in range(1, N):
    if A[i] < A[i - 1]:
        if m != A[i - 1]:
            P.append((m, A[i - 1]))
        m = A[i]
if m < A[N - 1]:
    P.append((m, A[N - 1]))

ans = 1000
for p in P:
    ans += (p[1] - p[0]) * (ans // p[0])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split())) + [0]
money = 1000
stock = 0
prev = 201

for i,price in enumerate(A):
    if price > prev and money >= prev:
        stock += money // prev
        money -= stock * prev

    elif price < prev and stock:
        money += prev * stock
        stock = 0
    prev = price

print(money)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(N, As):
    money = 1000
    stock = 0
    for i in range(len(As)):
        if i == N - 1:
            money += stock * As[i]
            stock = 0
            continue
        if As[i] > As[i + 1]:
            money += stock * As[i]
            stock = 0
        if As[i] < As[i + 1]:
            stock += money // As[i]
            money = money % As[i]
    print(money)


if __name__ == '__main__':
    # S = input()
    N = int(input())
    # N, M = map(int, input().split())
    As = [int(i) for i in input().split()]
    # Bs = [int(i) for i in input().split()]
    solve(N, As)

Submission status: Accepted

Solution code:
import math
N = int(input())
A = list(map(int, input().split()))
now = 1000
stack = 0
for i in range(0, N-1):
    if stack != 0: #売る
        now += stack*A[i]
        stack = 0
    if A[i] < A[i+1]: #買う
        stack = math.floor(now/A[i])
        now -= stack*A[i]
    
now += stack*A[N-1] #最終日　全部売る
stack = 0
print(now)

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
arr=list(map(int,input().split()))
have=0
ans=1000
rate=0
for i in range(n-1):
    
    if(have!=0):
        if(arr[i+1]<arr[i]):
            ans+=have*(arr[i]-rate)
            have=0
    else:
        if(arr[i+1]>arr[i]):
            have=ans//arr[i]
            rate=arr[i]
    #print(have,ans,rate)
#print(have)
if(have!=0):
    ans+=have*(arr[-1]-rate)
    have=0
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int,readline().split()))
k = 0
ss = sorted(a)
qq = ss[::-1]

if len(set(a)) == 1 or qq == a:
  print(1000)
  exit()
elif ss == a:
  k = 1000//a[0]
  print(k*a[-1]+1000-a[0]*k)
  exit()

m = 1000
if a[0] < a[1]:
  pp = 1000//a[0]
  m -= pp*a[0]
  k += pp

for i in range(n-2):
  if a[i] <= a[i+1] and a[i+1] > a[i+2]:
    m += k*a[i+1]
    k = 0
  if a[i] >= a[i+1] and a[i+1] < a[i+2]:
    pp = m//a[i+1]
    m -= pp*a[i+1]
    k += pp

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def S(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

alphabets = "abcdefghijklmnopqrstuvwxyz"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    N = I()
    A = LI()

    money = 1000

    partial = []
    tmp = [A[0]]
    last = A[0]

    for i in range(1, N):
        if A[i] >= last:
            tmp.append(A[i])
            last = A[i]
        else:
            partial.append(tmp)
            tmp = [A[i]]
            last = A[i]

    partial.append(tmp)

    for part in partial:
        if len(part) <= 1:
            continue
        last = part[-1]
        start = part[0]
        stock = money // start
        money = money % start + stock * last
    print(money)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
price = [int(x) for x in input().split()]

custom_price = []
i = 0
while True:
    # print(custom_price)
    # input()
    while i+1 < n and price[i+1] <= price[i]:
        i += 1
    custom_price.append(price[i])
    i += 1
    if i == n:
        break
    while i+1 < n and price[i+1] >= price[i]:
        i += 1
    custom_price.append(price[i])
    i += 1
    if i == n:
        break
if len(custom_price) % 2 == 1:
    custom_price.pop(-1)

i = 0
cur = 1000
while i < len(custom_price):
    stocks = cur//custom_price[i]
    cur += (stocks * (custom_price[i+1] - custom_price[i]))
    i += 2
print(cur)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
wallet = 1000
stock = 0
price = 0
for i in range(n - 1):
    if a[i] < a[i + 1]:
        price = a[i]
        buy = wallet // price
        stock += buy
        wallet -= (price * buy)
    elif a[i] > a[i + 1]:
        price = a[i]
        wallet += (price * stock)
        stock = 0
if stock != 0:
    wallet += (a[- 1] * stock)
print(wallet)

Submission status: Accepted

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

dp = [1000] * N
M = 1000
for i in range(N) :
  for j in range(i + 1, N) :
    if A[j] > A[i] :
      kabu = max(dp[:i + 1]) // A[i]
      kane = max(dp[:i + 1]) % A[i]
      dp[j] = max(dp[j], kane + kabu * A[j])

print(max(dp))

Submission status: Accepted

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

dp = [[[0,0] for _ in range(2)] for _ in range(N+1)]
dp[0][0] = [1000,0]

for i in range(N):
    for j in range(i+1):
        if(dp[j][0][0] > dp[i+1][0][0]):
            dp[i+1][0][0] = dp[j][0][0]
        x = dp[j][1][0] + dp[j][1][1] * A[i]
        if(x > dp[i+1][0][0]):
            dp[i+1][0][0] = x
        
        y = dp[j][0][0] // A[i]
        x = dp[j][0][0] % A[i]
        if((y > dp[i+1][1][1] ) or (y == dp[i+1][1][1] and x > dp[i+1][1][0])):
            dp[i+1][1][1] = y
            dp[i+1][1][0] = x
        
        y = dp[j][1][1] + dp[j][1][0] // A[i]
        x = dp[j][1][0] % A[i]
        if((y > dp[i+1][1][1] ) or (y == dp[i+1][1][1] and x > dp[i+1][1][0])):
            dp[i+1][1][1] = y
            dp[i+1][1][0] = x

print(dp[N][0][0])

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

s = [[0]*2 for i in range(n)]
m = [[0]*2 for i in range(n)]

# 売り
s[0][0] = 0
m[0][0] = 1000
# 買い
s[0][1] = 1000//a[0]
m[0][1] = 1000 - s[0][1] * a[0]

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

print(m[n-1][0])

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
chk = [0]*(n-1)
for i in range(n-1):
    if A[i + 1] > A[i]:
        chk[i] = 2
    elif A[i + 1] < A[i]:
        chk[i] = 0
    else:
        chk[i] = 1
c = 0
money = 1000
stock = 0
for i in range(n-1):
    if chk[i] == 2 and c == 0:
        stock = money//A[i]
        money -= stock*A[i]
        c = 1
    elif chk[i] == 0 and c == 1:
        money += stock*A[i]
        stock = 0
        c = 0
    if 2 not in chk[i:]:
        money += stock*A[i]
        stock = 0
        break
money += stock*A[n-1]
print(money)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=0
L=[]
Benef=[]
for i in range(N-1):
    if A[i]>A[i+1]:
        L.append((c,i))
        Benef.append(A[i]-A[c])
        c=i+1
    elif i==N-2:
        L.append((c,N-1))
        Benef.append(A[N-1]-A[c])
l=len(L)
maney=1000
for j in range(l):
    maney+=(maney//A[L[j][0]])*Benef[j]
print(maney)

Submission status: Accepted

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

dp = [1000] * (n+1)
for i in range(1, n+1):

    stock = dp[i] // a[i]
    amari = dp[i] % a[i]
    max_price = 0
    for j in range(min(n+1, i+1), n+1):
        price = stock * a[j] + amari
        if price > max_price:
            max_price = price
        dp[j] = max(max_price, dp[j])

print(dp[n])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
m = 1000
ind_st = 0
ind = 1
while ind_st < n:
    if ind < n:
        if a[ind]>=a[ind-1]:
            ind+=1
            continue
    
    kb = m//a[ind_st]
    m += kb*(a[ind-1]-a[ind_st])
    ind_st = ind
    ind = ind_st+1

print(m)

Submission status: Accepted

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

for i in range(N):
    if i == N-1:
        maney += stock * A[i]
        break
    if A[i] < A[i+1]:
        tmp_stock = maney // A[i]
        stock += tmp_stock
        maney -= tmp_stock * A[i]
    elif A[i] > A[i+1]:
        maney += stock * A[i]
        stock = 0
    else:
        pass

print(maney)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
# 今の株価が前の日より上がるなら売る，次の日より下がるなら買う．
max_money = 1000
kabu = 0
money = 1000
for i in range(N):
    if i == 0:
        if A[i]<A[i+1]:
            kabu = int(1000/A[i])
            money -= kabu*A[i]
        continue
    if i == N-1:
        if A[i-1]<A[i]:
            money += kabu * A[i]
            kabu = 0
            max_money = max(max_money,money)
        break

    if A[i-1]<A[i]:
        money += kabu * A[i]
        kabu = 0
        max_money = max(max_money,money)
    if A[i]<A[i+1]:
        kabu += int(money/A[i])
        money -= A[i]*int(money/A[i])

print(max_money)

Submission status: Accepted

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

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

# print(dA)
m=N-2
while m>0 and dA[m]<=0:
    m-=1
# print(m)

money=1000
stock=0
for i in range(m+1):
    if dA[i]>0:
        a = int(money / A[i])
        money -= (a*A[i])
        stock += a
    else:
        if dA[i]<0:
            money += (stock*A[i])
            stock = 0
    # print("i=",i," money=",money, " stock=", stock)

money += stock*A[m+1]
stock=0
# print("m+1=", m+1," A[m+1]=",A[m]," money=",money, " stock=", stock)

print(money)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

N = int(input())
A = list(map(int, input().split()))
mn = 1000
st = 0
for i in range(N-1):
    if A[i+1]>A[i]:
        by = mn//A[i]
        st += by
        mn -= by*A[i]
    if A[i+1]<=A[i]:
        mn += st*A[i]
        st = 0

print(st*A[-1]+mn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

# peak = n - 1
# for n in range(N - 2, -1, -1):
#     if As[n] > As[n + 1]:
#         peak = n
#     else:
#         break

def solve_slow(N, As):
    nums = [0, 1, 2]

    ans = 0
    for comb in product(nums, repeat=N):
        money = 1000
        stock = 0
        for c, a in zip(comb, As):
            # sell
            if c == 0:
                money += a * stock
                stock = 0
            # buy
            elif c == 1:
                stock += money // a
                money = money % a
            else:
                pass
            # print("c, money, stock = ", c, money, stock)
        # print()
        ans = max(ans, money)

    return ans
                

def solve_fast(N, As):
    def uniq(As):
        ret = []
        for a in As:
            if not ret:
                ret.append(a)
            else:
                if ret[-1] != a:
                    ret.append(a)
        return ret
    
    As = uniq(As)
    N = len(As)
    
    ans = 0
    money = 1000
    stock = 0
    for n in range(N):
        ans = max(ans, money + stock * As[n])
    
        if n == N - 1:
            break
    
        if As[n] < As[n + 1]:
            stock_diff, money = divmod(money, As[n])
            stock += stock_diff
            # stock, money = divmod(money, As[n])
        elif As[n] > As[n + 1]:
            money += stock * As[n]
            stock = 0
        # print("m, s = ", money, stock)
    
    return ans

print(solve_fast(N, As))
# print(solve_slow(N, As))

# while True:
#     N = 10
#     As = [random.randint(1, 10) for _ in range(N)]
#     s = solve_slow(N, As)
#     f = solve_fast(N, As)
#     if s != f:
#         print(N)
#         print(As)
#         s = solve_slow(N, As)
#         f = solve_fast(N, As)
#         break
#     else:
#         print("ok")

Submission status: Accepted

Solution code:
import io
import os

from collections import Counter, defaultdict, deque


from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 5)


def solve(N, A):
    @lru_cache(maxsize=None)
    def maxProfitAtEnd(i, money, stocks):
        if i == N - 1:
            # Sell all remaining
            return money + stocks * A[i]

        # no op
        best = maxProfitAtEnd(i + 1, money, stocks)
        # sell
        best = max(best, maxProfitAtEnd(i + 1, money + stocks * A[i], 0))
        # buy
        numStock = money // A[i]
        best = max(
            best, maxProfitAtEnd(i + 1, money - numStock * A[i], stocks + numStock)
        )

        return best

    return maxProfitAtEnd(0, 1000, 0)


def solve(N, A):
    money = 1000
    stock = 0
    for i in range(N - 1):
        if A[i] < A[i + 1]:
            # Buy on i
            numStock = money // A[i]
            money -= numStock * A[i]
            stock += numStock
            # Sell on i + 1
            money += stock * A[i + 1]
            stock = 0
    assert stock == 0
    return money


if __name__ == "__main__":
    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    (N,) = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    ans = solve(N, A)
    print(ans)

Submission status: Accepted

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

upFlag = False
B = []
for i in range(1, N):
    if A[i-1] < A[i]:
        if not upFlag:
            B.append(A[i-1])
            upFlag = True
    elif A[i-1] > A[i]:
        if upFlag:
            B.append(A[i-1])
            upFlag = False

if upFlag:
    B.append(A[-1])


X = 1000
Y = 0

for i, b in enumerate(B):
    if i%2 == 0:
        Y = X//b
        X = X%b
    else:
        X += Y*b
        Y = 0

print(X)

Submission status: Accepted

Solution code:
#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
n=int(input())
l=[int(x) for x in input().split()]
k=[]
def stockBuySell(price, n):  
    if (n == 1): 
        return

    i = 0
    while (i < (n - 1)): 
        while ((i < (n - 1)) and 
                (price[i + 1] <= price[i])): 
            i += 1
          
        if (i == n - 1): 
            break

        buy = i 
        i += 1
           
        while ((i < n) and (price[i] >= price[i - 1])): 
            i += 1
 
        sell = i - 1
        k.append(buy)
        k.append(sell)

stockBuySell(l,n)
cur=1000

for i in range(len(k)):
	if i%2==0:
		x=cur//l[k[i]]
		cur-=int(x)*l[k[i]]
	else:
		cur+=int(x)*l[k[i]]

print(cur)

Submission status: Accepted

Solution code:
from collections import deque, Counter, defaultdict
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect
from functools import lru_cache
import itertools

sys.setrecursionlimit(10 ** 8)

M = 10 ** 9 + 7
INF = 10 ** 17


def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    # N, K = [int(a) for a in input().split()]
    # A = [int(a) for a in input().split()]
    # T1, T2, T3 = [int(a) for a in input().split()]

    aa = []
    t = A[0]
    start = A[0]
    for ai in A[1:]:
        if t <= ai:
            t = ai
            if start is None:
                start = ai
            continue
        aa.append(
            (start, t)
        )
        t = ai
        start = ai
    if start is not None:
        aa.append((start, A[-1]))

    m = 1000
    for a, b in aa:
        if a is None:
            continue
        c = m // a
        m += (b - a) * c
    print(m)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = map(int,input().split())
a = list(map(int,input().split()))
newa=[a[0]]
for i in range(1,len(a)):
    if a[i]!=a[i-1]:
        newa.append(a[i])
check=[]
for i in range(1,len(newa)):
    if newa[i]>newa[i-1]:
        check.append(i)
        
stock=0
money=1000
if len(check)>=1:
    kau1=min(newa[0:check[0]])#最初のやすい値段
    stock=money//kau1
    money=money%kau1
    money+=newa[check[0]]*stock
    
    

    for i in range(len(check)-1):
        kau=min(newa[check[i]:check[i+1]])
        stock=money//kau
        money=money%kau
        money+=newa[check[i+1]]*stock
    
print(money)

Submission status: Accepted

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

for i in range(n-1):
  if stock == 0:
    if line[i] < line[i+1]:
      buy_stock = money // line[i]
      stock += buy_stock
      money -= buy_stock * line[i]
  else:
    if line[i] > line[i+1]:
      get_money = stock * line[i]
      money += get_money
      stock = 0

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

Submission status: Accepted

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

inc = False
money = 1000
kabu = 0
for i in range(N - 1):
    if (not inc) and A[i] < A[i + 1]:
        # 買う
        kabu = money // A[i]
        money -= kabu * A[i]
        inc = True
    elif inc and A[i] > A[i + 1]:
        money += kabu * A[i]
        kabu = 0
        inc = False

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

Submission status: Accepted

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

n_kabu = 0
money = 1000
for d in range(N-1):
    kabuka_today = A[d]
    kabuka_tomorrow = A[d+1]
    if kabuka_today < kabuka_tomorrow:
        can_buy = money//kabuka_today
        n_kabu += can_buy
        money -= can_buy*kabuka_today
    elif kabuka_today > kabuka_tomorrow:
        money += n_kabu*kabuka_today
        n_kabu = 0

money += n_kabu*A[N-1]

print(money)

Submission status: Accepted

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

state = None
for i in range(1,N):
    if state == None:
        if price[i] < price[i-1]:
            money += price[i-1]*stack
            stack = 0
            state = False
        elif price[i] > price[i-1]:
            stack = money//price[i-1]
            money = money%price[i-1]
            state = True
    elif state == True:
        if price[i] < price[i-1]:
            money += price[i-1]*stack
            stack = 0
            state = False
    elif state == False:
        if price[i] > price[i-1]:
            stack = money//price[i-1]
            money = money%price[i-1]
            state = True

if stack != 0:
    money += price[-1]*stack
    stack = 0

print(money)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int,input().split()))
money = 1000
stock = 0
Frag = False
for i in range(N):
    if Frag == True:
        if i == N-1:
            money = money + stock * A_list[i]
        else:
            if A_list[i] > A_list[i+1]:
                money = money + stock * A_list[i]
                stock = 0
                Frag = False
    if Frag == False:
        if i == N-1:
            break
        else:
            if A_list[i] < A_list[i+1]:
                stock = money // A_list[i]
                money = money - A_list[i] * stock
                Frag = True
print(money)

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()

turn = []
t = [a[0]]
for i in range(1,len(a)):
	if t[-1]<=a[i]:
		t.append(a[i])
	else:
		turn.append(t)
		t = [a[i]]
turn.append(t)


money = 1000
kabu = 0
for i in turn:
	if len(i)>=2:
		t = money//min(i)
		money-=t*min(i)
		money+=t*max(i)

print(money)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


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

Submission status: Accepted

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

money = 1000
stock = 0
buy_turn = True
ans = 0

for i, a in enumerate(A[:N]):
    ans = max(ans, money)
    if buy_turn:
        if A[i + 1] < A[i]:
            continue
        buy_turn = False
        stock += money // A[i]
        money = money % A[i]

    else:
        if A[i + 1] > A[i]:
            continue
        buy_turn = True
        money += A[i] * stock
        stock = 0
    ans = max(ans, money)

print(ans)

Submission status: Accepted

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


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

    result = []
    for idx, a in enumerate(A[:-1]):
        if A[idx] <= A[idx+1]:
            available = shojikin // A[idx]
            mochikabu += available
            shojikin -= available * A[idx]

        elif A[idx] > A[idx+1]:
            shojikin += mochikabu * A[idx]
            mochikabu = 0

    shojikin += mochikabu * A[-1]
    print(shojikin)
    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:
def readinput():
    n=int(input())
    a=list(map(int,input().split()))
    return n,a

def main(n,a):
    genkin=1000
    kabu=0
    for i in range(n-1):
        if a[i]>a[i+1]:
            # 売り
            genkin+=kabu*a[i]
            kabu=0
        elif a[i]<a[i+1]:
            # 買い
            kabu+=genkin//a[i]
            genkin=genkin%a[i]
    if kabu>0:
        genkin+=kabu*a[n-1]
    return genkin

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
x=1000
y=x//A[0]
z=x-y*A[0]
for i in range(n-1):
    a=x
    b=z+y*A[i+1]
    if a<b:
        x=b
    else:
        y=x//A[i+1]
        z=x-y*A[i+1]
print(x)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())

memo = {}
s = set()

ans = 0
def dfs(idx, money, stocks):
    global ans
    if idx==N:
        ans = max(ans, money)
        return
    a = As[idx]
    if (idx, stocks) in s:
        if money<=memo[(idx, stocks)]:
            return
    s.add((idx, stocks))
    memo[(idx, stocks)] = money
    dfs(idx+1, money+stocks*a, 0)
    dfs(idx+1, money, stocks)
    cnt = money//a
    dfs(idx+1, money-cnt*a, stocks+cnt)

dfs(0, 1000, 0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]

N= int(input())
A = [int(i) for i in input().split()]
ans = 1000
stock = 0

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

if(stock != 0):
  ans += stock*A[N-1]


print(ans)