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:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = II()
    a = LI()
    ans = 1000
    have = 0
    state = 0
    for i in range(n - 1):
        if state == 0:
            if a[i] < a[i + 1]:
                have += ans // a[i]
                ans %= a[i]
                state = 1
        else:
            if a[i] > a[i + 1]:
                ans += a[i] * have
                have = 0
                state = 0
    ans += a[-1] * have
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

money = 1000
ad = 0
for i in range(n - 1):
    if a[i + 1] > a[i]:
        buy_stock = (money // a[i])
        buy_money = buy_stock * a[i]
        sel = buy_stock * a[i + 1]
        remain = money - buy_money
        money = remain + sel
    #print(money)

print(money)

Submission status: Accepted

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

m = 1000
k = 0

for i in range(n):
    if i == n-1: # 最後ながら全部売る
        m = m + A[i]*k
    elif A[i] <= m and A[i] < A[i+1]: #お金あって明日上がるなら買う
        k = m//A[i]
        m = m - A[i]*k
    elif 0 < k and A[i+1] < A[i]: #株があって明日下がるなら売る
        m = m + A[i]*k
        k = 0
    else:
        pass

print (m)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N = getN()
A = getList()

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

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    C = 1000
    kabu = 0

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

    print(C)

resolve()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.append(0)
stock=0
cash=1000
for i in range(n):
  if a[i]>=a[i+1] and stock==0:
    continue
  elif a[i]<a[i+1] and stock==0:
    stock=cash//a[i]
    cash-=stock*a[i]
  elif a[i]<=a[i+1] and stock!=0:
    continue
  elif a[i]>a[i+1] and stock!=0:
    cash+=stock*a[i]
    stock=0
print(cash)

Submission status: Accepted

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

mon=1000
for i in range(1,N):
    if A[i]>A[i-1]:
        sho,mod=divmod(mon,A[i-1])
        mon=sho*A[i]+mod

print(mon)

Submission status: Accepted

Solution code:
def inN():
    return int(input())
def inL():
    return list(map(int,input().split()))

n = inN()
a = inL()

kab = 0
mon = 1000
f = []
i=0
state = 0
while(i < n-1):
    if state == 1:
        while(i<n-1):
            #print(a[i],a[i+1])
            if a[i+1]>=a[i]:
                f.append(0)
                i+=1
            else:
                f.append(2)
                #i+=1
                break
        state = 0
    elif state == 0:
        while(i<n-1):
            if a[i+1]<=a[i]:
                f.append(0)
                i +=1
            else:
                f.append(1)
                #i += 1
                state = 1
                break
    else:
        f.append(0)
    i += 1
f.append(2)
    
#print(f)   
for i in range(n):

    if f[i] == 1 :
        kab += mon//a[i]
        mon = mon % a[i]
    elif f[i] == 2:
        mon += a[i]*kab
        kab = 0
    
print(mon)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

CurrentMoney = 1000

for i in range(0, n-1):
    Stocks = 0
    if A[i] < A[i+1]: Stocks = CurrentMoney // A[i]
    CurrentMoney += (A[i+1] - A[i]) * Stocks

print(CurrentMoney)

Submission status: Accepted

Solution code:
N = 2
ARR = [157, 193]

N = 6
ARR = [200, 180, 160, 140, 120, 100]

N = 7
ARR = [100, 130, 130, 130, 115, 115, 150]

N = int(input())
ARR = list(map(int, input().split()))
#

# N = 5
# ARR = [111, 111, 111, 111, 111]

def prepare(n,arr):
    result = []
    for i in range(n):
        if i == 0:
            result.append(arr[0])
        else:
            if arr[i] != arr[i-1]:
                result.append(arr[i])
    return result


def calculate(arr):
    n = len(arr)
    result = []
    for i in range(n):
        if i == n-1:
            if arr[i] > arr[i-1]:
                result.append({'type': 1, 'value': arr[i]})
            continue

        if i == 0:
            if arr[1] > arr[0]:
                result.append({'type': 0, 'value': arr[i]})
            continue

        if (arr[i + 1] > arr[i]) and (arr[i] < arr[i - 1]):
            result.append({'type': 0, 'value': arr[i]})

        if (arr[i + 1] < arr[i]) and (arr[i] > arr[i - 1]):
            result.append({'type': 1, 'value': arr[i]})

    money = 1000
    num = 0
    for index,res in enumerate(result):
        type = res.get('type')
        value = res.get('value')

        if index == (len(result) - 1):
            if value == 1:
                continue

        if type == 0:
            tmpNum = money // value
            num += tmpNum
            money -= value * tmpNum
        else:
            money += num * value
            num = 0

    print(money)


arr = prepare(N, ARR)
calculate(arr)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
money = 1000
kabu = 0
todo = [0]*n
for i in range(n):
    if(i == 0):
        if(a[n-2-i] < a[n-1-i]):
            state = 1
            todo[n-1] = 1#uri
        elif(a[n-2-i] > a[n-1-i]):
            state = -1
            todo[n-1] = 0
        else:
            state = 0
            todo[n-1] = 0
    elif(i == n-1):
        if(state == 1):
            todo[n-1-i] = -1
    else:
        if(a[n-2-i] < a[n-1-i]):
            temp = 1
            if(state != temp):
                todo[n-1-i] = 1
            state = 1
        elif(a[n-2-i] > a[n-1-i]):
            temp = -1
            if(state != temp):
                todo[n-1-i] = -1#kai
            state = -1

for i in range(n):
    if(todo[i] == -1):
        if(money > 0):
            kazu = money//a[i]
            money = money - kazu * a[i]
            kabu = kazu                
    elif(todo[i] == 1):
        money = money + kabu * a[i]
        kabu = 0
    
print(money)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
syoji = 1000
kabu = 0
kai = 0
uri = 0
if N ==1:
  print(syoji)
  exit()
sw = 0
for i in range(N):
  if i<N-1 and kabu == 0 and A[i] < A [i+1]:
    for j in range(i+1,N):
      if sw ==1 and uri >= A[j]:
        break
 
      elif  A[i] < A[j]:
        uri = A[j]
        sw = 1
    if sw ==1:
      kabu = syoji // A[i]
      syoji = syoji - kabu*A[i]
      kai = A[i]
      sw = 0
  elif kabu > 0 and A[i]==uri:
    syoji = syoji + kabu*A[i]
    kabu = 0
    uri = 0
 
        
print(syoji)

Submission status: Accepted

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

    N = len(A)
    money = 1000
    for i in range(0,N):
        if i+1 >= N:
            break
        money2 = money // A[i] * A[i+1] +  money % A[i]
        money = max(money,money2)
    
    print(money)
        
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n = int(input())
A = tuple(map(int, input().split()))
m = 1000
kabu = 0
for pa, aa in zip(A[:n], A[1:]):
    if pa < aa:
        # buy all
        kk, m = divmod(m, pa)
        kabu += kk
    elif pa > aa:
        # sel all
        m += kabu * pa
        kabu = 0

if kabu > 0:
    m += kabu*aa

print(m)

Submission status: Accepted

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

money = 1000

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

print(money)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
tl=[l[0]]
for i in range(1,n):
    if l[i]!=l[i-1]:
        tl.append(l[i])


a=[]
ma=False

for i in range(len(tl)):
    if len(tl)==1:
        break
    if i==0:
        if tl[i]>tl[i+1]:
            ma=True
            a.append(tl[i])
        else:
            ma=False
            a.append(tl[i])
    elif i==len(tl)-1:
        if tl[i]>tl[i-1]:
            a.append(tl[i])
        else:
            a.append(tl[i])
    else:
        if tl[i-1]<tl[i] and tl[i+1]<tl[i]:
            a.append(tl[i])
        elif tl[i-1]>tl[i] and tl[i+1]>tl[i]:
            a.append(tl[i])

if ma:
    a.pop(0)


num=0
ans=1000

for i in range(0,len(a)-1,2):
    
    thing1=(ans//(a[i]))*a[i+1]
    thing2=num*a[i+1]
    if thing1>thing2:
        ans=thing1+ans%a[i]
        num=0
        
    elif thing2>thing1:
        ans=thing2

print(ans)

Submission status: Accepted

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

cash = 1000
hold_stock = 0

for i in range(N):
    stock_price = stocks[i]
    if hold_stock == 0:
        if i == N-1:
            break
        elif stock_price == max(stocks[i:]):
            pass
        else :
            j = 1
            next_stock_price = stocks[i+j]
            while stock_price == next_stock_price:
                j += 1
                next_stock_price = stocks[i+j]
                if i + j == N:
                    break
            if next_stock_price < stock_price:
                pass
            else :
                hold_stock, cash = divmod(cash, stock_price)
    else :
        if i == N-1:
            cash += hold_stock*stock_price
            hold_stock = 0

        elif stock_price == min(stocks[i:]):
            pass
        else :
            j = 1
            next_stock_price = stocks[i+j]
            while stock_price == next_stock_price:
                j += 1 
                next_stock_price = stocks[i+j]
                if i + j == N:
                    break
            if next_stock_price > stock_price:
                pass
            else :
                cash += hold_stock*stock_price
                hold_stock = 0

print(cash)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict


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

money = 1000
min_v = a[0]
max_v = a[0]

for i in range(1,n):
    if max_v > a[i]:
        money = money//min_v * max_v + money % min_v
        min_v = a[i]
        max_v = a[i]
    
    max_v = a[i] if a[i] > max_v else max_v
    min_v = a[i] if a[i] < min_v else min_v

    if i == n-1 and a[i] > min_v:
        money = money//min_v * max_v + money % min_v

    # print(i,max_v,min_v,money)

print(money)

Submission status: Accepted

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


for i in range(n-1):
    if a[i] < a[i+1]:
        t = x // a[i]
        k += t
        x -= t * a[i]
    else:
        x += k * a[i]
        k = 0
    #print(x,k)
print( x + k * a[-1])

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
lst1 = list(map(int,readline().split()))
ct = 1000
stock = 0
#次の日に上がるか下がるか
lst2 = [0]*n
for i in range(n-1):
    if lst1[i] < lst1[i+1]:
        lst2[i] = 1
    elif lst1[i] > lst1[i+1]:
        lst2[i] = -1
for i in range(n):
    if lst2[i] == 1:
        stock += ct//lst1[i]
        ct %= lst1[i]
    elif lst2[i] == -1:
        ct += stock*lst1[i]
        stock = 0


print(ct + stock*lst1[-1])

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
A = LI()

X = []
Y = 1000

for i in range(N):
    for x in X:
        if Y < x[0]*A[i]+x[1]:
            Y = x[0]*A[i]+x[1]
    X.append((Y//A[i],Y%A[i]))

print(Y)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
x.append(90)
y = 1000
k =0
for i in range(len(x)-1):
    if x[i] < x[i+1]:
        k += int(y/x[i])
        y -= x[i]*int(y/x[i])
    elif x[i] > x[i+1]:
        y += x[i]*k
        k = 0
    
print(y)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
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 li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N = ii()
A = li()

yen = 1000
kabu = 0

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

print(yen)

Submission status: Accepted

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

min_money = 0
ans = 1000
i = 0
while i < n:
    stock = 0
    rest = 0
    for k in range(i, n):
        if a[k] < a[k + 1]:
            min_money = a[k]
            stock = ans // a[k]
            rest = ans % a[k]
            i = k
            break
    
    if stock != 0:
        temp = stock * a[i+1]
        for k in range(i, n):
            if temp <= a[i+1] * stock:
                ans = a[i+1] * stock
    ans += rest
    i += 1

print(ans)

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
from fractions  import gcd
#input=sys.stdin.readline
import bisect
n=int(input())
a=list(map(int,input().split()))
i=0
m=1000
f=True
k=0
M=[1000]
res=[0]*(n-1)
for i in range(1,n):
    if a[i]<a[i-1]:
        res[i-1]=-1
    else:
        res[i-1]=1

for i in range(n):

    if f:
        if i==n-1:
          break
        if res[i]==1:
            f=False 
            k=m//a[i]
            m=m%a[i]
        else:
          continue
    else:
        if i==n-1 or res[i]==-1:
            m=k*a[i]+m
            M.append(m)
            f=True 
        else:
            continue
print(max(M))

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = list(map(int, input().split()))

    A.append(0)
    min_, max_ = A[0], A[0]
    money = 1000
    for i in range(N + 1):
        if A[i] < max_:
            d, m = divmod(money, min_)
            money = d * max_ + m
            min_, max_ = A[i], A[i]
            continue
        min_ = min(min_, A[i])
        max_ = max(max_, A[i])
    print(money)


main()

Submission status: Accepted

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

cash = 1000
stocks = 0

buy = []
sell = []

trend = 'down'

for i in range(n-1):
    if trend == 'down':

        if A[i] >= A[i+1]:
            continue
        else:
            trend = 'up'
            buy.append(i)
            if i == n-2:
                sell.append(i+1)
    # 'up'
    else:
        if A[i] <= A[i+1]:
            if i == n-2:
                sell.append(i+1)
            continue
        else:
            trend = 'down'
            sell.append(i)

while len(buy) and len(sell):
    b = buy.pop(0)
    count = cash // A[b]
    cash -= A[b] * count
    stocks += count

    s = sell.pop(0)
    cash += A[s] * stocks
    stocks = 0

print(cash)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().split())) + [0]
    
    B = []
    left, right = 0, 1
    while right != N + 2:
        if A[left:right] == list(sorted(A[left:right])):
            right += 1
        else:
            B.append(A[left:right - 1])
            left = right - 1
    
    score = 1000
    for a_lr in B:
        if len(a_lr) == 1:
            continue
        stock, score = divmod(score, a_lr[0])
        score += stock * a_lr[-1]
    print(score)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        for line in sys.stdin:
            lines.append(line.strip())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    

    n = int(lines[0])
    a = [int(e) for e in lines[1].split(' ')]

    return (n, a)


def solve(n, a):

    b = []
    for i in range(1, n):
        b.append(a[i] - a[i-1])
    
    current_money = 1000
    current_stock = 0
    for i in range(0, n):
        if i < n - 1 and b[i] >= 0:
            if current_stock == 0:
                current_stock = current_money // a[i]
                current_money = current_money - current_stock * a[i]
        else:
            if current_stock > 0:
                current_money = current_money + current_stock * a[i]
                current_stock = 0

        if debug:
            log("i=%d, current_money=%d, current_stock=%d" % (i, current_money, current_stock))

    if current_stock > 0:
        current_money = current_money + current_stock * a[n-1]


    return current_money

def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':
  
    main()

Submission status: Accepted

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

stack = 0
ans = 1000

Flag = False
i,j  = 0, 0

while i < n-1:
    if a[i] < a[i+1]:
        stack = ans // a[i]
        ans -= stack*a[i]
        j = i
        while j < n-1:
            if a[j] >= a[j+1]:
                ans += stack*a[j]
                stack = 0
                i = j
                Flag = True
                break
            j += 1

    if Flag:
        Flag = False
        continue

    i += 1

if a[-2] < a[-1]:
    ans += stack*a[-1]

print(ans)

Submission status: Accepted

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

money = 1000
min_val = 10000

for i in range(N):
    if (A[i] < min_val):
        min_val = A[i]

    if (A[i] > min_val):
        money += (money // min_val) * (A[i] - min_val)
        min_val = A[i]

print(money)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int,input().split()))
ans = 1000
stock = 0
for i in range(1,N):
  if List[i] > List[i-1]:
    temp = ans
    ans = temp%List[i-1]
    stock += temp//List[i-1]
  if List[i] < List[i-1]:
    ans += stock*List[i-1]
    stock = 0
if stock != 0:
  ans += stock*List[-1]
print(ans)

Submission status: Accepted

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

money = 1000
kabu = 0
toku = 0
min_val = 10000

for i in range(N):
    if (A[i] < min_val):
        min_val = A[i]

    if (A[i] > min_val):
        money += (money // min_val) * (A[i] - min_val)
        min_val = A[i]

print(money)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
A=list(map(int,input().split()))
edge=[[] for _ in range(N)]
for i in range(N-1):
    edge[i].append((i+1,1,1))
for i in range(N):
    for j in range(i+1,N):
        if A[i]<A[j]:
            edge[i].append((j,A[i],A[j]))
que=deque([0])
d=[0]*N
d[0]=1000
while que:
    v=que.popleft()
    for nv,be,af in edge[v]:
        tmp=d[v]//be
        if d[nv]<d[v]-tmp*be+tmp*af:
            d[nv]=d[v]-tmp*be+tmp*af
            que.append(nv)
print(d[-1])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)
 

def maxProfit(prices) -> int:
    if len(prices) < 2:
        return 0
    ans = 1000
    for i in range(1,len(prices)):
        if prices[i] > prices[i - 1]:
            stock = ans // prices[i - 1]
            ans += stock * (prices[i] - prices[i - 1]) 
    return ans

def main():
    n = int(input().strip())
    A = [int(i) for i in input().strip().split()]

    ans = maxProfit(A)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from math import sqrt, gcd, ceil, log
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))

