Problem:


Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.



Sample input:
7
100 130 130 130 115 115 150


Sample output:
1685

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    c = 1000
    for i in range(len(A)-1):
        if A[i] < A[i+1]:
            c = (c // A[i]) * A[i+1] + (c % A[i])

    return c


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
range = xrange
input = raw_input

n = int(input())
A = [int(x) for x in input().split()]

money = 1000
for i in range(n - 1):
    if A[i] < A[i + 1]:
        money += (money//A[i]) * (A[i + 1] - A[i])
print money

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    N = int(input())
    A = list(map(int, input().split()))
    # print(A)
    ans = 1000
    kabu = 0
    for i in range(N-1):
        if A[i] < A[i+1]:
            kabu += ans//A[i]
            ans = ans % A[i]
        else:
            ans += kabu * A[i]
            kabu = 0
    
    print(ans+kabu*A[N-1])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = IL()

now = 1000
for i in range(N-1):
  if A[i]<A[i+1]:
    n = now//A[i]
    now -= A[i]*n
    now += A[i+1]*n
print(now)

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
A.append(0)
cur = 1000
cnt = 0
for i in range(N):
    if A[i] > A[i+1]:
        # sell
        cur += cnt * A[i]
        cnt = 0
    elif A[i] < A[i+1]:
        # buy
        cnt += cur // A[i]
        cur %= A[i]
print(cur)

Submission status: Accepted

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

A = [10000] + A + [0]
now = A[0]
for i in range(N+1):
    if A[i+1] >= A[i]:
        mo += 0
    else:
        plus = (mo // now) * (A[i]-now)
        mo += plus
        now = A[i+1]
        
print(mo)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    a = nl()
    g = [1000] * (n+1)
    for i in range(n):
        g[i+1] = max(g[i], max(g[j+1]//a[j]*a[i] + g[j+1]%a[j] for j in range(i+1)))
    print(g[-1])
    return

solve()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
now = 1000
st = 0
for i in range(n-1):
  if a[i] < a[i+1]:
    tmp = now // a[i]
    now %= a[i]
    st += tmp
  else:
    now += st * a[i]
    st = 0
  #print(now,st)
print(now + a[-1] * st)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def examA():
    X = I()
    X -= 400
    ans = 8 - X//200
    print(ans)
    return

def examB():
    A, B, C = LI()
    K = I()
    cnt = 0
    while(A>=B):
        B *= 2
        cnt += 1
    while(B>=C):
        C *= 2
        cnt += 1

    if cnt<=K:
        print("Yes")
    else:
        print("No")
    return

def examC():
    N, K = LI()
    A = LI()
    ans = ["Yes"]*(N-K)
    for i in range(N-K):
        if A[i+K]<=A[i]:
            ans[i] = "No"
    for v in ans:
        print(v)
    return

def examD():
    N = I()
    A = LI()
    l = A[0]
    cnt = 1000
    for a in A[1:]:
        if l<a:
            cnt += cnt//l * (a-l)
            l = a
        elif l>a:
            l = a
    ans = cnt
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

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


main()

Submission status: Accepted

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

X=1000
Y=0

for k in range(N-1):
    if Y:
        X+=Y*A[k]
        Y=0
        
    if A[k]<A[k+1]:
        Y=X//A[k]
        X-=A[k]*Y

X+=A[-1]*Y
print(X)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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 LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    ans = 1000
    for i in range(n-1):
        if a[i] < a[i+1]:
            k = ans//a[i]
            ans -= k*a[i]
            ans += k*a[i+1]
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

money = 1000
stock = 0

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

print(money)

Submission status: Accepted

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

flag = 0
ans = 1000
cnt = 0
for i in range(n-1):
    if flag == 0 and A[i] < A[i+1]:
        cnt += ans//A[i]
        ans %= A[i]
        flag = 1
    elif flag == 1 and A[i] > A[i+1]:
        ans += cnt*A[i]
        cnt = 0
        flag = 0
print(ans + cnt*A[n-1])

Submission status: Accepted

Solution code:

N = int(input())

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

dp = [1000]

for i in range(N):

    now = dp[-1]
    for j in range(i):

        now = max(now , dp[j] % A[j] + dp[j]//A[j]*A[i])

    dp.append(now)

print (dp[-1])

Submission status: Accepted

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

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

money = 1000
maxm = 1000
kabu = 0
down = True # 下降しているという前提で始める

for i in range(len(A) - 1):
  if down: # 下がっているとき
    if A[i] < A[i + 1]: # 上がったなら買う
      kabu += money // A[i]
      money -= kabu * A[i]
      down = False
  else: # 上がっているとき
    if A[i] > A[i + 1]: # 下がったなら売る
      money += kabu * A[i]
      kabu = 0
      if money > maxm:
        maxm = money
      down = True
      
if kabu > 0: # 上がり続けているなら最後売れていない
  money += kabu * A[-1]
  
if money > maxm:
  maxm = money
  
print(maxm)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

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

    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
ans = 1000
for i in range(n-1):
    if lis[i] < lis[i+1]:
        num = ans//lis[i]
        ans %= lis[i]
        ans += num * lis[i+1]
print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())

kabu, yen = 0, 1000

for x, y in zip(A, A[1:] + [0]):
    # 円にもどす
    kabu, yen = 0, yen + x * kabu
    if x < y:
        # 買う
        kabu, yen = divmod(yen, x)

print(yen)

Submission status: Accepted

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

m = 1000

for a,b in zip(A,A[1:]):
    if a >= b: continue
    k,m = divmod(m,a)
    m += k*b
print(m)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
val = 1000
prv = a[0]
for i in range(1,n):
    c = a[i]
    if c>prv:
        val = val + (val//prv)*c - (val//prv)*prv
    prv = c
#     print(val)
print(val)

Submission status: Accepted

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

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

DP=[1000]*(N)

for i in range(N):
    DP[i]=max(DP[i-1],DP[i])
    st,rest=divmod(DP[i],A[i])

    #print(st,rest)
    for j in range(i+1,N):
        DP[j]=max(DP[j],st*A[j]+rest)

print(DP[-1])

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=1000
kab=0
for i in range(n):
  if a[i]<a[i+1]:
    kab+=cnt//a[i]
    cnt-=kab*a[i]
  else:
    cnt+=kab*a[i]
    kab=0
print(cnt)

Submission status: Runtime Error

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

groups = []
prev = A[0]
for i in range(1,n):
    if A[i] != prev:
        groups.append(prev)
        prev = A[i]
groups.append(prev)

money = 1000
stock = 0
if groups[0] < groups[1]:
    buy = money//groups[0]
    stock += buy
    money -= groups[0]*buy
    
for i in range(1,len(groups)-1):
    if groups[i-1] < groups[i] and groups[i] > groups[i+1]:# 周辺の最高値
        money += stock*groups[i]
        stock = 0
    elif groups[i-1] > groups[i] and groups[i] < groups[i+1]:# 周辺の最安値
        buy = money//groups[i]
        stock += buy
        money -= groups[i]*buy

money += stock*groups[-1]

print(money)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
money = 1000
nums = []
nums.append(a[0])
for i in range(1, n):
    if a[i] != a[i-1]:
        nums.append(a[i])
    else:
        continue
val = nums[0]
maxs = []
for j in range(1, len(nums)):
    if nums[j] > val:
        maxs.append(j)
    else:
        pass
    val = nums[j]
pos = 0
for idx in maxs:
    minVal = min(nums[pos:idx])
    stocks = money // minVal
    money = money - minVal * stocks + nums[idx] * stocks
    pos = idx + 1
print(money)

Submission status: Runtime Error

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

n = int(input())
*a, = map(int, input().split())
dp = [[-1] * 1000001 for i in range(n + 1)]
dp[0][0] = 1000
for i in range(n):
	for j in range(1000001):
		if dp[i][j] >= 0:
			dp[i + 1][j + dp[i][j] // a[i]] = max(dp[i + 1][j + dp[i][j] // a[i]], dp[i][j] % a[i])
			dp[i + 1][0] = max(dp[i + 1][0], dp[i][j] + a[i] * j)
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
print(dp[n][0])

Submission status: Runtime Error

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

n = int(input())
*a, = map(int, input().split())
dp = [[-1] * 100001 for i in range(n + 1)]
dp[0][0] = 1000
for i in range(n):
	for j in range(100001):
		if dp[i][j] >= 0:
			dp[i + 1][j + dp[i][j] // a[i]] = max(dp[i + 1][j + dp[i][j] // a[i]], dp[i][j] % a[i])
			dp[i + 1][0] = max(dp[i + 1][0], dp[i][j] + a[i] * j)
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
print(dp[n][0])

Submission status: Runtime Error

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

n = int(input())
*a, = map(int, input().split())
dp = [[-1] * 1001 for i in range(n + 1)]
dp[0][0] = 1000
for i in range(n):
	for j in range(1001):
		if dp[i][j] >= 0:
			if dp[i][j]:
				dp[i + 1][j + dp[i][j] // a[i]] = max(dp[i + 1][j + dp[i][j] // a[i]], dp[i][j] % a[i])
			dp[i + 1][0] = max(dp[i + 1][0], dp[i][j] + a[i] * j)
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
print(dp[n][0])

Submission status: Runtime Error

Solution code:
import copy, sys
n = int(input())
a_ls = list(map(int, input().split()))
b_ls = copy(a_ls)
for i in range(n-1):
    if b_ls[i] == a_ls[i+1]:
        b_ls.pop(i)
if not b_ls:
    print(1000)
    sys.exit()
c_ls = [b_ls[0]]
for i in range(1,len(b_ls)-1):
    if not (b_ls[i-1] < b_ls[i] < b_ls[i+1] or b_ls[i-1] > b_ls[i] > b_ls[i+1]):
        c_ls.append(b_ls[i])
c_ls.append(b_ls[-1])
money = 1000
stock = 0
m = len(c_ls)
for i in range(m-1):
    if c_ls[i] > c_ls[i+1]:
        money += c_ls[i] * stock
        stock = 0 
    else:  
        if money > 0:
            stock, money = divmod(money, c_ls[i])
if len(c_ls) > 1 and c_ls[-1] > c_ls[-2]:
    money += c_ls[-1] * stock
print(money)

Submission status: Runtime Error

Solution code:
import copy
n = int(input())
a_ls = list(map(int, input().split()))
b_ls = []
for i in range(n-1):
    if a_ls[i] != a_ls[i+1]:
        b_ls.append(a_ls[i])
if a_ls[-1] != a_ls[-2]:
    b_ls.append(a_ls[-1])
c_ls = [b_ls[0]]
for i in range(1,len(b_ls)-1):
    if not (b_ls[i-1] < b_ls[i] < b_ls[i+1] or b_ls[i-1] > b_ls[i] > b_ls[i+1]):
        c_ls.append(b_ls[i])
c_ls.append(b_ls[-1])
money = 1000
stock = 0
m = len(c_ls)
for i in range(m-1):
    if c_ls[i] > c_ls[i+1]:
        money += c_ls[i] * stock
        stock = 0 
    else:  
        if money > 0:
            stock, money = divmod(money, c_ls[i])
if len(c_ls) > 2 and c_ls[-1] > c_ls[-2]:
    money += c_ls[-1] * stock
print(money)

Submission status: Runtime Error

Solution code:
import copy
n = int(input())
a_ls = list(map(int, input().split()))
b_ls = []
for i in range(n-1):
    if a_ls[i] != a_ls[i+1]:
        b_ls.append(a_ls[i])
if a_ls[-1] != a_ls[-2]:
    b_ls.append(a_ls[-1])
c_ls = [b_ls[0]]
for i in range(1,len(b_ls)-1):
    if not (b_ls[i-1] < b_ls[i] < b_ls[i+1] or b_ls[i-1] > b_ls[i] > b_ls[i+1]):
        c_ls.append(b_ls[i])
c_ls.append(b_ls[-1])
money = 1000
stock = 0
m = len(c_ls)
for i in range(m-1):
    if c_ls[i] > c_ls[i+1]:
        money += c_ls[i] * stock
        stock = 0 
    else:  
        if money > 0:
            stock, money = divmod(money, c_ls[i])
if len(c_ls) > 1 and c_ls[-1] > c_ls[-2]:
    money += c_ls[-1] * stock
print(money)

Submission status: Runtime Error

Solution code:
import copy
n = int(input())
a_ls = list(map(int, input().split()))
b_ls = []
for i in range(n-1):
    if a_ls[i] != a_ls[i+1]:
        b_ls.append(a_ls[i])
if a_ls[-1] != a_ls[-2]:
    b_ls.append(a_ls[-1])
c_ls = [b_ls[0]]
for i in range(1,len(b_ls)-1):
    if not (b_ls[i-1] < b_ls[i] < b_ls[i+1] or b_ls[i-1] > b_ls[i] > b_ls[i+1]):
        c_ls.append(b_ls[i])
c_ls.append(b_ls[-1])
money = 1000
stock = 0
m = len(c_ls)
for i in range(m-1):
    if c_ls[i] > c_ls[i+1]:
        money += c_ls[i] * stock
        stock = 0 
    else:  
        if money > 0:
            stock, money = divmod(money, c_ls[i])
if c_ls[-1] > c_ls[-2]:
    money += c_ls[-1] * stock
print(money)

Submission status: Runtime Error

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

money = 1000
kabu = 0

a = []
for kabuka in given_a:
    if len(a) > 0 and a[-1] == kabuka:
        continue
    else:
        a.append(kabuka)

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

print(money)

Submission status: Runtime Error

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


dp = [0] * 10
dp[0] = 1000
for i in range(1,N):
    dp[i] = dp[i-1]
    for j in range(0,i):
        V = int(dp[j] / A[j])
        W = dp[j] + (A[i] - A[j]) * V
        dp[i] = max(dp[i], W)
print(dp[N-1])

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input()))
 
money = 1000
for i in range(n-1):
  if a[i] < a[i+1]:
    money += (a[i+1]-a[i]) * (money//a[i])
print(money)

Submission status: Runtime Error

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

money = 1000
for i in range(n-1):
  if a[i] < a[i+1]:
    money += (a[i-1]-a[i]) * (money//a[i])
print(money)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
money=1000
stock=0
a=[A[0]]
for i in range(1,n):
    if A[i]!=A[i-1]:
        a.append(A[i])
for i in range(len(a)):
    try:
        if a[i-1]>=a[i] and a[i]<=a[i+1]:
            stock+=money//a[i]
            money%=a[i]
        elif a[i-1]<=a[i] and a[i]>=a[i+1]:
            money+=a[i]*stock
            stock=0

    except:
        if i==0:
            if a[i]<=a[i+1]:
                stock+=money//a[i]
                money%=a[i]
        else:
            money+=a[i]*stock
            stock=0
print(money)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
I=list(map(int,input().split()))
num=1000
for i in range(n-1):
    if I[i]<I[i+1]:
        p=num//I[i])
        num-=p*I[i]
        num+=p*I[i+1]

print(num)

Submission status: Runtime Error

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

from collections import deque


cash = 1000
stock = 0

peak_min = deque()
peak_max = deque()

d_prev = 0
for i in range(1, n):
    a = A[i]
    if A[i] > A[i-1]:
        d = 1
    elif A[i] == A[i-1]:
        d = d_prev
    else:
        d = -1
    if d_prev*d == -1:
        if d == 1:
            peak_min.append(i-1)
        elif d == -1:
            peak_max.append(i-1)
    d_prev = d
#print(peak_min, peak_max)

if len(peak_min) == 0 and len(peak_max) == 0:
    if A[0] < A[-1]:
        ans = cash%A[0] + cash//A[0]*A[-1]
    else:
        ans = cash
elif len(peak_min) == 0:
    ans = cash%A[0] + cash//A[0]*A[peak_max]
elif len(peak_max) == 0:
    ans = cash%A[peak_min] + cash//A[peak_min]*A[-1]
else:
    if peak_max[0] < peak_min[0]:
        peak_min.appendleft(0)
    if peak_max[-1] < peak_min[-1]:
        peak_max.append(n-1)
#    print(peak_min, peak_max)
    
    while len(peak_min) != 0:
        i = peak_min.popleft()
        stock += cash//A[i]
        cash = cash%A[i]
#        print(i, cash , stock)
        
        i = peak_max.popleft()
        cash += stock*A[i]
        stock = 0
#        print(i, cash , stock)
    ans = cash
print(ans)

Submission status: Runtime Error

Solution code:
N = int(intput())
stocks = list(map(int, input().split()))
curr = 1000
for i in range(N - 1):
    cnt = 0
    if stocks[i] < stocks[i + 1]:
      cnt = curr // stocks[i]
    curr += cnt * (stocks[i + 1] - stocks[i])
print(curr)

Submission status: Runtime Error

Solution code:
N = int(intput())
stocks = list(map(int, input().split()))
curr = 1000
for i in range(N - 1):
  cnt = 0
  if stocks[i] < stocks[i + 1]:
    cnt = curr // stocks[i]
  curr += cnt * (stocks[i + 1] - stocks[i])
print(curr)

Submission status: Runtime Error

Solution code:
N = int(intput())
stocks = list(map(int, input().split()))
for i in range(N - 1):
  cnt = 0
  if stocks[i] < stocks[i + 1]:
    pass

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

#define ii(i,start,end,inc) for(int i = start; i < end; i += inc)
#define dd(i,start,end,inc) for(int i = start; i > end; i -= inc)
#define zz(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define BHAAG ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef pair<ll, ll> pll; 
typedef pair<int, int> pii; 
typedef pair<ull, ull> pull; 

const int  MAX = 1e6 + 3;
template <typename T> istream &operator>>(istream &os, vector<T> &v) { for (auto &e : v) os >> e; return os; }
template <typename T> void print(vector<T> &v, string sep = " ", string end = "\n") { for (auto it = v.begin(); it != v.end() - 1; ++it) { cout << *it << sep ; } cout << v.back() << end; }

// PRIMES 
vi my_primes;
void computePrimes(int mx) { my_primes.assign(mx + 1, 0); ii(i, 2, mx, 1) if (my_primes[i] == 0) ii(j, i + i, mx, i) if (my_primes[j] == 0) my_primes[j] = i; my_primes[0] = my_primes[1] = -1; }
inline bool isPrime(int x){ return my_primes[x] == 0; }

#define divs(a,b) ( b % a == 0 )

int main(int argc, char const *argv[])
{
    #ifdef JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int n;
    vi v(n); cin >> v;
    ll curMoney = 1000;
    for(int i = 0; i < n - 1; ++i){
        ll stocks = 0;
        if(v[i] < v[i+1]) stocks = curMoney / v[i];
        curMoney += (v[i+1] - v[i]) * stocks;
    }

    cout << curMoney ;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())

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

money = 1000

for i in range(N):
    t = money + (A[i + 1] - A[i]) * (money // A[i])
    money = max(money, t)

print(money)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.buffer.readline
N, K = map(int, input().split())
As = list(map(int, input().split()))
for i in range(K, N):
    print("Yes" if As[i] > As[i-K] else "No")

Submission status: Runtime Error

Solution code:

n= int(input())
arr = [int(x) for x in input().strip().split()]

mem = {}
def helper(i, mon, stk):
  	key = (i, mon, stk)
    if key in mem:
      	return mem[key]
      
    if i==len(arr):
        return mon
    
    ans = helper(i+1, mon, stk)
    buy = 1
    while True:
        if arr[i]*buy<=mon:
            ans = max(ans, helper(i+1, mon - arr[i]*buy, stk+buy))
        else:
            break
        
        buy += 1
    
    for s in range(1, stk+1):
        ans = max(ans, helper(i+1, mon+arr[i]*s, stk-s))
    
    mem[key] = ans
    return ans

    
print(helper(0, 1000, 0))