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: Time Limit Exceeded

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))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
n= int(input())
arr = [int(x) for x in input().strip().split()]

@lru_cache(None)
def helper(i, mon, stk):
    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))
    
    return ans

    
print(helper(0, 1000, 0))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
money=1000
stock=0
for i in range(N-1):
  if ls[i]<ls[i+1]:
    while money>=ls[i]:
      money-=ls[i]
      stock+=1
  elif ls[i]>ls[i+1]:
    money+=ls[i]*stock
    stock=0
money+=ls[N-1]*stock

print(money)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
money=1000
stock=0
for i in range(N-1):
  if ls[i]<ls[i+1]:
    while money>=ls[i]:
      money-=ls[i]
      stock+=1
  elif ls[i]>ls[i+1]:
    while stock>0:
      money+=ls[i]
      stock-=1
while stock>0:
  money+=ls[N-1]
  stock-=1
print(money)

Submission status: Time Limit Exceeded

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

def dfs(i, yen, kabu_yen, kabu_n):
    # print(i, yen, kabu_yen, kabu_n)

    if i>=n:
        return yen

    a = b = c = 0
    if yen >= A[i]: # 買える
        m = yen // A[i]
        a = dfs(i+1, yen - m*A[i], kabu_yen + m*A[i], kabu_n + m) # 買う
    if kabu_n > 0: # 売れる
        b = dfs(i+1,  yen + kabu_n*A[i], 0, 0) # 売る
    c = dfs(i+1, yen, kabu_yen, kabu_n) # 何もしない
    mx = max(a,b,c)
    return mx

mx = dfs(0, 1000, 0,0)
print(mx)

Submission status: Time Limit Exceeded

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

k_y = [1000]

