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: Wrong Answer

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=i+1
    i+=1
print(m)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
wallet = 1000
stock = 0
price = 0
for i in range(n - 1):
    if a[i] < a[i + 1]:
        price = a[i]
        stock += (wallet // price)
        wallet -= (price * stock)
    elif a[i] > a[i + 1]:
        price = a[i]
        wallet += (price * stock)
        stock = 0
if stock != 0:
    wallet += (a[- 1] * stock)
print(wallet)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
arr=list(map(int,input().split()))
have=0
ans=0
rate=0
for i in range(n-1):
    #print(have)
    if(have!=0):
        if(arr[i+1]<arr[i]):
            ans+=have*(arr[i]-rate)
            have=0
    else:
        if(arr[i+1]>arr[i]):
            have=1000//arr[i]
            rate=arr[i]
#print(have)
if(have!=0):
    ans+=have*(arr[-1]-rate)
    have=0
print(ans+1000)

Submission status: Wrong Answer

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)
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: Wrong Answer

Solution code:
from collections import deque, Counter, defaultdict
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect
from functools import lru_cache
import itertools

sys.setrecursionlimit(10 ** 8)

M = 10 ** 9 + 7
INF = 10 ** 17


def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    # N, K = [int(a) for a in input().split()]
    # A = [int(a) for a in input().split()]
    # T1, T2, T3 = [int(a) for a in input().split()]

    aa = []
    t = A[0]
    start = A[0]
    for ai in A[1:]:
        if t <= ai:
            t = ai
            if start is None:
                start = ai
            continue
        aa.append(
            (start, t)
        )
        t = ai
        start = None
    if start is not None:
        aa.append((start, A[-1]))

    m = 1000
    for a, b in aa:
        if a is None:
            continue
        c = m // a
        m += (b - a) * c
    print(m)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

s = 1000
c = 0
for i in range(n-1):
  if a[i+1] > a[i]:
    c = s//a[i]
    s -= c*a[i]
  elif a[i+1] == a[i]:
    continue
  else:
    s += c*a[i]
  # print (s)
if c == 0:
  print (max(s,1000))
else:
  s += c*a[-1]
  print(s)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]

