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

ans = 1000
checkpoint = [a[0]]

for i in range(1, n):
    if a[i] >= a[i - 1]:
        checkpoint.append(a[i])
    else:
        kabu = ans // checkpoint[0]
        ans += kabu * (checkpoint[-1] - checkpoint[0])
        checkpoint = [a[i]]

if checkpoint[-1] >= checkpoint[0]:
    kabu = ans // checkpoint[0]
    ans += kabu * (checkpoint[-1] - checkpoint[0])
    
print(ans)

Submission status: Accepted

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

balance = 1000
position = A[0]

for i in range(1,N):
    if A[i] > position:
        balance = (balance//position)*A[i] + balance%position
        position = A[i]
    else: position = A[i]

print(balance)

Submission status: Accepted

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

buy = []
sell = []

if A[0] < A[1]:
    buy.append(0)
    f = True #上がり目
else:
    f = False #下がり目
for i in range(1,len(A)-1):
    if f == True:
        if A[i] > A[i+1]:
            sell.append(i)
            f = False
    else:
        if A[i] < A[i+1]:
            buy.append(i)
            f = True
if f == True:
    sell.append(len(A)-1)

ans = 1000
stock = 0
for i in range(len(buy)):
    stock = ans // A[buy[i]]
    ans %= A[buy[i]]
    ans += A[sell[i]] * stock
    stock = 0
print(ans)

Submission status: Accepted

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

lis = [0]*N

isnextup = [0]*(N-1) # -1:下がる, 0: 同じ, 1:上がる
for day in range(N-1):
    today = A[day]
    tommorow = A[day+1]
    if today < tommorow:
        isnextup[day] = 1
    elif today > tommorow:
        isnextup[day] = -1
        

money = 1000
stock = 0
for day in range(N-1):
    if isnextup[day] == 1:#底値で買う
        price = A[day]
        div, mod = divmod(money, price)
        money -= price * div
        stock += div
    elif isnextup[day] == -1: #天井で売る
        price = A[day]
        money += price * stock
        stock = 0

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

Submission status: Accepted

Solution code:
""" D
    https://atcoder.jp/contests/m-solutions2020/tasks/m_solutions2020_d
"""
import sys
import math
from bisect import bisect_left

def readString():
    return sys.stdin.readline()

def readInteger():
    return int(readString())

def readStringSet(n):
    return sys.stdin.readline().split(" ")[:n]

def readIntegerSet(n):
    return list(map(int, readStringSet(n)))

def readIntegerMatrix(n, m):
    return [lambda _: readIntegerSet(m) for _ in range(0, n)]

def div_by(n, x, k):
    result = 0
    for i in n:
        if x[i] == "1":
            result = (result + pow(2, n-i-1)) % k
    return result

def popcount(x):
    return sum([int(c) for c in x])

def main(N, A):
    kabu = 0
    money = 1000
    for i in range(1, N):
        if A[i-1] < A[i]:
            kabu += money // A[i-1]
            money %= A[i-1]
        else:
            money += A[i-1] * kabu
            kabu = 0

    money += A[N-1] * kabu

    return money

if __name__ == "__main__":
    _N = readInteger()
    _A = readIntegerSet(_N)

    print(main(_N, _A))

Submission status: Accepted

Solution code:
import math
N = int(input())
price = list(map(int,input().split()))
price.append(0)
stock = 0
money = 1000
for i in range(N):
    if price[i] <= price[i+1]:
        times = math.floor(money/price[i])
        money -= price[i]*times
        stock += times
    else:
        money += price[i]*stock
        stock =0
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 == 0:
        if A[i] <= A[i+1]:
            num = money // A[i]
            money -= num * A[i]
            stock += num
    else:
        if A[i] >= A[i+1]:
            money += stock * A[i]
            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()))
kau = [0]*n
index = []
if a[0] < a[1]:
    kau[0] = 1
elif a[0] == a[1]:
    for j in range(1, n):
        if a[0] < a[j]:
            kau[0] = 1
            break
        if a[0] > a[j]:
            break
    
for i in range(1, n-1):
    if a[i-1] > a[i]:
        for j in range(i+1, n):
            if a[i] < a[j]:
                kau[i] = 1
                break
            if a[i] > a[j]:
                break
for i, j in enumerate(kau):
    if j == 1:
        index.append(i)
index.append(n)

