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()))
B = [0]*(N-1)

for i in range(N-1):
    B[i] = A[i+1] -  A[i]

#print(*B)

yen = 1000
i = 0
while i < N - 1:
    if B[i] > 0:
        j = i
        while j < N - 1 and B[j] >= 0:
            j += 1
        j -= 1
        #print("increase",i,j+1)
        kabu = yen // A[i]
        yen += kabu * (A[j+1] - A[i])
        i = j + 1
        continue
    if B[i] < 0:
        j = i
        while j < N - 1 and B[j] <= 0:
            j += 1
        j -= 1
        #print("decrease",i,j+1)
        i = j + 1
        continue
    i += 1

print(yen)

Submission status: Accepted

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

money = 1000
kabu = 0
pos = 'no'
p = 0
for i, t in enumerate(al):
    if pos != 'no':
        if p < t:
            money += (kabu*t)
            kabu = 0
            pos = 'no'

    if pos == 'no' and money // t >= 1:
        f = False
        for j, a in enumerate(al[i:]):
            if a > t:
                if t == min(al[i:i+j+1]):
                    f = True
                    break
        if f:
            kabu += (money // t)
            money -= kabu*t
            pos = 'b'
            p = t
print(money)

Submission status: Accepted

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

money = 1000
kabu = 0
tmp = a[0]
ans = 0
for i in a:
    if i > tmp:#上がったとき一番安かった日に買っていたことにする
        kabu += money//tmp
        money -= (money//tmp)*tmp
    elif i < tmp:#下がったとき一番高かった日に売っていたことにする
        money += kabu*tmp
        kabu = 0
        ans = max(money,ans)
    tmp = i

if kabu > 0:
    money += kabu*a[-1]

print(max(money,ans))

Submission status: Accepted

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

money = 1000
stock = 0
price = float("INF")
for i in range(N-1):
    if price < A[i] and stock != 0:
        money += stock * A[i]
        stock = 0
        price = float("INF")

    if A[i] < A[i+1]:
        stock += money // A[i]
        money -= A[i] * stock
        price = A[i]

if price < A[N-1] and stock != 0:
    money += stock * A[N-1]
    
print(money)

Submission status: Accepted

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

stair = []
startDate = 0
lastDate = N - 1
firstFlag = True
for i in range(1, N):
    if (A[i] == A[i - 1]):
        stair.append(0)
        continue
    if (A[i] < A[i - 1]):
        stair.append(-1)
    else:
        if (firstFlag):
            firstFlag = False
            startDate = i - 1
        stair.append(1)
        lastDate = i

upflag = False
base = 1000
ticket = 0
for i in range(startDate, lastDate):
    if (stair[i] == 0):
        continue
    if (upflag):
        if (stair[i] == -1):
            base += ticket * A[i]
            upflag = False
    else:
        if (stair[i] == 1):
            ticket = int(base / A[i])
            base -= ticket * A[i]
            upflag = True

if (ticket != 0):
    base += ticket * A[lastDate]

print(base)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
# dp[buy][sell]
dp = [0 for j in range(n)]
dp[0] = 1000
for i in range(n):
    ai = a[i]
    m = dp[i]
    s = m // ai

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

    for j in range(n - 1):
        dp[j + 1] = max(dp[j + 1], dp[j])
print(dp[-1])

Submission status: Accepted

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

ans=1000
i=0
while i<n:
    j=i
    mi=a[i]
    while j<n and mi>=a[j]:
        mi=min(mi,a[j])
        j+=1
    
    if j==n:
        break

    stock=ans//mi
    ans-=stock*mi
    ans+=a[j]*stock
    i=j

print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))

    now = 1000
    stock_num = 0

    for i in range(len(a)-1):
        # buy
        if stock_num == 0:
            if a[i] < a[i + 1]:
                stock_num = int(now / a[i])
                now -= a[i] * stock_num

        # sell
        else:
            if a[i] > a[i + 1]:
                now += a[i] * stock_num
                stock_num = 0

    if not stock_num == 0:
        now += a[-1] * stock_num
    print(now)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
