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 = input().split()
A = [int(A[i]) for i in range(len(A))]
ans = 1000
kabu = 0
for i in range(N):
    if i == N-1:
        ans += kabu*A[i]
        kabu = 0
    else:
        if A[i] < A[i+1]:
            kabu = ans // A[i]
            ans += -A[i]*kabu
        elif A[i] > A[i+1]:
            ans += kabu*A[i]
            kabu = 0
print(ans)

Submission status: Wrong Answer

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

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

for i in range(N-1):
  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
    else:
      temp = A[i+1]

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

print(money)

Submission status: Wrong Answer

Solution code:
# 全部使え オールオアナッシングや

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

dp = {}


def rec(i, money, stock):
    if (i, money, stock) in dp:
        return dp[(i, money, stock)]
    if i == n:
        return money
    ret = money
    # 有り金全部株に変える
    if money > 0:
        p = money // a[i]
        ret = max(ret, rec(i + 1, money - p * a[i], stock + p))
    # 有り株全部金に変える
    if stock > 0:
        ret = max(ret, rec(i + 1, money + stock * a[i], 0))
    dp[(i, money, stock)] = ret
    return ret


print(rec(0, 1000, 0))

Submission status: Wrong Answer

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

mo=1000
ch=0
tu=0
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[: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: Wrong Answer

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

Submission status: Wrong Answer

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

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

last_stock_val = 0
for i in range(N -1):
    if list_A[-1-i] < list_A[-2-i]:
        continue
    else:
        last_stock_val = list_A[-1-i]
        break
money += stock * last_stock_val
print(money)

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
lis = []
prv = -1
for i in range(n):
    if prv != A[i]:
        lis.append(A[i])
        prv = A[i]
#print(lis)
st = 1000
kabu = 0
for i in range(len(lis)):
    if i != len(lis)-1:
        if lis[i] < lis[i+1]:
            kabu = st//lis[i]
            st -= kabu * lis[i]
    if i != 0:
        if lis[i] > lis[i-1]:
            st += kabu * lis[i]
            kabu = 0
print(st)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
mi_a = min(a)
start = a.index(mi_a)
if start==n-1:
  print(1000)
  exit()
money = 1000
kabu = 0
l = [min(a)]
high = 1
for i in range(start+1,n-1):
  if high==1:
    if a[i]<=a[i+1]:
      continue
    else:
      l.append(a[i])
      high = 0
  else:
    if a[i]>=a[i+1]:
      continue
    else:
      l.append(a[i])
      high = 1
if high==1 and a[-2]<a[-1]:
  l.append(a[-1])
if high==0 and a[-2]>a[-1]:
  l.append(a[-1])
#print(l)
if len(l)%2==0:
  for i in range(len(l)):
    if i%2==0:
      kabu += money//l[i]
      money -= (money//l[i])*l[i]
    else:
      money += kabu*l[i]
      kabu = 0
   # print(money,kabu)
  print(money)
else:
  l.pop()
  for i in range(len(l)):
    if i%2==0:
      kabu += money//l[i]
      money -= (money//l[i])*l[i]
    else:
      money += kabu*l[i]
      kabu = 0
    #print(money,kabu)
  print(money)

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-1):
    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]
if hand[N-1][1] != 0:
    hand[N][0] = hand[N-1][0] + A[N-1]*hand[N-1][1]

print(hand[N][0])

Submission status: Wrong Answer

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_()
sell = set([])
buy = set([])
tmp = A[0]
f = 0
bf = False
for i in range(1, N):
    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 = 0
mx = A[0]
for i in range(1, N):
    if A[i] >= mx:
        mx = A[i]
        L = i
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: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
sign = 1
mountain, valley = [], []
for i in range(N-1):
  if A[i] < A[i+1]:
    valley.append(0)
    break
  elif A[i] > A[i+1]:
    mountain.append(0)
    break
for i in range(N-1):
  if A[i] > A[i+1]:
    if sign == 1:
      mountain.append(i)
      sign = -1
  elif A[i] < A[i+1]:
    if sign == -1:
      valley.append(i)
      sign = 1
if A[-1] > A[-2]:
  mountain.append(N-1)
M = 1000
kb = 0
lm, lv = len(mountain), len(valley)
for i in range(lv):
  flg = True
  cnt = i
  while flg:
    if cnt == lm:
      flg = False
      break
    elif cnt < lm:
      if mountain[cnt] < valley[i]:
        cnt += 1
      else:
        break
  if flg:
    kb += (M // A[valley[i]])
    M -= (M // A[valley[i]]) * A[valley[i]]
    M += kb * A[mountain[cnt]]
    kb = 0
print(M)

Submission status: Wrong Answer

Solution code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(input()))
    return np.array(x)

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

money = 1000
stock = 0

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

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

Submission status: Wrong Answer

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

if A[0] < A[1]:
  K[0] = -1
elif A[0] > A[1]:
  K[0] = 1
for i in range(1, N):
  if A[i-1] < A[i]:
    K[i] = 1 # 1は最高値
    if K[i-1] == 1: 
      K[i-1] == 0
  elif A[i-1] > A[i]:
    K[i] = -1 # -1は最低値
    if K[i-1] == -1:
      K[i-1] == 0
#print(K)
for i in range(N):
  if K[i] == -1:
    kab = s // A[i]
    s -= A[i]*kab
  elif K[i] == 1:
    s += A[i]*kab
    kab = 0
print(s)

Submission status: Wrong Answer

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

for i in range(N):
  if i!=N-1 and stock==0 and A[i]<A[i+1]:
    div,mod=divmod(money,A[i])
    stock=int(div)
    money=int(mod)
  elif stock!=0:
    if i!=N-1 and temp<=A[i+1]:
      temp=A[i+1]
    else:
      if i==N-1:
        temp=max(temp,A[i])
      money+=temp*stock
      stock=0
print(money)

Submission status: Wrong Answer

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]
while True:
    if i==n-1:
        break
    if f:
        for j in range(i+1,n):
            if a[j]>a[i]:
                m=(m//a[i])*a[j]+(m%a[i])
                i=j
                M.append(m)
                f=False
                break
        else:
            break
    else:
        for j in range(i+1,n):
            if a[j]<a[i]:
                i=j
                f=True
                break
        else:
            break
print(max(M))

Submission status: Wrong Answer

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]
    print(money)
  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: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
an=[]
for i in range(1,n):
    if a[i]>a[i-1]:
        an.append(a[i-1])
        break
pm=0
for j in range(i,n):
    #print(pm)
    if pm==0:
        if a[j]>=a[j-1]:
            if j==n-1:
                an.append(a[j])
            continue
        else:
            if j==n-1:
                break
            an.append(a[j-1])
            pm=1
    else:
        if a[j]<=a[j-1]:
            continue
        else:
            an.append(a[j-1])
            pm=0
            if j==n-1:
                an.append(a[j])
#print(an)
ans=1000
k=0
for i in range(len(an)):
    if i%2==0:
        k=ans//an[i]
        ans-=k*an[i]
    else:
        ans+=k*an[i]
        k=0
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
tmp=a[0]
up=0
check=0
stock=0
n=1000
for i in range(N-1):
    if i==0:
        if a[i]<=a[i+1]:
            check=1
            stock=n//a[i]
            n-=stock*a[0]
        else:
            ckeck=2
    else:
        if check==1:
            if a[i]<=a[i+1]:
                pass
            else:
                check=2
                n+=stock*a[i]
                stock=0
        elif check==2:
            if a[i]<=a[i+1]:
                check=1
                stock=n//a[i]
                n-=stock*a[i]
    #print(n,stock,check)
if check==1:
    n+=stock*a[-1]
print(n)

Submission status: Wrong Answer

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

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

#kane ha
b = 1000

#kabu ha
p = 0
q = 0

for e in a:
    q += p//e
    p = p%e

    b = max(b,p + q*e)

    if b // e > q:
        p = b%e
        q = b//e

print(b)

Submission status: Wrong Answer

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

dp = [[0]*(200) for i in range(N)]#所持金jの時のi日目の最大株
kabu0 = 1000#株をひとつも持ってない状態での所持金最大

dp[0][1000%A[0]] = 1000//A[0]

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

print(kabu0)

Submission status: Wrong Answer

Solution code:

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

MAX = 10 ** 5 + 10
dp = [[-1] * MAX for _ in range(N + 1)]
dp[0][0] = 1000
for i in range(N):
    for j in range(MAX):
        dp[i + 1][j] = dp[i][j]

    for j in range(1, MAX):
        dp[i + 1][j] = max(dp[i + 1][j],
                           dp[i + 1][j - 1] - X[i], dp[i][j - 1] - X[i])

    for j in reversed(range(MAX - 1)):
        if dp[i + 1][j + 1] >= 0:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j + 1] + X[i])
        if dp[i][j + 1] >= 0:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j + 1] + X[i])