def main():
	n = int(input()); arr = read()
	stocks = 0; money = 1000
	j = n-1
	while j > 0 and arr[j] <= arr[j-1]:
		j -= 1
	for i in range(j):
		if arr[i] < arr[i+1] and stocks == 0:
			stocks += money//arr[i]
			money = money%arr[i]
		elif arr[i] > arr[i+1] and stocks:
			money += stocks*arr[i]
			stocks = 0

	# print(j)
	if stocks:
		money += arr[j]*stocks

	print(money)







if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
cnt=0
w=1000
for i in range(len(A)):
    if i!=len(A)-1:
        if A[i]<A[i+1]:
            cnt+=w//A[i]
            w=w%A[i]
        if A[i]>A[i+1]:
            w+=cnt*A[i]
            cnt=0
    else:
        w+=cnt*A[i]
print(max(n,w))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#D - Road to Millionaire
N= int(input())
A= list(map(int,input().split()))
A_s = [0]
B = 1000
C = 0
B_1 = 0
C_1 = 0
for j in range(N-1):
    A_s.append(A[j+1]-A[j])
#print(A_s)

for i in range(N-1):
    if A_s[i+1] > 0:
        B_1 = B - (B // A[i])*A[i]
        C_1 = C + B // A[i]
        B = B_1
        C = C_1
    elif A_s[i+1] <= 0:
        if C > 0:
            B_1 = B + C * A[i ]
            C = 0
            B = B_1
        else:
            pass
    else:
        pass

if C > 0:
    B = B +C*A[N-1]

print(B)

Submission status: Accepted

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

B = []

def buy(m, s, a):
    t = m//a
    return m-t*a, s+t

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

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

B.append(1)

flag = True

MAX = 1000

for i in range(n-1):
    if flag and B[i] == 1:
        money, stock = buy(money, stock, A[i])
        flag = False
        MAX = max(money, MAX)
        # print('buy', i, stock, money)
    elif not flag and B[i] == -1:
        money, stock = sell(money, stock, A[i])
        flag = True
        MAX = max(money, MAX)
        # print('sell', i, stock, money)

money, stock = sell(money, stock, A[n-1])
MAX = max(money, MAX)
# print('sell', i, stock, money)

print(MAX)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
s=0
m=1000
l=0
for i in range(1,n):
  if a[i-1]<=a[i]:
    l=i
if l==0:
  print(1000)
  exit()
if l<n-1:
  for i in range(l):
    if a[i]<a[i+1]:
      s+=m//a[i]
      m-=a[i]*(m//a[i])
    if a[i]>a[i+1]:
      m+=s*a[i]
      s=0
  m+=s*a[l]
else:
  for i in range(n-1):
    if a[i]<a[i+1]:
      s+=m//a[i]
      m-=a[i]*(m//a[i])
    if a[i]>a[i+1]:
      m+=s*a[i]
      s=0
  m+=s*a[l]
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]:
    stock += int(money/A[i])
    money -= A[i]*int(money/A[i])
  if A[i]>A[i+1]:
    money += A[i]*stock
    stock = 0
money += A[n-1]*stock
print(money)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 1000
    s = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = [""]*n
    for i in reversed(range(n)):
        if a[i]>a[i-1]:
            b[i]="sell"
        elif a[i]<=a[i-1]:
            b[i]="buy"
    if a[0] <= a[1]:
      b[0] = "buy"
    elif a[0] > a[1]:
      b[0] = "sell"
    for i in range(n):
        if b[i] == "sell" and (i==n-1 or b[i+1] != "sell"):
            ans += s*a[i]
            s = 0
        elif b[i] == "buy" and i != n-1 and b[i+1] != "buy":
            s += ans//a[i]
            ans -= ans//a[i]*a[i]
    
    print(ans)

if __name__ == '__main__':
    main()