money = 1000
stock = 0
if A[0] < A[1]:
    stock += (money//A[0])
    money %= A[0]
    
tmp = A[0]
for i in range(1,N):
    if tmp < A[i]:
        money += (stock*A[i])
        stock = 0
        tmp = A[i]
    elif tmp > A[i]:
        stock += (money//A[i])
        money %= A[i]
        tmp = A[i]
        
print(max(money,1000))

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

s = [[0]*2 for i in range(n)]
m = [[0]*2 for i in range(n)]

# 売り
s[0][0] = 0
m[0][0] = 1000
# 買い
s[0][1] = 1000//a[0]
m[0][1] = 1000 - s[0][1] * a[0]

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

print(m[n-1][0])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
lis = [0]*n
te = 0
for i in range(n):
    if i >= 1 and a[i] >= a[0]:
        te = 1
    if i == n-1:
        break
    if a[i] > a[i+1]:
        lis[i] = -1
b = a[::-1]
for i in range(n-1):
    if b[i] < b[i+1]:
        if lis[n-i-1] == -1:
            pass
        else:
            lis[n-i-1] = 1
lis[-1] = -1
if te == 1:
    lis[0] = 1
m = 1000
k = 0
for i in range(n):
    if lis[i] == 1:
        if m >= a[i]:
            k += m//a[i]
            m = m%a[i]
    elif lis[i] == -1:
        m += k*a[i]
        k = 0
print(m)

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
import copy
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
#s = input()
a = list(map(int, sys.stdin.readline().split()))

c = 1000
i = 0
while (i < n):
    ma = a[i]
    mi = a[i]
    mapo = i
    mipo = i
    j = 0
    while (i+j < n):
        if a[i+j] < mi:
            mi = a[i+j]
            mipo = i+j

        if a[i+j] > ma:
            ma = a[i+j]
            mapo = i+j
            break

        j += 1

    if mapo == i:
        print(c)
        sys.exit()

    x = c // mi
    y = x * (ma - mi)
    c += y

    i += (j + 1)

print(c)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

N = i()
A = l()

M = 1000
S = 0
P = 0

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

Submission status: Wrong Answer

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

    sokosagashi = True
    a0 = a[0]
    ah = a0
    for d,ai in enumerate(a[1:]):
        if ai==a0:
            continue

        if ai>a0:
            if sokosagashi:
                print(d-1, 'sell', k, ah)
                m += k * ah
                k = 0
                sokosagashi = False
            # すべて株に変える
            k = m // a0
            m -= k * a0
            print(d-1, 'buy', k, a0)
            a0 = ai
            ah = ai
        elif ai<a0:
            sokosagashi = True
            a0 = ai
    
    if k>0:
        m += k * ai
    print(m)

main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
money = 1000
stock = 0
flag = 1
for i in range(n - 1):
    if i != 0:
        if a[i - 1] == a[i]:
            continue
    if flag:
        if a[i] < max(a[i + 1:]):
            stock += money // a[i]
            money -= stock * a[i]
            flag ^= 1
    else:
        if a[i] > min(a[i + 1:]):
            money += stock * a[i]
            stock = 0
            flag ^= 1

if stock != 0:
    money += stock * a[-1]
print(money)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
dp1 = [[0, 0] for i in range(n)]
dp2 = [[0, 0] for i in range(n)]
dp1[0]=[1000,0]
first = 1000//a[0]
dp2[0]=[1000-a[0]*first,first]

for i in range(1, n):
  temp = dp2[i-1][0]+dp2[i-1][1]*a[i]
  if temp>dp1[i-1][0]:
    dp1[i][0]=temp
  else:
    dp1[i][0]=dp1[i-1][0]
  temp = dp1[i-1][0]//a[i]
  if temp>dp2[i-1][1]:
    dp2[i][0]=dp1[i-1][0]-temp*a[i]
    dp2[i][1]=temp
  else: 
    dp2[i][0]=dp2[i-1][0]
    dp2[i][1]=dp2[i-1][1]
#print(dp1)
#print(dp2)
print(dp1[n-1][0])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1000
s = A[0]
S = A[0]
for i in range(1,N):
    if s > A[i]:
        s = A[i]
    elif S <= A[i]:
        S = A[i]
    else:
        t = ans//s
        ans = ans % s
        ans += t*S
        if i == N-1:
            break
        s,S = A[i+1],A[i+1]
    if i == N-1 and s < A[i]:
        t = ans//s
        ans = ans % s
        ans += t*A[i]
print(ans)

Submission status: Wrong Answer

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

ans = 1000
m = 1000
s = 0

for i in range(N):
    f = 0
    for j in range(i+1, N):
        if A[j] <= A[i] and f == 0:
            f = 1
        elif A[j] >= A[i] and f == 1:
            f = 2

    if f <= 1:
        s += m//A[i]
        m %= A[i]
    elif f == 2:
        m += s*A[i]
        s = 0
    ans = max(ans, m+s*A[i])

print(ans)

Submission status: Wrong Answer

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

    for i in range(n):
        if i > 0 and A[i] > A[i-1]:
            # sell
            funds += A[i] * stock
            stock = 0
        elif i < n-1 and A[i] < A[i+1]:
            # buy
            stock += funds // A[i]
            funds -= A[i] * stock

    print(funds)

resolve()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
A.append(0)
f = [0] * N
for i in range(N):
    if A[i] < A[i+1]:
        f[i] = 1
    elif A[i] > A[i+1]:
        f[i] = 2
cnt = 1000
stock = 0
prev = 0
for i in range(N):
    if prev != 1 and f[i] == 1:
        stock = cnt // A[i]
        cnt %= A[i]
    elif prev != 2 and f[i] == 2:
        cnt += stock * A[i]
        stock = 0
    prev = f[i]
print(cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[10]:


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


# In[11]:


mylist = []
s = 0
m = 1000
ans = m
for i in range(1,N):
    if A[i] > A[i-1]:
        mylist.append(1)
    elif A[i] < A[i-1]:
        mylist.append(-1)
    else:
        mylist.append(0)
for j,x in enumerate(mylist):
    if x > 0:
        s = m//A[j]
        m = m - s*A[j]
    elif x < 0:
        m = s*A[j]
        s = 0
    ans = max(ans, m+s*A[j])
ans = max(ans, m+s*A[-1])
print(ans)


# In[ ]:

Submission status: Wrong Answer

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+1] > a[i]:
    stock = wallet // a[i]
    wallet -= stock * a[i]
  elif a[i+1] < a[i]:
    wallet += stock * a[i]
    stock = 0

wallet += stock * a[-1]

print(wallet)

Submission status: Wrong Answer

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
        kabu, m = divmod(m, pa)
    elif pa > aa:
        # sel all
        m += kabu * pa
        kabu = 0

if kabu > 0:
    m += kabu*aa

print(m)

Submission status: Wrong Answer

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

money = 1000
stock = 0
for day in range(n-1):
    if a[day+1] > a[day]:
        stock += money // a[day]
        money -= stock * a[day]
    elif a[day+1] < a[day]:
        money += stock * a[day]
        stock = 0
money += stock * a[-1]
print(money)

Submission status: Wrong Answer

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

state = None
for i in range(1,N):
    if state == None:
        if price[i] < price[i-1]:
            money += price[i-1]*stack
            stack = 0
        elif price[i] > price[i-1]:
            stack = money//price[i-1]
            money = money%price[i-1]
    elif state == True and price[i] < price[i-1]:
        money += price[i-1]*stack
        stack = 0
    elif state == False and price[i] > price[i-1]:
        stack = money//price[i-1]
        money = money%price[i-1]

if stack != 0:
    money += price[-1]*stack
    stack = 0

print(money)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

money =1000
stock = 0

for i in range(n-1):
    if a[i] <a[i+1]:
        stock = money//a[i]
        money =money%a[i]
    elif a[i] >a[i+1]:
        money +=stock*a[i]
        stock = 0
print(money+stock*a[n-1])

Submission status: Wrong Answer

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]:
        stock = money//A[i-1]
        money -= A[i-1]*stock
    if temp>A[i]:
        if stock:
            money += A[i-1]*stock
            stock = 0
    temp = A[i]
    if i==N-1:
        if stock:
            money += A[i]*stock
print(money)

Submission status: Wrong Answer

Solution code:


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: Wrong Answer

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:
    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: Wrong Answer

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

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

money = 1000
kabu = 0
for i in range(N):
  if kabu > 0:
    if T[i-1] == 1:
      money += A[i] * kabu
      kabu = 0
  elif 1 in T[i:] and (i == 0 or T[i-1] == -1):
    buy = money // A[i]
    kabu += buy
    money -= buy * A[i]
print(money)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
row_price = float("inf")
high_price = -1
price = 1000
for i in range(n):
  if i == 0:
    row_price = a_list[i]
  if a_list[i] < row_price:
    row_price = a_list[i]
  elif a_list[i] > row_price:
      stock = price // row_price
      amari = price - row_price * stock
      price = a_list[i] * stock + amari
      row_price = float("inf")
      
print(price)

Submission status: Wrong Answer

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

if stock > 0:
  money += stock * li[n-1]
  
print(money)

Submission status: Wrong Answer

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

money = 1000
stock = 0

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

Submission status: Wrong Answer

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:
        k+=m//a[i]
        m-=k*a[i]

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

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

s = [[0]*2 for i in range(n)]
m = [[0]*2 for i in range(n)]

# 売り
s[0][0] = 0
m[0][0] = 1000
# 買い
s[0][1] = 1000//a[0]
m[0][1] = 1000 - s[0][1] * a[0]

for i in range(1,n):
    s[i][0] = 0
    m[i][0] = max(m[i-1][0], m[i-1][1]+s[i-1][1]*a[i])
    s[i][1] = max(s[i-1][0]+m[i-1][0]//a[i], s[i-1][1])
    if s[i-1][0] + m[i-1][0]//a[i] > s[i-1][1]:
        m[i][1] = m[i-1][0] - m[i-1][0]//a[i] * a[i]
    else:
        m[i][1] = m[i-1][1]

print(m[n-1][0])

Submission status: Wrong Answer

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

if A[0] <= A[1]:
    kabu += money // A[0]
    money = money % A[0]

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

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

Submission status: Wrong Answer

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

for i in range(N-1):
    if A[i] < A[i+1]:
        s, money =  divmod(money, A[i])
        stock += s
    elif A[i] == A[i+1]:
        money += stock * A[i]
        stock = 0
    else:
        continue
if stock > 0:
    money += stock * A[N-1]
print(money)

Submission status: Wrong Answer

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 large==0:
        small = A[i]
        large = A[i]
    else:
        if A[i] < large:
            if small < large:
                lst.append((small,large))
                small, large = 0, 0
            else:
                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: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
acd = A[0] <= A[1]
lst = [A[0]]
i = 1
while i < N:
    if acd:
        if A[i-1] > A[i]:
            lst.append(A[i-1])
            acd = 0
    else:
        if A[i-1] < A[i]:
            lst.append(A[i-1])
            acd = 1
    i += 1
if A[N-2] < A[N-1]:
    lst.append(A[N-1])
if len(lst) == 1:
    print(1000)
    exit()
if lst[0] > lst[1]:
    lst = lst[1:]
if len(lst) < 2:
    X = 1000
else:
    X = 1000
    for i in range(0, len(lst), 2):
        c = X//lst[i]
        X -= c*lst[i]
        X += c*lst[i+1]
print(X)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N = int(input())
A = list(map(int,input().split()))
sell = [0]*N
buy = [0]*N
hand = [[1000,0] for i in range(N+1)]
for i in range(1,N-1):
    if A[i] > A[i-1] and A[i] >= A[i+1]:
        sell[i] = 1
    if A[i] < A[i-1] and A[i] <= A[i+1]:
        buy[i] = 1
if A[0] < A[1]:
    buy[0] = 1
if A[N-1] > A[N-2]:
    sell[N-1] = 1
for i in range(N):
    if buy[i] == 1:
        hand[i+1][0] = hand[i][0] % A[i]
        hand[i+1][1] += hand[i][1] + hand[i][0] // A[i]
    elif sell[i] == 1:
        hand[i+1][0] = hand[i][0] + A[i]*hand[i][1]
        hand[i+1][1] = 0
    else:
        hand[i+1][0] = hand[i][0]
        hand[i+1][1] = hand[i][1]
print(hand[N][0])

Submission status: Wrong Answer

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

l = []
tmp = []
for i in range(n-1):
    if a[i] < a[i+1] and len(tmp) == 0:
        tmp.append(a[i])
    elif a[i] > a[i+1] and len(tmp) == 1:
        tmp.append(a[i])
        l.append(tmp)
        tmp = []
if a[-2] < a[-1]:
    tmp.append(a[-1])
    l.append(tmp)

res = 1000
for a, b in l:
    res = (res // a) * b + res % a

print(res)

Submission status: Wrong Answer

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

ans = 1000
tmp = A[0]
flg = False
for i in range(1, N):
    if flg:
        tmp = A[i]
        flg = False
        
    if tmp < A[i]: #真に大きい時
        cnt = ans//tmp
        ans += (A[i]-tmp) * cnt
        flg = True #株を売買した
    else:
        tmp = A[i]
print(ans)

Submission status: Wrong Answer

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

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

start = 0
money = 1000

for i in range(N - 1):
    if A[i + 1] > A[i]:
        tmp = 10 ** 3
        for k in range(i, start - 1, -1):
            tmp = min(A[k], tmp)
        print(A[i + 1], tmp)
        buy = money // tmp
        money -= tmp * buy
        money += buy * A[i + 1]
        start = i + 1

print(money)

Submission status: Wrong Answer

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

# dp = [[0, 0] for i in range(N+1)]
# #dp[i][0] i日目の所持金 i[1] i日目の株個数
# dp[0][0] = 1000
# dp[0][1] = 0
money = 1000
stock = 0
for i in range(N):
    if i==N-1:
        money += stock * A[i]
        break
    if A[i] < A[i+1]:
        stock = money//A[i]
        money -= A[i] * stock
    elif A[i] > A[i+1]:
        money += stock * A[i]
        stock = 0
print(money)

Submission status: Wrong Answer

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

money = 1000
kabu = 0

for i in range(N):
    a = A[i]
    nokori = sorted(A[i:])
    if kabu == 0:
        if a < nokori[-1]:
            buy = money // a
            money -= buy * a
            kabu += buy
    else:
        if a != nokori[0] or i == N-1:
            money += kabu * a
            kabu = 0
    # print(money, kabu)

print(money)

Submission status: Wrong Answer

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

    money = 1000
    kab = 0

    maxa = max(a)
    mina = min(a)
    last = 0
    first = 0
    for i in range(n):
        if a[i] == maxa:
            last = i
    for i in range(n):
        if a[i] == mina:
            first = i
            break

    if a[0] == maxa:
        print(1000)
    else:
        if last > first:
            for i in range(first, last):
                if a[i] < a[i+1]:
                    kab += (money//a[i])
                    money %= a[i]
                elif a[i] > a[i+1]:
                    money += kab * a[i]
                    kab = 0
            print(kab*a[-1]+money)
        else:
            for i in range(last):
                if a[i] < a[i + 1]:
                    kab += (money // a[i])
                    money %= a[i]
                elif a[i] > a[i + 1]:
                    money += kab * a[i]
                    kab = 0

            print(kab * a[-1] + money)
resolve()

Submission status: Wrong Answer

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

start = [[0, 1000], [0, 1000]]
#0: all stack, 1: all money
max_index = [i for i, v in enumerate(A) if v == max(A)][-1]
#print(max_index)

ans = 0
for i in range(N):
  if i == max_index:
    ans = start[0][0] * A[i] + start[0][1]
  else:
    money = max([
      start[0][0] * A[i] + start[0][1],  start[1][0] * A[i] + start[1][1]
    ])
    start = [[money//A[i], money - (money//A[i])*A[i] ],  [0, money]]
print(max([start[0][0] * A[i] + start[0][1],  start[1][0] * A[i] + start[1][1]]))

Submission status: Wrong Answer

Solution code:
n,*aa = map(int, open(0).read().split())

ans = 1000

mn=mx=aa[0]

for i in range(1,n):
    if mn<=aa[i] and mx<=aa[i]:
        mx = aa[i]
    elif aa[i]<=mn and aa[i]<=mx:
        mn=mx=aa[i]
    else:
        ans += (mx-mn)*(ans//mn)
        mn=mx=aa[i]
ans += (mx-mn)*(ans//mn)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1000
s = A[0]
S = A[0]
for i in range(1,N):
    if s > A[i]:
        s = A[i]
    elif S < A[i]:
        S = A[i]
    else:
        t = ans//s
        ans = ans % s
        ans += t*S
        if i == N-1:
            break
        s,S = A[i+1],A[i+1]
    if i == N-1 and s < A[i]:
        t = ans//s
        ans = ans % s
        ans += t*A[i]
print(ans)

Submission status: Wrong Answer

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


a = 1000
k = 0

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

print(a)

Submission status: Wrong Answer

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 large==0:
        small = A[i]
        large = A[i]
    else:
        if A[i] < large:
            if small < large:
                lst.append((small,large))
                small, large = 0, 0
        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: Wrong Answer

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:
    if a[i]<tmp[-1][1]:
      tmp.append(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: Wrong Answer

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

money = 1000
stock = 0

for i in range(N):
    if i == N-1:
        money += stock * a_list[i]
        break
    if a_list[i] < a_list[i+1]:
        stock = money // a_list[i]
        money = money % a_list[i]
    elif a_list[i] > a_list[i+1]:
        money += stock * a_list[i]
        stock = 0
    else:
        continue
print(money)