print(max(dp[-1]))

Submission status: Wrong Answer

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

mo=1000
ch=0
tu=0
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[:i])
    ka=mo//y
    mo=mo-ka*y
    mo=mo+a[i-1]*ka
    tu=i
  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: Wrong Answer

Solution code:
from collections import defaultdict,deque
import math
def gcd(a,b):
    return b if not a%b else gcd(b,a%b)
def lcm(a,b):
    return (a*b)//gcd(a,b)

def main():
    n = int(input())
    a=[int(i) for i in input().split()]
    temoti = 1000
    res = 0
    ind = 0
    kabusuu = 0
    for i in range(n-1):
        if(a[i] < a[i+1]):
            kabusuu += temoti//a[i]
            temoti -= kabusuu*a[i]
            if(i==n-2):
                temoti += a[i+1]*kabusuu
        if(a[i+1] < a[i]):
            temoti += a[i]*kabusuu
            kabusuu = 0


    print(temoti)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]

ans = []

now = a[0]
flag = 'u'

for i in range(n-1):
    if flag == 'u':
        if now < a[i+1]:
            ans.append(a[i])
            if i == n-2:
                #print(i)
                ans.append(a[i+1])
            #print(i)
            flag = 'd'
        now = a[i+1]
        
    else:
        if now > a[i+1]:
            ans.append(now)

            #print(i)
            flag = 'u'
        now = a[i+1]

