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

#所持金1000

flag=0
start=a[0]
b=[]

for i in range(1,n):
    #print(a[i],start)
    if flag==0:
        if start>a[i]:
            start=a[i]
        elif start<a[i]:
            end=a[i]
            flag=1
    if flag==1:
        if end<=a[i]:
            end=a[i]
            if i==n-1:
                b.append([start,end])
        elif end>a[i]:
            b.append([start,end])
            start=a[i]
            flag=0

saifu=1000
#print(b)
for (s,e) in b:
    saifu=saifu+(e-s)*(saifu//s)
    #print(saifu)

print(saifu)

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):
        if idx == N-1:
            break
        if A[idx] <= A[idx+1]:
            # mochikabuをmaxにし、shojikinをminに
            available = shojikin // A[idx]
            mochikabu += available
            shojikin -= available * A[idx]

        elif A[idx] > A[idx+1]:
            # shojikinをminにし、shojikinをmaxに
            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:
n = int(input())
L = list(map(int,input().split()))


a = 1000
k = 0

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

print(a)

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
money = 1000
stock = 0
i = 0
while i < N - 1:
    if A[i + 1] > A[i]:
        # 全額buy
        stock += money // A[i]
        money -= (money // A[i]) * A[i]
        # print('buy', i)
        # print(money, stock)
    if A[i + 1] < A[i]:
        money += stock * A[i]
        stock = 0
    if i == N - 2 and stock > 0:
        money += stock * A[-1]
        stock = 0

    # print(i, money, stock)
    i += 1
print(money)

Submission status: Accepted

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

d = []
for i in range(1,len(a)-1):
  if a[i-1] >= a[i] < a[i+1]:
    d.append((a[i],-1))
  elif a[i-1] <= a[i] > a[i+1]:
    d.append((a[i],1))

m = 1000
k = 0
for x,y in d:
  c = m//x
  if y == -1:
    k += c
    m -= x*c
  else:
    m += k*x
    k = 0
  #print(x,y,m,k)
print(max(m,1000))

Submission status: Accepted

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

cnt = 0
money = 1000
stock = 0
for i in range(N-1):
    if A[i] == A[i+1]:
        pass
    elif A[i] < A[i+1]:
        tmp = money // A[i]
        stock += tmp
        money -= tmp * A[i]
    else:
        money += stock * A[i]
        stock = 0

money += stock * A[-1]

print(money)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()

	katamuki = []
	for i,val in enumerate(A):
		if i == 0:
			continue
		if val < A[i-1]:
			katamuki.append("down")
		elif val > A[i-1]:
			katamuki.append("up")
		else:
			katamuki.append("to")

	ans = 1000
	for i,val in enumerate(A):
		if i == len(A)-1:
			break
		if val <= ans:
			if katamuki[i] == "up":
				tmp = ans // val
				ans = ans - (val * tmp)
				ans = ans + (tmp * A[i+1])

	print(ans)



#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Accepted

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

    up = False
    money = 1000
    has = 0  # 株数
    if A[0] < A[1]:
        v = money//A[0]
        has += v
        money -= v*A[0]
        up = True
    pre = A[0]
    for a in A[1:]:
        if not up:
            if pre >= a:
                pass
            else:
                # 局所最安値
                v = money//pre
                has += v
                money -= v*pre
                up = True
        else:
            if pre <= a:
                pass
            else:
                # 局所最高値
                money += has*pre
                has = 0
                up = False
        pre = a
    money += has*A[-1]
    print(money)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
lst=tuple(map(int,input().split()))

sm=1000
stock=0

#token=0:下降しきるまで待つフェイズ。token=1:上昇しきるまで待つフェイズ
token=0
for i in range(n-1):
    if token==0:
        if lst[i+1]<=lst[i] : continue 
        stock=sm//lst[i]       
        sm=sm%lst[i]
        token=1
    else :
        if lst[i+1]>=lst[i] : continue
        sm+=stock*lst[i]
        stock=0
        token=0

if token==1 :
    sm+=stock*lst[n-1]

print(sm)

Submission status: Accepted

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


def II():
    return int(sys.stdin.readline())


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


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()

    kabu_price = LI()

    cash = 1000
    kabu = 0

    for i in range(N - 1):
        ratio = kabu_price[i + 1] / kabu_price[i]

        if ratio == 1:
            continue

        if ratio < 1:
            if kabu > 0:
                cash += kabu * kabu_price[i]
                kabu = 0

        if ratio > 1:
            if cash >= kabu_price[i]:
                num_parcha = cash // kabu_price[i]
                kabu += num_parcha
                cash = cash % kabu_price[i]

    cash += kabu * kabu_price[-1]

    print(cash)


main()

Submission status: Accepted

Solution code:
"""
同じ日に購入オペレーションと売却オペレーションを行う意味がない。
買うときは、買えるだけ買う方がよい。
売る時は売れるだけ売る方がよい。

まず１日目、株を買うかどうか判断する。
翌日以降の連続する日付の中で、
"""

N = int(input())
A = list(map(int,input().split()))
M = 1000
stock = 0
buyPrice = 0
for i in range(N):
    if stock and A[i] >= buyPrice:
        #翌日の価格が今より高ければステイ。そうでなければ売る。
        if i==N-1 or A[i] >= A[i+1]:
            M += stock*A[i]
            stock = 0
    else:
        #翌日の価格が今より安ければステイ。そうでなければ買う。
        if i!=N-1 and A[i] < A[i+1]:
            stock += M//A[i]
            M %= A[i]
            buyPrice = A[i]
print(M)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

m = 1000
s = 0

mx = [0] * n
for i in range(1, n):
    if a[i-1] < a[i]:
        mx[i] = 1
        mx[i-1] = 0
        
mn = [0] * n
for i in range(1, n):
    if a[i-1] >= a[i]:
        mn[i] = 1
        mn[i-1] = 0
        
if a[0] < a[1]:
    mn[0] = 1
    
mn[-1] = 0

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

print(m)

Submission status: Accepted

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

die = True
for i in range(N-1):
    if A[i] < A[i+1]:
        die = False
        break
if die:
    print(1000)
    exit()

ud = [(0, A[0])]
for i in range(N-1):
    if A[i] < A[i+1]:
        ud.append((1, A[i+1]))
    elif A[i] > A[i+1]:
        ud.append((-1, A[i+1]))

hold = 0
hand = 1000
for i in range(len(ud)-1):
    if ud[i][0] == 0 and ud[i+1][0] == 1:
        buy = hand // ud[i][1]
        hold += buy
        hand -= buy * ud[i][1]
    if ud[i][0] == 0 and ud[i+1][0] == -1:
        pass
    if ud[i][0] == -1 and ud[i+1][0] == 1:
        buy = hand // ud[i][1]
        hold += buy
        hand -= buy * ud[i][1]
    if ud[i][0] == -1 and ud[i+1][0] == -1:
        pass
    if ud[i][0] == 1 and ud[i+1][0] == 1:
        pass
    if ud[i][0] == 1 and ud[i+1][0] == -1:
        hand += hold * ud[i][1]
        hold = 0
if hold:
    hand += hold * ud[-1][1]
    hold = 0
print(hand)

Submission status: Accepted

Solution code:
n, *A = map(int, open(0).read().split())
s = 1000
t = 0
for a0, a1 in zip(A, A[1:]):
    s += t*a0
    t = 0
    if a0 < a1:
        t = s // a0
        s -= t*a0
if t:
    s += t*a1
print(s)

Submission status: Accepted

Solution code:
import math

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

    stock_n=0
    money_n=1000

    for i in range(N-1):
        if A[i+1]>A[i] and stock_n==0:
            stock_n=money_n//A[i]
            money_n=money_n%A[i]
        elif A[i+1]<A[i] and stock_n!=0:
            money_n+=stock_n*A[i]
            stock_n=0

    if stock_n!=0:
        money_n+=stock_n*A[N-1]

    print(money_n)



if __name__=="__main__":
    main()

Submission status: Accepted

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

ret1 = []
ret2 = []

idx = 0
c = 1
while idx < n-1:
    if c and a[idx] < a[idx + 1]:
        ret1.append(a[idx])
        c = not(c)
    if not(c) and a[idx] > a[idx+1]:
        ret2.append(a[idx])
        c = not(c)
    idx += 1
if not(c):
    ret2.append(a[idx])
ans = 1000
for i in range(len(ret1)):
    ans += (ret2[i]-ret1[i]) * (ans//ret1[i])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int,input().split(" ")))
DP = [1000]*N
for i in range(N):
  for j in range(i+1,N):
    t = DP[i]//l[i]
    DP[j] = max(DP[j-1],(DP[i]-l[i]*t)+l[j]*t)
print(DP[-1])

Submission status: Accepted

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

die = True
for i in range(N-1):
    if A[i] < A[i+1]:
        die = False
        break
if die:
    print(1000)
    exit()

ud = [(0, A[0])]
for i in range(N-1):
    if A[i] < A[i+1]:
        ud.append((1, A[i+1]))
    elif A[i] > A[i+1]:
        ud.append((-1, A[i+1]))

hold = 0
hand = 1000
for i in range(len(ud)-1):
    if ud[i][0] == 0 and ud[i+1][0] == 1:
        buy = hand // ud[i][1]
        hold += buy
        hand -= buy * ud[i][1]
    if ud[i][0] == 0 and ud[i+1][0] == -1:
        pass
    if ud[i][0] == -1 and ud[i+1][0] == 1:
        buy = hand // ud[i][1]
        hold += buy
        hand -= buy * ud[i][1]
    if ud[i][0] == -1 and ud[i+1][0] == -1:
        pass
    if ud[i][0] == 1 and ud[i+1][0] == 1:
        pass
    if ud[i][0] == 1 and ud[i+1][0] == -1:
        hand += hold * ud[i][1]
        hold = 0

if hold:
    hand += hold * ud[-1][1]
    hold = 0

print(hand)

Submission status: Accepted

Solution code:
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
count = 0
ans = "No"

N = i()
A = l()

M = 1000
S = 0
P = 0

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

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

print(money)

Submission status: Accepted

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

updown = [0] * (N-1)
for i in range(N-1):
  if A[i] > A[i+1]:
    updown[i] = 0
  elif A[i] == A[i+1]:
    updown[i] = 1
  else:
    updown[i] = 2
    
down = 0
up = -1
for i in range(N-1):
  if updown[i]==0:
    if up!=-1:
      ans += (ans // A[down]) * (A[up] - A[down])
      up = -1
    down = i + 1
    
  elif updown[i]==2:
    up = i + 1

if up!=-1:
  ans += (ans // A[down]) * (A[up] - A[down])
print(ans)

Submission status: Accepted

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

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

for i in range(N):
    if A1[i] > 0:
        b = total // A[i]
        total -= A[i] * b
        kabu += b
    elif A1[i] < 0:
        total += kabu * A[i]
        kabu = 0
    else:
        continue
    #print(total, kabu)
total += kabu * A[N-1]
print(total)

Submission status: Accepted

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

Submission status: Accepted

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

t = [-1] * (N + 1)
t[0] = 1000
for i in range(N):
    t[i + 1] = max(t[i + 1], t[i])
    k = t[i] // A[i]
    y = t[i] % A[i]
    for j in range(i + 1, N):
        t[j + 1] = max(t[j + 1], k * A[j] + y)
print(t[N])

Submission status: Accepted

Solution code:
import sys
import numpy as np

def Ss():
  return sys.stdin.readline().rstrip().split(' ')

def Is():
  ss = Ss()
  return map(int, ss) if len(ss) > 1 else int(ss[0])

money = 1000
n = Is()
prices = [*Is()]

for today, tomorrow in zip(prices, prices[1:] + [0]):
  buyable_stock_count = int(money / today)
  new_money = money + buyable_stock_count * (tomorrow - today)
  if new_money > money:
    money = new_money

print(money)

Submission status: Accepted

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

money = 1000
kabu = 0
p = [1]*N

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

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

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

Submission status: Accepted

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

import itertools
a = [k for k, v in itertools.groupby(a)]

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

now_y=1000
now_k=0

for i in range(1,len(a)-1):
    if a[i-1] < a[i] < a[i+1]:
        continue
    if a[i-1] > a[i] > a[i+1]:
        continue

    if a[i-1] < a[i] > a[i+1]: # sell all
        now_y += now_k * a[i]
        now_k=0
        ans=max(ans,now_y)

    if a[i-1] > a[i] < a[i+1]: # buy all
        kabu = now_y // a[i]
        now_k += kabu
        now_y-=kabu*a[i]

print(ans)

Submission status: Accepted

Solution code:
from math import sqrt,ceil,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())


# for _ in range(ii()):

n=ii()
a=li()
i=0
ans=1000
while(i<n-1):

    while(i<n-1 and a[i]>=a[i+1]):
        i+=1
    if(i==n-1):
        break
    j=i
    i+=1
    while(i<n and a[i]>=a[i-1]):
        i+=1
    x1=ans//a[j]
    ans%=a[j]
    ans+=(x1*a[i-1])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
rl = sys.stdin.buffer.readline

N = int(rl())
AS = tuple(map(int, rl().split()))

posession = 1000
for i in range(N-1):
  if AS[i] < AS[i+1]:
    quantity = posession // AS[i]
    posession = (posession - quantity * AS[i]) + quantity * AS[i+1]

print(posession)

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+1] < A[i]:
        money += A[i] * kabu
        kabu = 0
    else:
        kabu += money//A[i]
        money -= A[i] * (money//A[i])
    #print(kabu,money)
money += A[-1] * kabu


print(money)

Submission status: Accepted

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

stock = 0
money = 1000
for i in range(N-1):
    if A[i+1] >= A[i]:
#         print('Buy')
        stock += money // A[i]
        money = money % A[i]
    else:
#         print('Sell')
        money += stock*A[i]
        stock = 0
        
#     print('money:{}',format(money))
#     print('stock:{}',format(stock))
            
# 最終日に株を持っているなら、それを売って所持金にする。
money += stock*A[N-1]
stock = 0 # この処理いらないけど一応。

print(money)

Submission status: Accepted

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

ans = 1000
k = 0
for i in range(len(A)-1):
    if A[i] < A[i+1] and (ans//A[i]) != 0:
        k = ans // A[i]
        ans = ans % A[i]
        ans += k * A[i+1]
        k = 0
    else:
        continue

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

A_diff = np.diff(A)

wollet = 1000
stock = 0
for i, rate in enumerate(A_diff):
    if rate > 0:
        stock_num = wollet // A[i]
        wollet -= stock_num*A[i]
        stock += stock_num
    elif rate < 0:
        wollet += stock*A[i]
        stock = 0
        
wollet += A[N-1]*stock

print(wollet)

Submission status: Accepted

Solution code:
# Code for D - Road to Millionaire
# Use input() to fetch data from STDIN
from math import floor
N = int(input())
A = list(map(int, input().split()))

total = 1000
for i in range(0, N - 1):
    if A[i] < A[i+1]:
        total += floor(total / A[i]) * ( A[i + 1] - A[i] )

print(total)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
A = LIST()

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

print(money)

Submission status: Accepted

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

up = True
points = []

for a0, a1 in zip(A, A[1:]):
    if up:
        if a0 < a1:
            up = False
            points.append(a0)
    else:
        if a0 > a1:
            up = True
            points.append(a0)

money = 1000
tickets = 0

for i in range(len(points)):
    if i % 2 == 1:
        money += points[i] * tickets
        tickets = 0
    if i % 2 == 0 and i + 1 < len(points):
        tickets = money // points[i]
        money -= tickets * points[i]

print(money)

Submission status: Accepted

Solution code:
n = int(input())

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

total = 1000

for i in range(n-1):
    if a[i+1] > a[i]:
        num = total//a[i]
        total -= a[i] * num
        total += num * a[i+1]

print(total)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
m=1000
k=0

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

Submission status: Accepted

Solution code:
def main():
    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]:
            M += A[i] * kabu
            kabu = 0
            kabu += M // A[i]
            M = M % A[i]
        else:
            M += A[i] * kabu
            kabu = 0

    else:
        M += kabu * A[i+1]
    print(M)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
a = [[i,0] for i in a]

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

kabu = 0
kane = 1000
for i in range(n):
    if a[i][1] < 0:
        kabu += kane // a[i][0]
        kane = kane % a[i][0]
    elif a[i][1] > 0:
        kane += kabu * a[i][0]
        kabu = 0

kane += kabu * a[i][0]

print(kane)

Submission status: Accepted

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

money=1000
kabu=0

uru_t=[0]*n
kai_t=[0]*n

kai_flag=False
uri_flag=True

for i in range(n-1):
    tmp_v=a[i]
    for j in range(i+1,n):
        if tmp_v<a[j]:
            kai_flag=True
            break
    if a[i]<=a[i+1]:
        uri_flag=False

    if kai_flag:
        num_kabu=money//a[i]
        kabu+=num_kabu
        money-=num_kabu*a[i]

    if uri_flag:
        money+=kabu*a[i]
        kabu=0

    # print(money, kabu, kai_flag, uri_flag)
    kai_flag=False
    uri_flag=True

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