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 = list(map(int, input().split()))


# 最終日から考えることが早い
# 最終日が上げ相場の場合、最終日で全力で売っている
# 最終日が下げ相場の場合、何もしていない

# 最終日の価格がその前も続いていいる場合は、どこに実質的な最終日があるかチェックする
final_d = N-1
for i in range(N-1, -1, -1):
    if A[i] == A[i-1]:
        final_d = i - 1
    else:
        break

money = 1000
kabu = 0

for i in range(final_d):
    print(money, kabu)
    if A[i] < A[i+1]:
        kabu = money // A[i]
        money = money % A[i]
        # 最終日の場合、上げ相場であれば決済をする
        # 実質的な最終日の場合は、決済が必要
        if i == final_d - 1:
            print(money, kabu)
            money += kabu * A[final_d]
    elif A[i] > A[i+1]:
        money += kabu * A[i]
        kabu = 0



print(money)

Submission status: Wrong Answer

Solution code:

n = int(input())
money = 1000
stock = 0
(*a,) = map(int, input().split())

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
    else:
        pass
money += stock * a[-1]
print(money)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

kane = 1000
kabu = 0

for i in range(n):
  if i == (n-1):
    kane = kane + kabu * A[i]
    kabu = 0
  elif A[i] < A[i+1]:
    if i+1 == (n-1):
      kabu = kane // A[i]
      kane = kane - (kabu * A[i])
    elif (kane // A[i]) == 0:
      pass
    else:
      if A[i+1] < A[i+2]:
        pass
      else:
        kabu = kane // A[i]
        kane = kane - (kabu * A[i])
  elif A[i] >= A[i+1]:
    if i+1 == (n-1):
      kane = kane + kabu * A[i]
      kabu = 0
    else:
      if A[i] < A[i+2]:
        pass
      else:
        kane = kane + kabu * A[i]
        kabu = 0
  else: 
    pass
  
print(kane)

Submission status: Wrong Answer

Solution code:
N= int(input())
A = list(map(int,input().split()))
A.insert(0,A[0])
A.append(0)

money = 1000
#min_price = A[0]
#max_price =A[0]
previous_tendency = "none"
current_tendency = "none"
count = 0

for n in range(1,len(A)-1):
    if A[n] < A[n+1]:
        current_tendency = "up"
    elif A[n] > A[n+1]:
        current_tendency = "down"
    else:
        current_tendency = "none"
        
    if previous_tendency != "down" and current_tendency != "up":
        #print(n,"chance",max(A[count:n+1]),min(A[count:n+1]),previous_tendency, current_tendency)
        money += (money//min(A[count:n+1]))*(max(A[count:n+1])-min(A[count:n+1]))
        #print(money)
        count = n
    #print(A[n],max_price,min_price,previous_tendency,current_tendency)
    if current_tendency != "none":
        previous_tendency = current_tendency
print(money)

Submission status: Wrong Answer

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

ud = [0] * (N-1)
for i in range(1,N):
    if A[i] > A[i-1]:
        ud[i-1] = 1
    if A[i] < A[i-1] :
        ud[i-1] = -1
    else:
        continue
        
tmp =0
bs = [''] * N
if ud[0] == 1:
    bs[0] = 'b'
    tmp = 1
if ud[0] == -1:
    tmp = -1

for i in range(1,len(ud)):
    if tmp == 0 and ud[i] != 0:
        tmp = ud[i]
    elif ud[i] == 0:
        continue
    elif tmp == -1 and ud[i] == 1:
        bs[i] = 'b'
        tmp = ud[i]
    elif tmp == 1 and ud[i] == -1:
        bs[i] = 's'
        tmp = ud[i]
bs[-1] = 's'

kabu = 0
zan = 1000
for i in range(N):
    if bs[i] == 'b':
        kabu = zan // A[i]
        zan -= kabu * A[i]
    if bs[i] == 's':
        zan += kabu * A[i]
        kabu = 0
print(zan)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
buy = list(A[i] < A[i+1] for i in range(len(A)-1))
buy.append(False)
sell = list(A[i-1] < A[i] for i in range(1, len(A)))
sell.insert(0, False)

money = 1000
stock = 0

for i in range(N):
    if buy[i]:
        stock += money // A[i]
        money = money % A[i]
    if sell[i]:
        money += stock * A[i]
        stock = 0

print(money)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [300] + list(map(int, input().split())) + [0]
m = 1000
c = 0

if max(A[1:]) == A[1]:
    print(m)
else:
    B = [['-', '+'][i < j] for i, j in zip(A, A[1:])]
    B = ''.join(B)
    o = []
    for i in range(len(B) - 1):
        b = B[i:i + 2]
        if b == '-+':
            o.append((i, 'B'))
        elif b == '+-':
            o.append((i, 'S'))

    A = A[1: -1]
    for x, y in o:
        #print(x, y)
        if y == 'B':
            tmp = m // A[x]
            c += tmp
            m -= tmp * A[x]
        else:
            m += c * A[x]
            c = 0

    print(m)

Submission status: Wrong Answer

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

stack = 0
ans = 1000

i,j  = 0, 0

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    n = II()
    aa = MII()
    balance = 1000
    ss = []
    idxs = []
    minv = aa[0]
    minv_idx = 0
    for i in range(1, n):
        if minv > aa[i]:
            minv = aa[i]
            minv_idx = i
        elif minv < aa[i]:
            ss.append(minv)
            idxs.append(minv_idx)
            minv = 200
    try:
        if idxs[-1] != n-1:
            idxs.append(n)
    except:
        print(1000)
        exit()
    for i in range(len(idxs)-1):
        buy = ss[i]
        sell = max(aa[idxs[i]:idxs[i+1]])
        num = balance // buy
        balance -= num * buy
        balance += sell * num
    print(balance)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# cook your dish here
n = int(input())
l = list(map(int, input().split()))
small = l[0]
ans = 1000
for i in range(1, n):
    if l[i]<small:
        small = l[i]
    elif l[i]>small:
        ans = ans + (ans//small)*(l[i]-small)
        if i<n-1:
            small = l[i+1]
            i=i+1
print(ans)

Submission status: Wrong Answer

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

for i in range(N-1):
    if (A[i] < A[i+1]):
        kabu += total//A[i]
        total -= kabu*A[i]
    elif(A[i]==A[i+1]):
        pass
    elif(A[i] > A[i+1]) and (A[i] < max(A[i+1:])):
        total += (kabu*A[i])
        kabu = 0


#最後
i += 1
total += (kabu * A[i])
kabu = 0
print(total)

Submission status: Wrong Answer

Solution code:
def main():
    import copy
    n = int(input())
    a = list(map(int,input().split()))
    ans = 1000
    for i in range(n-1):
        m = 1000
        ls = [0 for i in range(n)]
        ls[i] = 1
        l = i
        for j in range(i+1,n):
            if a[j]>a[l]:
                ls[j] = 1
                if ls[j-1]==1:
                    ls[j-1]=0
                l = j
            elif a[l]>a[j]:
                ls[j] = -1
                if ls[j-1]==-1:
                    ls[j-1] = 0
                l = j
            else:
                ls[j]=0
                l = j
        cnt = 1000//a[i]
        m = 1000-cnt*a[i]
        mk = 0
        for k in range(len(ls)):
            if ls[k]==1:
                m = m+cnt*a[k]
                cnt = 0
            elif ls[k]==-1:
                mk = copy.deepcopy(m)
                cnt = m//a[k]
                m = m-cnt * a[k]
        if cnt!=0:
            m = mk
        if m>ans:
            ans = m
    print(ans)
                

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

money = 1000
kabu = 0
for i in range(a):
    if i == a - 1:
        if b[i] > b[i-1]:
            money += b[i] * kabu
            kabu = 0            
    elif b[i] < b[i + 1]:
        kabu += int(money / b[i])
        money %= b[i]
    elif b[i] > b[i + 1]:
        money += b[i] * kabu
        kabu = 0
        
print(str(money))

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

mi = min(A)
ma = max(A)

for i in range(N):
    if mi == A[i]:
        mi_left = i

for i in range(N):
    if ma == A[N-i-1]:
        ma_right = N - i

B = A[mi_left:ma_right]
# print(B)
if len(B) == 0:
    print(1000)
    exit()
C = []
for i in range(len(B)-1):
    if B[i] == B[i+1]:
        continue
    C.append(B[i])
if B[-1] != B[-2]:
    C.append(B[-1])  

have = 1000
kabu_num = 0
for i, c in enumerate(C):
    if i % 2 == 0:
        if 0 <= i - 2 < len(C):
            if C[i-2] > C[i]:
                continue
        kabu_num += have // c
        have = have - kabu_num * c
    else:
        if 0 <= i - 2 < len(C):
            if C[i-2] > C[i]:
                continue
        have += kabu_num * c
        # kabu_num -= 
        kabu_num = 0

print(have)

Submission status: Wrong Answer

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #n,m,k=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    maxyen=0
    for sss in range(n):
        i=sss
        yen=1000
        kab=0
        while i < n:
            now=arr[i]
            nxt=999
            minxxx=99999
            for j in range(i+1,n):
                if j==(i+1) and arr[j]>now:
                    nxt=j
                    break
                minxxx=min(minxxx,arr[j])
                if arr[j]>now:
                    nxt=j
                    break
                else:
                    #kabu = yen // now
                    #yen = yen % now
                    #yen += kabu * minxxx
                    #kabu=0
                    now=minxxx
                    minxxx=99999
            if nxt < 999 and minxxx==99999:
                kabu = yen // now
                yen = yen % now
                yen += kabu * arr[nxt]
                kabu=0
            if nxt < 999 and minxxx<99999:
                kabu = yen // minxxx
                yen = yen % minxxx
                yen += kabu * arr[nxt]
                kabu=0

            i=nxt
        maxyen=max(maxyen,yen)
    print(maxyen)
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

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


def dfs(day, money,stock):
    if day == N:
        asn = max(ans, money+ stock * A[N-1])
        return
    price = A[day-1]
    dfs(day+1, money % price, stock + money//price)
    #dfs(day+1, money, stock)
    dfs(day+1, money + stock * price, 0)
    return

dfs(1,1000,0)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter 
n = int(input())
A = list(map(int, input().split()))
a = Counter(A)

cnt = 0
money = 1000
b = 0
c = 0
for i in range(n):
    a[A[i]] -= 1
    if a[A[i]] == 0: a.pop(A[i])
    if A[i-1] == A[i]: continue

    if b != 0 and b < A[i]:
        money += cnt * A[i]
        cnt = 0
        b = 0
    else:
        flag = False
        for j in a.keys():
            if A[i] < j:
                flag = True
                break
        if flag:
            cnt += money//A[i]
            money %= A[i]
            b = A[i]
            c = A[i]
print(money)

Submission status: Wrong Answer

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

L=[]
ave = sum(A) / N
for i in range(N):
    if A[i]<ave:
        L.append(0)
    else:
        L.append(1)


out=0
money=1000
kabu=0
for i in range(N-1):
    if L[i]==0:
        kabu+=money // A[i]
        money=money % A[i]

    else:
        money+=kabu*A[i]
        kabu=0
    # print('money,kabu:',money,kabu)
money+=kabu*A[N-1]  #last
out=max(out,money)

if out<1000:
    print(1000)
else:
    print(out)

Submission status: Wrong Answer

Solution code:
import sys
import math
sys.setrecursionlimit(int(1e6))
#dprint = print
def dprint(*args):
  pass

money = 1000
stump = 0
n = list(map(int, input().split()))[0]
A = list(map(int, input().split()))
dprint('n = ', n)
dprint('A = ', A)

if n == 1:
  print(money)
  sys.exit()

btm = A[0]
top = A[0]
for i in range(1, n):
  dprint('i = ', i)
  a_i = A[i]
  if (money>0):
    # buy
    if a_i <= btm:
      btm = a_i
    else:
      p = money // btm
      stump = stump + p
      money = money - (p*btm)
      top = a_i
      btm = a_i
      dprint('[buy] stump, money', stump, money)
  else:
    # sell
    if a_i >= top:
      top = a_i
    else:
      money = money + (top*stump)
      stump = 0
      top = a_i
      btm = a_i
      dprint('[sell] stump, money', stump, money)

if (stump>0):
  money = money + (top*stump)
  dprint('[sell] stump, money', stump, money)

print(money)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D
# CreatedDate:  2020-07-25 21:25:14 +0900
# LastModified: 2020-07-25 22:28:50 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    n = int(input())
    a = list(map(int, input().split()))
    have = 1000
    stock = 0
    pivot_index = 0
    obj_index = -1
    while pivot_index < n:
        tmp = -1
        for i in range(pivot_index+1, n):
            if a[i] - a[pivot_index] > 0:
                tmp = i
                break
        if tmp == -1:
            print(have)
            return
        obj_index = tmp
        tmp = obj_index
        for i in range(tmp+1, n):
            if a[i] - a[tmp] <= 0:
                break
            else:
                obj_index = i
        min_flactu = min(a[pivot_index:obj_index+1])
#        print(min_flactu, obj_index)
        stack = 0
        have = have % min_flactu + have // min_flactu * a[obj_index]
        pivot_index = obj_index
#        break
#    print(have)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

is_start_increase = [0]*N
is_end_increase = [0]*N
is_increasing = [0]*N

# for i in range(1, N):
#     if A[i-1] <= A[i]:
#         is_increasing[i] = 1
# next_is_end = 0
# is_total_start = 0
# for i in range(0, N-1):
#     if next_is_end:
#         if is_increasing[i] == 1 and is_increasing[i+1] == 0:
#             is_end_increase[i] = 1
#             next_is_end = 0

#     if not is_total_start:
#         if is_increasing[i+1] == 1:
#             is_start_increase[i] = 1
#             is_total_start = 1
#             next_is_end = 1
#     # if i == 0 and is_increasing[i+1] == 1:
#     #     is_start_increase[i] = 1
#     #     continue
#     # if i == 1 and is_start_increase[i-1]==1:
#     #     continue
#     elif next_is_end == 0:
#         if is_increasing[i] == 1 and is_increasing[i-1] == 0:
#             is_start_increase[i] = 1
#             next_is_end = 1

for i in range(N-1):
    if A[i] <= A[i+1]:
        is_increasing[i] = 1
if is_increasing[0] == 1:
    is_start_increase[0] = 1
for i in range(1, N-1):
    if is_increasing[i] == 1 and is_increasing[i-1] == 0:
        is_start_increase[i] = 1
    if is_increasing[i] == 0 and is_increasing[i-1] == 1:
        is_end_increase[i] = 1

money = 1000
stock = 0
stocks = []
for i in range(N):
    if i==N-1:
        print("last", i)
        money += stock * A[i]
        break
    if is_start_increase[i]:
        print("start", i)
        stock += money // A[i]
        money -= A[i] * stock
    if is_end_increase[i]:
        print("end", i)
        money += stock * A[i]
        stock = 0
    stocks.append(stock)

print(money, stock)
print(stocks)

Submission status: Wrong Answer

Solution code:
def judgeFinalSellDay(x):
  max = 0
  ret=0
  for i in range(n-1,-1,-1):
    if x[i] > max:
      max =x[i]
      ret=i
    elif x[i] < max:
      break
  return ret

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

final=judgeFinalSellDay(a)
money=1000
kabu=0
for i in range(n):
  if i == final:
    money+=kabu*a[i]
    kabu=0
    break
  else:
    if a[i+1] > a[i]:
      kabu=int(money/a[i])
      money=money%a[i]
    elif a[i+1] < a[i]:
      money+=kabu*a[i]
      kabu=0
print(money)

Submission status: Wrong Answer

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

movements = ['bull' if A[i+1]>A[i] else 'bear' for i in range(N-1)]
movements.append('bear')
#print(movements)

money = 1000
stock = 0
for i in range(N):
    if movements[i] == 'bull':
        stock = money // A[i]
        money = money % A[i]
#        print(i, money, stock)
    elif stock > 0:
        money += A[i] * stock
        stock = 0
#        print(i, money, stock)
    else:
        pass
#        print(i, 'no move')

print(money)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')

n = I()
a = LI()
ans = 1000
kabu = 0
values = list(Counter(a).keys())
# print(values)
m = len(values)
for i in range(m):
    if i+1 < m:
        # print(values[i+1])
        if values[i] < values[i+1]:
            # 買う
            ans, kabu = ans % values[i], ans // values[i]
            # print(f'{(ans, kabu)=}')
        elif values[i] > values[i+1]:
            # 売る
            ans, kabu = ans + values[i] * kabu, 0
    else:
        if values[i-1] < values[i]:
            # 売る
            ans, kabu = ans + values[i] * kabu, 0
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[0]*N
A=list(map(int,input().split()))
money=1000
money_temp=1000
kazu=0
for i in range(N-1):
    if(A[i]<A[i+1]):
        money=money+kazu*A[i-1]
        kazu=money//A[i]
        money_temp=max(money_temp,money-A[i]*kazu)
        money=money-A[i]*kazu
        #print('ue')
        #print(A[i])
        #print(A[i+1])
    elif(A[i]==A[i+1]):
        pass
    elif(A[i+1]<A[i]):
        stop=1
        ma=A[i]

money=money+kazu*A[N-1]

print(max(money,money_temp))

Submission status: Wrong Answer

Solution code:
n=int(input())
ls=list(map(int,input().split()))
c=sorted(ls)
mini=c[0]
maxi=c[-1]
lb=0
k=1000
st=0
pmin=c.index(mini)
pmax=c.index(maxi)
for i in range(n):
  if i==pmin:
    st+=k//ls[i]
    k=k-ls[i]*(k//ls[i])
    lb=ls[i]
  elif i==pmax:
    k+=ls[i]*st
    st=0
  else:
    if i<n-1:
      if ls[i]>ls[i+1]:
        k+=ls[i]*st
        st=0
      elif ls[i]<ls[i+1]:
        st+=k//ls[i]
        k=k-ls[i]*(k//ls[i])
        lb=ls[i]
    else:
      #最終日に売り抜くパターン！
      if lb<ls[i]:
        k+=ls[i]*st
        st=0
k+=lb*st
print(k)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

#print(N)
#print(A)

syozi = 1000
kabu = 0

for i in range(N-1):
  if A[i] < A[i+1]:
    kabu = int( syozi / A[i] )
    syozi = int( syozi % A[i] )
  elif A[i] > A[i+1]:
    syozi += A[i] * kabu
    kabu = 0
  else:
    pass

if kabu > 0:
  syozi += A[N-1] * kabu
  kabu = 0

print(syozi)

Submission status: Wrong Answer

Solution code:
N=int(input())
souba=list(map(int,input().split()))
urikaiba=list()
money=1000
num=0
for i in range(1,N-1):
    if (souba[i]-souba[i-1])*(souba[i+1]-souba[i])<=0 and ((souba[i]-souba[i-1])!=0 or (souba[i+1]-souba[i])!=0) and (souba[i]-souba[i-1])!=0:
        urikaiba.append(souba[i])
urikaiba.insert(0,souba[0])
urikaiba.append(souba[N-1])
print(*urikaiba)
for i in range(len(urikaiba)-1):
    if urikaiba[i+1]-urikaiba[i]>0:
        num=money//urikaiba[i]
        money=money-num*urikaiba[i]
    elif urikaiba[i+1]-urikaiba[i]<0:
        money=money+num*souba[i]
        num=0
if num==0:
    print(money)
else:
    print(money+num*souba[N-1])

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int,input().split()))
    ans = 1000
    for i in range(n-1):
        m = 1000
        ls = [0 for i in range(n)]
        ls[i] = 1
        l = i
        for j in range(i+1,n):
            if a[j]>a[l]:
                ls[j] = 1
                if ls[j-1]==1:
                    ls[j-1]=0
                l = j
            elif a[l]>a[j]:
                ls[j] = -1
                if ls[j-1]==-1:
                    ls[j-1] = 0
                l = j
            else:
                ls[j]=0
                l = j
        cnt = 1000//a[i]
        m = 1000-cnt*a[i]
        pt,pcnt = 0,0
        for k in range(len(ls)):
            if ls[k]==1:
                m = m+cnt*a[k]
                cnt = 0
            elif ls[k]==-1:
                cnt = m//a[k]
                m = m-cnt * a[k]
                pt = k
                pcnt = m//a[k]
        m = m+pcnt*a[pt]
        if m>ans:
            ans = m
    print(ans)
                

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
stuck=[0]*N
flag=True
money=1000
kabu=0
for i in range(N):
    if A[i]<A[i+1]:
        base=A[i]
        kabu=money//A[i]
        money-=A[i]*kabu
        flag=True
        break
    else:
        print(1000)
        exit()
for i in range(1,N):
    value=A[i]
    if base<value and flag:
        stuck[i]=1
        flag=False
        base=value
    elif base>value and not flag:
        stuck[i]=-1
        flag=True
        base=value
flag=False
for i in range(N):
    if stuck[i]==-1:
        kabu=money//A[i]
        money-=A[i]*kabu
        flag=True
    elif stuck[i]==1:
        money+=A[i]*kabu
        flag=False
if flag:
    money+=A[-1]*kabu
print(money)

Submission status: Wrong Answer

Solution code:
_ = int(input())
kakaku = list(map(int, input().split()))

money = 1000
num_item = 0

up_flag = False
down_flag = False

if kakaku[0] < kakaku[1]:
    up_flag = True
else:
    down_flag = True

dp = []

if up_flag:
    dp.append(kakaku[0])

# 減=>増=>減....の形にする
for i in range(1, len(kakaku)):
    # いま増加中or下後中
    if kakaku[i - 1] > kakaku[i]:
        if down_flag:
            pass
        else:
            down_flag=True
            up_flag=False
            dp.append(kakaku[i-1])
        down_flag = True
    elif kakaku[i-1] < kakaku[i]:
        if up_flag:
            pass
        else:
            down_flag = False
            up_flag = True
            dp.append(kakaku[i-1])
        up_flag = True
    # print('up', up_flag, 'down', down_flag, 'now item',kakaku[i], 'dp', dp)

if kakaku[-2] < kakaku[-1]:
    dp.append(kakaku[-1])

for i in range(len(dp)):
    if i % 2 == 0:
        num_item += money // dp[i]
        money -= dp[i] * num_item
    else:
        money += dp[i] * num_item
        num_item = 0

print(money)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
check_list = [0 for i in range(n)]
high_price = 0
row_price = a_list[0]
price = 1000
for i in range(1,n):
  if a_list[i] < a_list[i-1]:
    check_list[i] = "down"
  elif a_list[i] == a_list[i-1]:
    check_list[i] = "stay"
  else:
    check_list[i] = "up"  
hoge = "down" 
hoge_idx = 0
new_check_list = [0]
for i in range(1,n):
  if check_list[i] == hoge:
    new_check_list[-1] = "stay"
    new_check_list.append(hoge) 
  else:
    hoge = check_list[i]
    hoge_idx = i
    new_check_list.append(hoge)
down_price = a_list[0]
for i in range(1,n):
  if new_check_list[i] == "down":
    down_price = a_list[i]
  if new_check_list[i] == "up":
    stock = price // down_price
    amari = price - stock * down_price
    price = stock * a_list[i] + amari

print(price)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import os
from sys import stdin, stdout

n = 0
seq = []
dp = []

def getMaximum(idx, stock, money):
    if idx == n-1:
        return money + stock*seq[idx]

    if stock < 10000 and dp[idx][stock] >= money:
        return dp[idx][stock]

    ret = getMaximum(idx+1, stock, money)

    # buying
    k = 1
    cost = seq[idx]
    while cost <= money:
        ret = max(ret, getMaximum(idx+1, stock+k, money-cost))
        k += 1
        cost += seq[idx]

    # selling
    cost = seq[idx]
    for k in range(1,stock+1):
        ret = max(ret, getMaximum(idx+1, stock-k, money+cost))
        cost += seq[idx]

    if stock < 10000:
        dp[idx][stock] = max(ret, dp[idx][stock])

    return ret


def solve(tc):
    global n
    global seq
    global dp

    n = int(stdin.readline().strip())
    seq = list(map(int, stdin.readline().split()))
    dp = [[-1 for j in range(10000)] for i in range(n)]

    print(getMaximum(0, 0, 1000))


tcs = 1
tc = 1
while tc <= tcs:
    solve(tc)
    tc += 1

Submission status: Wrong Answer

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

yosan = 1000
kabu = 1000//a[0]
syozi = 1000%a[0]
ans = 1000
tmp = 0
for i in range(1,n) :
    if a[i-1] < a[i] and kabu != 0:
        syozi += (kabu*a[i])
        kabu = 0
    elif a[i-1] > a[i] and syozi > a[i]:
        tmp = syozi//a[i]
        kabu += tmp
        syozi -= tmp*a[i]
    ans = max(syozi, ans)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


for i in range(n):
    # 今後上がらなければ何もしない
    if i != n-1 and max(A[i:n]) <= A[i]:
        # print('以降上がらないので修了')
        break
    # 最初は買う
    if i == 0:
        num = math.floor(money / A[i])
        # print(str(i)+'日目'+str(num)+'株買う')
        kabu += num
        money -= num * A[i]
        then_kabuka = A[i]
        continue
    # 買った値段より高ければ売る
    if A[i] > then_kabuka and kabu > 0:
        # print(str(i)+'日目'+str(kabu)+'株売る')
        money += A[i] * kabu
        kabu = 0
        #then_kabuka = 0
        continue
    # 今後値段が上がるなら買う
    if i+1 < n and A[i] < A[i+1]:
        num = math.floor(money / A[i])
        # print(str(i)+'日目'+str(num)+'株買う')
        kabu += num
        money -= num * A[i]
        then_kabuka = A[i]
        continue
    # 同じ値段なら何もしない
    if A[i] == then_kabuka:
        # print('何もしない')
        continue
print(money)

Submission status: Wrong Answer

Solution code:
def load_to_millionar():
    days = int(input())
    prices = tuple(map(int, input().split()))

    if max(prices) == prices[0]:
        print(1000)
        return

    k = 1000 // prices[0]
    total = 1000 - prices[0] * k
    maxp = prices[0]

    for d in range(1, days):

        # 売る
        if prices[d] > maxp or d == days - 1:
            total += k * prices[d]
            k = 0
            maxp = prices[d]

        # 買う
        elif max(prices[d:]) > maxp > prices[d]:
            buy = total // prices[d]
            total -= buy * prices[d]
            k += buy

    print(total)


if __name__ == '__main__':
    load_to_millionar()