for i in range(n):
	genkin_list = [ hk * a[i] + hy for hk,hy in enumerate(k_y) ]  #所持株、金
	genkin_list_max = max(genkin_list)
	max_index = genkin_list.index(genkin_list_max)
	k_y = [ genkin_list_max - a[i] * j for j in range(genkin_list_max // a[i]+1 ) ]
	
print( k_y[0] )

Submission status: Time Limit Exceeded

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

k_y = [1000]

for i in range(n):
	genkin_list = [ hk * a[i] + hy for hk,hy in enumerate(k_y) ]  #所持株、金
	genkin_list_max = max(genkin_list)
	max_index = genkin_list.index(genkin_list_max)
	
	k_y = [ a[i]*(max_index-hk)+k_y[max_index] for hk,hy in enumerate(k_y) ]
	k_y2 = [ genkin_list_max - a[i] * x for x in range( len(k_y), genkin_list_max // a[i]+1 ) ]
	k_y = k_y + k_y2
print( k_y[0] )

Submission status: Time Limit Exceeded

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

k_y = [1000]

for i in range(n):
	genkin_list = [ hk * a[i] + hy for hk,hy in enumerate(k_y) ]  #所持株、金
	genkin_list_max = max(genkin_list)
	max_index = genkin_list.index(genkin_list_max)
	
	k_y = [ a[i]*(max_index-hk)+k_y[max_index] for hk,hy in enumerate(k_y) ]
	#print(  len(k_y), genkin_list_max // a[i] )
	for x in range( len(k_y), genkin_list_max // a[i]+1 ):
		k_y.append( genkin_list_max - a[i] * x )
	#print(k_y)
print( k_y[0] )

Submission status: Time Limit Exceeded

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 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: Time Limit Exceeded

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

def dfs(day, money, kab, prev_kab):
    ans = 0
    if day == N:
        money += A[-1]*kab
        return money
    else:
        if prev_kab != A[day-1]:
            # buy
            buy_kab = money//A[day-1]
            new_ans = dfs(day+1, money-A[day-1]*buy_kab, kab+buy_kab, prev_kab)
            ans = max(ans, new_ans)
            # sell
            sell_kab = kab
            new_ans = dfs(day+1, money+A[day-1]*sell_kab, kab-sell_kab, prev_kab)
            ans = max(ans, new_ans)
            # nothing
            new_ans = dfs(day+1, money, kab, A[day-1])
        else:
            new_ans = dfs(day+1, money, kab, A[day-1])
            ans = max(ans, new_ans)
    return ans

ans = dfs(1, 1000, 0, -1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
MAX = 1000000


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

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

Submission status: Time Limit Exceeded

Solution code:
MAX = 1000000000


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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import numpy as np

n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))

ms = [[1000, 0]] # 現金, 所有株数

def transaction(money, stock):
    for s_n in range(-math.floor(money / a[i]), stock+1):
        if s_n == 0:
            continue
        elif s_n > 0 and stock <= 0:
            continue
        elif s_n < 0 and money <= 0:
            continue
        state = [money + a[i]*s_n, stock-s_n]
        if state not in ms:
            ms.append(state)

for i in range(n):
    for (money, stock) in ms:
        transaction(money, stock)

print(max(np.array(ms)[:,0]))

Submission status: Time Limit Exceeded

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:
      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: Time Limit Exceeded

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

def dfs(day, money, kab, prev_kab):
    ans = 0
    if day == N:
        money += A[-1]*kab
        return money
    else:
        if prev_kab != A[day-1]:
            # buy
            buy_kab = 1
            while money-A[day-1]*buy_kab >= 0:
                new_ans = dfs(day+1, money-A[day-1]*buy_kab, kab+buy_kab, prev_kab)
                ans = max(ans, new_ans)
                buy_kab += 1
            # sell or do nothing
            for sell_kab in range(0, kab+1):
                new_ans = dfs(day+1, money+A[day-1]*sell_kab, kab-sell_kab, prev_kab)
                ans = max(ans, new_ans)
        else:
            new_ans = dfs(day+1, money, kab, A[day-1])
            ans = max(ans, new_ans)
    return ans

ans = dfs(1, 1000, 0, -1)
print(ans)

Submission status: Time Limit Exceeded

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

def dfs(day, money, kab, prev_kab):
    ans = 0
    if day == N:
        money += A[-1]*kab
        return money
    else:
        if prev_kab != A[day-1]:
            # buy
            buy_kab = 1
            while money-A[day-1]*buy_kab >= 0:
                new_ans = dfs(day+1, money-A[day-1]*buy_kab, kab+buy_kab, prev_kab)
                ans = max(ans, new_ans)
                buy_kab += 1
            # sell or do nothing
            for sell_kab in range(0, kab+1):
                new_ans = dfs(day+1, money+A[day-1]*sell_kab, kab-sell_kab, prev_kab)
                ans = max(ans, new_ans)
        else:
            new_ans = dfs(day+1, money, kab, kab)
            ans = max(ans, new_ans)
    return ans

ans = dfs(1, 1000, 0, -1)
print(ans)

Submission status: Time Limit Exceeded

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

max = 1000
s = 1000
kabu = 0
for k in itertools.combinations_with_replacement(range(-11,12), n):
	for i in range(n):
		if k[i] < 0 and abs(k[i])*a[i] <= s:
			s -= abs(k[i])*a[i]
			kabu += abs(k[i])
		if k[i] > 0 and kabu >= k[i]:
			s += k[i]*a[i]
			kabu -= k[i]

	if s > max:
		max = s
	s = 1000
	kabu = 0
print(max)

Submission status: Time Limit Exceeded

Solution code:
len = int(input())
lis = list(map(int, input().split()))

cash = 1000
ans = 0

def recur(l,kabu,sum):
    global ans
    if l == len:
        ans = max(ans, sum)
    else:
        recur(l+1,0,sum+kabu*lis[l]) # 全部現金にするパターン
        recur(l+1,kabu+sum//lis[l] ,sum%lis[l]) # 出来るだけ買うパターン

recur(0,0,1000)
print(ans)

Submission status: Time Limit Exceeded

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

money = 1000
num = 0
buy_p = 0
i = 0

while i < N-1:
    if A[i] >= A[i+1]:
        i += 1
        continue
    elif i == N-2:
        if A[i] < A[N-1]:
            num, money = divmod(money, A[i])
            money += A[N-1] * num
        break
    for j in range(i+2, N):
        if A[j-1] > A[j]:
            num, money = divmod(money, A[i])
            money += A[j-1] * num
            i = j
            break

print(money)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import numpy as np

n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))

ms = [[1000, 0]] # 現金, 所有株数

def transaction(money, stock):
    if stock > 0:
        # 売るケース
        for s_n in range(1, stock+1):
            state = [money + a[i]*s_n, stock-s_n]
            if state not in ms:
                ms.append(state)
    if money > 0:
        # 買うケース
        for s_n in range(1, math.floor(money / a[i])+1):
            state = [money - a[i]*s_n, stock+s_n]
            if state not in ms:
                ms.append(state)

for i in range(n):
    for (money, stock) in ms:
        transaction(money, stock)

print(max(np.array(ms)[:,0]))

Submission status: Time Limit Exceeded

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

n = 0
seq = []
dp = []

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

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

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

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

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

    return ret


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

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

    print(getMaximum(0, 0, 1000))


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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
price = list(map(int,input().split()))
start = 1000
for i in range(N-1):
    if price[i]<price[i+1]:
        for j in range(start//price[i]):
            start += price[i+1]-price[i]


print(max(1000,start))

Submission status: Time Limit Exceeded

Solution code:
import sys


def _i(): return int(sys.stdin.readline().strip())


def _ia(): return map(int, sys.stdin.readline().strip().split())


def main():
    n = _i()
    a = list(_ia())

    for i in range(1, n):
        if a[i]-a[i-1] > 0:
            j = i - 1
            break
    else:
        return 1000

    pairs = []
    m = a[j]
    flag = True
    for i in range(j, n-1):
        if flag and a[i+1] - a[i] < 0:
            pairs.append([m, a[i]])
            flag = False
        elif (not flag) and a[i+1] - a[i] > 0:
            m = a[i]
            flag = True
    if flag:
        pairs.append([m, a[-1]])

    w = {0: 1000}
    for p in pairs:
        low, hi = p
        tmp = {}
        for c, val in w.items():
            for a, v in enumerate(range(0, val+1, low)):
                cnt = c + a
                tmp[cnt] = max(tmp.get(cnt, 0), val - v)

        for c, val in tmp.items():
            for a in range(0, c+1):
                cnt = c - a
                w[cnt] = max(tmp.get(cnt, 0), val + a * hi)

    return max(w.values())


if __name__ == "__main__":
    print(main())

Submission status: Time Limit Exceeded

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
lis = []
prv = -1
for i in range(n):
    if prv != A[i]:
        lis.append(A[i])
        prv = A[i]
def dfs(money,kabu,now):
    if now == len(lis):
        global ans
        ans = max(money,ans)
        return
    if kabu != 0:
        if now + 1 == len(lis):
            dfs(money + kabu*lis[now],0,now+1)
        else:
            if lis[now] > lis[now + 1]:
                dfs(money + kabu*lis[now],0,now+1)
    if money//lis[now] >= 1:
        if now + 1 == len(lis):
            dfs(money - lis[now] * (money//lis[now]),money//lis[now],now+1)
        else:
            if lis[now] < lis[now + 1]:
                dfs(money - lis[now] * (money//lis[now]),money//lis[now],now+1)
    dfs(money,kabu,now+1)
ans = 1000
dfs(1000,0,0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
n = int(input())
#k, x = map(int, input().split())
al = list(map(int, input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
money=[]
m=2
day=0
def dfs(cur, day, kabu):
    global money
    if day==n:
        money.append(cur)
        return
    # buy
    for v in range(0,cur//al[day]+1):
        buynum=v*al[day]
        nowkabu=kabu+v
        dfs(cur-v*al[day],day+1,kabu+v)
        #A.append(v)
        #dfs(A)
        #A.pop()
    # sell
    for w in range(0,kabu+1):
        sellnum=w
        dfs(cur+w*al[day],day+1,kabu-w)
dfs(1000,day,0)
print(max(money))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)
from functools import lru_cache
 
    
class Solution:
    def __init__(self,prices):
        self._max = 0
        self.prices = prices
        self.n = len(prices)
    
    @lru_cache(None)
    def transaction(self, money, stock, i, last_buy):
        if i == len(self.prices):
            self._max = max(self._max, money)
            return
        
        _stock,_money = stock,money

        ## buy
        if money >= self.prices[i]:
            stock, money = divmod(money, self.prices[i])
            self.transaction(money, stock, i + 1, self.prices[i])
        # backtracking
        money,stock = _money,_stock
 
        ## sell
        if stock > 0 and self.prices[i] > last_buy:
            stock, money = 0, stock * self.prices[i] + money
            if money > 1000:
                self.transaction(money, 0, i + 1,0)
        # backtracking
        money,stock = _money,_stock
 
        ## do nothing
        self.transaction(money, stock, i + 1,last_buy)
 
 
def main():
    n = int(input().strip())
    A = [int(i) for i in input().strip().split()]
    solver = Solution(A)
    solver.transaction(1000, 0, 0, 0)
    print(solver._max)
    

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
a = list(map(int,input().split()))
stack = deque()
stack.append([1000,0,0,200])
ans = []
while stack:
  money,kabu,i,temp = stack.pop()
  ans.append(money)
  if i<n:
    stack.append([money,kabu,i+1,temp])
    if a[i]<temp and a[i]<=money:
      kabu += money//a[i]
      money %= a[i]
      stack.append([money,kabu,i+1,a[i]])
    elif a[i]>temp and kabu>=1:
      money += a[i]*kabu
      kabu = 0
      stack.append([money,kabu,i+1,a[i]])
    ans.append(money)
print(max(ans))

Submission status: Time Limit Exceeded

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

max_money = 0

for i in range(2 ** n):
  money = 1000
  kabu = 0
  next_ = 'buy'
  for j in range(n):
    if 1&(i>>j):
      if next_ == 'buy':
        kabu = money // a[j]
        money = money % a[j]
        next_ = 'sale'
      else:
        money += kabu * a[j]
        kabu = 0
        next_ = 'buy'
  max_money = max(max_money,money)

print(max_money)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(4100000)

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


def dfs(day, money,stock):
    if day == N:
        pricelist.append(money+ stock * A[N-1])
        return
    price = A[day-1]
    dfs(day+1, money % price, stock + money//price)
    dfs(day+1, money, stock)
    dfs(day+1, money + stock * price, 0)
    return

dfs(1,1000,0)

print(max(pricelist))

Submission status: Time Limit Exceeded

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

k_y = [1000]
k_y_new = [ 1000 ]
for i in range(n):
	for hk,hy in enumerate(k_y): #所持株、金
		kk = 0#想定所持株数
		while True:
			genkin = (hk - kk) * a[i] + hy
			
			if genkin < 0 :
				break
			else:
				if kk >= len(k_y_new):
					k_y_new.append( genkin )
				else:
					k_y_new[kk] = max(k_y_new[kk], genkin)
				kk = kk + 1
		#print( k_y_new )
	k_y = [y for y in k_y_new]
print( k_y_new[0] )

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
a = list(map(int,input().split()))
stack = deque()
stack.append([1000,0,0,200])
ans = []
while stack:
  money,kabu,i,temp = stack.pop()
  ans.append(money)
  if i<n:
    stack.append([money,kabu,i+1,temp])
    if a[i]<temp and a[i]<=money:
      kabu += money//a[i]
      money %= a[i]
    elif a[i]>temp and kabu>=1:
      money += a[i]*kabu
      kabu = 0
    stack.append([money,kabu,i+1,a[i]])
    ans.append(money)
print(max(ans))

Submission status: Time Limit Exceeded

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

ans = 0
def dfs(i, stock, money):
    global N
    global A
    global ans

    # print(i, stock, money)
    if i == N:
        ans = max(ans, money)
        return

    nextStock = int(stock)
    nextMoney = int(money)
    dfs(i+1, nextStock, nextMoney)
    
    buyStock = money//A[i]
    nextStock = stock + buyStock
    nextMoney = money - buyStock*A[i]
    dfs(i+1, nextStock, nextMoney)

    nextStock = 0
    nextMoney = money + stock * A[i]
    dfs(i+1, nextStock, nextMoney)

dfs(0, 0, 1000)
print(ans)

Submission status: Time Limit Exceeded

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

money = 1000
num = 0
buy_p = 0
i = 0

while(i<N-1):
    if A[i] >= A[i+1]:
        i += 1
        continue
    elif i == N-2:
        if A[i] < A[N-1]:
            num, money = divmod(money, A[i])
            money += A[N-1] * num
        break
    for j in range(i+2, N):
        if A[j-1] > A[j]:
            num, money = divmod(money, A[i])
            money += A[j-1] * num
            i = j
            break

print(money)

Submission status: Time Limit Exceeded

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]:
        while stock:
            stock -= 1
            money += A[i]
    else:
        while money >= A[i]:
            money -= A[i]
            stock += 1

print(stock * A[n-1] + money)

Submission status: Time Limit Exceeded

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

def dfs(day, money, kab):
    ans = 0
    if day == N:
        money += A[-1]*kab
        return money
    else:
        # buy
        buy_kab = 1
        while money-A[day-1]*buy_kab >= 0:
            new_ans = dfs(day+1, money-A[day-1]*buy_kab, kab+buy_kab)
            ans = max(ans, new_ans)
            buy_kab += 1
        # sell or do nothing
        for sell_kab in range(0, kab+1):
            new_ans = dfs(day+1, money+A[day-1]*sell_kab, kab-sell_kab)
            ans = max(ans, new_ans)
    return ans

ans = dfs(1, 1000, 0)
print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    money = 1000
    kabu_c = 0
    l = list()
    for i in range(n-1):
        if A[i+1] - A[i] > 0:
            l.append(1)
        else:
            l.append(0)
    for idx, i in enumerate(l):
        if i == 1:
            while  money - A[idx] >= 0:
                money = money - A[idx]
                kabu_c += 1
        if i == 0:
            money = money + (kabu_c * A[idx])
            kabu_c = 0
    if kabu_c > 0:
        money = money + (kabu_c * A[n-1])
    print(money)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#: Author - Soumya Saurav
import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline
import collections,sys,threading
sys.setrecursionlimit(10**9)
threading.stack_size(10**8)

########################################
def main():
	n = int(input())
	arr = list(map(int , input().split()))
	from functools import lru_cache
	@lru_cache(None)
	def f(j, p, s):
		#print(j,p,s)
		global ans
		if p < 0 or s < 0:
			return -float("inf")
		if j == n:
			return p
		i = 1
		t1 = 0
		t2 = 0
		while p - arr[j]*i >= 0:
			t1 = max(t1 , f(j+1,p - arr[j]*i,s+i))
			i+=1
		i = 1
		while s - i >= 0:
			t2 = max(f(j+1,p+arr[j]*i,s-i), t2)
			i+=1
		return max(t2,t1,f(j+1,p,s))
	print(f(0,1000,0))



threading.Thread(target=main).start()

Submission status: Time Limit Exceeded

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

money = 1000
num = 0
buy_p = 0
i = 0

while(i<N-1):
    if A[i] >= A[i+1]:
        i += 1
        continue
    elif i == N-2:
        if A[i] < A[N-1]:
            num = money // A[i]
            money -= A[i] * num
            money += A[N-1] * num
        break
    for j in range(i+2, N):
        if A[j-1] > A[j]:
            num = money // A[i]
            money -= A[i] * num
            money += A[j-1] * num
            i = j
            break

print(money)

Submission status: Time Limit Exceeded

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
lis = []
prv = -1
for i in range(n):
    if prv != A[i]:
        lis.append(A[i])
        prv = A[i]
#print(lis)
dp = [[1000] * len(lis) for _ in range(len(lis))]
def dfs(money,kabu,now):
    if now == len(lis):
        return a.append(money)
    if kabu != 0:
        dfs(money + kabu*lis[now],0,now+1)
    if money//lis[now] >= 1:
        dfs(money - lis[now] * (money//lis[now]),money//lis[now],now+1)
    dfs(money,kabu,now+1)
a = []

dfs(1000,0,0)

print(max(a))

Submission status: Time Limit Exceeded

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

money = 1000
num = 0
buy_p = 0
i = 0

while(i<N-1):
    if A[i] >= A[i+1]:
        i += 1
        continue
    elif i == N-2:
        if A[i] < A[N-1]:
            num = money // A[i]
            money -= A[i] * num
            money += A[N-1] * num
        break
    for j in range(i+1, N):
        if A[j-1] > A[j]:
            num = money // A[i]
            money -= A[i] * num
            money += A[j-1] * num
            i = j
            break

print(money)