ans = 1000
for i in range(len(index)-1):
    kabu = ans//a[index[i]]
    ans -= a[index[i]]*kabu
    ans += max(a[index[i]+1:index[i+1]])*kabu
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
m = 1000
ka = 0
for i in range(n-1):
    if a[i+1] > a[i]:
        #print(m,"m",a[i],ka)
        ka += m // a[i]
        #print(ka,"ka")
        m = m % a[i]
    else:
        if ka > 0:
            m += ka * a[i]
            ka = 0
    if i == n-2:
        if ka > 0:
            #print("t",ka,m)
            m += ka * a[i+1]
    #print(m,ka,i)
print(m)

Submission status: Accepted

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

print(start)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

b = [0] * (n-1)

m = 1000
s = 0


# for i in range(n-1):
#    b = a[i+1] - a[i]

for i in range(n-1):
    t = 0
    if a[i+1] > a[i]:
        t = m // a[i]   # 株
        s += t
        m -= a[i] * t
    elif a[i+1] < a[i]:
        t = s * a[i]    # 円
        s = 0
        m += t

if s > 0:
    m += s*a[n-1]

print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
price={}
flg=10
ans=1000
stock=0
for i in range(1,N):
    if A[i]!=A[i-1]:
        if A[i]>A[i-1]:
            flg=1
            stock=ans//A[0]
            ans-=A[0]*stock
            break
        else:
            flg=0
            break
for i in range(1,N):
    if flg==1:
        if A[i]<A[i-1]:
            flg=0
            price[i-1]=1
    else:
        if A[i]>A[i-1]:
            flg=1
            price[i-1]=0
for i in price.keys():
    if price[i]==1:
        ans+=(int(A[i])*stock)
        stock=0
    else:
        stock=ans//A[i]
        ans-=A[i]*stock
print(ans+(A[-1]*stock))

Submission status: Accepted

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

L = []
for i in range(N-1):
    if A[i] < A[i+1]:
        L.append(i)

M = 1000
for i in L:
    k = M // A[i]
    M -= k * A[i]
    M += k * A[i+1]
print(M)

Submission status: Accepted

Solution code:
T = 1
# T = int(input())
##-----------------start coding-----------------
for ti in range(1, T + 1):
    n = int(input())
    a = []
    a.extend([int(e) for e in input().split()])
    money, stocks = 1000, 0
    rise = [i for i in range(n)]
    for _ in range(n - 1, 0, -1):
        if a[_] >= a[_ - 1]:
            rise[_ - 1] = rise[_]
    t = 0
    while t < n:
        if rise[t] == t:
            # sell
            money += a[t] * stocks
            stocks = 0
            t += 1
        else:
            deal = money // a[t]
            stocks += deal
            money -= deal * a[t]
            t = rise[t]
    print(money)
    # print('Case #{}: {}'.format(ti, '...'))

Submission status: Accepted

Solution code:
from itertools import groupby

n = int(input())
a = [i for i, _ in groupby(map(int, input().split()))]
n = len(a)

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

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]:
        count = ans//a[i]
        ans += (a[i+1]-a[i])*count
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
money = 1000
stock = 0
index = 0
while True:
    if stock == 0:
        if a[index] < a[index + 1]:
            stock += money // a[index]
            money -= stock * a[index]
        index += 1
    else:
        if a[index] > a[index + 1]:
            money += stock * a[index]
            stock = 0
        index += 1
    if index == n - 1:
        break
if stock != 0:
    money += stock * a[-1]
print(money)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A_LIST = list(map(int, input().split()))
money = 1000
stock = 0
for i in range(N - 1):
    if A_LIST[i] < A_LIST[i + 1]:
        # 購入
        stock += money // A_LIST[i]
        money %= A_LIST[i]
        # print("購入")
        # print(money)

    if A_LIST[i] > A_LIST[i + 1]:
        # 売却
        money += A_LIST[i] * stock
        stock = 0
        # print("売却")
        # print(money)

# 最後
money += A_LIST[-1] * stock
stock = 0
print(money)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.append(0)

m = 1000
s = 0

for i in range(n):
    if l[i+1] > l[i]:
        s += m//l[i]
        m %= l[i]
    elif l[i+1] < l[i]:
        m += s * l[i]
        s = 0
        
print(m)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a0=a[0]
a1=a[0]
money=1000
stock=0
for i in range(n):
  if a[i]>a1:
    #if a[i]>a1:
    a1=a[i]
    #print(a1)
  else:
    stock=money//a0
    #print(stock)
    money=money%a0
    money+=stock*a1
    stock=0
    a0=a[i]
    a1=a[i]
