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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
dp = [ 0 for n in range(N)]
dp[0] = 1000
#print(dp)
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])

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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
    #print("get", kabu_now)
  elif(top[i] == "h"):
    kabu_now = kabu
    mony += kabu_now*new_a[i]
    kabu -= kabu_now
    #print("bye", kabu_now)
print(mony)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)
    #print(l,ll)
  else:
    have+=(mx-mn)*int(have/mn)
    #print(mx,mn,l,ll)
    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)

Submission status: Accepted

Solution code:
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])
# print(diff)

#if all():
#  print(1000)
#  sys.exit
flag = 'buy'
money = 1000
stock = 0
for i in range(n):
  # print(flag, diff[i], money, stock)
  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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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):
    #print('{}:{}:{}'.format(i,ans,kabu))
    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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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:
        # action
        if st1 == 'up':
            buy(i)
        else:
            sell(i)
    st0 = st1

if kabu:
    sell(N - 1)


print(money)

Submission status: Accepted

Solution code:
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
    # print("{}: buy: {} {}".format(i, m, k))

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

    m += (rate * k)
    k = 0
    # print("{}: sell: {} {}".format(i, m, k))


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)

Submission status: Accepted

Solution code:
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)):
            # print(j)
            # print(next_sell_value, a[j], a[i])
            if next_sell_value < a[j]:
                next_sell_value = a[j]
                next_sell = j
                buy_stock = True
                # print("buy", next_sell_value, a[i])
            if next_sell_value > a[j]:
                # print("break", i, j)
                break
        else:
            break
        
        # print(next_sell)

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

        i = j
    
    print(money)




    
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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:  # and not lower
            # 次に高くなるまでに谷がない: 谷があるならそこで買うべき。
            adding, mod = divmod(money, current_price)
            money = mod
            stocks += adding
    money += current_price * stocks
    print(money)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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):
    #print(num, money)
    if a[i] < a[i + 1]:
        num += money // a[i]
        money -= a[i] * num
        money += num * a[i + 1]
        num = 0
print(money)

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import math


def a():
    x = int(input())

    if x < 600:
        print(8)
    elif x < 800:
        print(7)
    elif x < 1000:
        print(6)
    elif x < 1200:
        print(5)
    elif x < 1400:
        print(4)
    elif x < 1600:
        print(3)
    elif x < 1800:
        print(2)
    else:
        print(1)


def b():
    a, b, c = map(int, input().split())
    k = int(input())
    count = 0

    while b <= a:
        b *= 2
        count += 1

    while c <= b:
        c *= 2
        count += 1
    if count <= k:
        print('Yes')
    else:
        print('No')


def c():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    for i in range(k, n):
        if a[i] > a[i-k]:
            print('Yes')
        else:
            print('No')


def d():
    n = int(input())
    a = list(map(int, input().split()))
    a = [a[1]] + a + [a[n-2]]
    money = 1000
    kabu = 0
    for i, v in enumerate(a[1:n+1], 1):
        if (v > a[i-1] and v >= a[i+1]) or (v >= a[i-1] and v > a[i+1]):
            # sell
            money += kabu * v
            kabu = 0
        elif (v < a[i-1] and v <= a[i+1]) or (v <= a[i-1] and v < a[i+1]):
            # buy
            temp = money // v
            money -= temp * v
            kabu += temp
        # print(money,kabu)
    money += kabu * a[n]
    print(money)


# a()
# b()
# c()
d()

Submission status: Accepted

Solution code:
money = 1000
kabu = 0

input()
A = [int(x) for x in input().strip().split(" ")]
As = [[]]

for i, a in enumerate(A):
    if len(As[-1]) == 0:
        As[-1].append(a)
    elif As[-1][-1] <= a:
        As[-1].append(a)
    else:
        As.append([a])
As = [As_ for As_ in As if len(As_) > 1]
for As_ in As:
    kabu, money = divmod(money, As_[0])
    money += kabu * As_[-1]
print(money)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
lis = [0]*n
for i in range(n-1):
    if a[i] > a[i+1]:
        lis[i] = -1
b = a[::-1]
for i in range(n-1):
    if b[i] < b[i+1]:
        if lis[n-i-1] == -1:
            pass
        else:
            lis[n-i-1] = 1
lis[-1] = -1
sw = 0
for i in range(1,n):
    if a[0] < a[i]:
        sw = 1
        break
    if a[0] > a[i]:
        sw = 0
        break
if sw == 1:
    lis[0] = 1
m = 1000
k = 0
for i in range(n):
    if lis[i] == 1:
        k += m//a[i]
        m = m%a[i]
    elif lis[i] == -1:
        m += k*a[i]
        k = 0
print(m)

Submission status: Accepted

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