old = A[0]
a = 0 # 持ち株
b = 1000 # 所持金
for i in range(1, N):
    Ai = A[i]
    if old < Ai:
        a += b // old
        b = b % old
    elif old > Ai:
        b += old * a
        a = 0
    old = Ai
b += old * a
print(b)

Submission status: Accepted

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

M = 1000
X = 0
i = 0

while i<N-1:
  if A[i]<A[i+1]:
    M = M + X * A[i]
    X = M // A[i]
    M = M - X * A[i]
    i += 1
  elif A[i]>A[i+1]:
    M = M + X * A[i]
    X = 0
    i += 1
  else:
    i += 1

print(A[N-1] * X + M)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
A = LI()
money,stock = 1000,0
temp = A[0]
for i in range(1,N):
    if temp<A[i]:
        add_stock = money//A[i-1]
        money -= A[i-1]*add_stock
        stock += add_stock
    if temp>A[i]:
        if stock:
            money += A[i-1]*stock
            stock = 0
    if i==N-1:
        if stock:
            money += A[i]*stock
    temp = A[i]
print(money)

Submission status: Accepted

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

shojikin = 1000
kabu = 0
#l[商、あまり]
l = [0,0]
s = [0,0]
for i in range(n-1) :
    if kabu == 0 : #所持株が０なら買う
        if a[i] < a[i+1] :
            l[0] = shojikin // a[i]
            l[1] = shojikin % a[i]
            shojikin = l[1]
            kabu += l[0]
            l[0] = 0
            l[1] = 0
    if kabu > 0 :
        if a[i] > a[i+1] :
            shojikin += kabu * a[i]
            kabu = 0

if kabu > 0 :
    shojikin += kabu * a[n-1]

print(shojikin)

Submission status: Accepted

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