okane = 1000
#print(ans)
for i in range(len(ans)//2):
    okane += (okane // ans[2*i])*(ans[2*i+1]-ans[2*i])
    #print(okane)
    #okane -= kosu * ans[i]
print(okane)

Submission status: Wrong Answer

Solution code:
import math
a = input()
b = input().split(" ")
money = 1000
senet = 0
chk = 0
for i in range(int(a)):
  for j in range(int(a)):
    if j != int(a)-1:
      if b[j] > b[j+1]:
        pass;
      else:
        chk = 1 
  if chk == 0:
    break;
  elif i == int(a)-1:
    if senet != 0:
      money = money + (senet * int(b[i]))
    else:
      pass;
  elif i == 0:
    senet += math.floor(money / int(b[i]))
    money = money-(math.floor(money / int(b[i]))*int(b[i]))
  elif int(b[i]) < int(b[i+1]):
    senet += math.floor(money / int(b[i]))
    money = money-(math.floor(money / int(b[i]))*int(b[i]))
  elif int(b[i]) > int(b[i+1]):
    money += senet * int(b[i])
    senet = 0 
  elif int(b[i]) == int(b[i+1]):
    pass;
print(money)

Submission status: Wrong Answer

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

now=1000
amari=0
stock=0
i=0
while i<N-1:
    if A[i]<A[i+1]:
        stock=now//A[i]
        amari=now%A[i]
    elif A[i]>A[i-1]:
        now=max(A[i]*stock+amari,now)
    i+=1

now=max(amari+stock*A[i],now)
print(now)

Submission status: Wrong Answer

Solution code:

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

MAX = 10 ** 5
dp = [[-1] * MAX for _ in range(N + 1)]
dp[0][0] = 1000
for i in range(N):
    for j in range(MAX):
        dp[i + 1][j] = dp[i][j]

    for j in range(MAX - 1):
        dp[i + 1][j] = max(dp[i + 1][j],
                           dp[i + 1][j - 1] - X[i], dp[i][j - 1] - X[i])

    for j in reversed(range(MAX - 1)):
        if dp[i + 1][j + 1] >= 0:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j + 1] + X[i])
        if dp[i][j + 1] >= 0:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j + 1] + X[i])

