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:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy
import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputintlist(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloatlist(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

def to10(A):
    ret=0
    for a in range(len(A)-1,-1,-1):
        ret+=X[a]*2**(N-1-a)
    return ret
######################################################################################################

N=int(input())
A=inputintlist()
A.append(-inf)
num=A[0]
l=0
kane=1000
kabu=0
for a in range(0,N):
    if A[a]<=A[a+1]:
        pass
    else:
        kabu=kane//A[l]
        kane=kane%A[l]
        kane+=kabu*A[a]
        kabu=0
        l=a+1
    

print(kane)

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 - 1] < a[i]:
        gain = (money // a[i - 1]) * a[i]
        money = money % a[i - 1] + gain

print(money)

Submission status: Accepted

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

for i in range(len(a)-1):
    if a[i] >= a[i+1]:
        wallet += stock*a[i]
        stock = 0
    else:
        stock += wallet//a[i]
        wallet %= a[i]

wallet += stock*a[n-1]

print(wallet)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
ans=1000
for i in range(N-1):
  if A[i]<A[i+1]:
    ans+=(ans//A[i])*(A[i+1]-A[i])
print(ans)

Submission status: Accepted

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

money = 1000
stock = 0

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

print(money)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
a_list = list(map(int, input().split()))
x = 1000
b_list = []
start_a = a_list[n - 1]
a = start_a
flag = 0
for i in range(2, len(a_list) + 1):
    if a_list[n - i] <= a:
        flag = 1
        a = a_list[n - i]
        if n - i == 0:
            b_list.append(start_a)
            b_list.append(a)
    else:
        if flag == 0:
            a = a_list[n - i]
            start_a = a
        else:
            b_list.append(start_a)
            b_list.append(a)
            a = a_list[n - i]
            start_a = a
b_list.reverse()
for i in range(len(b_list) // 2):
    c = x // b_list[2 * i]
    x -= b_list[2 * i] * c
    x += b_list[2 * i + 1] * c

print(x)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(hoge) for hoge in input().split()]
Money = 1000
Kabu = 0
for n,a in enumerate(A):
    if n>0:
        if a>=A[n]:#値上がりした
            Money += a*Kabu
            Kabu = 0
        else:
            Kabu += Money//a
            Money %= a
    if n<N-1:
        if a>=A[n+1]:#値下がりする
            Money += a*Kabu
            Kabu = 0
        else:
            Kabu += Money//a
            Money %= a
Money += A[-1] * Kabu
print(Money)

Submission status: Accepted

Solution code:
def main():
    #N,K=map(int,input().split())
    N=int(input())
    A=list(map(int,input().split()))
    t=[A[0]]
    bef=A[0]
    flag=1
    for i in range(1,N):
        if flag==1:
            if bef > A[i]:
                t.append(bef)
                bef=A[i]
                flag=-1
            else:
                bef=A[i]
        elif flag==-1:
            if bef < A[i]:
                t.append(bef)
                bef=A[i]
                flag=1
            else:
                bef=A[i]
    if flag==1:
        t.append(bef)
    money=1000
    while t:
        b=t.pop(0)
        a=t.pop(0)
        cn=money//b
        money=money%b
        money+=cn*a
    print(money)

main()

Submission status: Accepted

Solution code:
n = int(input())
m = 1000
a = list(map(int, input().split()))
for i in range(n-1):
  if(a[i]<a[i+1]):
    #買って売るべき
    m =m+ (m//a[i])*(a[i+1]-a[i])
print(m)

Submission status: Accepted

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

bs = []
b = True
for i in range(n-1):
    if b and a[i] < a[i+1]:
        bs.append(i)
        b = False
    elif not b and a[i] > a[i+1]:
        bs.append(i)
        b = True

if not b:
    bs.append(n-1)

m = 1000
for i in range(0, len(bs), 2):
    s = m // a[bs[i]]
    m += s * (a[bs[i+1]]-a[bs[i]])

print(m)

Submission status: Accepted

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

Submission status: Accepted

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

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

lst = []
small, large = 0, 0
for i in range(N):
    if i==0:
        small = A[i]
        large = A[i]
    else:
        if A[i] < large:
            if small < large:
                lst.append((small,large))
            small = A[i]
            large = A[i]
        else:
            large = A[i]
else:
    if small < large:
        lst.append((small,large))
# print(lst)
ans = 1000
for s, l in lst:
    stock, ans = divmod(ans, s)
    ans += stock * l
print(ans)

Submission status: Accepted

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

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

if S!=0:
  M+=S*A[-1]

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

m, s = 1000, 0
for i in range(1, len(arr)):
    if arr[i - 1] < arr[i]:
        s += m // arr[i - 1]
        m -= m // arr[i - 1] * arr[i - 1]
        m += s * arr[i]
        s = 0

print(m)

Submission status: Accepted

Solution code:
n=int(input())
a=[float('Inf')]
a+=list(map(int,input().split()))
i=0
m=1000
while i<n:
    if a[i]>a[i+1]:
        s=a[i+1]
    if a[i]<a[i+1]:
        m=m//s*a[i+1]+m%s
        s=a[i+1]
    i+=1
print(m)

Submission status: Accepted

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

money = 1000
max_money = money

open_price = a[0]
close_price = a[0]

for i in range(1, n):
	if a[i] >= a[i - 1]:
		close_price = a[i]
	else:			
		#print(i, a[i])
		#print(open_price, close_price)
		if close_price > open_price:
			num = money // open_price
			left = money - num * open_price
			money = left + num * close_price
			max_money = max(money, max_money)
		open_price = a[i]
		close_price = 0
		#print(money)

if close_price > open_price:
	num = money // open_price
	left = money - num * open_price
	money = left + num * close_price
	max_money = max(money, max_money)

print(max_money)

Submission status: Accepted

Solution code:
import sys
 
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))

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

print(m)

Submission status: Accepted

Solution code:
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import reduce
import math
import itertools
import heapq
#import numpy as np
import bisect
import sys
#import sympy
sys.setrecursionlimit(10**6)


def bfs(s, n, node):
    # 頂点が探索済みかどうかのチェック配列
    check = [False for _ in range(n)]
    check[s] = True
    # 次見る頂点を格納するキュー
    queue = deque([s])
    visited_num = 1

    # 問題固有の配列
    #color_check=[[True for _ in range(l)] for _ in range(n)]
    color = [-1 for _ in range(n)]
    color[s] = 0

    while visited_num < n:
        # グラフが全連結ではない場合
        if len(queue) == 0:
            # 空配列を返す
            # 現状までの計算済み配列を返してもいいと思う
            return color

        now_vertex = queue.popleft()
        # インデックスがない場合は
        for next_vertex in node[now_vertex]:

            if check[next_vertex] == True:
                continue

            queue.append(next_vertex)
            check[next_vertex] = True

            # 問題固有の計算
            color[next_vertex] = color[now_vertex]+1
            visited_num += 1

    return color


mod = 10**9+7
# n=int(input())
# n,m=list(map(int,input().split()))
# a=list(map(int,input().split()))
def ceil(x, y): return (x+y-1)//y
def input_list(): return list(map(int, input().split()))


def buy(buy_cost, sell_cost, money):
    return money % buy_cost+(money//buy_cost)*sell_cost


n = int(input())
a = input_list()

mon = 1000
i = 0
while i < n:
    # print(i)
    for j in range(i+1, n):
        if a[j] > a[i]:
            mon = buy(a[i], a[j], mon)
            #print(i, ":", j, "->", mon)
            i = j-1
            break
        if a[j] <= a[i]:
            break
    i += 1


print(mon)

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 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n=k()
a =l()
first = 1000
for i in range(n-1):
    if a[i] > a[i+1]:
        stuck = a[cnt:i+1]
        b = first//min(stuck)
        first += (max(stuck)-min(stuck))*b
        cnt = i+1

stuck=a[cnt:]
b = first//min(stuck)
first += (max(stuck)-min(stuck))*b

print(first)

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]:
        tmp = m // a[i]
        k += tmp
        m -= tmp * a[i]
    else:
        m += k * a[i]
        k = 0
m += k * a[-1]
print(m)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = 1000
i = 0
j = 1
while True:
  if i+1 < n and a[i] < a[i+1]:
    if j+1 < n and a[j] < a[j+1]:
      j += 1
    elif j+1 < n and a[j] >= a[j+1]:
      s = (s//a[i])*a[j]+s%a[i]
      if j+2 == n:
        break
      else:
        i = j+1
        j += 2
    else:
      if a[i] < a[j]:
        s = (s//a[i])*a[j]+s%a[i]
        break
  elif i+1 < n and a[i] >= a[i+1]:
    i += 1
    j += 1
  else:
    break
print(s)

Submission status: Accepted

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

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

money += kanum * li[N-1]
print(money)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()
    A = in_nl() + [0]

    money = 1000
    kabu = 0

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

        # print(kabu, money)

    print(money)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

money = 1000
stack = 0

check = [0]*n

for i in range(n-1):
    if li[i] < li[i+1]:
        check[i] = 'up'
    elif li[i] == li[i+1]:
        check[i] = '-'
    else:
        check[i] = 'down'

for i in range(n):
    if check[i] == 'up':

        get_stack = money//li[i]
        money -= get_stack * li[i]
        stack += get_stack
        
    elif check[i] == '-':
        continue
    
    else:
        
        money += stack * li[i]
        stack = 0
        
print(money)

Submission status: Accepted

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


before = A[0]
raising = False
buy = []
sell = []
for i in range(1, len(A)):
  v = A[i]
  if before < v:
    if not raising:
      buy.append(i - 1)
      raising = True
  if v < before:
    if raising:
      sell.append(i - 1)
    raising = False
  before = v

m = 1000
k = 0
for i in range(N):
  v = A[i]
  if i in buy:
    k = m // v
    m = m - k * v
  if i in sell:
    m += k * v
    k = 0
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 + 1] > a[i]:
        buy = money // a[i]
        stock += buy
        money -= buy * a[i]
    if a[i + 1] < a[i]:
        money += stock * a[i]
        stock = 0
print(money + stock * a[n - 1])

Submission status: Accepted

Solution code:

# 13
# 5 5 6 7 6 6 5 6 7 7 7 6 6 7

def main():
    n = int(input())
    al = list(map(int, input().split()))
    
    buy_is = []
    sell_is = []

    if n == 2:
        if al[0] < al[1]:
            money = 1000
            buy_kabu = 1000//al[0]
            money -= (buy_kabu*al[0])
            money += al[1]*buy_kabu
            print(money)
        else:
            print(1000)
        exit()


    if al[0] < al[1]:
        buy_is.append(0)

    
    for i, (a0,a1,a2) in enumerate( zip(al[0:-2], al[1:-1], al[2:]) ):
        if a0 >= a1 and a1 < a2:
            buy_is.append(i+1)
        elif a0 <= a1 and a1 > a2:
            sell_is.append(i+1)


    money = 1000
    kabu = 0
    for i in range(n):
        if i in buy_is:
            buy_kabu = money//al[i]
            kabu += buy_kabu
            money -= (buy_kabu*al[i])
        elif i in sell_is or i == n-1:
            money += al[i]*kabu
            kabu = 0
    
    print(money)
    # print(buy_is)
    # print(sell_is)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    ans = 1000
    t, p = [], []
    if A[0] < A[1]:
        t.append(0)
    for i in range(1, n - 1):
        if A[i - 1] >= A[i] < A[i + 1]:
            t.append(i)
        elif A[i - 1] <= A[i] > A[i + 1]:
            p.append(i)
    b = 0
    for i in range(n):
        if i in t:
            b += ans // A[i]
            ans %= A[i]
        if i in p:
            ans += b * A[i]
            b = 0
    if b > 0:
        ans += b * A[-1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

cur_m = 1000
cur_s = 0

state = -1 # 1:up 0:down
prev = 0
for ai in a:
  if state == -1:
    prev = ai
    state = 0
  elif state == 1:
    if prev > ai: # peak
      # sell at prev
      cur_m += (prev * cur_s)
      cur_s = 0
      state = 0
  else:
    if prev < ai:
      # buy at prev
      buy_num = cur_m // prev
      cur_s += buy_num
      cur_m -= prev * buy_num
      state = 1
  prev = ai

if cur_s > 0:
  cur_m += (a[-1] * cur_s)

print(cur_m)

Submission status: Accepted

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

money = 1000
# 谷で買い、山で売るを繰り返すのが最適

def search_V(st):
    for j in range(st, n):
        if a[j] > a[j-1]:
            return j-1
    return -1

def search_A(st):
    for j in range(st, n):
        if a[j] < a[j-1]:
            return j-1
    return -1

i = 0
stock = 0
while i < n:
    # 谷を探す
    V = search_V(i+1)
    if V == -1:
        break
    else:
        # ありったけ買う
        stock, money = divmod(money, a[V])
        
    # 山を探す
    A = search_A(V+1)
    if A == -1:
        # ありったけ売る        
        money += stock * a[A]
        break
    else:
        money += stock * a[A]
    
    i = A

print(money)

Submission status: Accepted

Solution code:
#

import sys
input=sys.stdin.readline

def main():
    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]:
            m+=s*A[i]
            s=0
        else:
            n=int(m//A[i])
            m-=n*A[i]
            s+=n
    m+=s*A[-1]
    print(m)
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:

def main():
  n = int(input())
  A = list(map(int, input().split()))
  A.append(0)

  money = 1000
  kabu = 0
  for i in range(n):
    a = A[i]
    nxt = A[i + 1]

    if a < nxt:
      cnt = money // a
      money -= cnt * a
      kabu += cnt
    elif a > nxt:
      money += kabu * a
      kabu = 0

  print(money)


main()

Submission status: Accepted

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

l = []
flag = False
for i in range(N-1):
  if flag:
    if A[i] > A[i+1]:
      l.append(A[i])
      flag = False
  else:
    if A[i] < A[i+1]:
      l.append(A[i])
      flag = True
if flag:
  l.append(A[-1])

flag = False
ans = 1000
stock = 0
for n in l:
  if flag:
    ans += stock * n
    stock = 0
    flag = False
  else:
    stock = ans // n
    ans -= stock * n
    flag = True
print(ans)

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]:
        if money >= A[i]:
            kabu = money//A[i]
            money = money%A[i]
    elif A[i]>A[i+1]:
        if kabu > 0:
            money += kabu*A[i]
            kabu = 0
if kabu>0:
    money += kabu*A[-1]
print(max(money,1000))

Submission status: Accepted

Solution code:
import sys
import numpy as np

readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
a = np.array(readline().split(), dtype=np.int64)
a = np.insert(a, n, 0)
C = 1000
I = 0
for i in range(n):
    if a[i] < a[i+1]:
        x = C // a[i]
        I += x
        C -= x * a[i]
    if a[i] > a[i+1]:
        C += I * a[i]
        I = 0
print(C)

Submission status: Accepted

Solution code:
import math

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

Money=1000
Kabu=0

def MoneyWar(Money,Kabu,A,N):
  for i in range(N-1):
    if A[i]<A[i+1]:
      Kounyu=math.floor(Money/A[i])
      Money-=A[i]*Kounyu
      Kabu+=Kounyu

    if A[i]>A[i+1]:
      Money+=Kabu*A[i]
      Kabu=0

      
  if Kabu>0:
    Money+=Kabu*A[-1]
    
  return Money

print(MoneyWar(Money,Kabu,A,N))

Submission status: Accepted

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

Submission status: Accepted

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

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

m=1000
k=0
for i in range(n-1):
    if l[i]==1:
        p=m//a[i]
        k+=p
        m-=p*a[i]

    if l[i]==-1:
        m+=k*a[i]
        k=0
#    print(k,m)
m+=k*a[n-1]
print(m)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=list(map(int,input().split()))
i=0
A.insert(0,float("inf"))
#print(A)
buy=[0]*N
sell=[0]*N
for i in range(N-1):
    if (A[i]>=A[i+1])and(A[i+2]>=A[i+1]):
        buy[i]=1
    if (A[i]<=A[i+1])and(A[i+2]<=A[i+1]):
        sell[i]=1
kabu=0
money=1000
#print(buy,sell)
#print(money,kabu)
A=A[1:]
#print(A)
for i in range(N):
    if sell[i]==1:
        money=money+kabu*A[i]
        kabu=0
    if buy[i]==1:
        kabu=kabu+money//A[i]
        money=money-(money//A[i])*A[i]
    #print(money,kabu,A[i])
    #input()
#print(money,kabu)
money=money+kabu*A[N-1]
print(money)

Submission status: Accepted

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