ans = 1000
left = A[0]
right = 0
for i in range(n-1):
    right = A[i]
    if A[i]>A[i+1]:
        ans+=(right-left)*(ans//left)
        left = A[i+1]
        #print(left)
#print(ans,left,right)
if left<A[-1]:
    ans+=(A[-1]-left)*(ans//left)
print(ans)

Submission status: Accepted

Solution code:
import sys
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]

N = ri()
A = ri_()
A.append(-1)
sell = set([])
buy = set([])
tmp = A[0]
f = 0
bf = False
for i in range(1, N + 1):
    if tmp < A[i]:
        if f == 0:
            f = 1
            buy.add(0)
        if f == 1:
            f = 1
        else:
            buy.add(i - 1)
            f = 1
    elif tmp > A[i]:
        if f == 0 or f == 2:
            f = 2
        else:
            sell.add(i - 1)
            f = 2
    tmp = A[i]
m = 1000
k = 0
L = max(sell) if sell else N - 1
for i in range(L):
    if i in buy:
        k = k + m // A[i]
        m = m % A[i]
    elif i in sell:
        m = m + k * A[i]
        k = 0
print( m + k * A[L])

Submission status: Accepted

Solution code:
import math
N = int(input())
A = list(map(int, input().split()))
C = 1
m = A[0]
M = A[0]
money = 1000;
for i in range(1,N):
  if C == 1 and M <= A[i]:
    M = A[i]
  elif C == 1 and M > A[i]:
    money = (money % m) + math.floor(money / m)*M
    C = 0
    m = A[i]
  elif C == 0 and m >= A[i]:
    m = A[i]
  elif C == 0 and m < A[i]:
    M = A[i]
    C = 1
if C == 1:
  money = (money % m) + math.floor(money / m)*M
print(money)

Submission status: Accepted

Solution code:
n = int(input())
A = [201]+list(map(int,input().split()))+[99]
maxp = 0
minp = A[0]
money = 1000
stock = 0
for i in range(n+1):
  if A[i] >= A[i+1] and A[i]>minp:
    money = (money//minp)*A[i]+money%minp
    minp = A[i+1]
  minp = min(minp,A[i+1])
print(money)

Submission status: Accepted

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

c = 1000
s = 0

for i in range(0,N-1):
  if ((A[i]-A[i-1])*(A[i+1]-A[i])>0) and i>0:
    continue
  if A[i]<A[i+1]:
    s = s + c//A[i]
    c = c - c//A[i]*A[i]
  else:
    c = c + s * A[i]
    s = 0

c = c + s * A[i+1]
s = 0

print(c)

Submission status: Accepted

Solution code:
def main():
    N = int( input())
    A = list( map( int, input().split()))
    ans = 1000
    stock = 0
    stock_price = 1000
    for i in range(N-1):
        a = A[i]
        # print(i, stock_price,a)
        if stock > 0 and stock_price <= a:
            ans += stock*a
            stock = 0
        # print("ans", ans)
        if A[i+1] < a:
            pass
        else:
            stock += ans//a
            ans -= ans//a*a
            stock_price = a
        # print(ans, stock)
    if stock_price < A[N-1]:
        ans += stock*A[N-1]
    else:
        ans += stock*stock_price
    print(ans)
            
        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

profit = 0 
money = 1000
for i in range(0, len(prices)- 1): 
  dayDiff = prices[i+1] - prices[i]
  if dayDiff > 0:
    stock = money//prices[i]
    profit = prices[i+1]*stock
    money = profit + money - stock*prices[i]
 

print(money)

Submission status: Accepted

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

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

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

print(dp[-1])

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

if __name__=='__main__':
    n = I()
    a = list(IL())
    count = 0
    ans = 1000
    for i in range(len(a)-1):
        if a[i]<a[i+1]:
            tmp = ans//a[i]
            count += tmp
            ans -= tmp*a[i]
        elif a[i]>a[i+1]:
            ans += a[i]*count
            count = 0
    ans += count*a[-1]
    print(ans)
    exit()

Submission status: Accepted

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

money = 1000
stock = 0

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

print(money)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mny = 1000
up = True
i = 0
while True:
    if a[i+1] > a[i]:
        cnt = 0
        while a[i + cnt + 1] >= a[i + cnt]:
            cnt += 1
            if i + cnt == n - 1:
                mny += (mny//a[i])*(a[i+cnt] - a[i])
                i += cnt
                break
        if i == n - 1:
            break
        mny += (mny//a[i])*(a[i+cnt] - a[i])
        i += cnt
    else:
        i += 1
    if i == n - 1:
        break
print(mny)

Submission status: Accepted

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

now = 1000
stock = 0
down = []

for i in range(1, n):
    if A[i-1] > A[i]:
        down.append(i-1)

if len(down) > 0:
    if down[0] != 0:
        stock = 1000//A[0]
        now %= A[0]

    for i in range(len(down)):
        now += stock*A[down[i]]
        stock = 0
        if (i != len(down)-1 and down[i+1] == down[i]+1) or down[i] == n-2:
            pass
        else:
            a = A[down[i]+1]
            stock = now//a
            now %= a
            # print(now)
    if down[-1] == n-2:
        pass
    else:
        now += stock*A[n-1]
    # print(down)
else:
    now += (now//A[0])*(A[-1]-A[0])

print(now)

Submission status: Accepted

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

def short_long_decisions(xs):
    return [ *map(lambda t: xs[t[0]] <= t[1], zip(range(len(xs)), xs[1:])), False ]

stocks  = 0
money   = 1000
actions = short_long_decisions(ai)

for i, action in enumerate(actions):
    if action:
        buy = money // ai[i]
        stocks += buy
        money -= buy * ai[i]

    else:
        money += stocks * ai[i]
        stocks = 0

print(money)

Submission status: Accepted

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

flag=0
b=[]
c=[]
for i in range(n-1):
    if a[i]<a[i+1]:
        if flag>0:
            pass
        else:
            flag=1
            b.append(a[i])
    else:
        if flag<=0:
            pass
        else:
            flag=-1
            c.append(a[i])
    #print(a[i])
    
if flag==1:
    c.append(a[n-1])
    
            
if c==[]:
    print(1000)
else:
    money=1000
    kabu=0
    for i in range(len(b)):
        kabu=money//b[i]
        money=money-kabu*b[i]+kabu*c[i]
        kabu=0
    print(money)
    
#print(b,c)

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
m=1000
d,dn=p[:-1],p[1:]
for i,j in zip(d,dn):
  m = m//i*j+m%i if j>i else m
print(m)

Submission status: Accepted

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

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

    print(hand)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def m():
    N = int(input())
    A = list(map(int, input().split()))
    prev = 0
    cur = 1
    maximum = mini = A[prev]
    stock, money = 0, 1000
    while cur < N:
        while A[prev] <= A[cur]:
            maximum = max(maximum, A[cur])
            prev = cur
            cur += 1
            if cur >= N:
                stock += (money // mini)
                money -= (money // mini) * mini
                money += (stock * maximum)
                return  money

        stock += (money // mini)
        money -= (money // mini) * mini
        money += (stock*maximum)
        stock = 0

        mini = A[cur]
        maximum = A[cur]
        prev = cur
        cur += 1

    return money

print(m())

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
a=lmp()
a.insert(0,10000)
a.append(0)
i=0
m=len(a)
while i<m-1:
    if a[i]==a[i+1]:
        del a[i]
        i-=1
    i+=1
    m=len(a)

n=len(a)-2

l=[0]*(n+2)
for i in range(1,n+1):
    if a[i-1]<a[i]>a[i+1]:
        l[i]=1
    elif a[i-1]>a[i]<a[i+1]:
        l[i]=-1
sto=[0,1000]
for i in range(n+2):
    if l[n+1-i]==-1:
        l[n+1-i]=0
        break
    elif l[n+1-i]==1:
        break
for i in range(n+2):
    if l[i]==-1:
        sto[0],sto[1]=divmod(sto[1],a[i])
    if l[i]==1:
        sto[1]+=sto[0]*a[i]
        sto[0]=0
print(sto[1])

Submission status: Accepted

Solution code:
N = int(input())
# = str(input())
# = map(int, input().split())
A = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]
wallet = 1000

for i in range(N-1):
    if A[i] < A[i+1]:
        chicket, money = divmod(wallet, A[i])
        wallet = money
        wallet += chicket*A[i+1]

print(wallet)

Submission status: Accepted

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

for i, a in enumerate(A):
  for j in (range(i+1, len(A))):
    if A[j] > a:
      n = cur // a
      cur -= n * a
      kabu += n
      break
    elif A[j] < a:
      cur += a * kabu
      kabu = 0 
      break
    else:
      continue

cur += a * kabu
print(cur)

Submission status: Accepted

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

money = 1000

# 購入中の株式
kabu_num = 0

for i in range(N - 1):
    a1 = A[i]
    a2 = A[i + 1]
    if a1 > a2:
        # 売る
        if kabu_num != 0:
            money += a1 * kabu_num
            kabu_num = 0
    else:
        # 買う
        if money >= a1:
            kabu_num = math.floor(money / a1)
            money -= a1 * kabu_num

money += kabu_num * A[N - 1]

print(money)

Submission status: Accepted

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

money = 1000
temp = A[0]
up = None

for i in range(N-1):
  # print((i, up))
  if A[i] == A[i+1]:
    continue

  if up is None:
    if temp < A[i+1]:
      up = True
    elif temp > A[i+1]:
      up = False
    continue

  if up:
    if A[i] < A[i+1]:
      continue
    else:
      up = False
      d = money // temp
      money += d * (A[i] - temp)
      temp = A[i+1]
  else:
    if A[i] < A[i+1]:
      up = True
      temp = A[i]
    else:
      temp = A[i+1]

if up:
  d = money // temp
  money += d * (A[-1] - temp)

print(money)

Submission status: Accepted

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

g = [1000, 0]

for i in range(1, N):
    if A[i] > A[i-1]:
        g[1] += g[0] // A[i-1] 
        g[0] %= A[i-1]
        g[0] += g[1] * A[i]
        g[1] = 0
    elif A[i] < A[i-1]:
        g[0] += g[1] * A[i-1]
        g[1] = g[0] // A[i]
        g[0] %= A[i]

print(g[0] + g[1] * A[-1])

Submission status: Accepted

Solution code:
N = int(input())
A = []
S = 1000
pre =300
now = 0
_min = 200
_max = 100
for _i in input().split():
  now = int(_i)
  if now > pre:
    S += (now - pre) * (S // pre)
  pre = now
else:
  print(S)

Submission status: Accepted

Solution code:

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

last_value = A[0]
kabu = 0
money = 1000
for n in range(1, N):
    if A[n] > last_value:
        kabu = int(money / last_value)
        money -= last_value * kabu
        money += A[n] * kabu
        last_value = A[n]
    elif A[n] < last_value:
        last_value = A[n]

print(money)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
money=1000
stock=0
updown=[0 for i in range(n)]
flag=False

for i in range(1,n):
  if a[i]>a[i-1]:
    updown[i]=1
  if a[i]<a[i-1]:
    updown[i]=2
if a[0]<a[1]:
  buy=money//a[0]
  money-=buy*a[0]
  stock+=buy
  flag=True
for i in range(1,n):
  if updown[i]==1 and flag==False:
    buy=money//a[i-1]
    money-=buy*a[i-1]
    stock+=buy
    flag=True
  if updown[i]==2 and flag==True:
    sell=stock
    money+=sell*a[i-1]
    stock-=sell
    flag=False
if stock>0:
  money+=stock*a[n-1]
print(money)

Submission status: Accepted

Solution code:
import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase


"""def gcd(a,b):
    if b==0:
        return a
    else:
        return gcd(b,a%b)"""

"""def pw(a,b):
    result=1
    while(b>0):
        if(b%2==1): result*=a
        a*=a
        b//=2
    return result"""



def inpt():
    return [int(k) for k in input().split()]


def main():
    n=int(input())
    ar=inpt()
    mx=ar[0]
    mn=10000000
    amt=1000
    s=0
    for i in ar:
        if(i<mx):
            s=amt//mn
            amt=amt%mn
            amt+=(s*mx)
            mn=i
            mx=i
        mx=max(mx,i)
        mn=min(mn,i)
    s = amt // mn
    amt = amt % mn
    amt += (s * mx)
    print(amt)


BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np


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

    A = np.int32(A)
    D = np.diff(A)
    if np.all(D <= 0):
        return 1000
    if np.all(D >= 0):
        stock, money = divmod(1000, A[0])
        money = stock * A[-1] + money
        return money

    money = 1000
    s = 0
    while s < N - 1:
        t = s + 1
        if A[t] >= A[s]:
            while t < N and A[t] >= A[t - 1]:
                t += 1
            stock, money = divmod(money, A[s])
            money = stock * A[t - 1] + money
            # print('++:', A[s:t], money)
            s = t
        else:
            while t < N and A[t] < A[t - 1]:
                t += 1
            # print('--:', A[s:t], money)
            s = t - 1
    
    return money


print(solve())

Submission status: Accepted

Solution code:
# coding: utf-8


def solve(*args: str) -> str:
    n = int(args[0])
    A = list(map(int, args[1].split()))
    budget = 1000
    stock = 0
    temp = 0

    for i in range(n-1):
        if A[i] < A[i+1] and A[i] <= temp+budget:
            budget += temp
            temp = 0
            stock = budget//A[i]
            budget %= A[i]
        elif A[i+1] < A[i]:
            budget += A[i]*stock
            stock = 0

    return str(A[-1]*stock+budget)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    #l,r,d=map(int,input().split())
    n=int(input())
    a=list(map(int,input().split()))
    now=1000
    nokoritemp=1000
    kabu=0
    bos=[0 for i in range(n)] #0=buy 1=sell 2=none
    for i in range(n-1):
        if a[i]>a[i+1]:
            bos[i]=1
    bos[n - 1] = 1
#    for i in range(n-1):
 #       if bos[i+1]==1 and bos[i]==1:
 #           bos[i]=2



    for i in range(n):
        if bos[i]==1:
            now=now+kabu*a[i]
            kabu=0
        elif bos[i]==0:
            urukabu=0
            kaukabu=0
            for j in range(kabu+1):
                kaukabu=(a[i]*j+now)//a[i]
                temp=j*a[i]+now-((a[i]*j+now)//a[i])*a[i]
                if now>=temp:
                    now=temp
                    urukabu=j
            kabu=kabu+kaukabu-urukabu
    print(now)



if __name__ == "__main__":

    main()

Submission status: Accepted

Solution code:
days = int(input())
yen = 1000
prices = list(map(int, input().split()))
stocks = 0

for i in range(0, days-1):
  if prices[i] < prices[i+1]:
    stocks += yen // prices[i]
    yen %= prices[i]
  elif prices[i] > prices[i+1]:
    yen += stocks*prices[i]
    stocks = 0


yen += stocks*prices[days-1]

print(yen)

Submission status: Accepted

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

mo=1000
ch=0
tu=0
b=sorted(a)
if a==b:
  ka=mo//a[0]
  mo=mo-ka*a[0]
  mo=mo+ka*a[-1]
  
else:
  
  for i in range(1,n):
    if a[i-1]<a[i]:
      ch+=1
    if a[i-1]>a[i] and ch>0:
      y=min(a[tu:i])
      ka=mo//y
      mo=mo-ka*y
      mo=mo+a[i-1]*ka
      tu=i
      ch=0
    if i==n-1:
      if a[i-1]<a[i]:
        y=a[i-1]
        ka=mo//y
        mo=mo-ka*y
        mo=mo+a[i]*ka
  
    
print(mo)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
x, y, z = 1000, 0, 1000
for a in A:
  s1, t1 = divmod(x, a)
  s2, t2 = divmod(z, a)
  s2 += y
  if s1 > s2:
    y, z = s1, t1
  elif s2 > s1:
    y, z = s2, t2
  else:
    y, z = s1, max(t1, t2)
  x = max(x, y*a+z)
ans = x
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
amt=1000
st=0
i=0
while(i<n-1):
    if arr[i]<arr[i+1]:
        mi=arr[i]
        while(i<n-1 and arr[i]<=arr[i+1]):
            i+=1
        st+=amt//mi
        amt-=mi*st
        amt+=arr[i]*st
        st=0
    else:
        while(i<n-1 and arr[i]>=arr[i+1]):
            i+=1
print(amt)

Submission status: Accepted

Solution code:
#D
N = int(input())
A = list(map(int, input().split()))
A.append(0)

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

print(cash)

Submission status: Accepted

Solution code:
n= int(input())
a = list(map(int,input().split()))
s=0
k=0
top=[]
bottom=[]
t=0
ans=1000
ka=0
while a[s]==a[s+1] and s<n-1:
    s+=1
    if s == n - 1:
        break
if s==n-1:
    print(ans)
else:
    if a[s]>a[s+1]:
        k=-1
        t=1
    else:
        k=1

    while s<n-1:
        while a[s]*k <= a[s+1]*k and s<n-1:
            s+=1
            if s==n-1:
                break
        if k ==-1:
            bottom.append(a[s])
        else:
            top.append(a[s])
        k *= -1
        s+=1
    if k == 1:
        top.append(a[-1])
    else:
        bottom.append(a[-1])
    if t==1:
        for i in range(len(top)):
            ka+=ans//bottom[i]
            ans = ans % bottom[i]
            ans += ka*top[i]
            ka = 0
    else:
        for i in range(len(top)):

            if i== 0:
                ka += ans // a[i]
                ans = ans % a[i]
                ans += ka * top[i]
                ka = 0
            else:
                ka+=ans//bottom[i-1]
                ans = ans % bottom[i-1]
                ans += ka*top[i]
                ka = 0
    print(ans)

Submission status: Accepted

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