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: Runtime Error

Solution code:
n = int(input())
list_A = list(map(int, input().split()))
list_N = [0]
money = 1000
for i in range(1, n):
    if list_A[i-1] < list_A[i]:
        list_N.append(1)
    else:
        list_N.append(0)


k = 0
while True:
    k += 1
    if list_N[k-1] == 0 and list_N[k] == 1:
        l = k-1
        roll = money // list_A[l]
        money = money % list_A[l]
        money += list_A[k]*roll
    if k == n:
        break

print(money)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
pro = 0
b = a[0]
for i in range(n):
    if a[i]>b:
        pro = 1
        break
if pro==0:
    print('1000')
else:
    sum = 1000
    t = 0
    if n>3 :
        while sum > 0 and t< n-3:
            if a[t]>a[t+1]:
                continue
            else:
                if a[t+1]>a[t+2]:
                    num = sum // a[t]
                    sum = sum + a[t+1]*num - a[t]*num
                    t=t+1
                
                else :
                    num = sum // a[t]
                    sum = sum + a[t+2]*num - a[t]*num
                    a.pop(t+2)
            t = t+1
    elif n == 3:
        if a[1]>a[2]:
            num = sum // a[0]
            sum = sum + a[1]*num - a[0]*num
                
        else :
            num = sum // a[0]
            sum = sum + a[2]*num - a[0]*num
    else:
        num = sum // a[0]
        sum = sum + a[1]*num - a[0]*num
    print(sum)

Submission status: Runtime Error

Solution code:
N = int(input()) #1行目のNを取得する
Input = [int(input()) for i in range(N)] #複数行の数値の入力を取得
 
for i in range(N):
  j = i + 1
  
  Stock = 0
  Money = 1000
  
  if j = 1:
    if Input[i] < Input[i+1]:
      Stock = Money // Input[i]
      Money = Money % Input[i]
     
    continue
   
  if j = N:
    if Stock != 0:
      money = money + Stock * Input[i]
    continue
  
  if Input[i - 1] < Input[i]:
    money = money + Stock * Input[i]
  
  if Input[i + 1] > Input[i]:
    Stock = Money // Input[i]
    Money = Money % Input[i]

    
print(money)

Submission status: Runtime Error

Solution code:
N = int(input()) #1行目のNを取得する
Input = [int(input()) for i in range(N)] #複数行の数値の入力を取得

for i in range(N):
  j = i + 1
  
  Stock = 0
  Money = 1000
  
  if j = 1:
    if Input[i] < Input[i+1]:
      Stock = Money // Input[i]
      Money = Money % Input[i]
     
    break
   
  if j = N:
    if Stock != 0:
      money = money + Stock * Input[i]
    break
  
  if Input[i - 1] < Input[i]:
    money = money + Stock * Input[i]
  
  if Input[i + 1] > Input[i]:
    Stock = Money // Input[i]
    Money = Money % Input[i]

Submission status: Runtime Error

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

money = 1000
stock = 0

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

print(money)

Submission status: Runtime Error

Solution code:
n,*a=open(0).read().split()
x=1000
for i in range(n-1):x+=x//a[i]*max(0,a[i+1]-a[i])    
print(x)

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()
 
money = 1000
stock = 0
pivot = []
flag = 0
 
for i in range(n):
    pivot.append(int(a[i+1]) - int(a[i]))
 
pivot.append(-1)
pivot.append(-1)
 
i=0
 
for j in range(n):
    if(pivot[j]>0):
        i = j
        break
    else:
        flag = -1
 
