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 solve(A,N):
    A.append(0)
    money,kabu = 1000,0
    ans = money
    pt = 0
    while pt < N:
        if A[pt] < A[pt+1]:
            ans = max(ans,money)
            kabu += (money // A[pt])
            money %= A[pt]
            pt += 1
            while pt < N:
                if A[pt] > A[pt+1]:
                    money += (A[pt] * kabu)
                    kabu = 0
                    ans = money
                    pt += 1
                    break
                else:
                    pt += 1
        else:
            pt += 1

    return ans

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

    print(solve(A,N))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

y = 1000
k = 0

# 同額をSkip
B = []
for i in range(len(A)-1):
  if A[i] == A[i+1]: continue
  B.append(A[i])

# print("A=",A)
# print("B=",B)
B = A
for i in range(len(B)-1):
  b = B[i]

  if B[i] == B[i+1]: continue

  if B[i] < B[i+1]:
    # ⇒買う
    kabusu = y // b
    kakaku = kabusu * b
    y -= kakaku
    k += kabusu

    # print(i,b,y,k, "kabusu, kakaku", kabusu, kakaku)

  else:
    # ⇒売る
    urine = k * b
    y += urine
    k = 0

    # print(i,b,y,k, "urine", urine)


if k>0:
  b = B[-1]
  urine = k * b
  y += urine

print(y)

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
c = 1000
i = 0

while i < N:
    # 単調減少
    while i < (N-1) and A[i] > A[i+1]:
        i += 1
    if i == (N-1):
        break
    s = i
    # 単調増加
    while i < (N-1) and A[i] <= A[i+1]:
        i += 1
    if (i-s) > 0:
        c += (A[i]-A[s]) * (c//A[s])
        i += 1
print(c)

Submission status: Accepted

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

amt = 1000
for i in range(1, n):
    if nums[i] > nums[i-1]:
        q = amt//nums[i-1]
        amt -= q*nums[i-1]
        amt += q*nums[i]
print(amt)

Submission status: Accepted

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

stock = 0
prop = 1000
grad = -1

for i in range(n - 1):
    diff = a[i + 1] - a[i]
    if grad == -1 and diff > 0:
        trade_num = prop // a[i]
        stock = trade_num
        prop -= (trade_num * a[i])
        grad = 1
    elif grad == 1 and diff < 0:
        prop += stock * a[i]
        stock = 0
        grad = -1
if grad == 1:
    prop += stock * a[n - 1]

print(prop)

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys


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


def std_in():
    return sys.stdin.readline().strip()


def main():
    n = int(std_in())
    a = list(get_ints())
    a.append(0)
    m = 1000
    k = 0

    for i in range(n):
        if i == n:
            break
        if a[i] > a[i+1]:
            m += k * a[i]
            k = 0
        elif a[i] < a[i+1]:
            q, mod = divmod(m, a[i])
            k += q
            m = mod
    print(m)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
total=1000
z=[]
t=float("infinity")
for i in range(n):
	if len(z)==0:
		z.append(l[i])
	else:
		if z[-1]<l[i]:
			z.append(l[i])
		elif z[-1]>l[i]:
			if len(z)>1:
				f=total//z[0]
				total=total%z[0]
				total+=f*z[-1]
			z=[l[i]]
if len(z)>1:
	f=total//z[0]
	total=total%z[0]
	total+=f*z[-1]
print(total)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(X) for X in input().split()]
Stock = 0
Money = 1000
for T in range(0,N-1):
    if A[T+1]>A[T]:
        Purchase = Money//A[T]
        Stock += Purchase
        Money -= Purchase*A[T]  
    elif A[T+1]<A[T]:
        Money += Stock*A[T]
        Stock = 0
if Stock>0:
    Money += Stock*A[N-1]
    Stock = 0
print(Money)

Submission status: Accepted

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

now=1000
dp_poss=[0 for i in range(n+1)]
dp_stck=[0 for i in range(n+1)]

buy_choice=[False]*(n+1)
tmp=a[1]
for i in range(1,n):
    if tmp==a[i+1]:continue
    if tmp<a[i+1]:
        buy_choice[i]=True
    tmp=a[i+1]
buy_choice[-1]=False


# day 0
dp_poss[0]=1000

#day 1~
for i in range(1,n+1):
    if buy_choice[i]:
        afford=dp_poss[i-1]//a[i]
        dp_poss[i]=dp_poss[i-1]-afford*a[i]
        dp_stck[i]=dp_stck[i-1]+afford
    else:
        dp_poss[i]=dp_poss[i-1]+dp_stck[i-1]*a[i]

print(dp_poss[n])

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
money = 1000
sell = 0
num = 0
for i in range(N-1):
    if A[i] < A[i+1]:
        num += int(money / A[i])
        money %= A[i]
        sell = 1
    elif sell == 1:
        money += num * A[i]
        num = 0
        sell = 0
if num > 0:
    money += num*A[N-1]
print(money)

Submission status: Accepted

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

num_max = 0
num_min = 0
d_flg = False
u_flg = False

if n == 2 and a[1] > a[0]:
    b = a
else:
    for i in range(1, n-1):
        if a[i-1] <= a[i] and a[i] > a[i+1]:
            b.append(a[i])
            d_flg = False
            u_flg = True
        elif a[i-1] >= a[i] and a[i] < a[i+1]:
            b.append(a[i])
            u_flg = False
            d_flg = True

    if u_flg is True and b[-1] < a[-1]:
        b[-1] = a[-1]
    elif u_flg is True and b[-1] > a[-1]:
        b.append(a[-1])
    elif d_flg is True and b[-1] < a[-1]:
        b.append(a[-1])
    elif u_flg is True and b[-1] > a[-1]:
        b[-1] = a[-1]


if len(b) == 1:
    print(1000)
    exit()

for i in range(len(b)-1):
    if b[i] > b[i+1] and kabu > 0:
        money += kabu * b[i]
        kabu = 0

    elif b[i] < b[i+1] and money >= b[i]:
        kabu = money // b[i]
        money -= kabu * b[i]

money += kabu * b[i+1]

print(money)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
L = L + [0]
#print(L)
flag = False
up,down = [],[]
for i in range(n):
    if L[i] < L[i+1] and flag == False:
        flag =True
        up.append(i)
    elif L[i] > L[i+1] and flag == True:
        flag = False
        down.append(i)
#print(up,down)
sisan = 1000
div,buy,sell = 0,0,0
for i in range(n):
    if i in up:
        div = sisan//L[i]
        #print(div,sisan,i)
        buy = div*L[i]
        sisan -= buy
        #print(div,buy,sisan)
    elif i in down:
        sell = div*L[i]
        sisan +=sell
        #print(sell,sisan,i)
print(sisan)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cash = 1000
bill = 0
table = []

for i in range(n-1):
    if a[i] > a[i+1]:
        table.append(False)
    elif a[i] == a[i+1]:
        table.append("-")
    else:
        table.append(True)
# print(table)
if table[0] == True:
    bill, cash = divmod(cash, a[0])
# print(cash, bill)
i = 0
while i < n-1:
    if table[i] == True:
        while not table[i] == False:
            i += 1
            if i == n-1:
                break
        cash += bill * a[i]
        bill = 0
        # print(cash, bill)
    else:
        while not table[i] == True:
            i += 1
            if i == n-1:
                break
        bill, cash = divmod(cash, a[i])
        # print(cash, bill)
cash += bill * a[-1]
print(cash)

Submission status: Accepted

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

Submission status: Accepted

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

c_amount = 1000
for i in range(N-1):
    if(A[i]>=A[i+1]):
        pass
    else:
        c_amount+=(c_amount//A[i])*(A[i+1]-A[i])

print(c_amount)

Submission status: Accepted

Solution code:
N = int(input())
sum = 1000
A = list(map(int, input().split()))
pre = A[0]
greater = False
less = False
for a in A[1:]:
  if greater:
    if pre > a:
      less = True
      greater = False
      sum = sum % i + (sum // i) * pre
  elif less:
    if pre < a:
      greater = True
      less = False
      i = pre
  else:
    if pre > a:
      less = True
    elif pre < a:
      greater = True
      i = pre
  pre = a
if greater:
  sum = sum % i + (sum // i) * pre
print(sum)

Submission status: Accepted

Solution code:
import math
def main():
    N = int(input())
    A = list(map(int, input().split()))

    has_stock = False
    money = 1000
    stock = 0
    for day_i in range(N-1):
        if not has_stock:
            if A[day_i] < A[day_i + 1]:
                has_stock = True
                num = math.floor(money / A[day_i])
                stock += num
                money -= num * A[day_i]
                # print(day_i, "buy", money, stock)
        else:
            if A[day_i] > A[day_i + 1]:
                has_stock = False

                money += stock * A[day_i]
                stock = 0
                # print(day_i, "sell", money, stock)
    
    if has_stock:
        money += stock * A[N-1]
    print(money)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.append(-1)
up = []
pre = False
sw = False
for i in range(1,N+1):
    if A[i] > A[i-1]:
        sw = True
    elif A[i] < A[i-1]:
        sw = False
    if sw != pre:
        up.append(i-1)
    pre = sw
if up == []:
    print(1000)
    exit()
money = 1000
p = False
for j in up:
    if p == False:
        stock = money//A[j]
        money -= stock*A[j]
    else:
        money += stock*A[j]
        stock = 0
    p = not p
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 stock:
        if A[i] > A[i + 1]:
            money += stock * A[i]
            stock = 0
    else:
        if A[i] < A[i + 1]:
            stock, money = divmod(money, A[i])

print(money + stock * A[i+1])

Submission status: Accepted

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

stock=0
money=1000
#極小は-1、極大は1、何もしないは0
graph=[0]*n
if a[0]<a[1]:
    graph[0]=-1
    increase=1
elif a[0]==a[1]:
    increase=0
else:
    increase=-1

for i in range(1,n-1):
    #株価上昇中
    if increase==1:
        if a[i]>a[i+1]:
            graph[i]=1
            increase=-1
    #株価下落中
    if increase==-1:
        if a[i]<a[i+1]:
            graph[i]=-1
            increase=1
    #横ばい
    if increase==0:
        if a[i]<a[i+1]:
            graph[i]=-1
            increase=1
        if a[i]>a[i+1]:
            graph[i]=1
            increase=-1

if a[n-2]<a[n-1]:
    graph[n-1]=1

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

Submission status: Accepted

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

money = 1000
stock = 0

diff = ['-'] * n
for i in range(1, n):
    if a[i] < a[i - 1]:
        diff[i] = '-'
    elif a[i] > a[i - 1]:
        diff[i] = '+'
    elif a[i] == a[i - 1]:
        diff[i] = diff[i - 1]

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

print(money)

Submission status: Accepted

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

up_down_list = []
init = True
for i in range(len(l)):
    
    ai = l[i] 
    
    if init:
        up_down_list.append(-2)
        tmp_ai = ai
        init = False
        continue
        

    
    if ai > tmp_ai:
        up_down_list.append(1)
        
    elif ai == tmp_ai:
        up_down_list.append(0)
        
    else:
        up_down_list.append(-1)
    
    
    tmp_ai = ai
    
uri = []
kai = []

s_flag = False
for n in range(len(up_down_list)):
    
    inv_n = len(up_down_list) - (n + 1)
    
    if s_flag == False:
        if up_down_list[inv_n] == 1:
            s_flag = True
            uri.append(inv_n)
            continue
    else:
        
        if up_down_list[inv_n] < 0:
            
            kai.append(inv_n)
            s_flag = False
        
        
        

s = 0
yen = 1000

for k in range(len(kai)):
    
    inv_k = len(kai) - (k+1)
    
    kaine = l[kai[inv_k]]
    urine = l[uri[inv_k]]
    
    s=int(yen / kaine)
    yen = yen - kaine * s
    
    yen = yen + urine * s

print(yen)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
A.append(0)
f = [0] * N
for i in range(N):
    if A[i] < A[i+1]:
        f[i] = 1
    elif A[i] > A[i+1]:
        f[i] = 2
cnt = 1000
stock = 0
prev = 0
for i in range(N):
    if stock == 0 and prev != 1 and f[i] == 1:
        stock = cnt // A[i]
        cnt %= A[i]
    elif prev != 2 and f[i] == 2:
        cnt += stock * A[i]
        stock = 0
    prev = f[i]
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
rate = list(map(lambda x:int(x), input().split(' ')))
isGrowing = False
money = 1000
upstart = None
for i in range(N-1):
    if not isGrowing and rate[i] < rate[i+1]:
        isGrowing = True
        upstart = i
    elif isGrowing and rate[i] > rate[i+1] and upstart is not None:
        money = (money//rate[upstart])*rate[i] + money%rate[upstart]
        upstart = i+1
        isGrowing = False
if isGrowing:
    money = (money//rate[upstart])*rate[N-1] + money%rate[upstart]
print(money)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

    kai = set()
    uri = set()
    i = 1
    while i < n:
        if A[i - 1] < A[i]:
            kai.add(i - 1)
            while i < n and A[i - 1] <= A[i]:
                i += 1
            uri.add(i - 1)
            while i < n and A[i - 1] >= A[i]:
                i += 1
        else:
            i += 1

    if len(kai) == 0:
        print(1000)
        exit()

    money = 1000
    kabu = 0
    for i in range(n):
        if i in kai:
            if i == n - 1:
                continue
            kabu, money = divmod(money, A[i])
        elif i in uri:
            money += kabu * A[i]
            kabu = 0
    print(money)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys
k=1000
N=int(input())
L=list(map(int,input().split()))
i=0
while i<N-1:
  if L[i]<L[i+1]:
    n=L[i]
    break
  i+=1
  if i==N-1:
    print(1000)
    sys.exit()
n=0
for j in range(i,N):
  if j==N-1 and n!=0:
    m=max(L[s:])
    c=k%n
    k=((k//n)*m)+c
    n=0
    break
  if j==N-1 and n==0:
    break
  if L[j]<L[j+1] and n==0:
    n=L[j]
    s=j
  if L[j]>L[j+1] and n!=0:
    m=L[j]
    c=k%n
    k=((k//n)*m)+c
    n=0
print(k)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
start = n-1
for i in range(n-1):
  if a[i]<a[i+1]:
    start = i
    break
#print(start)
money = 1000
kabu = 0
l = [a[start]]
high = 1
for i in range(start+1,n-1):
  if high==1:
    if a[i]<a[i+1]:
      continue
    else:
      l.append(a[i])
      high = 0
  else:
    if a[i]>a[i+1]:
      continue
    else:
      l.append(a[i])
      high = 1
if high==1 and a[-2]<a[-1]:
  l.append(a[-1])
if high==0 and a[-2]>a[-1]:
  l.append(a[-1])
#print(l)
if len(l)%2==0:
  for i in range(len(l)):
    if i%2==0:
      kabu += money//l[i]
      money -= (money//l[i])*l[i]
    else:
      money += kabu*l[i]
      kabu = 0
   # print(money,kabu)
  print(money)
else:
  l.pop()
  for i in range(len(l)):
    if i%2==0:
      kabu += money//l[i]
      money -= (money//l[i])*l[i]
    else:
      money += kabu*l[i]
      kabu = 0
    #print(money,kabu)
  print(money)

Submission status: Accepted

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

for i in range(n):
    if i == n-1:
        if s:
            m += A[i] * s
            s = 0
            break
        else:
            break

    if A[i] < A[i+1] and m > A[i]:
        s = m // A[i]
        m -= A[i] * s
    elif A[i] > A[i+1]:
        if s:
            m += A[i] * s
            s = 0
        else:
            continue

print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    c = 1000
    s = 0

    for i in range(0, n-1):
        j = i + 1  # 比較対象(翌日)
        diff = A[j] - A[i]
        # 次にup 今持っている現金を全て株にする
        if diff > 0:
            s += c // A[i]
            c = c % A[i]
        # 次にdown 今持っている株を全て現金にする
        elif diff < 0:
            c += s * A[i]
            s = 0
        #print(i, s, c)

    # 最後に株を全て現金に戻す
    c += s * A[n-1]

    print(c)


if __name__ == "__main__":
    resolve()

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    money = 1000
    kabu = 0
    
    flag = ''
    for n in range(N):
        if A[0] > A[n]:
            flag = 'down'
            break
        elif A[0] < A[n]:
            flag = 'up'
            kabu = money//A[0]
            money -= A[0]*kabu
            break
        
    if flag == '':
        print(money)
    else:
        limit = 0
        for n in reversed(range(1,N)):
            if A[n-1] < A[n]:
                limit = n
                break
                
        if limit == 0:
            print(money)
        else:
            for n in range(1,N+1):
                if n == limit+1:
                    money += A[n-1]*kabu
                    break
                else:
                    if flag == 'down':
                        if A[n-1] < A[n]:
                            kabu = money//A[n-1]
                            money -= A[n-1]*kabu
                            flag = 'up'
                    elif flag == 'up':
                        if A[n-1] > A[n]:
                            money += A[n-1]*kabu
                            flag = 'down'
            print(money)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
import itertools
#from scipy import signal


n = int(input())
ajs = input().split()
ajs = [int(aj) for aj in ajs]
ajs = np.array(ajs)

#actions = itertools.product([-1, 0, 1], repeat=n)
#for action in actions:

ajs_delta = ajs[1:] - ajs[:-1]

for i in range(1, ajs_delta.shape[0]):
  if ajs_delta[i] == 0:
    ajs_delta[i] = ajs_delta[i-1]
    

  
kyokuchi = {'lower':[], 'upper':[]}
#maxId = signal.argrelmax(ajs)

first_value = 0
for aj_delta in ajs_delta:
  if aj_delta != 0:
    first_value = aj_delta
    break

if ajs_delta[0] > 0:
  kyokuchi['lower'].append(0)
elif ajs_delta[0] < 0:
  #kyokuchi['upper'].append(0)
  pass
elif ajs_delta[0]  == 0 and first_value > 0:
  kyokuchi['lower'].append(0)

for i in range(0, ajs_delta.shape[0]-1):
  if ajs_delta[i] * ajs_delta[i+1] < 0:
    if ajs_delta[i] > 0:
      kyokuchi['upper'].append(i+1)
    else:
      kyokuchi['lower'].append(i+1)

if ajs_delta[-1] > 0:
  kyokuchi['upper'].append(n-1)
else:
  #kyokuchi['lower'].append(n-1)
  pass


kyokuchi_s = sorted(kyokuchi['upper'] + kyokuchi['lower'])

current_money = 1000
pos = 0

for idx in kyokuchi_s:
  if pos >= 0:#買い
    stock = int(current_money / ajs[idx])
    current_money = current_money % ajs[idx]
    pos = -1
  else:
    current_money += stock * ajs[idx]
    stock = 0
    pos = 1
    
print(current_money)

Submission status: Accepted

Solution code:
n=int(input())
m=list(map(int,input().split()))
a=[]
if m[0]<m[1]:
    a.append([0,-1])
for i in range(1,n-1):
    if m[i]>m[i-1] and m[i]>=m[i+1]:
        a.append([i,1])
    elif m[i]<=m[i-1] and m[i]<m[i+1]:
        a.append([i,-1])
    elif m[i]<m[i-1] and m[i]<=m[i+1]:
        a.append([i,-1])
    elif m[i]>=m[i-1] and m[i]>m[i+1]:
        a.append([i,1])
if m[n-1]>m[n-2]:
    a.append([n-1,1])
#print(a)
k=0
r=1000
t=0
for i in range(len(a)):
    if a[i][1]==-1 and t==0:
        k=r//m[a[i][0]]
        r-=k*m[a[i][0]]
        t=1
    elif a[i][1]==1 and t==1:
        
        
        r+=k*m[a[i][0]]
        k=0
        t=0
        
    #print(k,r)
print(r)

Submission status: Accepted

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

future = []

for i in range(n-1):
  if a[i] < a[i+1]:
    future.append("u")
  elif a[i] == a[i+1]:
    future.append("e")
  else:
    future.append("d")

try:
  last_u = future[::-1].index("u")
except ValueError:
  print(1000)
else:
  last_u = n - last_u - 1
  for i in range(last_u):
    if future[i] == "u":
      if kabu != 0:
        ans += kabu * a[i]
        kabu = 0
      kabu = ans // a[i]
      ans -= kabu * a[i]
    if future[i] == "d":
      if kabu != 0:
        ans += kabu * a[i]
        kabu = 0
  if kabu != 0:
    ans += kabu * a[last_u]
  print(ans)

Submission status: Accepted

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

if l[n-1]>buy_price and stock:
    money+=stock*l[n-1]

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]:
        buy = money // a[i]
        money -= buy * a[i]
        stock += buy
    if a[i] > a[i + 1]:
        money += stock * a[i]
        stock = 0
money += stock * a[n - 1]
print(money)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
@author: H_Hoshigi
"""
def main():
    N = int(input())
    A_lsit = list(map(int, input().split()))

    DP_list = [1000]*N

    for i in range(N):
        bought_stock_number = DP_list[i]//A_lsit[i]
        remained_monye = DP_list[i]%A_lsit[i]
        max_i = DP_list[i]
        for j in range(i+1, N):
            DP_list[j] = max(
                max_i,
                DP_list[j],
                bought_stock_number*A_lsit[j] + remained_monye
            )
    print(DP_list[-1])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=1000
for i in range(1,N):
  if A[i]>A[i-1]:
    Key=ans//A[i-1]
    key=ans%A[i-1]
    Ans=Key*A[i]+key
    ans=Ans
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque


def run():
    N, *A = map(int, read().split())
    buy = True
    money = 1000
    stock = 0
    for i, a in enumerate(A[:-1]):
        if buy:
            if a < A[i+1]:
                stock += money // a
                money %= a
                buy = False
        else:
            if a > A[i+1]:
                money += stock * a
                stock = 0
                buy = True
    money += stock * A[-1]
    print(money)



if __name__ == "__main__":
    run()

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+1] > A[i]:
        if money >= A[i]:
            n = money // A[i]
            stock += n
            money -= A[i] * n
    elif A[i+1] == A[i]:
        continue
    else:
        if stock > 1:
            money += stock * A[i]
            stock = 0

if stock > 0:
    money += stock * A[-1]

print(money)

Submission status: Accepted

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

money = 1000
kabu, comp = 0, 0
for cnt, i in enumerate(a):
    comp = a[cnt+1]-a[cnt] if cnt+1<len(a) else -1
    if kabu == 0 and comp > 0:
        kabu = money // i
        money -= kabu*i
    elif kabu > 0 and comp < 0:
        money += kabu*i
        kabu = 0

print(money)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
c = 1000
s = 0
l = len(A)
for i, a in enumerate(A):
    if i < l - 1:
        if a == A[i+1]:
            continue
        elif a < A[i+1]:
            if c >= a:
                ss = c // a
                s += ss
                c -= ss * a
        else:
            if s > 0:
                c += s * a
                s = 0
    else:
        if s > 0:
            c += s * a
            s = 0
print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]

ans = []

now = a[0]
flag = 'u'

for i in range(n-1):
    if flag == 'u':
        if now < a[i+1]:
            ans.append(a[i])
            if i == n-2:
                #print(i)
                ans.append(a[i+1])
                break
            #print(i)
            flag = 'd'
        now = a[i+1]
        
    else:
        if now > a[i+1]:
            ans.append(now)

            #print(i)
            flag = 'u'
        now = a[i+1]

if flag == 'd':
    ans.append(a[n-1])

okane = 1000
#print(ans)
for i in range(len(ans)//2):
    okane += (okane // ans[2*i])*(ans[2*i+1]-ans[2*i])
    #print(okane)
    #okane -= kosu * ans[i]
print(okane)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
s=0
m=1000
for i in range(0,n-1):
    #print(i)
    if(a[i]>a[i+1] and s>0):
        m=m+s*a[i]
        s=0
    elif(a[i]<a[i+1] and m>=a[i]):
        s=m//a[i]
        m=m%a[i]
if(s>0):
    m=m+s*a[n-1]
print(m)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
#dp1 = [[0, 0] for i in range(n)]
#dp2 = [[0, 0] for i in range(n)]
s=[0]*n
m=[0]*n

if a[1]>a[0]:
  s[0]=1000//a[0]
  m[0]=1000-a[0]*s[0]
else:
  s[0]=0
  m[0]=1000

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

m[n-1]=m[n-2]+a[n-1]*s[n-2]
print(m[n-1])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
c = [0]*n
cash = 1000
stock = 0
#1:底 2:頂点 0:どちらでもない

for i in range(1,n):
    if a[i-1]<a[i]:
        b[i]='+'
    elif a[i-1]>a[i]:
        b[i]='-'
    elif a[i-1]==a[i]:
        b[i]=b[i-1]

if b[1]=='+':
    c[0]=1
elif b[1]=='-':
    c[0]=2

for i in range(1,n-1):
    if b[i]=='+' and b[i+1]=='-':
        c[i]=2
    elif b[i]=='-' and b[i+1]=='+':
        c[i]=1

if b[n-1]=='+':
    c[n-1]=2
elif b[n-1]=='-':
    c[n-1]=1
last = n
ch = 0
for i in range(n):
    if c[n-1-i]==1:
        ch = 1
    elif c[n-1-i]==2 and ch==0:
        break
    elif c[n-1-i]==2 and ch==1:
        last = n-i
        break
    elif ch == 1 and i==n-1:
        last=0
        break

if c[0]==0:
    for i in range(1,n):
        if c[i]==2:
            c[0]=1
            break

for i in range(last):
    if c[i]==1:
        stock += cash//a[i]
        cash -= cash//a[i]*a[i]
    if c[i]==2:
        cash += stock*a[i]
        stock =0
print(cash)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
P = ['n'] * N
s = 1
if A[N-1] > A[N-2]:
    P[N-1] = 'sell'
    s = 0
for i in range(N-2):
    if s == 1:
        if A[N-i-2] > A[N-i-3]:
            P[N-i-2] = 'sell'
            s = 0
    else:
        if A[N-i-2] < A[N-i-3]:
            s = 1
            P[N-i-2] = 'buy'
if s == 0:
    P[0] = 'buy'
    s = 1
else:
    P[0] = 'n'

m = 1000
stock = 0
    
for i in range(N):
    if s == 1:
        if P[i] == 'buy':
            stock = m // A[i]
            m = m % A[i]
            s = 0
    else:
        if P[i] == 'sell':
            m += stock*A[i]
            stock = 0
            s = 1
print(m)