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.

Solution (Python):
import sys

n = int(input())
a_list = list(map(int, input().split()))
trade_list = []
prev = "+"
money = 1000
kabu = 0

while a_list[len(a_list) - 1] <= a_list[len(a_list) - 2]:
    a_list.pop()
    if len(a_list) == 1:
        print(1000)
        sys.exit()

while a_list[0] >= a_list[1]:
    a_list.pop(0)
    if len(a_list) == 1:
        print(1000)
        sys.exit()

trade_list.append(a_list[0])




for i in range(len(a_list) - 1):
    if a_list[i] > a_list[i + 1]:
        now = "-"
    elif a_list[i] < a_list[i + 1]:
        now = "+"
    else:
        now = prev

    if prev != now:
        trade_list.append(a_list[i])
    prev = now

trade_list.append(a_list[-1])

for i in range(len(trade_list)):
    
    if i % 2 == 0:
        kabu = money // trade_list[i]
        money -= kabu * trade_list[i]
    else:
        money += kabu * trade_list[i]

print(money)

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

fp = A[0]
tmp = 0
c = 1000
stock = 0
IsSearchTop = False
first = True
for i in range(1,N):
    if first:
        if fp > A[i]:
            fp = A[i]
        elif fp < A[i]:
            stock += c // fp
            c -= stock * fp
            IsSearchTop = True
            first = False
            fp = A[i]
            if i == N - 1:
                c += stock * fp
    else:
        if IsSearchTop:
            if fp <= A[i]:
                fp = A[i]
                if i == N-1:
                    c += stock * fp
            elif fp > A[i]:
                c += stock * fp
                stock = 0
                fp = A[i]
                IsSearchTop = False
        else:
            if fp >= A[i]:
                fp = A[i]
            elif fp < A[i]:
                stock += c // fp
                c -= stock * fp
                IsSearchTop = True
                fp = A[i]
                if i == N - 1:
                    c += stock * fp
print(c)

Solution (Python):
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]:
        stock += money//A[i]
        money %= A[i]
        money += stock*A[i+1]
        stock = 0
print(money)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
At_list = [A_list[0]]
D_list = list()
Dt_list = list()
mone = 1000
stoc = 0
for i in range(1,len(A_list)):
    D_list.append(A_list[i]-A_list[i-1])
for h in range(len(D_list)):
    if D_list[h] != 0:
        Dt_list.append(D_list[h])
        At_list.append(A_list[h + 1])
for j in range(len(Dt_list)):
    if Dt_list[j] > 0:
        stoc = stoc + mone // At_list[j]
        mone = mone % At_list[j]
    elif Dt_list[j] < 0:
        mone = mone + stoc * At_list[j]
        stoc = 0
        last_sell = j
if len(Dt_list) != 0:
    if At_list[-1] >= At_list[-2]:
        mone = mone + stoc * At_list[-1]
    else:
        mone = mone + stoc * At_list[last_sell]
else:
    mone = 1000
print(mone)

Solution (Python):
def INT():
    return int(input())

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

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

N = INT()
A = LI()
ans = 1000

for i in range(1, N):
    tmp = ans
    if A[i] > A[i - 1]:
        purchase = tmp // A[i - 1]
        ans = max(ans, tmp + (A[i] - A[i - 1]) * purchase)
        
print(ans)

Solution (Python):

import sys

input = sys.stdin.readline

def main(n, lst):
    peaks = []
    minn = float("inf")
    maxx = float("-inf")
    money = 1000
    for i in lst:
        if i > maxx:
            if maxx != float("-inf"):
                times, rem = divmod(money, maxx)
                money = i * times + rem
                maxx = i
            else:
                maxx = i
        else:
            maxx = i
    return money
            
n = int(input())
lst = list(map(int, input().split()))
print(main(n, lst))

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
M = 1000