stock=money//a0
money=money%a0
money+=stock*a1
print(int(money))

Submission status: Accepted

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

a_ls+=[0]

m=1000
k=0

for i in range(N-1):
	m+=k*a_ls[i]
	k=0
	if a_ls[i]<a_ls[i+1]:
		k=m//a_ls[i]
		m-=(m//a_ls[i])*a_ls[i]
	


print(m+k*a_ls[N-1])

# 2darray [[0] * 4 for i in range(3)]

Submission status: Accepted

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

orl = 1000

for i in range(len(li)-1):
    if li[i] < li[i+1]:
        orl = int(orl/li[i]) * (li[i+1]) + (orl - li[i] * int(orl/li[i]))

print(orl)

Submission status: Accepted

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

kabu = 0
money = 1000

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

Submission status: Accepted

Solution code:
FAST_IO = 1
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()

    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
rrmm = lambda n: [rrm() for _ in xrange(n)]
N = rri()
arr = rrm()
def solve(N, prices):
    ans = 1000
    for i in range(1, len(prices)):
        if prices[i]>prices[i-1]:
            ans += (prices[i]-prices[i-1])*(ans//prices[i-1])
    return ans
print(solve(N, arr))

Submission status: Accepted

Solution code:
def main():
    import sys
    N=int(sys.stdin.readline())
    A=list(map(int,sys.stdin.readline().split()))
    B=[0]*N

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

        elif A[i]>A[i+1]:
            money+=kabu*A[i]
            kabu=0

    money+=kabu*A[-1]
    print(money)
    
main()

Submission status: Accepted

Solution code:

n=int(input())
A=list(map(int,input().split()))
x=1000
k=0
flag=False
def buy(x,k,cost):
    k+=x//cost
    x-=(x//cost)*cost
    return x,k

def sell(x,k,cost):
    x+=k*cost
    k=0
    return x,k

for i in range(n-1):
    today=A[i]
    tomorrow=A[i+1]
    if flag:
        if today<=tomorrow:
            pass
        else:
            x,k=sell(x,k,today)
            flag=False
    else:
        if today<=tomorrow:
            x,k=buy(x,k,today)
            flag=True
        else:
            pass
        
print(x+k*A[n-1])

Submission status: Accepted

Solution code:
def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]
    X=list(map(int,input().split()))
    A=list(map(int,input().split()))
    N=X[0]
    syojikin=1000
    kabu=0
    A.append(-1)
    for i in range(N):
        if A[i]<A[i+1]:
            kabu+=syojikin//A[i]
            syojikin=syojikin%A[i]
        elif A[i]>A[i+1]:
            syojikin+=kabu*A[i]
            kabu=0
    print(syojikin)






if __name__ == '__main__':
    main()

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]:
        s += c // A[i]
        c %= A[i]
    else:
        c += s * A[i]
        s = 0

if A[N - 2] < A[N - 1]:
    c += s * A[N - 1]

print(c)

Submission status: Accepted

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

sign = -1
temp = []
temp2 = []
for i in range(N-1):
    if sign == -1:
        if A[i] >= A[i+1]:
            continue
        else:
            temp.append(A[i])
            sign = 1
    else:
        if A[i] <= A[i+1]:
            continue
        else:
            temp2.append(A[i])
            sign = -1

if sign == 1:
    if A[i] <= A[i+1]:
        temp2.append(A[i+1])
    else:
        temp2.append(A[i])


S = 1000
for a, b in zip(temp, temp2):
    cnt = S//a
    S += cnt*(b-a)

print(S)

Submission status: Accepted

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

L = [(1000, 0)] # Money, Stock;

for i in range(N):
  L2 = []
  for x in L:
    # Stay
    L2.append(x)
    # Buy
    if x[0] >= A[i]:
      L2.append( (x[0] % A[i], x[1] + x[0] // A[i] ) )
    # Sell
    if x[1] > 0:
      L2.append( (x[0] + x[1] * A[i], 0) )
  #print("L2", L2)
  
  #Pick Up Usefull Pair:
  L = []
  for x in range(len(L2)):
    for y in range(len(L2)):
      if y == x:
        continue
      ok = True
      if (L2[x][0] < L2[y][0] and L2[x][1] < L2[y][1] or 
          L2[x][0] == L2[y][0] and L2[x][1] < L2[y][1] or 
          L2[x][0] < L2[y][0] and L2[x][1] == L2[y][1] or 
          L2[x][0] == L2[y][0] and L2[x][1] == L2[y][1] and x > y):
        ok = False
        break
    if ok:
      L.append(L2[x])
  #print("L", L)
  
  
ans = 0
for i in L:
  ans = max(ans, i[0])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
default = True
for i in range(N-1):
    if A[i] < A[i+1]:
        default = False
        break
if default:
    print(1000)
    exit()
updown = [(0, A[0])]
for i in range(N-1):
    if A[i] < A[i+1]:
        updown.append((1, A[i+1]))
    elif A[i] > A[i+1]:
        updown.append((-1, A[i+1]))
stock = 0
money = 1000
for i in range(len(updown)-1):
    if updown[i][0] == 0 and updown[i+1][0] == 1:
        buy = money // updown[i][1]
        stock += buy
        money -= buy * updown[i][1]
    if updown[i][0] == 0 and updown[i+1][0] == -1:
        pass
    if updown[i][0] == -1 and updown[i+1][0] == 1:
        buy = money // updown[i][1]
        stock += buy
        money -= buy * updown[i][1]
    if updown[i][0] == -1 and updown[i+1][0] == -1:
        pass
    if updown[i][0] == 1 and updown[i+1][0] == 1:
        pass
    if updown[i][0] == 1 and updown[i+1][0] == -1:
        money += stock * updown[i][1]
        stock = 0
if stock:
    money += stock * updown[-1][1]
    stock = 0
print(money)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===

def main():
    n = ni()
    a = na()
    # a.insert(0, INF)
    a.append(-INF)

    kabu = 0
    kane = 1000

    turn = []
    for i in range(n):
        if a[i + 1] > a[i]:
            turn.append(1)
        elif a[i + 1] < a[i]:
            turn.append(-1)
        else:
            turn.append(0)

    katteruflg = False
    for i in range(len(turn)):
        if turn[i] == 1:
            if katteruflg:
                continue
            kabu = kane // a[i]
            kane -= a[i] * kabu
            katteruflg = True
        elif turn[i] == -1:
            kane += a[i] * kabu
            kabu = 0
            katteruflg = False

    print(kane)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 入力
N = int(input())
A_list = list(map(int, input().split()))

money = 1000
stock = 0

for i in range(N-1):
    tmp_stock = 0
    if(A_list[i] < A_list[i+1]):
        #購入
        tmp_stock = money // A_list[i]
        money = money - A_list[i] * tmp_stock
        stock += tmp_stock
    elif(A_list[i] > A_list[i+1]):
        #売却
        tmp_stock = stock
        money = money + A_list[i] * tmp_stock
        stock -= tmp_stock
    else:
        pass
        #何もしない

#最終日売却
tmp_stock = stock
money = money + A_list[N-1] * tmp_stock
stock -= tmp_stock

print(money)

Submission status: Accepted

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline


def array(size, init=0):
    return [[init for j in range(size[1])] for i in range(size[0])]


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


def II():
    return int(input())


directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


def permutate(x, A):
    n = len(x)
    res = [-1]*n
    for i in range(n):

        res[i] = x[A[i]]
    return res


def generate_bin(digit):
    for b in range(2**digit):
        b = bin(b)
        tmp = b[2:].zfill(digit)
        yield list(map(int, tmp))
        # b=list(map(int,list(b[2:].zfill(digit))))
        # yield b


def search(x, count):
    #print("top", x, count)

    for d in directions:
        nx = d+x
        # print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)

    return count


N = II()
A = acinput()

A.insert(0, A[0]+1)
A.append(A[-1]-1)

tmp = []
for i in range(len(A)):
    if A[i] != A[i-1]:
        tmp.append(A[i])

A = tmp

x = 1000
k = 0

for i in range(len(A)-1):
    #print(i, x, k)
    if A[i-1] > A[i] and A[i] < A[i+1]:
        k += x//A[i]
        x = x % A[i]
    elif A[i-1] < A[i] and A[i] > A[i+1]:
        #print(i, x, k)
        x += A[i]*k
        k = 0


print(x)

# print(A)

Submission status: Accepted

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

def solve(N, A):
    x = 1000
    y = 0
    op = "buy"
    for i in range(N):
        if i == N-1:
            x += y * A[i]
        elif op == "buy":
            if A[i] >= A[i+1]:
                continue
            else:
                c = x // A[i]
                x -= c * A[i]
                y += c
                op = "sell"
        elif op == "sell":
            if A[i] <= A[i+1]:
                continue
            else:
                c = y
                x += c * A[i]
                y -= c
                op = "buy"
    return max(x, 1000)


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N = II()
    A = LI()

    dp = [[0, 0] for _ in range(N+1)]
    # print(dp)
    dp[0][0] = (1000, 0)
    dp[0][1] = (0, 0)
    # print(dp)

    def buy(lst, a):
        mon, stock = lst
        n = mon // a
        mon = mon - n * a
        stock += n
        return mon, stock

    def sell(lst, a):
        mon, stock = lst
        mon += stock * a
        stock = 0
        return mon, stock

    for i in range(1, N+1):
        a = A[i-1]

        # Buy
        bb = buy(dp[i-1][0], a)
        sb = buy(dp[i-1][1], a)

        if bb[1] == sb[1]:
            if bb[0] > sb[0]:
                dp[i][0] = bb
            else:
                dp[i][0] = sb
        elif bb[1] > sb[1]:
            dp[i][0] = bb
        else:
            dp[i][0] = sb

        # Sell
        bs = sell(dp[i-1][0], a)
        ss = sell(dp[i-1][1], a)
        if bs[0] > ss[0]:
            dp[i][1] = bs
        else:
            dp[i][1] = ss

        # print(dp)

    print(max(dp[N][0][0], dp[N][1][0]))

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict
from itertools import product
sys.setrecursionlimit(500000)
MOD = 10**9+7

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

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    A = list(mi())

    stock = 0
    money = 1000

    u = []
    d = []

    i = 0
    while i < N-1:
        if A[i] <= A[i+1]:
            tmp = i
            while i+1 < N and A[i] <= A[i+1]:
                i += 1
            u.append((tmp, i))
        else:
            i += 1

    for v in u:
        stock += money//A[v[0]]
        money -= A[v[0]] * (money//A[v[0]])

        money += A[v[1]] * stock
        stock = 0

    print(money)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

low_num = 200
high_num = 1000
have_flag = False
money = 1000
stock = 0
last_money = 1000
for i in range(N):
    if have_flag:
        if high_num <= A_list[i]:
            high_num = A_list[i]
        else:
            have_flag = False
            money = money + stock * high_num
            stock = 0
            low_num = A_list[i]
            last_money = money
    else:
        if low_num >= A_list[i]:
            low_num = A_list[i]
        else:
            have_flag = True
            stock = money // low_num
            money = money % low_num
            high_num = A_list[i]

if have_flag:
    if last_money < money + stock * A_list[N - 1]:
        last_money = money + stock * A_list[N - 1]

print(last_money)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 1000
ans_list1 = []
ans_list2 = []
l = [0,0]
d = [0,0]

for i in range(1,n):
    if a[i-1] <= a[i]:
        l[1] = i
        ans_list2.append(d)
        d = [i,i]
    else:
        d[1] = i
        ans_list1.append(l)
        l = [i,i]
ans_list1.append(l)
ans_list2.append(d)

for i in range(len(ans_list1)):
    k = ans // a[ans_list1[i][0]]
    ans += (a[ans_list1[i][1]] - a[ans_list1[i][0]]) * k

print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
m=1000
for i in range(1,n):
    if a[i]>a[i-1]:
        m=m//a[i-1]*a[i]+m%a[i-1]
print(m)

Submission status: Accepted

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

money = 1000
stock = 0
for day in range(n-1):
    if a[day+1] > a[day]:
        buyStock = money // a[day]
        if buyStock > 0:
            stock += buyStock
            money -= buyStock * a[day]
    elif a[day+1] < a[day]:
        money += stock * a[day]
        stock = 0
money += stock * a[-1]
print(money)

Submission status: Accepted

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

score=1000

for i in range(N-1):
    if A[i] < A[i+1]:
        num=score//A[i]
        score+=num*(A[i+1]-A[i])

print(score)

Submission status: Accepted

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

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

p = 1000
kabu = 0

uri = False

for i in range(n):
    if i == n-1 or (uri and a[i+1] < a[i]):
        #uri
        p += kabu * a[i]
        kabu = 0
        uri = False
    elif (not uri) and a[i+1] > a[i]:
        #kai
        kabu = p // a[i]
        p -= a[i] * kabu
        uri = True

    #print(p, kabu)

print(p)

Submission status: Accepted

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