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:
# import math
# import statistics
a=int(input())
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#    c.append(i)
#e1,e2,e3 = map(int,input().split())
#K = input()
f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
now=1000
buy=0
sell=0
kabu=0
for i in range(a-1):
    if f[i]<f[i+1]:
        if now>=f[i]:
            buy=now//f[i]
            now=now-buy*f[i]
            kabu=buy
            buy=0
    if f[i]>f[i+1] :
        if kabu>=1:
            sell=kabu
            now=now+sell*f[i]
            kabu=0
            sell=0
    if f[i]==f[i+1]:
        continue
now = now+f[-1]*kabu
print(now)

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): 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()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

n = k()
a = l()
sum = 1000

for i in range(n-1):
    if a[i] > a[i+1]:
        xx = a[cnt:i+1]
        b = sum//min(xx)
        sum += (max(xx)-min(xx))*b
        cnt = i+1

xx = a[cnt:]
b = sum//min(xx)
sum += (max(xx)-min(xx))*b

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))

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


def wa(lis,a):
    return lis[0]*a+lis[1]

for i in range(N):
    #0が株売り　1が株買い
    dp[i+1][0]=dp[i][0] if wa(dp[i][0],A[i])>=wa(dp[i][1],A[i]) else [0,dp[i][1][0]*A[i]+dp[i][1][1]]
    #株が多くなるように買う
    dp[i+1][1]=dp[i][1] if wa(dp[i][1],A[i])>wa(dp[i][0],A[i]) else [dp[i][0][1]//A[i],dp[i][0][1]-dp[i][0][1]//A[i]*A[i]]
    
    
print(dp[-1][0][-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]:
        kabu += money//a[i]
        money %= a[i]
    else:
        money += kabu * a[i]
        kabu = 0

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

print(ans)

Submission status: Accepted

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

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

    if A[i] < A[j]:
      stock += money // A[i]
      money -= A[i] * (money // A[i])
      break

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

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()))
    kabu = 0
    kane = 1000
    for i in range(n-1):
        if A[i+1] > A[i]:
            kabu += kane // A[i]
            kane %= A[i]
        elif A[i+1] < A[i]:
            kane += kabu * A[i]
            kabu = 0
    kane += kabu * A[n-1]
    print(kane)

if __name__=='__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

# N日目の最大の所持金
dp = [1000] + [0] * (N-1)
for i in range(1, N):
    dp[i] = dp[i-1]

    for j in range(i):
        # j日に買える最大の株数
        max_unit = dp[j] // A[j]
        # その場合の所持金の変化
        money = dp[j] + (A[i]-A[j])*max_unit
        dp[i] = max(money, dp[i])

print(dp[-1])

Submission status: Accepted

Solution code:
# WA

N = int(input())
A = [int(_) for _ in input().split()]

dp = [1000]
K = [0]
C = [0]

for i in range(N):
    k, c = divmod(dp[-1], A[i])
    v = dp[-1]
    for j in range(i+1):
        u = K[j] * A[i] + C[j]
        v = max(u, v)
    K.append(k)
    C.append(c)
    dp.append(v)
print(dp[-1])

Submission status: Accepted

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

for i in range(1,n):
    tmp = dp[i-1]
    for j in range(i):
        k = dp[j]//a[j]
        m = dp[j]-a[j]*k
        tmp = max(tmp,m + a[i] * k)
    dp[i] = tmp

print(dp[n-1])

Submission status: Accepted

Solution code:
N=int(input())
A=[int(a) for a in input().split()]
dp=[-1 for _ in range(N)]
dp[0]=1000
for i in range(1,N):
    tmp=dp[i-1]
    for j in range(i):
        K=dp[j]//A[j]
        res=dp[j]-K*A[j]
        tmp=max(tmp,res+A[i]*K)
    dp[i]=tmp
print(dp[-1])

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 = Stock + (Money//A[i])
		Money = Money % A[i]
	elif A[i] > A[i+1]:
		Money = Money + Stock * A[i]
		Stock = 0

ans = Money + Stock * A[i+1]
print(ans)

Submission status: Accepted

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

money = 1000
stock = 0
for day in range(N-1):
    today_price = A[day]
    tommorow_price = A[day+1]
    if today_price < tommorow_price:
        div,mod = divmod(money, today_price)
        stock += div
        money = mod
    elif today_price > tommorow_price:
        money += today_price * stock
        stock = 0
money += A[N-1] * stock
print(money)

Submission status: Accepted

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

cash = 1000

for buy, sell in zip(a, a[1:]):
    if buy >= sell:
        continue
    stock, cash = divmod(cash, buy)
    cash += stock * sell

print(cash)

Submission status: Accepted

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

buy, sell = [], []
for i in range(n):
    if a[i] < a[i+1] and len(buy) - len(sell) == 0:
        buy.append(i)
    if len(buy) - len(sell) == 1 and a[i] > a[i+1]:
        sell.append(i)

cash = 1000
for i, j in zip(buy, sell):
    stock, rest = divmod(cash, a[i])
    cash = stock * a[j] + rest

print(cash)

Submission status: Accepted

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

dp = [0]*(N+1)
dp[0] = 1000

for i in range(1, N+1):
    dp[i] = dp[i-1]
    for j in range(1, i):
        dp[i] = max(dp[i], (dp[j]//A[j-1])*A[i-1] + (dp[j]%A[j-1]))

print(dp[-1])

Submission status: Accepted

Solution code:
n,*A=map(int,open(0).read().split());m=1000
for x,y in zip(A,A[1:]):m+=m//x*(y-x)*(x<y)
print(m)

Submission status: Accepted

Solution code:
n,*A=map(int,open(0).read().split());m=1000
for x,y in zip(A,A[1:]):
  if x<y:m=m//x*y+m%x
print(m)

Submission status: Accepted

Solution code:
from sys import stdin
inp = lambda : stdin.readline().strip()

n= int(inp())
prices = [int(x) for x in inp().split()]

money= 1000
buy = -1
if prices[0] <=money: 
    buy = prices[0]
for i in range(1, len(prices)):
    if prices[i] > prices[i - 1]:
        if buy != -1:
            money = money%buy + (money//buy)*prices[i]
            buy = prices[i]
    else:
        if prices[i] < buy and prices[i] < money:
            buy = prices[i]
print(money)

Submission status: Accepted

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

money = 1000
num_stock = 0
for a, b in zip(A, A[1:]):
    if a < b:
        cnt, money = divmod(money, a)
        num_stock += cnt
    elif a > b:
        money += num_stock * a
        num_stock = 0
money += num_stock * A[-1]
print(money)

Submission status: Accepted

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

dp = [0] * (n + 1000)
dp[0] = 1000

for i in range(1, n):
    for j in range(i):
        stock = dp[j] // a[j] * a[i] + dp[j] % a[j]
        dp[i] = max(dp[i - 1], stock)
print(dp[n-1])

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
n = I()
A = readInts()

ans = 1000
money = 1000
kabu = 0
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]
        kabu = 0
print(money)

Submission status: Accepted

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

dp = [0]*(n+1)
dp[0] = 1000

for i in range(1, n):
    dp[i] = dp[i-1]
    for j in range(i):
        stock = dp[j]//a[j]
        cost = dp[j]+(a[i]-a[j])*stock
        dp[i] = max(dp[i], cost)
print(dp[n-1])

Submission status: Accepted

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

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

print(ret)

Submission status: Accepted

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

ret = 1000
prev = A[0]
for i in range(1, n):
  if A[i] - prev > 0:
    ret = A[i] * (ret // prev) + ret % prev
  if A[i] != prev:
    prev = A[i]

print(ret)

Submission status: Accepted

Solution code:
N = int(input())
kabu = [int(i) for i in input().split()]
money = 1000
hoji = 0

for i in range(N-1):
    if kabu[i] < kabu[i+1] and not hoji:
        hoji = money//kabu[i]
        money = money - hoji*kabu[i]
    if kabu[i] > kabu[i+1] and hoji:
        money = money + hoji*kabu[i]
        hoji = 0
if hoji:
    money = money + hoji*kabu[i+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):
    if i == N-1 or A[i] > A[i+1]: 
        m += s * A[i]
        s = 0
    elif A[i] < A[i+1]:
        x = m // A[i]
        s += x
        m -= x * A[i]
print(m)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
dp=[1000]*(n+1) #dp[i]:=i日目の取引が終わった時の最大値
for i in range(n):
    for j in range(i):
        dp[i]=max(dp[i],a[i] * (dp[j]//a[j]) + dp[j]%a[j])
    dp[i]=max(dp[i],dp[i-1])
print(dp[n-1])

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
dp = [0]*(N+1)
dp[0] = 1000

for i in range(1, N+1):
    dp[i] = dp[i-1]
    
    for j in range(i):
        dp[i] = max(dp[i], dp[j]//A[j]*A[i-1]+dp[j]%A[j])

print(dp[N])

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
dp=[0]*n
dp[0]=1000
for i in range(1,n):
    dp[i]=dp[i-1]
    for j in range(i):
        dp[i]=max(dp[i],(dp[j]//a[j])*a[i]+dp[j]%a[j])
print(dp[n-1])

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
nums = []
nums.append(a[0])
for i in range(1, n):
    if a[i] != a[i-1]:
        nums.append(a[i])
    else:
        continue
val = nums[0]
maxs = []
for j in range(1, len(nums)):
    if nums[j] > val:
        maxs.append(j)
    else:
        pass
    val = nums[j]
pos = 0
money = 1000
for idx in maxs:
    minVal = min(nums[pos:idx])
    stocks = money // minVal
    money = money - minVal * stocks + nums[idx] * stocks
    pos = idx
print(money)

Submission status: Accepted

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

stock=0
money=1000
d=-1
for i in range(len(A)-1):
	if A[i+1]-A[i]>0 and d==-1:
		'''kau'''
		stock=money//A[i]
		money=money%A[i]
		d=1
	elif A[i+1]-A[i]<0 and d==1:
		'''uru'''
		money+=A[i]*stock
		stock=0
		d=-1
	#print(i,money,stock)
if stock!=0:
	money += A[-1]*stock
	stock=0
print(money)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
list=[[0]*n]
list=input().split()
for i in range(n):
  list[i]=int(list[i])

yen=1000
stock=0
keikou=0
for i in range(1,n):
  if list[i]-list[i-1]<=0:
    keikou+=1
if keikou!=n-1:
  for t in range(1,n):
    if list[t]-list[t-1]>=0:
      stock+=int(yen/list[t-1])
      yen-=list[t-1]*int(yen/list[t-1])
      yen+=list[t]*stock
      stock=0
print(yen)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[a[0]]
f=-1
for i in a:
  if b[-1]<i:
    if f==1:b[-1]=i
    else:
      b.append(i)
      f=1
  if b[-1]>i:
    if f==-1:b[-1]=i
    else:
      b.append(i)
      f=-1
if f==-1:del b[-1]
if len(b)==0:exit(print(1000))
ans=1000
for i in range(0,len(b),2):
  bb=ans//b[i]
  ans-=bb*b[i]
  ans+=bb*b[i+1]
print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

# 連続する場合は無視できるので圧縮
tmp = list(map(int, input().split()))
arr = [tmp[0]]
for i in range(1, n):
    if arr[-1] != tmp[i]:
        arr.append(tmp[i])

# 株価が上がる前に全力で買って、上がったら全力で売る。を貪欲にやる
money, stock = 1000, 0
for i in range(1, len(arr)):
    if arr[i - 1] < arr[i]:
        # 全力買い
        buy, money = divmod(money, arr[i - 1])
        stock += buy
        # 全力売り
        money += stock * arr[i]
        stock = 0

print(money)

Submission status: Accepted

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

Submission status: Accepted

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

#n = int(input())
#l = list(map(int, input().split()))

'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''


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

Submission status: Accepted

Solution code:
# まず底をみつけてそのあと山で売るを繰り返す
n = int(input())
arr = list(map(int, input().split()))
arr2 = []
cur = arr[0] if arr[0] < arr[1] else 10**10
f = 1
for i in range(1, n):
    if f == 1:
        # print("底値を求める")
        if cur < arr[i]:
            arr2.append(cur)
            f *= -1
    else:
        # print("天井を求める")
        if cur > arr[i]:
            arr2.append(cur)
            f *= -1
    cur = arr[i]
if f == -1:
    arr2.append(cur)
ans = 1000
stock = 0
for i, a in enumerate(arr2):
    if i % 2 == 0:
        # 買いまくる
        stock = ans // a
        ans %= a
    else:
        # うりまくる
        ans += stock * a
        stock = 0
print(ans)

Submission status: Accepted

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

money=1000
pos = 1
kabu = 0

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
c = 1000  # cash
s = 0  # stock
up = False
for i in range(n-1):
    if A[i] < A[i+1]:
        if not up:
            s, c = divmod(c, A[i])
            up = True
            #print(A[i], c, s)
    elif A[i] > A[i+1]:
        if up:
            c += s * A[i]
            s = 0
            up = False
            #print(A[i], c, s)
    else:
        pass
else:
    c += s * A[-1]
print(c)

Submission status: Accepted

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

money=1000
best = money
pos = 1
kabu = 0

#初動 買うか買わないか
if A[0]<=A[1]:
    kabu = money//A[0]
    money -= kabu*A[0]
    pos=0
else:
    pos=1

#二回目以降　買うか売るか保留か
for i in range(1,N):
    if pos==0:#売り
        if A[i-1]>=A[i]:
            money+=kabu*A[i-1]
            kabu=0
            pos=1
    elif pos==1:#買い
        if A[i-1]<A[i]:
            kabu = money//A[i-1]
            money -= kabu*A[i-1]
            pos=0
            
    best = max(best,money+kabu*A[i])
            
print(best)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
A.append(0)
L=[0]*N
L[0]=1000

# dpしろ

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

Submission status: Accepted

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

for i in range(days - 1):
  if price[i] < price[i + 1]:
    new = (money // price[i])
    stock += new
    money -= (new * price[i])
    
  else:
    money += stock * price[i]
    stock = 0

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
INF = 10**18
def li():
    return [int(x) for x in input().split()]

N = int(input())
A = [INF] + li()
M = 1000
K = 0

for i in range(1,N+1):
    if i == N:
        sell_cnt = K
        K = 0
        M += sell_cnt * A[i]
        break
    if A[i+1] > A[i] and A[i] <= A[i-1]:
        buy_cnt = M // A[i]
        K += buy_cnt
        M -= buy_cnt * A[i]
    elif A[i+1] < A[i] and A[i] >= A[i-1]:
        sell_cnt = K
        K = 0
        M += sell_cnt * A[i]

print(M)

Submission status: Accepted

Solution code:
N = int(input())

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

is_rising = False
money = 1000
stock = 0

for i in range(N - 1):
    if i == 0:
        if A_li[0] < A_li[1]:
            # できるだけ買う
            stock = money // A_li[0]
            money -= A_li[0] * stock
            is_rising = True
    else:
        if is_rising:
            if A_li[i] > A_li[i + 1]:
                # 全て売る
                money += A_li[i] * stock
                stock = 0
                is_rising = False
        else:
            if A_li[i] < A_li[i + 1]:
                # できるだけ買う
                stock = money // A_li[i]
                money -= A_li[i] * stock
                is_rising = True
    if i + 1 == N - 1:
        # 全て売って終了
        money += A_li[i+1] * stock
        stock = 0

print(money)

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    N = int(input())
    A = list(map(int, input().split()))
    m = 1000
    k = 0

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

    print(m)

main()

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
# 最初の所持金
money = 1000
# 所有株数
kabu_c = 0
# 予言で翌日上がるかどうかのリスト
l = list()
for i in range(n-1):
    if A[i+1] - A[i] > 0:
        l.append(1)
    else:
        l.append(0)
for idx, i in enumerate(l):
    # 翌日上がってかつその時の株価で買える所持金があるなら買う。
    if i == 1 and money - A[idx] >= 0:
        kabu_c = money // A[idx]
        money = money - (kabu_c * A[idx])
    # 翌日上がらないなら売っちゃう（その時点での利幅ゲット）
    if i == 0:
        money = money + (kabu_c * A[idx])
        kabu_c = 0
# 上がり調子だと最終日で売り切ってない可能性があるので所持している株は最終日で売り切る
if kabu_c > 0:
    money = money + (kabu_c * A[n-1])
print(money)

Submission status: Accepted

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

kabu = 0
yen = 1000

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

if kabu > 0:
  yen += A[-1] * kabu
  kabu = 0

print(yen)

Submission status: Accepted

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

money = 1000
s = 0
for i in range(N - 1):
    # begginig of i-th day
    # sell all of stocks
    money += s * A[i]
    s = 0

    if A[i] < A[i + 1]:
        s = money // A[i]
        money %= A[i]

    # print('end of {0}-th day, money: {1}, stock: {2}'.format(i, money, s))

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

Submission status: Accepted

Solution code:
## necessary imports

import sys
input = sys.stdin.readline
from bisect import bisect_left;
from bisect import bisect_right;
from math import ceil, factorial;

def ceil(x):
    if x != int(x):
        x = int(x) + 1;
    return x;
    
# swap_array function
def swaparr(arr, a,b):
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;

## gcd function
def gcd(a,b):
    if b == 0:
        return a;
    return gcd(b, a % b);

## nCr function efficient using Binomial Cofficient
def nCr(n, k): 
    if(k > n - k): 
        k = n - k; 
    res = 1;
    for i in range(k): 
        res = res * (n - i);
        res = res / (i + 1); 
    return int(res);

## prime factorization
def primefs(n):
    ## if n == 1    ## calculating primes
    primes = {}
    while(n%2 == 0 and n > 0):
        primes[2] = primes.get(2, 0) + 1
        n = n//2
    for i in range(3, int(n**0.5)+2, 2):
        while(n%i == 0 and n > 0):
            primes[i] = primes.get(i, 0) + 1
            n = n//i
    if n > 2:
        primes[n] = primes.get(n, 0) + 1
    ## prime factoriazation of n is stored in dictionary
    ## primes and can be accesed. O(sqrt n)
    return primes

## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p): 
    res = 1
    x = x % p  
    if (x == 0) : 
        return 0
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
        y = y >> 1      
        x = (x * x) % p 
    return res 

## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
    temp = a
    a = b
    b = temp
    return a,b;

# find function with path compression included (recursive)
# def find(x, link):
#     if link[x] == x:
#         return x
#     link[x] = find(link[x], link);
#     return link[x];

# find function with path compression (ITERATIVE)
def find(x, link):
    p = x;
    while( p != link[p]):
        p = link[p];
    
    while( x != p):
        nex = link[x];
        link[x] = p;
        x = nex;
    return p;


# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
    x = find(x, link)
    y = find(y, link)
    if size[x] < size[y]:
        x,y = swap(x,y)
    if x != y:
        size[x] += size[y]
        link[y] = x

## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n): 
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
    return prime

#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
    spf[1] = 1;
    for i in range(2, MAXN):
        spf[i] = i;
    for i in range(4, MAXN, 2):
        spf[i] = 2;
    for i in range(3, ceil(MAXN ** 0.5), 2):
        if spf[i] == i:
            for j in range(i*i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i;
    ## function for storing smallest prime factors (spf) in the array

################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
    ret = {};
    while x != 1:
        ret[spf[x]] = ret.get(spf[x], 0) + 1;
        x = x//spf[x]
    return ret;
    ## this function is useful for multiple queries only, o/w use
    ## primefs function above. complexity O(log n)

## taking integer array input
def int_array():
    return list(map(int, input().strip().split()));

def float_array():
    return list(map(float, input().strip().split()));

## taking string array input
def str_array():
    return input().strip().split();

#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');

################### ---------------- TEMPLATE ENDS HERE ---------------- ###################

n = int(input()); a = int_array();
ans = 1000; shares = 0;
for i in range(n - 1):
    if a[i] < a[i + 1]:
        ## buying on the first day;
        shares += ans // a[i];
        ans %= a[i];
        ## selling on the next;
        ans += shares * a[i + 1];
        shares = 0;
print(ans);