cb = -1
for i in range(N):
    cv = A[i]
    if cb == -1 and i + 1 < N:
        nv = A[i+1]
        if nv > cv:
            cb = cv
            continue
    
    if cb >= 0:
        if i == N-1:
            M += (cv - cb) * (M // cb)
            break
        nv = A[i+1]
        if nv < cv:
            M += (cv - cb) * (M // cb)
            cb = -1
print(M)

Solution (Python):
n = int(input())
lsa = list(map(int,input().split()))
ls = []

for i in range(n - 1):
    if lsa[i] < lsa[i + 1]:
        ls.append("up")
    elif lsa[i] == lsa[i + 1]:
        ls.append("keep")
    else:
        ls.append("down")

lssb = []
for i in range(n - 1):
    if i == 0:
        if ls[0] == "up":
            lssb.append(1)
        else:
            lssb.append(0)
    else:
        if ls[i - 1] == "up":
            if ls[i] == "down":
                lssb.append(2)
            else:
                lssb.append(0)
        elif ls[i - 1] == "keep":
            if ls[i] == "up":
                lssb.append(1)
            elif ls[i] == "down":
                lssb.append(2)
            else:
                lssb.append(0)
        else:
            if ls[i] == "up":
                lssb.append(1)
            else:
                lssb.append(0)
lssb.append(2)

money = 1000
kabu = 0

for i in range(n):
    if lssb[i] == 1:
        kabu += money // lsa[i]
        money = money % lsa[i]
    elif lssb[i] == 2:
        money += kabu * lsa[i]
        kabu = 0

print(money)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
dp = [ 0 for n in range(N)]
dp[0] = 1000

for n in range(1, N):
    mymax = dp[n-1]
    for nn in range(n):
        mymoney = dp[nn] - (dp[nn]//A_list[nn] * A_list[nn]) + dp[nn]//A_list[nn]*A_list[n]
        if mymax < mymoney:
            mymax = mymoney
    dp[n] = mymax
print(dp[-1])

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  

N = I()
B = [0] + LI()

A = [0,B[1]]
for i in range(1,N):
    if B[i] != B[i+1]:
        A.append(B[i+1])

N = len(A)-1

MAX = [0,1000] + [0]*N

from functools import lru_cache

@lru_cache(maxsize=None)
def dfs(n,money):
    if n == N+1:
        return money
    else:
        res = money
        for i in range(n,N):  
            for j in range(i+1,N+1):  
                if A[i] == min(A[i:j+1]) and A[j] == max(A[i:j+1]) and A[i] < A[j]:
                    if money+(A[j]-A[i])*(money//A[i]) > MAX[j+1]:
                        res = max(res,dfs(j+1,money+(A[j]-A[i])*(money//A[i])))
                        MAX[j+1] = money+(A[j]-A[i])*(money//A[i])
        return res

print(dfs(1,1000))

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
kabu=0
ans=1000
for i in range(N-1):
    if A[i] > A[i+1]:
        ans=ans+kabu*A[i]
        kabu=0

    if A[i] < A[i+1] and ans >= A[i]:
        kabu=ans // A[i]
        ans=ans % A[i]

ans=ans+kabu*A[N-1]

print(ans)

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

new_a = [a[0]]

now = a[0]
for i in range(1, n):
  if(now != a[i]):
    new_a.append(a[i])
    now = a[i]
    
n = len(new_a)

if(n == 1):
  print(1000)
  exit()

top = ["" for _ in range(n)]
if(new_a[0] < new_a[1]):
  top[0] = "l"
  top[1] = "h"
else:
  top[0] = "h"
  top[1] = "l"
  
for i in range(1, n):
  if(new_a[i-1] < new_a[i]):
    if(top[i-1] == "l"):
      top[i] = "h"
    else:
      top[i-1] = "n"
      top[i] = "h"
  else:
    if(top[i-1] == "l"):
      top[i-1] = "n"
      top[i] = "l"
    else:
      top[i] = "l"

mony = 1000
kabu = 0
for i in range(n):
  if(top[i] == "l"):
    if(i == n-1):
      break
    kabu_now = mony//new_a[i]
    mony -= new_a[i]*kabu_now
    kabu += kabu_now
    
  elif(top[i] == "h"):
    kabu_now = kabu
    mony += kabu_now*new_a[i]
    kabu -= kabu_now
    
print(mony)

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split())) + [-1]

money = 1000
k = 0

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

print(money)

Solution (Python):
import  heapq
n=int(input())
aa=list(map(int,input().split()))
count=1
have=1000
l=[aa[0]]
ll=[-aa[0]]
heapq.heapify(l)
heapq.heapify(ll)
while count<n:
  mx=-heapq.heappop(ll)
  mn=heapq.heappop(l)
  if mx<=aa[count]:
    heapq.heappush(ll,-aa[count])
    heapq.heappush(l,aa[count])
    heapq.heappush(ll,-mn)
    heapq.heappush(l,mn)
    heapq.heappush(ll,-mx)
    heapq.heappush(l,mx)
    
  else:
    have+=(mx-mn)*int(have/mn)
    
    if count<=n-1:
      l=[aa[count]]
      ll=[-aa[count]]
      heapq.heapify(l)
      heapq.heapify(ll)
  count+=1
if len(l)!=0:
  mx=-heapq.heappop(ll)
  mn=heapq.heappop(l)
  have+=(mx-mn)*int(have/mn)
  
  
print(have)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
a = [int(_) for _ in list(input().split())]
a.append(0)
diff = []
for i in range(len(a)-1):
  diff.append(a[i]-a[i+1])





flag = 'buy'
money = 1000
stock = 0
for i in range(n):
  
  if (flag=='buy') & (diff[i]<0):
    stock = money//a[i]
    money -= money//a[i]*a[i]
    flag = 'sell'
    continue
  elif (flag=='sell') & (diff[i]>0):
    money += stock*a[i]
    stock = 0
    flag = 'buy'
    continue
  else:
    continue
print(money)

Solution (Python):
N, = map(int, input().split())
X = list(map(int, input().split()))
X.append(0)
S = 1000
st = 0
for i in range(N):
    if X[i+1] > X[i]:
        d, S = divmod(S, X[i])
        st += d
    else:
        S += st * X[i]
        st = 0
print(S)

Solution (Python):
N = int(input())
money = 1000
line = list(map(int, input().split()))
prices = []
increaseFlag = False
decreaseFlag = False
upFlag = False
for cnt, val in enumerate(line):
    if cnt == 0:
        prices.append(val)
    elif prices[-1] == val:
        continue
    elif prices[-1] > val:
        increaseFlag = False
        if len(prices) == 1:
            prices[-1] = val
        else:
            if decreaseFlag:
                prices[-1] = val
            else:
                prices.append(val)
                decreaseFlag = True
    elif prices[-1] < val:
        decreaseFlag = False
        upFlag = True
        if increaseFlag:
            prices[-1] = val
        else:
            prices.append(val)
            increaseFlag = True
if len(prices) == 1:
    print(1000)
elif not upFlag:
    print(1000)
else:
    kabuNum = 0
    leng = len(prices) - (len(prices) % 2)
    for i in range(leng):
        if i % 2 == 0:
            kabuNum = money // prices[i]
            money -= kabuNum * prices[i]
        else:
            money += kabuNum * prices[i]
    print(money)

Solution (Python):
n = int(input())
ary = list(map(int,input().split()))
money = 1000
kabu = 0

for i in range(n-1):
    if ary[i] < ary[i+1] and money>ary[i]:
        kabu += money//ary[i]
        money -= kabu*ary[i]
    elif ary[i] > ary[i+1]:
        money += kabu*ary[i]
        kabu = 0

print(money+kabu*ary[n-1])

Solution (Python):
N = int(input())
A = input().split()
A = [int(A[i]) for i in range(len(A))]
ans = 1000
kabu = 0
for i in range(N):
    
    if i == N-1:
        ans += kabu*A[i]
        kabu = 0
    else:
        if A[i] < A[i+1]:
            _kabu = ans // A[i]
            ans += -A[i]*_kabu
            kabu += _kabu
        elif A[i] > A[i+1]:
            ans += kabu*A[i]
            kabu = 0
print(ans)

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

for i in range(n):
    if a[0]<a[i]:
        up=True
        nin=a[0]
        break
    elif a[0]>a[i]:
        up=False
        nin=a[i]
        break
    if i==n-1:
        print(1000)
        exit()

total=1000

for i in range(1,n-1):
    if up:
        if a[i-1]<=a[i] and a[i]>a[i+1]:
            total+=(total//nin)*(a[i]-nin)
            up=False
    else:
        if a[i-1]>=a[i] and a[i]<a[i+1]:
            nin=a[i]
            up=True
if nin<a[n-1]:
    total+=(total//nin)*(a[n-1]-nin)
print(total)

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

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

C = 1000
S = 0

for i in range(N-1):
    if A[i] < A[i+1] and C >= A[i+1]:
        S = C // A[i]
        C = C % A[i]
    elif A[i] > A[i+1] and S > 0:
        C += A[i]*S
        S = 0

if S > 0:
    C += S*A[-1]

print(C)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
money=1000
k=0
row=0
high=0

for i in range(N-1):
  if row==0 and A[i]<A[i+1]:
    row=min(A[k:i+1])
    k=i+1
  if A[i]>A[i+1]:
    high=max(A[k:i+1])
    k=i+1
    if row==0:
      continue
    div,mod=divmod(money,row)
    money=div*high+mod
    row=0
  if i==N-2:
    high=max(A[k:])
    if row==0:
      continue
    div,mod=divmod(money,row)
    money=div*high+mod
print(money)

Solution (Python):
import sys

def input():
    return sys.stdin.readline()[:-1]

class M_Kun:
    def __init__(self):
        self.money = 1000
        self.stock = 0
        
    def buy(self, value):
        num = self.money // value
        self.money -= value * num
        self.stock += num
        
    def sell(self, value):
        self.money += value * self.stock
        self.stock = 0


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

    local_min = [False]*N
    local_max = [False]*N

    is_ascending = False
    for a in A[1:]:
        if a > A[0]:
            is_ascending = True
            break
        elif a < A[0]:
            break

    if is_ascending:
        local_min[0] = True
    else:
        local_max[0] = True

    for i in range(N-1):
        if A[i] == A[i+1]:
            continue
        elif is_ascending and A[i] > A[i+1]:
            local_max[i] = True
            is_ascending = False
        elif (not is_ascending) and A[i] < A[i+1]:
            local_min[i] = True
            is_ascending = True

    if is_ascending:
        local_max[-1] = True


    m_kun = M_Kun()

    for i in range(N):
        if local_min[i]:
            m_kun.buy(A[i])
        if local_max[i]:
            m_kun.sell(A[i])

    print(m_kun.money)
    
main()

Solution (Python):
n,*l=map(int,open(0).read().split())
a,c=1000,0
for i in range(n-1):
  if l[i]>l[i+1]: a+=c*l[i]; c=0
  if l[i]<l[i+1]:
    t=a//l[i]; a-=t*l[i]; c+=t
print(a+c*l[n-1])

Solution (Python):
N = int(input())
A = list(map(int, input().strip().split()))
day=0
money=1000
stock=0
is_up=True
while day<N :
    if is_up :
        past_day=day
        while A[day]<=A[day+1]:
            day+=1
            if day==N-1:
                break
        stock=int(money/A[past_day])
        money+=stock*(A[day]-A[past_day])
        day+=1
        if day==N-1:
            break
        is_up=False
    else :
        while A[day]>=A[day+1] :
            day+=1
            if day==N-1:
                day+=1
                break
        is_up=True


print("{}".format(money))

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

now = 1000
kabu = 0

li = "-"

no = a[0]

for i in range(1,n):
    if a[i] <= no:
        li += "-"
        no = a[i]
    else:
        li += "+"
        no = a[i]

li += "-"

for i in range(n):
    
    if li[i] == "-" and li[i+1] == "+":
        kabu += now//a[i]
        now -= a[i]*(now//a[i])
    elif li[i] == "+" and li[i+1] == "-":
        now += kabu*a[i]
        kabu = 0

print(now)

Solution (Python):
n=int(input())
a=list(map(int, input().split())) 
m = 1000
tmp = 999
kb = 0

for i in range(n):
    if tmp > a[i]:
        kb = int(m / a[i])
        d = m - kb * a[i]
        tmp = a[i]
    elif tmp < a[i] and kb != 0:
        m = kb * a[i] + d
        tmp = a[i]
        if i < n - 1:
            if a[i] > a[i+1]:
                kb = 0

print(m)

Solution (Python):
N=int(input())
A=[int(s) for s in input().split()]
dp=[0]*N
dp[0]=1000
for i in range(1,N):
    dp[i]=max(dp[i-1],(dp[i-1]//A[i-1])*A[i]+dp[i-1]%A[i-1])
print(dp[N-1])

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int, readline().split()))

s = 0
e = N - 1
buy = 0
m = 1000

while s +1 < N and A[s] >= A[s+1]:
    s += 1
while e-1>=0 and A[e-1]>=A[e]:
    e -= 1
    
if s >= e:
    print(m)
else:
    while s < e: 
        i = s
        t = m//A[s]
        while i+1<N and A[i]<=A[i+1]:
            i += 1
        m += t*(-A[s] + A[i])
        s=i
        while s+1<N and A[s]>=A[s+1]:
            s+=1
    print(m)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]

action = [0] * N
money = 1000
kabu = []


def state(a, b):
    return 'up' if a - b >= 0 else 'down'


def buy(i):
    global money
    kabu.append([A[i], (money // A[i])])
    money = money - ((money // A[i]) * A[i])
    kabu.append([A[i], (money // A[i])])


def sell(i):
    global kabu
    global money
    if not kabu:
        buy(0)
    for value, count in kabu:
        money += A[i] * count
    kabu = []


st0 = state(A[1], A[0])
if st0 == 'up':
    buy(0)

for i in range(1, N - 1):
    st1 = state(A[i + 1], A[i])
    if st0 != st1:
        
        if st1 == 'up':
            buy(i)
        else:
            sell(i)
    st0 = st1

if kabu:
    sell(N - 1)


print(money)

Solution (Python):
import sys
N = int(input())
Ai = list(map(int, input().split()))

lastIdx = -1

m = 1000
k = 0

for j in range(N - 1, 0, -1):
    if Ai[j] > Ai[j - 1]:
        lastIdx = j
        break

if lastIdx == -1:
    print(1000)
    sys.exit()

def buy(i):
    global m, k
    rate = Ai[i]

    ki = m // rate
    k += ki
    m -= ki * rate
    

def sell(i):
    global m, k
    rate = Ai[i]

    m += (rate * k)
    k = 0
    


buy(0)

is_buy = False
for i in range(0, lastIdx):
    if is_buy:
        if Ai[i] >= Ai[i + 1]:
            continue
        else:
            buy(i)
            is_buy = False
    else:
        if Ai[i] <= Ai[i + 1]:
            continue
        else:
            sell(i)
            is_buy = True
sell(lastIdx)
print(m)

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

    money = 1000

    stock = 0
    i = 0
    while i < len(a):
        next_sell_value = a[i]
        next_sell = i
        buy_stock = False
        for j in range(i+1, len(a)):
            
            
            if next_sell_value < a[j]:
                next_sell_value = a[j]
                next_sell = j
                buy_stock = True
                
            if next_sell_value > a[j]:
                
                break
        else:
            break
        
        

        if buy_stock:
            stock = money // a[i]
            money += stock * (a[next_sell] - a[i])

        i = j
    
    print(money)




    
    return


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    A = list(map(int,input().split()))

    l = []
    s = 1000
    flg = 1
    b = A[0]
    i = 1
    while i < n:
        if flg == 1:
            if b >= A[i]:
                b = A[i]
                i += 1
            elif b < A[i]:
                l.append(b)
                b = A[i]
                flg = 0
        
        elif flg == 0:
            if b <= A[i]:
                b = A[i]
                i += 1
                if i == n:
                    l.append(b)
            elif b > A[i]:
                l.append(b)
                b = A[i]
                flg = 1
    
    if len(l)%2 != 0:
        exit(1)
    for i,j in enumerate(l):
        if i%2 == 0:
            t = s//j
            s -= t*j
        else:
            s += t*j
            t = 0

    print(s)

main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
X = [A[0]]
for i in range(1, N):
  if A[i] != A[i-1]:
    X.append(A[i])
S = []
n = len(X)
if n==1:
  print(1000)
else:
  status = 0
  result = 1000
  now_m = 1000
  k = 0
  if X[0]<X[1]:
    S.append(0)
  for i in range(1, n-1):
    if (X[i-1]<X[i] and X[i]>X[i+1]) or (X[i-1]>X[i] and X[i]<X[i+1]):
      S.append(i)
  if X[n-2]<X[n-1]:
    S.append(n-1)
  for s in S:
    if status==0:
        k = now_m//X[s]
        result = max(result, now_m)
        now_m -= k*X[s]
        status = 1
    else:
        now_m += k*X[s]
        result = max(result, now_m)
        k = 0
        status = 0
  print(result)

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

ans = 1000
diff = [0 for _ in range(N)]

for i in range(1, N):
    if A[i] == A[i - 1]:  diff[i] = 0
    elif A[i] > A[i - 1]: diff[i] = 1
    else:  diff[i] = -1

money = 1000
kabu = 0

for i in range(1, N):
    if kabu == 0:
        if diff[i] == 1:
            kabu = money // A[i - 1]
            money -= A[i - 1] * kabu
    else:
        if diff[i] == -1:
            money += kabu * A[i - 1]
            kabu = 0
    
    ans = max(ans, money)

money += kabu * A[-1]
ans = max(ans, money)

print(ans)

Solution (Python):
def main():
    input()
    array = [int(x) for x in input().split()]
    stocks = 0
    money = 1000

    for day, current_price in enumerate(array):
        lower = False
        higher = False
        for future_day, future_price in enumerate(array[day:]):
            if future_price > current_price:
                higher = True
                
                break
            if future_price < current_price:
                lower = True
        if lower:
            
            
            money += current_price * stocks
            stocks = 0
        elif higher:  
            
            adding, mod = divmod(money, current_price)
            money = mod
            stocks += adding
    money += current_price * stocks
    print(money)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a_=[]
for i in range(n):
  if i==0:
    if a[i]<a[i+1]:
      a_.append(-1)
    elif a[i]>a[i+1]:
      a_.append(1)
    else:
      a_.append(0)
  elif i==n-1:
    if a[i-1]<a[i]:
      a_.append(1)
    elif a[i-1]>a[i]:
      a_.append(0)
    else:
      a_.append(0)  
  else:
    if a[i-1]<=a[i] and a[i+1]<=a[i]:
      a_.append(1)
    elif a[i-1]>=a[i] and a[i+1]>=a[i]:
      a_.append(-1)
    else:
      a_.append(0)
m=1000
cou=0
for i in range(n):
  if a_[i]==-1:
    cou+=m//a[i]
    m=m%a[i]
  elif a_[i]==1:
    m+=a[i]*cou
    cou=0
  else:
    continue
print(m)

Solution (Python):

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

tommorow = [j-i for i, j in zip(a[:-1], a[1:])]
tommorow.append(-100)
max_price = 1000
kai = True
money = 1000
stock = 0
for i in range(len(a)):
    if kai:
        if tommorow[i] > 0:
            
            stock = money // a[i]
            money %= a[i]
            kai = False
    else:
        if tommorow[i] < 0:
            
            money += stock * a[i]
            max_price = max(max_price, money)
            kai = True

print(max_price)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def buy(lis, coin):
    k = coin//min(lis)
    return k, coin - min(lis)*k


def sell(lis, kabu):
    return max(lis)*kabu


n, *a = map(int, read().split())

buying = True
kabu = 0
coin = 1000
lis = []
for i in range(n):
    if buying:
        if i == n-1:
            break
        lis.append(a[i])
        if a[i] < a[i+1]:
            k, coin = buy(lis, coin)
            kabu += k
            lis = []
            buying = False

    else:
        lis.append(a[i])
        if i == n-1 or a[i] > a[i+1]:
            coin += sell(lis, kabu)
            kabu = 0
            lis = []
            buying = True

print(coin)

Solution (Python):
N = int(input())
rate = list(map(int,input().split()))
rate.append(100)
position = 0
flag = 'motanai'
money = 1000
for i in range(N):
  if flag == 'motanai' and rate[i]<rate[i+1]:
    position = money//rate[i]
    money = money%rate[i]
    flag = 'motu'
  elif flag == 'motu' and rate[i]>rate[i+1]:
    money += position*rate[i]
    position = 0
    flag = 'motanai'
  else:
    continue
print(money)

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

Solution (Python):
import sys
import heapq, math
from bisect import bisect_right
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

B = [A[0]]

for i in range(1, N):
    if A[i - 1] != A[i]:
        B.append(A[i])

N = len(B)


def solve():
    ret = 1000
    l = 0
    while l < N:
        while l < N - 1 and B[l] > B[l + 1]:
            l += 1

        if l == N - 1:
            return ret

        C = [B[l]]
        while l < N - 1 and B[l] < B[l + 1]:
            C.append(B[l + 1])
            l += 1

        cnt = ret // C[0]
        ret -= cnt * C[0]
        ret += cnt * C[-1]

        l += 1
    return ret


print(solve())

Solution (Python):
import sys
import math
n= int(input())
As = list(map(int, input().split()))
m = 1000
kab=0
for i in range(n-1):
    if As[i]<As[i+1]:
        kab+= math.floor(m/As[i])
        m=m%As[i]
        m=m+As[i+1]*kab
        kab=0
print(m)

Solution (Python):
import numpy as np
from scipy import signal


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

    dA = list()
    for i in range(N-1):
        dA.append(A[i+1]-A[i])

    idn = list()
    upflag = False
    downflag = False
    if dA[0] > 0:
        upflag = True
        tmp = 0
    elif dA[0] < 0:
        downflag = True
    for i, a in enumerate(dA[1:], 1):
        if downflag and a > 0:
            tmp = i
            upflag = True
            downflag = False
        elif upflag and a < 0:
            idn.append((tmp, i))
            upflag = False
            downflag = True
        elif (upflag is False) and (downflag is False):
            if a > 0:
                upflag = True
                tmp = i - 1
            elif a < 0:
                downflag = True
    if upflag:
        idn.append((tmp, N-1))

    ans = 1000
    for n in idn:
        ans = (ans//A[n[0]]) * A[n[1]] + (ans % A[n[0]])

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
lis = list(map(int, input().split()))


a = [0]*n

for i in range(n-1):
    if lis[i] < lis[i+1]:
        a[i+1] = 1
    elif lis[i] == lis[i+1]:
        a[i+1] = a[i]
    else:
        a[i+1] = -1


mon = 1000
kabu = 0

for i in range(n-1):
    if a[i+1] > a[i]:
        kabu = mon // lis[i]
        mon = mon % lis[i]
    elif a[i+1] < a[i]:
        mon += kabu * lis[i]
        kabu = 0

if kabu != 0:
    mon += kabu * lis[-1]

print(mon)

Solution (Python):
import sys
input = sys.stdin.readline

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

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

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

K = 0
m = 1000

for i in range(N):
    if i == N-1:
        m += K*A[i]
        break
    for k in range(1,N-i):
        if A[i] < A[i+k]:
            if K > 0:
                m += K*A[i]
            K = m//A[i]
            m = m%A[i]
            break
        else:
            if A[i] > A[i+k]:
                m += K*A[i]
                K = 0
                break
            else:
                continue
print(m)