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()))
money = 1000
cab = 0
lowprice = A[0]
value = []
check = True
for i in range(1,N):
    if(lowprice>A[i]):
        lowprice = A[i]
        check= True
    elif(lowprice<A[i] and check == True):
        high_price = A[i]
        if(i<N-1):
            if(high_price >= A[i+1]):
                cab=money//lowprice
                money-=(lowprice*cab)
                money+=(high_price*cab)
                cab = 0
                check = False
                lowprice = high_price
        else:
            cab=money//lowprice
            money-=(lowprice*cab)
            money+=(high_price*cab)
            cab = 0
            check = False
            lowprice = high_price


print(money)

Submission status: Wrong Answer

Solution code:
n=int(input())
#n,k=map(int,input().split())
v=list(map(int,input().split()))
l=list()
for i in v:
  if i not in l:
    l.append(i)
n=len(l)
if n==1:
  print(1000)
else: 
  d=list()
  if l[0]>l[1]:
      d.append(l[0])
  if l[0]<l[1]:
      d.append(l[0])
  for i in range(1,n-1):
    if l[i-1]<l[i] and l[i+1]<l[i]:
      d.append(l[i])
    if l[i-1]>l[i] and l[i+1]>l[i]:
      d.append(l[i])
  if l[-1]>l[-2]:
      d.append(l[-1])
  if l[-1]<l[-2]:
      d.append(l[-1])
  #print(d)
  n=len(d)
  p=1000
  if n>0:
    for i in range(1,n,2):
      if l[i-1]<l[i]:
        z=(p//l[i-1])*l[i]
        r=p%l[i-1]
        p=z+r
    print(p)
  else:
    print(p)

Submission status: Wrong Answer

Solution code:


n = int(input())
a = list(map(int, input().split()))
nmax = a[0]
stock = 1000 // a[0]
money = 1000 - stock * a[0]
laststock = 0
lastmoney = 1000

for d in range(1, n):
    if nmax < a[d]:
        money += a[d] * stock
        stock = 0
        nmax = a[d]
    elif nmax > a[d]:
        if money > 0:
            laststock = stock
            lastmoney = money
            stock += money // a[d]
            money -= stock * a[d]
            nmax = a[d]
        else:
            stock = lastmoney // a[d]
            money = laststock * a[d]
            laststock = stock
            lastmoney = money
            nmax = a[d]
    else:
        pass

if money < 1000:
    money = 1000

print(money)

Submission status: Wrong Answer

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

mon = 1000
stock = 0

lim_a = [a_list[0]]
for i in range(1,N-1):
    if lim_a[-1] == a_list[i]:
        continue
    if a_list[i] <= a_list[i+1] and a_list[i] <= a_list[i-1]:
        lim_a.append(a_list[i])
    elif a_list[i] >= a_list[i+1] and a_list[i] >= a_list[i-1]:
        lim_a.append(a_list[i])

if len(lim_a) >= 2:
    if lim_a[-2] <= lim_a[-1] <= a_list[-1]:
        lim_a[-1] = a_list[-1]
    elif lim_a[-2] >= lim_a[-1] >= a_list[-1]:
        lim_a[-1] = a_list[-1]
    else:
        lim_a.append(a_list[-1])
else:
    
    lim_a.append(a_list[-1])


s = 0 if lim_a[0] < lim_a[1] else 1
e = 0 if lim_a[-2] < lim_a[-1] else 1
for i in range(s, len(lim_a)-e):
    if (i + s)%2 == 0:
        # buy
        stock += mon//lim_a[i]
        mon -= lim_a[i] * stock
    else:
        # sell
        mon += lim_a[i] * stock
        stock = 0

print(max(mon, 1000))

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

ans = 1000
buy = A[0]
sell = 0
cnt = 0
flg = False
for i in range(1, N):
    if flg:
        buy = A[i]
        flg = False

    if buy < A[i] and sell < A[i]: #真に大きい時
        if cnt: #株を既に買っていたら
            sell = A[i]
        else:
            cnt = ans//buy #買う株は決まった
            sell = A[i] #売る時の株の値段
    else:
        if sell:
            ans += (sell - buy) * cnt
            flg = True #株を売った
            sell = 0
            cnt = 0
        else: #買う株をできるだけ安いのにしたい
            buy = min(buy, A[i])

if sell: #最後に売る株が決まったらまだ売ってない
    ans += (sell - buy) * cnt
print(ans)

Submission status: Wrong Answer

Solution code:
n = 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
    else:
        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

if num_item != 0:
    money += dp[-1] * num_item

print(money)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
money=1000
kabu=0
flg=0
first=True
if a[1]-a[0]>=0:
    flg="+"
else:
    flg="-"
x=a[1]

if len(a)==2:
    if a[1]>a[0]:
        kabu+=money//a[0]
        money-=a[0]*(money//a[0])
        money+=kabu*a[1]
    print(money)
    sys.exit()

for i , y in enumerate(a[2:],2):
    if flg=="+"  and y-x>=0:
        x=y
    elif flg=="-" and y-x<=0:
        x=y
    elif flg=="+" and y-x<0:
        if first:
            kabu+=money//a[0]
            money-=a[0]*(money//a[0])
            first=False
        flg="-"
        money+=kabu*a[i-1]
        kabu=0
    elif flg=="-" and y-x>0:
        if first:
            first=False
        flg="+"
        kabu+=money//a[i-1]
        money-=a[i-1]*(money//a[i-1])
    print(flg,money,kabu)
if kabu!=0:
    if flg=="+":
        money+=kabu*a[-1]
    elif flg=="-":
        X=a[-1]
        for Y in a.reverse()[1:]:
            if X<=Y:
                X=Y
            else:
                money+=kabu*Y
                break
print(money)

Submission status: Wrong Answer

Solution code:
def INT():
    return int(input())

def MI():
    return map(int, input().split())

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

N = INT()
A = LI()
ans = 1000
purchase = 0

for i in range(1, N):
    tmp = ans
    
    if A[i] > A[i - 1]:
        for j in range(1000):
            if tmp - A[i - 1] < 0:
                break
            tmp -= A[i - 1]
            purchase += 1
        ans = max(ans, tmp + A[i] * purchase)
        purchase = 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]):
        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
    else:
        money_temp=max(money_temp,money+kazu*A[i])
        money=money+kazu*A[i]
        kazu=0
        #print('sita')
        #print(A[i])
        #print(A[i+1])

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

print(max(money,money_temp))

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    kai = A[0]
    uri = A[0]
    flag=0
    temoti = 1000
    for i in range(1,N-1):
        if kai < A[i] and uri<A[i]:
            uri=A[i]
            flag=1
        elif kai > A[i]:
            kai = A[i]
            flag=0
            uri = A[i]
        if flag==1:
            if uri>A[i]:
                temoti = temoti + temoti // kai *(uri-kai)
                kai = A[i]
                uri = A[i]
                flag=0
    
    if uri< A[N-1]:
        uri = A[N-1]
    if kai<uri:
        temoti = temoti + temoti // kai * (uri-kai)

    print(temoti)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

def main():
    n = int(input())
    a = list(map(int, input().split()))
    m = 1000

    lst = []
    flg = 'd'
    for i in range(n-1):
        if flg == 'd' and a[i] < a[i+1]:
            lst.append(a[i])
            flg = 'u'
        elif flg == 'u' and a[i] > a[i+1]:
            lst.append(a[i])
            flg = 'd'

    if flg == 'u':
        lst.append(a[n-1])

    for j in range(0, len(lst), 2):
        p, q = lst[j], lst[j+1]
        m += (q - p) * (m // p)
        print(p, q)

    print(m)

if __name__ == '__main__':
    main()

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)

btm = A[0]
top = A[0]
for i in range(1, len(A)):
  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:
N=int(input())
A=list(map(int, input().split()))

kabu=0
ans=1000

for i in range(N-1):
    if A[i] > A[i+1]:
        ans=ans+kabu*A[i]
        kabu=0

    if A[i] < A[i+1]:
        if ans >= A[i]:
            kabu=ans // A[i]
            ans=ans % A[i]

ans=ans+kabu*A[N-1]

Submission status: Wrong Answer

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

M = 1000
X = 0

for i in range(N-1):
    if A[i] < A[i+1]:
        X = int(M/A[i])
        M = int(M - X * A[i])
    
    elif A[i] > A[i+1]:
        M = int(M + A[i] * X)
        X = 0
    
M = M + X * A[N-1]

print(int(M))

Submission status: Wrong Answer

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

stack = 0
ans = 1000

buyFlag = False
sellFlag = False

i,j  = 0, 0

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

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

print(ans)

Submission status: Wrong Answer

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

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

money = 1000
stock = 0

if 1 in cng:
    stock = money // A[1]
    money -= stock * A[1]

for i in range(2, N):
    if cng[i+1] == 1:
        stock = money//A[i]
        money -= stock * A[i]
    elif cng[i+1] == -1:
        money += stock * A[i]
        stock = 0

if stock != 0:
    money += stock * A[N]

print(money)

Submission status: Wrong Answer

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

mon = 1000
stock = 0

lim_a = [a_list[0]]
for i in range(1,N-1):
    if lim_a[-1] == a_list[i]:
        continue
    if a_list[i] <= a_list[i+1] and a_list[i] <= a_list[i-1]:
        lim_a.append(a_list[i])
    elif a_list[i] >= a_list[i+1] and a_list[i] >= a_list[i-1]:
        lim_a.append(a_list[i])

if len(lim_a) >= 3:
    if lim_a[-2] <= lim_a[-1] <= a_list[-1]:
        lim_a[-1] = a_list[-1]
    elif lim_a[-2] >= lim_a[-1] >= a_list[-1]:
        lim_a[-1] = a_list[-1]
    else:
        lim_a.append(a_list[-1])
else:
    
    lim_a.append(a_list[-1])

s = 0 if lim_a[0] < lim_a[1] else 1
e = 0 if lim_a[-2] < lim_a[-1] else 1
for i in range(s, len(lim_a)-e):
    if (i + s)%2 == 0:
        # buy
        stock += mon//lim_a[i]
        mon -= lim_a[i] * stock
    else:
        # sell
        mon += lim_a[i] * stock
        stock = 0

print(max(mon, 1000))

Submission status: Wrong Answer

Solution code:
def INT():
    return int(input())

def MI():
    return map(int, input().split())

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

N = INT()
A = LI()
ans = 1000
purchase = 0

for i in range(1, N):
    tmp = ans
    
    if A[i] > A[i - 1]:
        for j in range(100):
            if tmp - A[i - 1] < 0:
                break
            tmp -= A[i - 1]
            purchase += 1
        ans = max(ans, tmp + A[i] * purchase)
        purchase = 0

print(ans)

Submission status: Wrong Answer

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


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

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

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


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

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

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

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

    money = 1000
    num = 0
    for res in result:
        type = res.get('type')
        value = res.get('value')
        if type == 0:
            num += money // value
            money -= num * value
        else:
            money += num * value
            num = 0

    print(money)


calculate(N, ARR)

Submission status: Wrong Answer

Solution code:
n=int(input())
#n,k=map(int,input().split())
v=list(map(int,input().split()))
l=list()
for i in v:
  if i not in l:
    l.append(i)
n=len(l)
if n==1:
  print(1000)
else: 
  d=list()
  if l[0]>l[1]:
      d.append(l[0])
  if l[0]<l[1]:
      d.append(l[0])
  for i in range(1,n-1):
    if l[i-1]<l[i] and l[i+1]<l[i]:
      d.append(l[i])
    if l[i-1]>l[i] and l[i+1]>l[i]:
      d.append(l[i])
  if l[-1]>l[-2]:
      d.append(l[-1])
  if l[-1]<l[-2]:
      d.append(l[-1])
  #print(d)
  n=len(d)
  p=1000
  if n>0:
    for i in range(1,n):
      if l[i-1]<l[i]:
        z=(p//l[i-1])*l[i]
        r=p%l[i-1]
        p=z+r
    print(p)
  else:
    print(p)

Submission status: Wrong Answer

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

b = []

#1：購入、-1：売却、0：何もしない

for i in range(1, n):
  if a[i - 1] > a[i]:
    b.append(-1)
  elif a[i - 1] < a[i]:
    if -1 in b:
      b.append(1)
    else:
      b.append(0)
  else:
    b.append(0)
    
b.reverse()

for i in b:
  if i == 1:
    b.insert(0, 0)
    break
  elif i == -1:
    b.insert(0, 1)
    break

if len(b) != n:
  b.insert(0, 0)
    
a.reverse()
money = 1000
kabu = 0
    
for i in range(n):
  if b[i] == 1:
    kabu, money = divmod(money, a[i])
  if b[i] == -1:
    money = money + kabu * a[i]
    kabu = 0

print(money)

Submission status: Wrong Answer

Solution code:
import sys

def f_buy(m, s, a):
  tmp = m//a
  return m-tmp*a, s+tmp

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

n = int(input())
la = list(map(int, input().split()))
lb = [0]*(n-1)
for i in range(n-1):
  if la[i+1]-la[i] > 0:
    lb[i] = 1
  if la[i+1]-la[i] < 0:
    lb[i] = -1

if lb.count(1) == 0:
  print(1000)
  sys.exit()

m = 1000
s = 0
frg = 0
for i in range(n-1):
  if lb[i] == 1:
    if frg != 1:
      m, s = f_buy(m, s, la[i])
    frg = 1
  if lb[i] == -1:
    if frg != -1:
      m, s = f_sell(m, s, la[i])
    frg = -1

if lb[n-2] == 1:
  m, s = f_sell(m, s, la[n-1])
if lb[n-2] == -1:
  m, s = f_buy(m, s, la[n-1])
print(m)

Submission status: Wrong Answer

Solution code:
k = int(input())
a = list(map(int,input().split()))
a.append(1)
a.append(0)

money = 1000
stock = 0

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

Submission status: Wrong Answer

Solution code:

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

    money = 1000
    trading = []
    buy = True

    for i in range(1, len(A)):
        if buy:
            if A[i] <= A[i-1]:
                continue
            else:
                trading.append(A[i-1])
                if i == len(A)-1:
                    trading.append(A[i])
                buy = False
        else:
            if A[i] >= A[i-1]:
                continue
            else:
                trading.append(A[i-1])
                buy = True
    
    if len(trading) % 2 == 1:
        trading.pop(-1)
    
    stock = 0
    
    for i in range(len(trading)):
        if i % 2 == 0:
            stock = money // trading[i]
            money -= stock * trading[i]
        else:
            money += stock * trading[i]
            stock = 0

    print(money)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
money = [[1000, 0]]
ans = 1000
B = [1 for _ in range(N)] #-1 -> buy 1 ->sell 0 -> nothing
for k in range(N-1):
  if A[k] < A[k+1]:
    B[k] = -1
  elif A[k] == A[k+1]:
    B[k] = 0

for k in range(N):
  if B[k] == -1:
    new = [money[0]]
    for j in range(len(money)):
      if A[k] <= money[j][0]:
        new.append([money[j][0]%A[k], money[j][1] + money[j][0]//A[k]])
    money = new
  
  if B[k] == 1:
    new = []
    ans = 0
    for j in range(len(money)):
      ans = max(money[j][0] + money[j][1]*A[k], ans)
    money[0] = [ans, 0]

print(ans)

Submission status: Wrong Answer

Solution code:
import  heapq
n=int(input())
aa=list(map(int,input().split()))
count=1
have=1000
l=[aa[0]]
ll=[-aa[0]]
heapq.heapify(l)
heapq.heapify(ll)
while count<n:
  mx=-heapq.heappop(ll)
  mn=heapq.heappop(l)
  if mx<=aa[count]:
    heapq.heappush(ll,-aa[count])
    heapq.heappush(l,aa[count])
    heapq.heappush(ll,-mn)
    heapq.heappush(l,mn)
    heapq.heappush(ll,-mx)
    heapq.heappush(l,mx)
    #print(l,ll)
  else:
    have+=(mx-mn)*int(have/mn)
    #print(mx,mn,l,ll)
    if count<=n-2:
      count+=1
      l=[aa[count]]
      ll=[-aa[count]]
      heapq.heapify(l)
      heapq.heapify(ll)
  count+=1
if len(l)!=0:
  mx=-heapq.heappop(ll)
  mn=heapq.heappop(l)
  have+=(mx-mn)*int(have/mn)
  
  
print(have)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [*map(int, input().split())]

Y = 1000
buy, sell = [], []
for i in range(N):
  if not buy:
    buy.append(A[i])
  elif A[i] <= buy[-1]:
    buy[-1] = A[i]
  elif not sell:
    sell.append(A[i])
  elif A[i] >= sell[-1]:
    sell[-1] = A[i]
  else:
    div, mod = divmod(Y, buy.pop())
    Y = sell.pop() * div + mod
    buy.append(A[i])
if buy and sell and buy[-1] < sell[-1]:
    div, mod = divmod(Y, buy.pop())
    Y = sell.pop() * div + mod
print(Y)

Submission status: Wrong Answer

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

#シングル化
a2=[a[0]]
for i in range(1,len(a)):
    if a[i-1]!=a[i]:
        a2.append(a[i])

c=1000
s=0
mn=a2[0]
mx=a2[0]
for i in range(1,len(a2)):
    #次の日の方が小さい場合キープ
    if mn>a2[i]:
        #キープ
        mn=a2[i]
    else:
        #買う
        x=0
        while 1:
            x+=1
            if c<x*mn:
                s=x-1
                c-=s*mn
                mn=201
                break
    #次の方が大きい場合キープ
    if mx<a2[i]:
        #キープ
        mx=a2[i]
    else:
        #売る
        c+=mx*s
        s=0
        mx=99

#最後は換金
c+=mx*s
print(c)

Submission status: Wrong Answer

Solution code:
import sys
N, *A = map(int, open(0).read().split())

money = 1_000

stock = 0
L = []
for i in range(len(A)):
    a = A[i]
    if i+1 < len(A) and a == A[i+1]:
        continue
    if stock == 0:
        stock = 10
        L.append(a)
        continue
    if L[-1] < a:
        stock = 0
        L.append(a)

# print(L)

if len(L) == 1:
    print(money)
    sys.exit()

stock = 0
for l in L:
    if stock == 0:
        stock = money // l
        money -= stock * l
    else:
        money += stock * l
        stock = 0

print(money)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
money = 1000
stocks = 0
down = 1
buy_price = -1
for i in range(n-1):
    # 下がってるけど次上がる→買う
    if down and A[i+1] > A[i]:
        down = -1
        stocks = money // A[i]
        money %= A[i]
        buy_price = A[i]
    # 上がってるけど次下がる→売る
    elif down == -1 and A[i+1] < A[i]:
        down = 1
        money += A[i] * stocks
        stoks = 0
        buy_price = -1
if buy_price != -1 and buy_price < A[-1]:
    money += A[-1] * stocks
print(money)

Submission status: Wrong Answer

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

for i in range(n-1):
    if ary[i] < ary[i+1]:
        kabu += money//ary[i]
        money -= kabu*ary[i]
    elif ary[i] > ary[i+1]:
        money += kabu*ary[i]
        kabu = 0

print(money+kabu*ary[n-1])

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
t=1000
s=0
for i in range(len(l)-1):
	if l[i]<l[i+1]:
		s=t//l[i]
		t%=l[i]
	if l[i]>l[i+1]:
		t+=l[i]*s
		s=0
if s:
	t+=l[-1]*s
print(t)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    A = list(map(int, input().split()))
    yd = A[0]
    B = 0
    tmp = 1000
    for i in range(1,N):
        if yd < A[i]:
            B = tmp//yd
            tmp = tmp - yd * B
        if yd > A[i]:
            if B > 0:
                tmp += yd * B
        yd = A[i]
    if B > 0:
        tmp += yd*B
    print(tmp)    

if __name__ == '__main__':
    main()

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]
k+=lb*st
print(k)

Submission status: Wrong Answer

Solution code:
import math
from itertools import groupby
N = int(input())
A = list(map(int, (input().split())))
cash = 1000
stock = 0
A = [i[0] for i in groupby(A)]
if (A[0] == max(A)):
    print(cash)
else:
    for i in range(len(A)-1):
        if (A[i] > A[i+1]):
            cash = cash + stock*A[i]
            stock = 0
        else:
            stock = stock + math.floor(cash/A[i])
            cash = cash - math.floor(cash/A[i])*A[i]
    cash = cash + stock*A[i+1]
    print(cash)

Submission status: Wrong Answer

Solution code:
n, *a = map(int, open(0).read().split())
j = 0
k = 1000
s = []
from itertools import*
a = [i for i,_ in groupby(a)]
for i in a:
  if j==0:A=i;j=1
  elif A<i:s+=[A,i],;j=0
for A,B in s:
  k = k//A*B + k%A
print(k)

Submission status: Wrong Answer

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

ans = 1000
diff = [0 for _ in range(N)]

for i in range(1, N):
    if A[i] == A[i - 1]:  diff[i] = 0
    elif A[i] > A[i - 1]: diff[i] = 1
    else:  diff[i] = -1

money = 1000
kabu = 0

for i in range(1, N):
    if kabu == 0:
        if diff[i] == 1:
            kabu = money // A[i - 1]
            money = money - A[i - 1] * kabu
    else:
        if diff[i] == -1:
            money = kabu * A[i - 1]
            kabu = 0
    
    ans = max(ans, money)

money += kabu * A[-1]
ans = max(ans, money)
print(ans)

Submission status: Wrong Answer

Solution code:
MAX = 5000

n = int(input())
a = list(map(int, input().split()))
dp = [[-1 for j in range(MAX + 1)] for i in range(n)]
dp[0][0] = 1000
for k in range(1, MAX + 1):
    if 1000 - a[0] * k < 0:
        break
    dp[0][k] = 1000 - a[0] * k
# print(dp[0])
for i in range(1, n):
    for j in range(MAX + 1):
        dp[i][j] = max(dp[i][j], dp[i - 1][j])
        # 買うとき
        for k in range(1, MAX + 1):
            if j - k < 0:
                break
            if dp[i - 1][j - k] - a[i] * k < 0:
                break
            dp[i][j] = max(dp[i][j], dp[i - 1][j - k] - a[i] * k)
        # 売る時
        for k in range(1, MAX + 1):
            if j + k >= MAX + 1:
                break
            if dp[i - 1][j + k] < 0:
                break
            dp[i][j] = max(dp[i][j], dp[i - 1][j + k] + a[i] * k)
    # print(dp[i])
# print(dp)
ans = max(dp[-1])
print(max(ans, 1000))

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] == min(A) and i != n-1:
        # 全体で最安なら買い
        k = m//A[i]
        m = m- A[i]*k
    elif A[i] < m and A[i] < max(A[i::]) and i != n-1:
        # 今後、現在の株価より高い株価があれば買い
        k = m//A[i]
        m = m - A[i]*k
    elif 0 < k and A[i] == max(A):
        # 全体で最高なら売り
        m = m + A[i]*k
        k = 0
    elif 0 < k and A[i] != min(A[i::]):
    #今後、現在の株価より安い株価があれば売り:
        m = m + A[i]*k
        k = 0
    else:
        pass

print (m)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [0]+list(map(int,input().split()))
money = 1000
stock = 0

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

#  print(i,money,stock)
print(money)

Submission status: Wrong Answer

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

# 頂点を集める
now = a_list[0]
desc = True if a_list[0] > a_list[1] else False
i = 1
kabu = 0
money = 1000
ans = 1000
if not desc:
  kabu = money // now
  money -= now * kabu
while i < len(a_list)-1:
  if desc:
    if now >= a_list[i+1]:
      now = a_list[i+1]
    else:
      # 次上がるなら株買う
      kabu = money // now
      money -= now * kabu
      desc = False
  else:
    if now <= a_list[i+1]:
      now = a_list[i+1]
    else:
      # 次下がるなら売る
      money += kabu * now
      kabu = 0
      desc = True
  now = a_list[i+1]
  i+=1
  ans = max(ans,money)

money += kabu * a_list[-1]
ans = max(ans,money)
print(ans)

Submission status: Wrong Answer

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

M = 1000
X = 0

for i in range(N-1):
    if A[i] < A[i+1]:
        X = int(M/A[i])
        M = int(M - X * A[i])
    
    elif A[i] > A[i+1]:
        M = int(M + A[i] * X)
        X = 0
    
if X > 0:
    M = M + X * A[N-1]

print(int(M))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.insert(0, a[0])
a.append(a[-1])
INF = 10 ** 9 + 7

money = 1000
stock = 0
mi = INF
ma = 0
for i in range(1, n + 1):
    mi = min(mi, a[i])
    ma = max(ma, a[i])
    if a[i - 1] < a[i] >= a[i + 1]:
        stock = money // mi
        money %= mi
        money += stock * ma
        mi = INF
        ma = 0

print(money)

Submission status: Wrong Answer

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    stocks = 0
    money = 1000
    i = 0
    while i < N:
        buy = i
        sell = i
        while sell < N-1 and A[sell] <= A[sell+1]:
            sell += 1
        if buy == sell:
            print(money)
            return
        
        stocks = money//A[buy]
        money = money - stocks*A[buy]
        money += stocks*A[sell]
        stocks = 0
        i = sell+1
    print(money)


if '__main__' == __name__:
    resolve()