print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
maxl=[]
minl=[]
for i in range(n):
    if i==0:
        if l[0]>l[1]:
            maxl.append(0)
        elif l[0]<l[1]:
            minl.append(0)
    elif i==n-1:
        if l[n-2]<l[n-1]:
            maxl.append(n-1)
    else:
        if l[i-1]<l[i] and l[i]>=l[i+1]:
            maxl.append(i)
        elif l[i-1]>l[i] and l[i]<=l[i+1]:
            minl.append(i)
ma=len(maxl)
mi=len(minl)
if ma>mi:
    money=1000
    for i in range(2*mi):
        if i%2==0:
            k=minl[i//2]
            kabu=money//l[k]
            money-=l[k]*kabu
        else:
            k=maxl[i//2+1]
            money+=l[k]*kabu
    print(money)
else:
    money=1000
    for i in range(2*ma):
        if i%2==0:
            k=minl[i//2]
            kabu=money//l[k]
            money-=l[k]*kabu
        else:
            k=maxl[i//2]
            money+=l[k]*kabu
    print(money)

Submission status: Wrong Answer

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline


def array(size, init=0):
    return [[init for j in range(size[1])] for i in range(size[0])]


def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())


directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


def permutate(x, A):
    n = len(x)
    res = [-1]*n
    for i in range(n):

        res[i] = x[A[i]]
    return res


def generate_bin(digit):
    for b in range(2**digit):
        b = bin(b)
        tmp = b[2:].zfill(digit)
        yield list(map(int, tmp))
        # b=list(map(int,list(b[2:].zfill(digit))))
        # yield b


def search(x, count):
    #print("top", x, count)

    for d in directions:
        nx = d+x
        # print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)

    return count


N = II()
A = acinput()

A.insert(0, A[0]+1)
A.append(A[-1]-1)

tmp = []
for i in range(len(A)):
    if A[i] != A[i-1]:
        tmp.append(A[i])

A = tmp

x = 1000
k = 0

for i in range(len(A)-1):
    print(i, x, k)
    if A[i-1] > A[i] and A[i] < A[i+1]:
        k += x//A[i]
        x = x % A[i]
    elif A[i-1] < A[i] and A[i] > A[i+1]:
        #print(i, x, k)
        x += A[i]*k
        k = 0


print(x)

# print(A)

Submission status: Wrong Answer

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]
    if a[i+1] < a[i]:#sell
        money += hold*a[i]
        hold = 0
print(money+a[-1]*hold)

Submission status: Wrong Answer

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

wallet = 1000
stock = 0

for i in range(N - 1):
    if A[i] < A[i + 1]:  # 次値上がり→買う
        stock += wallet // A[i]
        wallet -= stock * A[i]
    elif A[i] > A[i + 1] and stock > 0:  # 次値下がり and 株持っている→売り
        wallet += stock * A[i]
        stock = 0

# 最後残っていたら全部売る
wallet += stock * A[-1]

print(wallet)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

cnt = 0

M = 1000

min_ind = 0

def kau(M, a, cnt):
    result_cnt = M // a
    result_kingaku = M - (result_cnt * a)
    return result_kingaku, result_cnt
    
def uru(M, a, cnt):
    result_cnt = 0
    result_kingaku = M + a * cnt
    return result_kingaku, result_cnt

for i in range(N - 1):
    if A[i + 1] > A[i]:
        # 買う
        M, cnt = kau(M, A[i], cnt)
    elif A[i + 1] == A[i]:
        # 何もしない
        pass
    else:
        # 売る
        M, cnt = uru(M, A[i], cnt)

if cnt:
    M, cnt = uru(M, A[-1], cnt)
print(M)

Submission status: Wrong Answer

Solution code:

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

MAX = 10 ** 5
dp = [[-1] * MAX for _ in range(N + 1)]
dp[0][0] = 1000
for i in range(N):
    for j in range(MAX):
        dp[i + 1][0] = dp[i][0]

    for j in range(MAX - 1):
        dp[i + 1][j] = max(dp[i + 1][j],
                           dp[i + 1][j - 1] - X[i], dp[i][j - 1] - X[i])

    for j in reversed(range(MAX - 1)):
        if dp[i + 1][j + 1] >= 0:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j + 1] + X[i])
        if dp[i][j + 1] >= 0:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j + 1] + X[i])

