Problem:


Score: 400 points

Problem Statement

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

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

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

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

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

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

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

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

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

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

Initially, he has 1000 yen and no stocks.

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

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

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

Day 4: Do nothing.

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

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

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

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

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

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

Sample Input 3

2
157 193

Sample Output 3

1216

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



Sample input:
7
100 130 130 130 115 115 150


Sample output:
1685

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    *A, = map(int, input().split())
    A += [0]

    money = 1000
    stock = 0
    for a, b in zip(A, A[1:]):
        money += stock * a
        stock = 0
        if a < b:
            buy, rest = divmod(money, a)
            stock = buy
            money = rest
    print(money)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

mon = 1000
kabu = 0

for i in range(N-1):
  if A[i] <= A[i+1]:
    kabu, mon = divmod(mon, A[i])
    mon += kabu * A[i+1]
    
print(mon)

Submission status: Accepted

Solution code:
n = int(input())
stocks = [int(x) for x in input().split()]
mymoney = 1000
mystock = 0

for i in range(0,len(stocks)-1):     # コメントアウト部分はデバッグ用コード
      #print(stocks[i],stocks[i+1],end=":")
      if stocks[i] < stocks[i+1]:    # 明日の方が株価が高くなる場合は、今日のうちに株を買えるだけ買う
            #print("buying",end=" ")
            mystock += mymoney//stocks[i]
            mymoney = mymoney%stocks[i]
      elif stocks[i] >= stocks[i+1]:  # 明日の方が株価が安くなる場合には、今日のうちに手持ちの株を売却
            #print("selling",end=" ")
            mymoney += mystock*stocks[i]
            mystock = 0
      #print(mymoney,mystock)

i += 1
mymoney += mystock * stocks[i]      # 最終日に手持ちの株をすべて売却して現金化
mystock = 0