if(flag != -1):
    stock = int(money//(int(a[i])))
    money = int(money%(int(a[i])))
 
i+=1
if(flag != -1):
    while(i<n):
        if(i==n-1):
            if(pivot[i]>0):
                money = money + stock*int(a[i+1])
                stock = 0
            break
 
        elif(pivot[i]<0):
            while(pivot[i+1]<=0 and flag != -1):
                if(i==n-2):
                    flag = -1
                i+=1
            if(flag != -1):
                stock = int(money//(int(a[i+1])))
                money = int(money%(int(a[i+1])))
                i+=1
        else:
            while(pivot[i+1]>=0 and flag != -1):
                if(i==n-2):
                    flag = -1
                i+=1
            money = money + stock*int(a[i+1])
            stock = 0
            i+=1
 
print(money)

Submission status: Runtime Error

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

money = 1000
stock = 0
pivot = []
flag = 0

for i in range(n):
    pivot.append(int(a[i+1]) - int(a[i]))

pivot.append(-1)
pivot.append(-1)

i=0

for j in range(n):
    if(pivot[j]>0):
        i = j
        break
    else:
        flag = -1

if(flag != -1):
    stock = int(money//int(a[i]))
    money = int(money%(int(a[i])))

i+=1
if(flag != -1):
    while(i<n):
        if(i==n-1):
            if(pivot[i]>0):
                money = money + stock*int(a[i+1])
                stock = 0
            break

        elif(pivot[i]<0):
            while(pivot[i+1]<=0 and flag != -1):
                if(i==n-2):
                    flag = -1
                i+=1
            if(flag != -1):
                stock = int(money//int(a[i+1]))
                money = int(money%(int(a[i+1])))
                i+=1
        else:
            while(pivot[i+1]>=0 and flag != -1):
                if(i==n-2):
                    flag = -1
                i+=1
            money = money + stock*int(a[i+1])
            stock = 0
            i+=1

print(money)

Submission status: Runtime Error

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

money = 1000
stock = 0
pivot = []
flag = 0

for i in range(n):
    pivot.append(int(a[i+1]) - int(a[i]))

pivot.append(-1)
pivot.append(-1)

i=0

for j in range(n):
    if(pivot[j]>0):
        i = j
        break
    else:
        flag = -1

if(flag != -1):
    stock = int(money//int(a[i]))
    money = int(money%int(a[i]))

i+=1
if(flag != -1):
    while(i<n):
        if(i==n-1):
            if(pivot[i]>0):
                money = money + stock*int(a[i+1])
                stock = 0
            break

        elif(pivot[i]<0):
            while(pivot[i+1]<=0 and flag != -1):
                if(i==n-2):
                    flag = -1
                i+=1
            if(flag != -1):
                stock = int(money//int(a[i+1]))
                money = int(money%int(a[i+1]))
                i+=1
        else:
            while(pivot[i+1]>=0 and flag != -1):
                if(i==n-2):
                    flag = -1
                i+=1
            money = money + stock*int(a[i+1])
            stock = 0
            i+=1

print(money)

Submission status: Runtime Error

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

money = 1000
stock = 0
for i in range(n):
    money += stock * a[i]
    stock = 0

    if a[i] < a[i + 1]:
        stock = money // a[i]
        money %= a[i]

print(money)

Submission status: Runtime Error

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

money = 1000
stock = 0
pivot = []
flag = 0

for i in range(n-1):
    pivot.append(int(a[i+1]) - int(a[i]))

pivot.append(-1)

i=0

for j in range(n):
    if(pivot[j]>0):
        i = j
        break
    else:
        flag = -1

if(flag != -1):
    stock = int(money//int(a[i]))
    money = int(money%int(a[i]))

i+=1
if(flag != -1):
    while(i<n):
        if(i==n-1):
            if(pivot[i-1]>0):
                money = money + stock*int(a[i])
                stock = 0
            break

        if(pivot[i]<0):
            while(pivot[i+1]<=0):
                i+=1
            stock = int(money//int(a[i]))
            money = int(money%int(a[i]))
            i+=1
        else:
            while(pivot[i+1]>=0):
                i+=1
            money = money + stock*int(a[i+1])
            stock = 0
            i+=1

print(money)

Submission status: Runtime Error

Solution code:
import sys

n = int(input())
a = input().split()

money = 1000
stock = 0
pivot = []

for i in range(n-1):
    pivot.append(int(a[i+1]) - int(a[i]))

pivot.append(-1)

i=0

for j in range(n):
    if(pivot[j]>0):
        i = j
        break
    else:
        print("money=",money)
        sys.exit()

stock = int(money//int(a[i]))
money = int(money%int(a[i]))

i+=1

while(i<n):
    if(i==n-1):
        if(pivot[i-1]>0):
            money = money + stock*int(a[i])
            stock = 0
        sys.exit()

    if(pivot[i]<0):
        while(pivot[i+1]<=0):
            i+=1
        stock = int(money//int(a[i]))
        money = int(money%int(a[i]))
        i+=1
    else:
        while(pivot[i+1]>=0):
            i+=1
        money = money + stock*int(a[i+1])
        stock = 0
        i+=1

print(money)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
days = int(input())
vals = [int(x) for x in input().split()]
has = 1000
for i in range(days - 1)
    if vals[i] <= vals[i+1]:
        can, has = divmod(has, vals[i])
        has += can * (vals[i+1])
return has

Submission status: Runtime Error

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

dp = [0] * N
dp[0] = 1000

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

print(dp[N])

Submission status: Runtime Error

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

kabu = 0
money = 1000
for i in range(N-1):
  if A[i] < A[i+1]:
    mod,money = divmod(money , A[i])
    kabu += mod
  if A[i] > A[i+1]
    money = money + A[i+1] * kabu
    kabu = 0

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

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
money=1000
i=0
while i<N-1:
    k=1
    while A[i]==A[i+k]:
        k+=1
   
    if A[i+k]>A[i]:
        kabu=money//A[i]
        money-=kabu*A[i]
        money+=kabu*A[i+k]
        i=i+k
    elif A[i+k]<A[i]:
        i=i+k

print(money)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N_all = int(readline())
A_all = list(map(int,readline().split()))
A = [A_all[0]]
for i in range(1,N_all):
    if A[-1] != A_all[i]:
        A.append(A_all[i])
N = len(A) 
money = 1000
kabu = 0 
for i in range(N):
    money += A[i]*kabu
    kabu = 0
    if i == 0:
        if A[i] < A[i+1]:
        	kabu = money // A[i]
        	money -= kabu * A[i]
    elif i == N-1:
        continue
    else:
        if A[i-1] > A[i] and A[i] < A[i+1]:
            kabu = money // A[i]
            money -= kabu * A[i]
print(money)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N_all = int(readline())
A_all = list(map(int,readline().split()))
A = [A_all[0]]
for i in range(1,N_all):
    if A[-1] != A_all[i]:
        A.append(A_all[i])
N = len(A) 
money = 1000
kabu = 0 
for i in range(N):
    money += A[i]*kabu
    kabu = 0
    if i == 0:
        if A[i] < A[i+1]:
        	kabu = money // A[i]
        	money -= kabu * A[i]
    elif A[i-1] > A[i] and A[i] < A[i+1] and i < N-1: 
        kabu = money // A[i]
        money -= kabu * A[i]
print(money)

Submission status: Runtime Error

Solution code:
n = int(input())
values = np.array(list(map(int, input().split())))

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

Submission status: Runtime Error

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

state_of_up_or_down = False
money = 1000
min_price = a[0]
for i in range(1, n):
    if a[i] > a[i - 1]:
        # min_price = min(a[i - 1], min_price)
        max_price = a[i]
        state_of_up_or_down = True
        if i == n - 1:
            money = (money // min_price) * max_price + money % min_price
    elif a[i] == a[i - 1]:
        if i == n - 1:
            money = (money // min_price) * max_price + money % min_price        
        pass
    else:
        if state_of_up_or_down:
            money = (money // min_price) * max_price + money % min_price
            state_of_up_or_down = False
        # max_price = a[i]
        min_price = a[i]
    # print(money, min_price, state_of_up_or_down)
print(money)

Submission status: Runtime Error

Solution code:
n = int(input())
values = np.array(list(map(int, input().split())))

kabu = 0
money = 1000
for i in range(n-1):
    if values[i] < values[i+1]:
        kabu = money // values[i] 
        money += (values[i+1] - values[i]) * kabu
        
    else:
        continue
        
print(money)

Submission status: Runtime Error

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

nn=1000//100*2**(n//2+1)
dp=[[0]*nn for i in range(n)]

ni=1000//a[0]
for i in range(ni+1):    
    dp[0][i]=1000-a[0]*i

for k in range(1,n):
    for i in range(nn):
        xmax=0
        for ii in range(nn):
            x=dp[k-1][ii]+(ii-i)*a[k]
            if x<0:continue
            if dp[k-1][i]//a[k-1]<ii:break
            xmax=max(xmax,x) 
        dp[k][i]=xmax

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

print(m)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int, input().split()))
mon=1000
kai=0
for i in range(n-1):
  if a[i]>a[i+1] and a[i+1]>a[i+2]:
    mon+=kai*a[i]
    kai=0
  elif a[i]<a[i+1] and a[i+1]<a[i+2]:
    kai=int(mon/a[i])
    mon-=kai*a[i]

mon+=kai*a[n-1]

print(mon)

Submission status: Runtime Error

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

mon+=kai*a[n-1]

print(mon)

Submission status: Runtime Error

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

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

INF=10**20
def main():
    N=ii()
    A=list(mi()) + [0]

    dp = [[0] * (2*10**5+10) for _ in range(N+1)]
    x = 1000//A[0]
    dp[0][x] = 1000 - x * A[0]


    for i in range(x):
        dp[0][i] = 1000 - i * A[0]

    mx = x
    for i in range(N):
        _mx = mx
        for m in range(mx+1):
            if not m < len(dp[i]): continue
            for n in [0,m]:
                dp[i+1][m-n] = max(0,dp[i+1][m-n],dp[i][m] + A[i+1] * n)
                # assert dp[i+1][m-n] >= 0
            
            if A[i+1] != 0:
                j = dp[i][m] // A[i+1]
                dp[i+1][m+j] = max(0,dp[i+1][m+j],dp[i][m] - A[i+1] * j)
                _mx = max(_mx,m+j)
                # assert dp[i+1][m+j] >= 0

        mx = _mx

    ans = 0
    for i in range(len(dp[N-1])):
        if dp[N-1][i] == 0: continue
        ans = max(ans, dp[N-1][i] + A[N-1] * i)

    # print(dp[3][10])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

INF=10**20
def main():
    N=ii()
    A=list(mi()) + [0]

    dp = [[0] * (2*10**5+10) for _ in range(N+1)]
    x = 1000//A[0]
    dp[0][x] = 1000 - x * A[0]


    for i in range(x):
        dp[0][i] = 1000 - i * A[0]

    mx = x
    for i in range(N):
        _mx = mx
        for m in range(mx+1):
            for n in [0,m]:
                dp[i+1][m-n] = max(0,dp[i+1][m-n],dp[i][m] + A[i+1] * n)
                # assert dp[i+1][m-n] >= 0
            
            if A[i+1] != 0:
                j = dp[i][m] // A[i+1]
                dp[i+1][m+j] = max(0,dp[i+1][m+j],dp[i][m] - A[i+1] * j)
                _mx = max(_mx,m+j)
                # assert dp[i+1][m+j] >= 0

        mx = _mx

    ans = 0
    for i in range(len(dp[N-1])):
        if dp[N-1][i] == 0: continue
        ans = max(ans, dp[N-1][i] + A[N-1] * i)

    # print(dp[3][10])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

#print(N)
#print(A)

syozi = 1000
kabu = 0

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

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

print(syozi)

Submission status: Runtime Error

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

#hoge日めにhoge枚の株券持ち
dp = [[-10 for _ in range(2010)] for _ in range(n+1)]

dp[0][0] = 1000

#　i日目にj枚の株を持っているときに
for i in range(n):
    for j in range(2005):
        if dp[i][j] < 0:
            continue
        #print("i: %d, j: %d, dp: %d"%(i,j,dp[i][j]) )
        maxbuy = dp[i][j]//l[i]
        for k in range(maxbuy+1):
            dp[i+1][j+k] = max(dp[i+1][j+k], dp[i][j]-l[i]*k)
        for k in range(j+1):
            dp[i+1][j-k] = max(dp[i+1][j-k], dp[i][j]+l[i]*k)
print(dp[n][0])

Submission status: Runtime Error

Solution code:
N = int(input())

Xaxis = [[] for _ in range(200001)]
Yaxis = [[] for _ in range(200001)]
sum = [[] for _ in range(400001)]
dif = [[] for _ in range(40001)]
value = []
ans = 10**8

for i in range(N):
    X,Y,P = input().split()
    X,Y = int(X),int(Y)
    value.append([X,Y,P])
    Xaxis[X].append(i)
    Yaxis[Y].append(i)
    sum[X+Y].append(i)
    dif[X-Y].append(i)

for i in Xaxis:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'D' and value[i[k]][2] == 'U'):
                    if value[i[j]][1] >= value[i[k]][1]:
                        ans = min((value[i[j]][1]-value[i[k]][1])*5,ans)
                elif (value[i[j]][2] == 'U' and value[i[k]][2] == 'D'):
                    if value[i[j]][1] <= value[i[k]][1]:
                        ans = min((value[i[k]][1]-value[i[j]][1])*5,ans)

for i in Yaxis:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'L' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*5,ans)
                elif (value[i[j]][2] == 'R' and value[i[k]][2] == 'L'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*5,ans)

for i in sum:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if  (value[i[j]][2] == 'L' and value[i[k]][2] == 'D') or (value[i[j]][2] == 'U' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*10,ans)
                elif (value[i[j]][2] == 'D' and value[i[k]][2] == 'L') or (value[i[j]][2] == 'R' and value[i[k]][2] == 'U'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*10,ans)

for i in dif:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'L' and value[i[k]][2] == 'U') or (value[i[j]][2] == 'D' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*10,ans)
                elif (value[i[j]][2] == 'U' and value[i[k]][2] == 'L') or (value[i[j]][2] == 'R' and value[i[k]][2] == 'D'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*10,ans)

if ans == 10**8:
    print('SAFE')
else:
    print(ans)

Submission status: Runtime Error

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=201
mx=99
for i in range(len(a2)):
    #次の日の方が小さい場合キープ
    if mn>a2[i]:
        #キープ
        mn=a2[i]
    else:
        #買う
        s=c%mn
        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: Runtime Error

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

money = 1000

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

Submission status: Runtime Error

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

for i in range(1,n):
  if a[i-1] != a[i]:
    a_tmp.append(a[i])

n = len(a_tmp)

if a_tmp[0] < a_tmp[1]:
  a_new.append(a_tmp[0])

for i in range(1,n-1):
  if a_tmp[i-1] >= a_tmp[i] and a_tmp[i+1] >= a_tmp[i]:
    a_new.append(a_tmp[i])
  elif a[i-1] <= a_tmp[i] and a_tmp[i+1] <= a_tmp[i]:
    a_new.append(a_tmp[i])

a_new.append(a_tmp[n-1])

if len(a_new) == 1:
  next_ = 'sale'
else:
  next_ = 'buy'
for i in range(len(a_new)):
  if next_ == 'buy':
    kabu = money // a_new[i]
    money = money % a_new[i]
    next_ = 'sale'
  else:
    money += kabu * a_new[i]
    kabu = 0
    next_ = 'buy'

print(money)

Submission status: Runtime Error

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])
    
	M = M + X * A[i+1]
    X = 0

print(int(M))

Submission status: Runtime Error

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])
    
	M = M + X * A[i+1]

print(int(M))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_cur = a[0]

m = 1000
s = 0

for i in range(1, n):
  a_next = a[i]
  elif a_cur < a_next:
    # buy current and sell next
    s = m // a_cur
    m += s * (a_next - a_cur)
    a_cur = a_next
    if s > 0:
      m += s * a[-1]
      print(m)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())

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

a = 0
b = 0
Money = 1000

for i in range(len(A)):
  if a == 0:
    a = A[i]
    continue
  if b <= A[i] or b == 0:
    b = A[i]
    continue
  else:
    if a < b:
      Money = Money // a * b + Money % a
    a = b = A[i+1]
    
if a < b:
      Money = Money // a * b + Money % a
print(Money)

Submission status: Runtime Error

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
vs = list(Counter(a).keys())
i = 0
m = len(vs)
# 極小値で売る、極大値で買う
while i < m:
    if 0 < i < m - 1:
        if vs[i-1] > vs[i] < vs[i+1]:
            # 買う
            ans, kabu = ans % vs[i], ans // vs[i]
            # print(i, ans, kabu)

        elif vs[i-1] < vs[i] > vs[i+1]:
            # 売る
            ans, kabu = ans + vs[i] * kabu, 0
            # print(i, ans, kabu)
    else:
        if i == 0:
            if vs[i] < vs[i+1]:
                # 買う
                ans, kabu = ans % vs[i], ans // vs[i]
                # print(i, ans, kabu)
        else:
            if vs[i - 1] < vs[i]:
                # 売る
                ans, kabu = ans + vs[i] * kabu, 0
                # print(i, ans, kabu)
    i += 1
# print(i)

print(ans)

Submission status: Runtime Error

Solution code:

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

max_money = 1000
stock = 0

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


print(max_money)

Submission status: Runtime Error

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
vs = list(Counter(a).keys())
i = 0
m = len(vs)
# 極小値で売る、極大値で買う
while i < m:
    if 0 < i < m - 1:
        if vs[i-1] > vs[i] < vs[i+1]:
            # 買う
            ans, kabu = ans % vs[i], ans // vs[i]
            print(i, ans, kabu)

        elif vs[i-1] < vs[i] > vs[i+1]:
            # 売る
            ans, kabu = ans + vs[i] * kabu, 0
            # print(i, ans, kabu)
    else:
        if i == 0:
            if vs[i] < vs[i+1]:
                # 買う
                ans, kabu = ans % vs[i], ans // vs[i]
                # print(i, ans, kabu)
        else:
            if vs[i - 1] < vs[i]:
                # 売る
                ans, kabu = ans + vs[i] * kabu, 0
                # print(i, ans, kabu)
    i += 1
# print(i)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 1000
i = 0
while(len(A) - i > 1):
    s = [s for s in A[i + 1:] if s > A[i]]
    if len(s) == 0:
        break
    si = A.index(s[0])
    b = min(A[i:si])
    stock = ans // b
    ans %= b
    ans += (stock * s[0])
    i = si + 1
    
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))

tmp = [1000]
a= np.array(a)

for i in range(n-1):
    if a[i]>a[i+1]:
        pass
    else:
        tmp=a[i:]
        break

ans=1000
hand=0

if len(tmp)==1:
    print(1000)
    
else:
    am = np.diff(tmp)
    a = tmp[1:][am!=0]
    am= am[am!=0]
    hand = ans//tmp[0]
    ans = ans%tmp[0]
    for i in range(len(a)):
        if am[i]<0:
            hand = ans//a[i]
            ans = ans%a[i]
        else:
            ans=ans+a[i]*hand
            hand = 0
    print(ans+a[i]*hand)

Submission status: Runtime Error

Solution code:

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

max_money = 1000
stock = 0

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


print(max_money)

Submission status: Runtime Error

Solution code:
import nnumpy as np
n = int(input())
a = list(map(int, input().split()))

tmp = [1000]
a= np.array(a)

for i in range(n-1):
    if a[i]>a[i+1]:
        pass
    else:
        tmp=a[i:]
        break

ans=1000
hand=0

if len(tmp)==1:
    print(1000)
    
else:
    am = np.diff(tmp)
    a = tmp[1:][am!=0]
    am= am[am!=0]
    hand = ans//tmp[0]
    ans = ans%tmp[0]
    for i in range(len(a)):
        if am[i]<0:
            hand = ans//a[i]
            ans = ans%a[i]
        else:
            ans=ans+a[i]*hand
            hand = 0
    print(ans+a[i]*hand)