print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
mi_a = min(a)
start = a.index(mi_a)
if start==n-1:
  print(1000)
  exit()
money = 1000
kabu = 0
l = [min(a)]
high = 1
for i in range(start+1,n-1):
  if high==1:
    if a[i]<=a[i+1]:
      continue
    else:
      l.append(a[i])
      high = 0
  else:
    if a[i]>=a[i+1]:
      continue
    else:
      l.append(a[i])
      high = 1
if high==1 and a[-2]<a[-1]:
  l.append(a[-1])
if high==0 and a[-2]>a[-1]:
  l.append(a[-1])
for i in range(len(l)):
  if i%2==0:
    kabu += money//l[i]
    money -= (money//l[i])*l[i]
  else:
    money += kabu*l[i]
    kabu = 0
  #print(money,kabu)
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):
    for j in range(i+1, N):
        if stock == 0:
            if A[i] < A[j]:
                stock += money // A[i]
                money -= stock * A[i]
                break
        else:
            if A[j-1] > A[j]:
                money += stock*A[j-1]
                stock = 0
                break

if stock > 0:
    money += stock * A[-1]
    stock = 0

print(money)

Submission status: Wrong Answer

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

delta_a = [0] * (n-1)
for i in range(n-1):
  delta_a[i] = a[i+1] - a[i]

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1000
s = A[0]
S = A[0]
st = 0
for i in range(1,N):
    if st == 0 and A[i-1] >= A[i]:
        s = A[i]
    elif st == 0 and A[i-1] < A[i]:
        st = 1
    elif st == 1 and A[i-1] <= A[i]:
        S = A[i]
    else:
        t = ans//s
        ans = ans % s
        ans += t*S
        st = 0
    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=[int(i) for i in input().split()]
y=1000
c=0
m=0
c2=0
for i in range(n):
    c1=0
    for j in range(n):
        if(l[j]>l[i]):
            c1=1
            break
    if(c1==0 and m==0):
        break
    elif(c1==0 and m==1):
        y+=(l[i]*c)
        break
    else:
        if(m==0):
            if(c2!=0):
                if(l[i]<k1):
                    m=1
                    c=y//l[i]
                    y%=l[i]
                    k=l[i]
            else:
                c2=1
                m=1
                c=y//l[i]
                y%=l[i]
                k=l[i]
        else:
            if(l[i]>k):
                y+=(l[i]*c)
                k1=l[i]
                m=0
print(y)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
lis = [0]*n
for i in range(n-1):
    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 a[0] < a[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:
n = int(input())
a = list(map(int, input().split()))

real = 1000
unreal = 0

for i in range(n-1):
    print(i)

    if a[i] < a[i+1]:
        unreal, real = divmod(real, a[i])

    elif a[i] > a[i+1]:
        real += (unreal * a[i])
        unreal = 0

    else:
        continue

real += unreal * a[n-1]
print(real)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(n):

    if A[i] < m and A[i] != max(A[i::]):
        # 現在の株価が今後も含めて最高値でなければ全部株にする
        k = m//A[i]
        m = m - A[i]*k

    if not i == 0:
        if A[i] == max(A[0:i+1]): # 現在の株価がこれまでの最高値なら全部現金にする
            m = m + A[i]*k
            k = 0

print (m)

Submission status: Wrong Answer

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

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

p += n*A[-1]
print(p)

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:
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]:
    yen %= prices[i]
    stocks = yen // prices[i]
  elif prices[i] > prices[i+1]:
    yen += stocks*prices[i]
    stocks = 0
  print(yen)

yen += stocks*prices[days-1]

print(yen)

Submission status: Wrong Answer

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

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N=ii()
    A=list(mi())

    if A[0] == max(A):
        print(1000)
        exit()

    pre = INF
    kabu = 0
    p = 1000
    for a in A:
        if pre > a:
            x = p//a
            p -= x*a
            kabu += x
        elif pre < a:
            p += a*kabu
            kabu = 0
        pre = a
    
    p += a*kabu
    print(p)





if __name__ == "__main__":
    main()