print(mymoney)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    A=list(map(int,input().split()))
    ans=1000
    stocks=0
    for i in range(N-1):
        if A[i]>A[i+1]:
            ans+=stocks*A[i]
            stocks=0
        else:
            stocks+=(ans//A[i])
            ans-=(ans//A[i])*A[i]
    print(ans+stocks*A[-1])
if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

# a = get_int()
def get_int():
    return int(input())

# a = get_string()
def get_string():
    return input()

# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]

# a_list = get_string_list():
def get_string_list():
    return input().split()

# a, b = get_int_multi()
def get_int_multi():  # a, b = get_int_multi()
    return map(int, input().split())

# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))

# print("{} {}".format(a, b))
# for num in range(0, a):
# a_list[idx]
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''

def main():

    n = get_int()
    a_list = get_int_list()

    w_min = a_list[0]
    w_max = a_list[0]
    pre = a_list[0]
    kabu = 0
    kane = 1000

    for i in range(1, n):

        if pre < a_list[i] and kabu == 0:
            kabu = kane // w_min
            kane = kane - kabu * w_min
            w_max = a_list[i]

        elif pre > a_list[i] and kabu > 0:
            kane = kane + w_max * kabu
            kabu = 0
            w_min = a_list[i]

        elif pre < a_list[i] and kabu > 0:
            w_max = a_list[i]

        elif pre > a_list[i] and kabu == 0:
            w_min = a_list[i]



        pre = a_list[i]

    if kabu > 0:
        kane = kane + w_max * kabu

    print(kane)






if __name__ == '__main__':
    main()

Submission status: Accepted

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]:
    mon+=kai*a[i]
    kai=0
  elif a[i]<a[i+1] and kai==0:
    kai=int(mon/a[i])
    mon-=kai*a[i]

mon+=kai*a[n-1]

print(mon)

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    N=I()
    A=LI()
    L=[0]*N
    
    #売り:1
    for i in range(1,N):
        if A[i]>A[i-1]:
            L[i]=1
            if L[i-1]==1:
                L[i-1]=0
            else:
                L[i-1]=2
                
    mon=1000
    kabu=0
    
    for i in range(N):
        if L[i]==2:
            kabu=mon//A[i]
            mon-=kabu*A[i]
        elif L[i]==1:
            mon+=kabu*A[i]
            kabu=0
            
    print(mon)
                
    

main()

Submission status: Accepted

Solution code:
num = int(input())
kabu = [int(x) for x in input().split()]
kabu.append(-1)
mkabu = 0
max = 0
money = 1000
MAX = 0

for i in range(num):
    if MAX < kabu[i]:
        MAX = kabu[i]


for i in range(num):
    if kabu[i] == -1:
        bleak
    if 0 < mkabu: #売る
        money = money + kabu[i] * mkabu
        mkabu = 0
#        print("売る", money)
#    print("MAX", max)
    if kabu[i] < kabu[i+1]: #買う
        mkabu = money // kabu[i]
        money = money % kabu[i]
#        print("買う", money)
if 0 < mkabu:
    money = money + kabu[-1] * mkabu

print(money)

Submission status: Accepted

Solution code:
def solve(n, a):
    ans = 1000
    for i in range(n-1):
        if a[i] < a[i+1]:
            ans += (ans // a[i]) * (a[i+1] - a[i])
    return ans

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

Submission status: Accepted

Solution code:
from typing import List


def solve(A: List[int], N: int) -> int:
    current_money = 1000
    for i in range(N - 1):
        stocks = 0

        if A[i] < A[i + 1]:
            stocks = current_money // A[i]
        current_money += (A[i + 1] - A[i]) * stocks
    return current_money


def main():
    N = int(input())
    A = [int(x) for x in input().split(" ")]

    print(solve(A, N))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans += stock*a[-1]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1  # sys.maxsize # float("inf")
MOD = 10 ** 9 + 7


def debug(*x):
    print(*x, file=sys.stderr)


def solve(N, XS):
    cash = 1000
    stock = 0
    for i in range(N - 1):
        price = XS[i]
        if cash >= price:
            if XS[i + 1] > price:
                # buy
                s = cash // price
                stock += s
                cash -= s * price
                continue
        if stock > 0:
            if XS[i + 1] < price:
                # sell
                cash += stock * price
                stock = 0
    # last day
    cash += stock * XS[-1]
    return cash


def main():
    # parse input
    N = int(input())
    XS = list(map(int, input().split()))
    print(solve(N, XS))


# tests
T1 = """
7
100 130 130 130 115 115 150
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
1685
"""

T2 = """
6
200 180 160 140 120 100
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
1000
"""


def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g)


def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii")


input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

if sys.argv[-1] == "-t":
    print("testing")
    _test()
    sys.exit()

main()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(money)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

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

money = 1000
stock = 0

def is_minimal(idx):
    if idx!=N-1:
        if A[idx+1]>A[idx]:
            return True
        else:
            return False

def is_maximal(idx):
    if idx!=N-1:
        if A[idx+1]<A[idx]:
            return True
        else:
            return False

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

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

Submission status: Accepted

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

currentmoney = 1000
stock = 0

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

print(currentmoney)

Submission status: Accepted

Solution code:
# Problem D - Road to Millionaire

# input
N = int(input())
a_nums = list(map(int, input().split()))

# initialization
max_list = [0] * N
max_list[0] = 1000

# max calc
for i in range(1, N):
    for j in range(0, i):
        a_remain = max_list[j] % a_nums[j]
        a_count = max_list[j] // a_nums[j]
        max_list[i] = max(max_list[j], a_nums[i]*a_count+a_remain)

# output
print(max_list[N-1])

Submission status: Accepted

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

Submission status: Accepted

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

money = 1000
stock = 0

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

Submission status: Accepted

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

x = 1000
for i in range(n-1):
    l = a[i]
    r = a[i+1]
    if (l < r):
        k = x//l
        x %= l
        x += k*r

print(x)

Submission status: Accepted

Solution code:
N = int(input())
An = list(map(int, input().split()))
dp = [0 for x in range(N+10)]
dp[0] = 1000
for i in range(1, N):
    stock = dp[i-1] // An[i-1]
#     print(dp[i-1], An[i-1], stock)
    money = dp[i-1]
    money -= stock * An[i-1]
    money += stock * An[i]
#     print(money)
    dp[i] = max(dp[i-1], money)
print(dp[N-1])

Submission status: Accepted

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

if a[0] <= a[1]:
    stock = money // a[0]
    money -= stock * a[0]

for i in range(1, n-1):
    if a[i] <= a[i-1] and a[i] <= a[i+1] and stock == 0:
        stock = money // a[i]
        money -= stock * a[i]
    if a[i] >= a[i-1] and a[i] >= a[i+1] and stock != 0:
        money += stock * a[i]
        stock = 0
if stock > 0:
    money += stock * a[-1]
    stock = 0
print(money)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    # 単調増加になる区間の両端で売買
    actions = ['' for _ in range(N)]
    left = 0
    for right in range(1, N):
        if A[right - 1] > A[right]:
            if left != right - 1:
                actions[left] = 'buy'
                actions[right - 1] = 'sell'
            left = right
    if left != right:
        actions[left] = 'buy'
        actions[right] = 'sell'
    money = 1000
    stock = 0
    for i, action in enumerate(actions):
        if action == 'buy':
            stock = money // A[i]
            money -= stock * A[i]
        elif action == 'sell':
            money += stock * A[i]
            stock = 0
    print(money)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

dp=[0 for i in range(N)]
dp[0]=1000

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

Submission status: Accepted

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

cash = 1000
stock = 0

for i in range(len(A)-1):
  next_trend = A[i+1] - A[i]
  price = A[i]
  
  
  if next_trend > 0 : #上がりするので最大限株へ
    n = cash//price
    cash, stock = cash - n*price, stock + n
  if next_trend < 0: #値下がりするので最大限現金化
    cash = cash + stock*price
    stock = 0
  #print(next_trend, price, cash,stock)

if stock:
  cash += stock*A[-1]
  
print(cash)

Submission status: Accepted

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

possess = 1000

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


print(possess)

Submission status: Accepted

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

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

Submission status: Accepted

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):
    kabu = dp[j]//a[j]
    money = dp[j]+(a[i]-a[j])*kabu
    dp[i] = max(dp[i],money)
print(dp[-1])