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())
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:
            print()
            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:
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**3+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 range(m+1):
                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:
                for j in range((dp[i][m] // A[i+1])+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:
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <iomanip>
#include <bitset>
#include <set>
#include <map>
#include <stdio.h>
#include <numeric>
#include <cstring>

#define rep(i,n) for (int i = 0; i < (n); i++)
#define rep1(i,n) for (int i = 1; i < (n); i++)
#define FOR(i,a,b) for (int i=(a); i < (b); i++)
#define MOD 1000000007 //10^9+7
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using PLL = pair<long long, long long>;
const int INF = numeric_limits<int>::max();

// 負の数にも対応した % 演算
long long mod(long long val, long long m) {
    long long res = val % m;
    if (res < 0) res += m;
    return res;
}

//greatest common divisor
long long gcd(ll a, ll b) 
{
    if (a % b == 0) {
        return b;
    } else {
        return gcd(b, a % b);
    }
}

//least common multiple
long long lcm(ll a, ll b)
{
    return a / gcd(a, b) * b ;
}

int main()
{
    int n; cin >> n;
    vector<int> a(n+1,0);
    rep(i,n) cin >> a[i];
    ll m = 1000;
    ll cabu = 0;
    rep(i,n) {
        if (a[i] < a[i+1]) {
            cabu += m / a[i];
            m -= (m / a[i]) * a[i];
        } else if (a[i] > a[i+1]){
            m += cabu * a[i];
            cabu = 0;
        } else {
            continue;
        }
    }
    cout << m << endl;

    return 0;
}

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**3+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 range(m+1):
                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:
                for j in range((dp[i][m] // A[i+1])+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 copy
from collections import deque


# def fig(A):
#     x = np.array(range(len(A)))
#     y = np.array(A)
#     plt.plot(x, y)
#     plt.show()


def buy(money, A, stock):
    stock += money // A
    money %= A
    return money, stock


def sell(money, A, stock):
    money += stock * A
    return money, 0


def do_both(money, A0, A1, stock):
    money, stock = buy(money, A0, stock)
    money, stock = sell(money, A1, stock)
    return (money, stock)


def shaping(A):
    # 連続値を削除
    B = A[:1]
    for a in A[1:]:
        if B[-1] != a:
            B.append(a)
    # 上昇あるいは下降の途中値を削除
    A = copy.copy(B)
    B = A[:1]
    for i in range(1, len(A)-1):
        if not (A[i-1] < A[i] < A[i+1] or A[i-1] > A[i] > A[i+1]):
            B.append(A[i])
    B.append(A[-1])
    # 最初と最後を調整
    A = copy.copy(B)
    #   最初よりその次のほうが低ければ、その次を開始地点とする
    try:
        if A[1] < A[0]:
            A = A[1:]
    except:
        pass
    #   最後よりその前のほうが高ければ、その前を終了地点とする
    try:
        if A[-2] > A[-1]:
            A = A[:-1]
    except:
        pass
    # (下, 上)のタプルに変換して返す
    return [(A[i], A[i+1]) for i in range(0, len(A)-1, 2)]


def bfs(q, A, i, num):
    if i >= len(A):
        return q
    money, stock = q.popleft()
    q.append(do_both(money, *A[i], 0))
    q.append((money, stock))
    if num == 2**i:
        return bfs(q, A, i+1, 1)
    else:
        return bfs(q, A, i, num+1)


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))

    A = shaping(A)
    q = deque()
    if len(A) == 0:
        print(1000)
        exit()
    q.append(do_both(1000, *A[0], 0))
    q.append((1000, 0))
    q = bfs(q, A, 1, 1)

    ans = 0
    for a in q:
        if a[0] >= ans:
            ans = a[0]

    print(ans)

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**3+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 range(m+1):
                dp[i+1][m-n] = max(dp[i+1][m-n],dp[i][m] + A[i+1] * n)
                assert dp[i+1][m-n] >= 0
            
            if A[i+1] != 0:
                for j in range((dp[i][m] // A[i+1])+1):
                    dp[i+1][m+j] = max(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()))
total = 1000
kabu = 0
 
for i in range(N-1):
    if (A[i] < A[i+1]):
        kabu += total//A[i]
        total -= kabu*A[i]
    elif(A[i]==A[i+1]):
        pass
    elif(A[i] > A[i+1])
        total += (kabu*A[i])
        kabu = 0
 
 
#最後
i += 1
total += (kabu * A[i])
kabu = 0
print(total)

Submission status: Runtime Error

Solution code:
# Kこの街について，それぞれ横に串刺しにするか縦に串刺しにするかを選んでDFS
N = int(input())

L = [0] * N
for i in range(N):
    X,Y,P = map(int,input().split())
    L[i] = [X,Y,P]

def dfs(XL,YL,PL,K):
    # もう残りのやつがなかったら，０になる
    if not PL:
        return 0
    # 残りがあるけど，全部線を引き終えたら，残りのポイントたちについて距離を計算
    if len(XL)+len(YL)==K+2:
        score = 0
        for x,y,p in PL:
            x_dis = min([abs(x-xl) for xl in XL])
            y_dis = min([abs(y-yl) for yl in YL])
            min_dis = min(x_dis,y_dis)
            score += min_dis*p

        return score
    # それ以外のときは線を引いていく
    score = []
    for i, pl in enumerate(PL):
        x,y,_ = pl
        x = [x]
        y = [y]
        score.append(min(dfs(XL+x,YL,PL[:i]+PL[i+1:],K),dfs(XL,YL+y,PL[:i]+PL[i+1:],K)))
    return min(score)


for i in range(N+1):
    print((dfs([0],[0],L,i)))

Submission status: Runtime Error

Solution code:
import itertools
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_compact = []
    sabun = []
    for i in range(1, n):
        sabun.append(a[i] - a[i-1])
    for i, kabuka in enumerate(a):
        if i == 0:
            a_compact.append(kabuka)
        elif i == len(a)-1:
            if a[-2] != a[-1]:
                acom.append(kabuka)
        else:
            if sabun[i-1] != 0:
                check = sabun[i-1] * sabun[i]
                if check <= 0:
                    a_compact.append(kabuka)
    sabun_compact = []

    for i in range(1, len(a_compact)):
        sabun_compact.append(a_compact[i] - a_compact[i-1])
    money = 1000
    kabu = 0
    for i, s in enumerate(sabun_compact):
        # print(i)
        # print('money' + str(money))
        # print('kabunum' + str(kabu))
        if s > 0:
            kabu = money//a_compact[i]
            money -= a_compact[i]*kabu
        else:
            money += a_compact[i]*kabu
            kabu = 0
    money += a_compact[-1]*kabu
    print(money)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
syoji = 1000
kabu = 0
kai = 0
uri = 0
if N ==1:
  print(syoji)
  exit()
sw = 0
for i in range(N):
  if i<N-1 and kabu == 0 and A[i] < A [i+1]:
    for j in range(i+1,N):
      if sw ==1 and uri >= A[j]:
        break
 
      elif  A[i] < A[j]:
        uri = A[j]
        sw = 1
 
    if sw ==1:
    	kabu = syoji // A[i]
        syoji = syoji - (num-1)*A[i]
        kai = A[i]
        sw = 0
  elif kabu > 0 and A[i]==uri:
    syoji = syoji + kabu*A[i]
    kabu = 0
    uri = 0
 
        
print(syoji)

Submission status: Runtime Error

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

prices = sorted(list(set(a)),key=a.index)

lmaxmin = []

for i in range(len(prices)):
    if i == 0:
        if prices[i] < prices[i+1]:
            lmaxmin.append(i)
    elif i == len(prices) - 1:
        if prices[i-1] < prices[i]:
            lmaxmin.append(i)
    elif prices[i] < prices[i+1] and prices[i-1] > prices[i]:
        lmaxmin.append(i)
    elif prices[i] > prices[i+1] and prices[i-1] < prices[i]:
        lmaxmin.append(i)


finalVal = 0

for i in range(2 ** len(lmaxmin)):
    curentVal = 1000
    stockNum = 0
    for j in range(len(lmaxmin)):
        if ((i >> j) & 1):
            ind = lmaxmin[j]
            if ind == 0:
                num = curentVal // prices[ind]
                curentVal -= prices[ind] * num
                stockNum += num
            elif ind == len(prices) - 1:
                num = stockNum
                curentVal += prices[ind] * num
                stockNum -= num
            elif prices[ind] < prices[ind+1]:
                num = curentVal // prices[ind]
                curentVal -= prices[ind] * num
                stockNum += num
            else:
                num = stockNum
                curentVal += prices[ind] * num
                stockNum -= num
    
    if finalVal < curentVal:
        finalVal = curentVal

print(finalVal)

Submission status: Runtime Error

Solution code:
import itertools
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_compact = []
    sabun = []
    for i in range(1, n):
        sabun.append(a[i] - a[i-1])
    for i, kabuka in enumerate(a):
        if i == 0:
            a_compact.append(kabuka)
        else:
            if sabun[i-1] != 0:
                check = sabun[i-1] * sabun[i]
                if check <= 0:
                    a_compact.append(kabuka)
    sabun_compact = []

    for i in range(1, len(a_compact)):
        sabun_compact.append(a_compact[i] - a_compact[i-1])
    money = 1000
    kabu = 0
    for i, s in enumerate(sabun_compact):
        # print(i)
        # print('money' + str(money))
        # print('kabunum' + str(kabu))
        if s > 0:
            kabu = money//a_compact[i]
            money -= a_compact[i]*kabu
        else:
            money += a_compact[i]*kabu
            kabu = 0
    money += a_compact[-1]*kabu
    print(money)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
for k in range(N-1):
  if A[k] == A[k+1]:
    A[k] = 0
B = []
for l in A:
  if l != 0:
    B.append(l)
n = len(B)

money = 1000
mx = []
mi = []
if B[0] < B[1]:
  mi.append(B[0])
for i in range(1,n-1):
  if B[i] > B[i-1] and B[i] > B[i+1]:
    mx.append(B[i])
  if B[i] < B[i-1] and B[i] < B[i+1]:
    mi.append(B[i])
if B[n-1] > B[n-2]:
  mx.append(B[n-1])
  
for j in range(len(mx)):
  konyu = money // mi[j]
  money += (mx[j] - mi[j]) * konyu

print(money)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
 
for i in range(N-1,0,-1) :
    if A[i-1]>A[i] :
        if A[i]<A[i+1] :
            continue
        A.pop(i)
 
if len(A)==1 :
    print(1000)
    exit()

n=len(A)
money=1000
kabu=0
 
 
for i in range(n-1) : 
    if i==n-2 :
        if A[i-1]<=A[i] :
            continue
        elif A[i-1]>A[i] :
            kabu+=money//A[i]
            money-=A[i]*(money//A[i])
 
    if A[i]<A[i+1] and money>=A[i] : #株を買う
        kabu+=money//A[i]
        money-=A[i]*(money//A[i])
 
    elif A[i]>A[i+1] and kabu>0 :    #株を売る
        if A[i+1]>=A[i+2] :          #下がり続けていれば何もしない
            continue
        elif A[i+1]<A[i+2] :
            money+=kabu*A[i]
            kabu=0
 
else :
    money+=kabu*A[n-1]
    print(money)

Submission status: Runtime Error

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

ma = 0
mi = a[0]
money = 1000
stock = 0
a_sorted = sorted(a, reverse=True)

if a[0] != a_sorted[0]:
  for i in range(1, n):
    if mi > a[i]:
      mi = a[i]

    elif ma < a[i]:
      ma = a[i]

    elif ma > a[i]:
      stock = money // mi
      money = money - (stock * mi)
      
      money = money + stock * ma
      stock = 0
      
      mi = a[i]
      ma = a[i]
 
      
if mi < ma and stock > 0:
  stock = money // mi
  money = money + (stock * (ma-mi))
elif stock > 0:
  money = ma * stock
elif mi < ma and stock == 0:
  stock = money // mi
  money = money + (stock * (ma-mi))
elif ma < mi and stock == 0:
  stock = money // ma
  money = money + (stock * (ma-mi))

  
print(money)

Submission status: Runtime Error

Solution code:
n=int(input())
d = list(map(int,input().split()))
day = [d[0]]
for i in range(n-1):
    if d[i] == d[i+1]:
        continue
    day.append(d[i+1])
day = [1000]+day+[0]
print(day)
up = []
for i in range(len(day)-2):
    if day[i] < day[i+1] and day[i+1] > day[i+2]:
        up.append(day[i+1])
    elif day[i] > day[i+1] and day[i+1] < day[i+2]:
        up.append(day[i+1])
print(up)
if day[-1] > up[-1]:
    up.append(day[-1])

kabu = 0
money = 1000
for i in range(len(up)-1):
    if up[i] < up[i+1]:
        kabu += money//up[i]
        money -= kabu*up[i]
    elif up[i] > up[i+1]:
        money += kabu*up[i]
        kabu = 0
    #print(kabu,money)
money += kabu*up[-2]
print(money)

Submission status: Runtime Error

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

for i in range(n - 1):
    if a[i] < a[i + 1] and a[i + 1] > a[i + 2]:
        s = m // a[i]
        m -= s * a[i]
    elif a[i] < a[i + 1] and a[i + 1] <= a[i + 2]:
        continue
    else:
        m += s * a[i]
        s = 0
m += s * a[n - 1]
print(m)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
for k in range(N-1):
  if A[k] == A[k+1]:
    A[k] = 0
B = []
for l in A:
  if l != 0:
    B.append(l)
n = len(B)

money = 1000
mx = []
mi = []
if B[0] < B[1]:
  mi.append(B[0])
for i in range(1,n-1):
  if B[i] > B[i-1] and B[i] > B[i+1]:
    mx.append(B[i])
  if B[i] < B[i-1] and B[i] < B[i+1]:
    mi.append(B[i])
if B[n-1] > B[n-2]:
  mx.append(B[n-1])
  
for j in range(len(mx)):
  konyu = money // mi[j]
  money += (mx[j] - mi[j]) * konyu

if B[0] == max(B)
money = 1000
print(money)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 1000
for i in range(n - 1):
    dp = [0] * 1000 * 200
    dp[0] = 0
    c = b
    for j in range(b + 1):
        dp[j + a[i]] = max(dp[j + a[i]], dp[j] + a[i + 1])
    for j in range(b + 1):
        c = max(c, dp[j] + b - j)
    b = c
print(b)

Submission status: Runtime Error

Solution code:
def solve():
    N=int(input())
    A=list(map(int,input().split()))
    mini,maxi=min(A),max(A)
    cnt=1000//mini
    print(maxi*cnt+1000%mini)
    
if __name__=="__main__"
    solve()

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
tl=[l[0]]
for i in range(1,n):
    if l[i]!=l[i-1]:
        tl.append(l[i])


a=[]
ma=False

for i in range(len(tl)):
    if i==0:
        if tl[i]>tl[i+1]:
            ma=True
            a.append(tl[i])
        else:
            ma=False
            a.append(tl[i])
    elif i==len(tl)-1:
        if tl[i]>tl[i-1]:
            a.append(tl[i])
        else:
            a.append(tl[i])
    else:
        if tl[i-1]<tl[i] and tl[i+1]<tl[i]:
            a.append(tl[i])
        elif tl[i-1]>tl[i] and tl[i+1]>tl[i]:
            a.append(tl[i])

if ma:
    a.pop(0)


num=0
ans=1000

for i in range(0,len(a)-1,2):
    
    thing1=(ans//(a[i]))*a[i+1]
    thing2=num*a[i+1]
    if thing1>thing2:
        ans=thing1+ans%a[i]
        num=0
        
    elif thing2>thing1:
        ans=thing2

print(ans)

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**3+10) for _ in range(N+1)]
    x = 1000//A[0]
    dp[0][x] = 1000 - x * A[0]

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

    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 range(m+1):
                dp[i+1][m-n] = dp[i][m] + A[i+1] * n
                assert dp[i+1][m-n] >= 0
            
            if A[i+1] != 0:
                for j in range((dp[i][m] // A[i+1])+1):
                    dp[i+1][m+j] = max(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(max(dp[N]))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

ls = []
if A[0] < A[1]:
  ls.append(A[0])
for i in range(1,N-1):
  if A[i] == A[i-1]:
    continue
  elif A[i-1] < A[i] <= A[i+1]:
    continue
  elif A[i-1] > A[i] >= A[i+1]:
    continue
  ls.append(A[i])  
if A[-1] > A[-2]:
  ls.append(A[-1])
    
if ls == []:
  print(1000)
else:
  for i,j in enumerate(ls_1):
    if i%2 == 0:
      stock += my_money//j
      my_money %= j
    else:
      my_money += stock*j
      stock = 0
  print(my_money)

Submission status: Runtime Error

Solution code:
N = int(input())
values = [int(s) for s in input().split()]
rest = 1000
for i in range(N):
  if rest >= 1000:
  	if values[i] >= values[i+1]:
    	have = values[i] // rest
	    rest = values[i] % rest
  elif value != 0:
    rest += values[i] * have
print(rest)

Submission status: Runtime Error

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
from fractions  import gcd
#input=sys.stdin.readline
import bisect
n=int(input())
a=list(map(int,input().split()))
i=0
m=1000
f=True
k=0
M=[1000]
res=[0]*(n-1)
for i in range(1,n):
    if a[i]<a[i-1]:
        res[i-1]=-1
    else:
        res[i-1]=1

for i in range(n):

    if f:
        if res[i]==-1:
            continue 
        else:
            f=False
            
            k=m//a[i]
            m=m%a[i]
    else:
        if i==n-1 or res[i]==-1:
            m=k*a[i]+m
            M.append(m)
            f=True 
        else:
            continue
print(max(M))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 1000
for i in range(n - 1):
    dp = [0] * 100000
    dp[0] = 0
    c = b
    for j in range(b + 1):
        dp[j + a[i]] = max(dp[j + a[i]], dp[j] + a[i + 1])
    for j in range(b + 1):
        c = max(c, dp[j] + b - j)
    b = c
print(b)

Submission status: Runtime Error

Solution code:
from collections import Counter 
n = int(input())
x = list(map(int, input().split()))
A = [x[i] for i in range(n) if i!=n-1 and x[i] != x[i+1]]
if A[-1] != x[-1]: A.append(x[-1])
a = Counter(A)
cnt = 0
money = 1000
b = 0
for i in range(len(A)-1):
    a[A[i]] -= 1
    if a[A[i]] == 0: a.pop(A[i])

    if i == len(A)-2 and cnt >0:
        if A[i] < A[i+1]: continue

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

Submission status: Runtime Error

Solution code:
def inN():
    return int(input())
def inL():
    return list(map(int,input().split()))

n = inN()
a = inL()

kab = 0
mon = 1000
f = []
i=0
state = 0
while(i < n-1):
    if i == 0:
        if a[i+1] > a[i]:
            f.append(1)
            state = 1
        else:
            f.append(0)
    elif state == 1:
        while(i<n-1 and a[i+1]>=a[i]):
            f.append(0)
            i+=1
        f.append(2)
        state = 0
    elif state == 0:
        while(i<n-1):
            if a[i+1]<=a[i]:
                f.append(0)
                i +=1
            else:
                f.append(1)
                i += 1
                state = 1
                break
    else:
        f.append(0)
    i += 1
f.append(2)
    
print(f)   
for i in range(n):

    if f[i] == 1 :
        kab += mon//a[i]
        mon = mon % a[i]
    elif f[i] == 2:
        mon += a[i]*kab
        kab = 0
    
print(mon)

Submission status: Runtime Error

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

high = max(a)
low = min(a)

if a[0] == high:
    print(1000)
else:
    s = a.index(high)
    a = a[:s+1]
    wallet = 1000
    stock = 0
    markets = []
    market = [a[0]]
    for i in range(n-1):
        if a[i+1] == a[i]:
            continue
        if a[i+1] > a[i]:
            market.append(a[i+1])
            markets.append(market)
            market = [a[i+1]]
        else:
            market.append(a[i+1])

    for i in range(len(markets)):
        if i == 0:
            stock += wallet // markets[i][0]
            wallet %= markets[i][0]
        elif i != 0:
            stock += wallet // markets[i][1]
            wallet %= markets[i][1]
        wallet += stock * markets[i][-1]
        stock = 0
    print(wallet)

Submission status: Runtime Error

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

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

N = getN()
A = getList()
# dp[i][j]: i日目にj株持っている時の最大金額
dp = [[-float('inf')] * 301 for i in range(N + 1)]
dp[0][0] = 1000

for i in range(N):
    for j in range(301):
        # 何もしない
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        # 株を買う
        if dp[i][j] > 0:
            for l in range((dp[i][j] // A[i]) + 1):
                dp[i + 1][j + l] = max(dp[i + 1][j + l], dp[i][j + l], dp[i][j] - A[i] * l)
        for b in range(j + 1):
            dp[i + 1][j - b] = max(dp[i + 1][j - b], dp[i][j - b], dp[i][j] + A[i] * b)
print(dp[N][0])

Submission status: Runtime Error

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

max_stock = 1000000
dp = [[] for _ in range(n+1)]
dp[0] = [1000, 0]

for i in range(n):
    for k in dp[i]:
        dp[i+1].append(k) # do nothing
        dp[i+1].append([k[0] % a[i] , k[1] + k[0] // a[i]]) # buy stock
        dp[i+1].append([k[0] + k[1]*a[i]]) # sell stock
max_ = 0
for i in dp[n]:
    if i[0] > max_:
        max_ = i[0]
print(max_)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
syoji = 1000
kabu = 0
kai = 0
uri = 0
if N ==1:
  print(syoji)
  exit()
sw = 0
for i in range(N):
  if kabu == 0 and A[i] < A [i+1]:
    for j in range(i+1,N):
      if sw ==1 and uri >= A[j]:
        break

      elif  A[i] < A[j]:
        uri = A[j]
        sw = 1

    if sw ==1:
      for num in range (1000000000):
        if syoji < num*A[i]:
          kabu = num-1
          syoji = syoji - (num-1)*A[i]
          kai = A[i]
          sw = 0
          break
  elif kabu > 0 and A[i]==uri:
    syoji = syoji + kabu*A[i]
    kabu = 0
    uri = 0

        
print(syoji)

Submission status: Runtime Error

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

print(num)

Submission status: Runtime Error

Solution code:
import sys
from math import sqrt, gcd, ceil, log
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))

def main():
	n = int(input()); arr = read()
	stocks = 0; money = 1000
	j = n-1
	while arr[j] <= arr[j-1]:
		j -= 1
	for i in range(j):
		if arr[i] < arr[i+1] and stocks == 0:
			stocks += money//arr[i]
			money = money%arr[i]
		elif arr[i] > arr[i+1] and stocks:
			money += stocks*arr[i]
			stocks = 0

	# print(j)
	if stocks:
		money += arr[j]*stocks

	print(money)







if __name__ == "__main__":
	main()