Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Maximum Profit

You can obtain profits from foreign exchange margin transactions. For example,  if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) &times; 1000 = 8000 yen.

  Write a program which reads values of a currency $R_t$ at a certain time $t$ ($t = 0, 1, 2, ... n-1$), and reports the maximum value of $R_j - R_i$ where $j > i$ .

Input

The first line contains an integer $n$. In the following $n$ lines, $R_t$ ($t = 0, 1, 2, ... n-1$) are given in order.

Output

Print the maximum value in a line.

Constraints

  
$2 \leq n \leq 200,000$

  
$1 \leq R_t \leq 10^9$

Sample Input 1

6
5
3
1
3
4
3

Sample Output 1

3

Sample Input 2

3
4
3
2

Sample Output 2

-1



Sample input:
6
5
3
1
3
4
3


Sample output:
3

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for i in range(n)]

#min_rは，r[0:i]での最小値
#max_rは，r[0:i]での最大値
min_r = min(r[0], r[1])
max_r = max(r[0], r[1])
ans = r[1] - r[0]
for i in range(2, n):
    #高々考えられる可能性は2通り
    if min_r > r[i]:
        min_r = r[i] #最小値を更新する
    else:
        ans = max(r[i] - min_r, ans) #答えを更新する

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for _ in range(n)]

maxv = R[1] - R[0]
minv = R[0]
for i in range(1, n):
    value = R[i] - minv
    if value > maxv:
        maxv = value
    if R[i] < minv:
        minv = R[i]

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input())for _ in range(n)]

minv = r[0]
maxv = -10**9

for i in range(1,n):
  maxv = max(maxv, r[i] - minv)
  minv = min(minv, r[i])

print(maxv)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
rs = deque([int(input()) for i in range(n)])

maxv, minv = -(10**10), 10**10

while len(rs) > 0:
    now = rs.popleft()
    maxv = (now-minv) if maxv < (now-minv) else maxv
    minv = now if now < minv else minv
    #if maxv < (now-minv):
    #    maxv = (now-minv)
    #if now < minv:
    #    minv = now
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
numbers = []

minv = int(input())
num = int(input())

max_r = num - minv

if(minv > num):
    minv = num

for i in range(2,n):
    num = int(input())
    if(max_r <(num - minv)):
        max_r = num - minv

    if(minv > num):
        minv = num

print(max_r)

Submission status: Accepted

Solution code:
n, *R = map(int, open(0).read().split())
A = [int(-1e9) for _ in range(n)]
B = [int(1e9) for _ in range(n)]
A[n-1] = max(A[n-1], R[n-1])
B[0] = min(B[0], R[0])
for i in range(n-1, 0, -1):
    A[i-1] = max(R[i-1], A[i])
for i in range(n-1):
    B[i+1] = min(B[i], R[i+1])

ans = int(-3e9)
for i in range(n-1):
    ans = max(ans, A[i+1] - B[i])
print(ans)

Submission status: Accepted

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


def main():
    sc = Scan()
    n = sc.intarr()[0]
    array = sc.intarr_ver(n)

    minimum = array[0]
    ans = -1000000001
    for i in range(1, n):
        if array[i] - minimum > ans:
            ans = array[i] - minimum
        if array[i] < minimum:
            minimum = array[i]

    print(ans)


class Scan():

    def intarr(self):
        line = input()
        array = line.split(' ')
        num_array = [int(n) for n in array]
        return num_array

    def intarr_ver(self, n):
        return [int(input()) for _ in range(n)]

    def strarr(self):
        line = input()
        array = line.split(' ')
        array[-1] = array[-1].strip('\n')
        return array


main()

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for i in range(n)]
minv, maxv = r[0], -(10**9)
for j in r[1:]:
    maxv = max(maxv, j - minv)
    minv = min(minv, j)

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

l = [0 for i in range(n)]

for i in range(n):
  l[i] = int(input())

min = l[0]
max = l[1]-l[0]

for j in range(1,n):
  sa = l[j]-min
  if max < sa:
    max = sa
  if l[j] < min:
    min = l[j]


print(max)

Submission status: Accepted

Solution code:
n = int(input())

R0 = int(input())

minv = R0
maxv = -10**(9)
for i in range(1,n):
    Ri = int(input())
    if maxv < Ri - minv: maxv = Ri - minv
    if Ri < minv: minv = Ri

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
x = []

for i in range(n):
    x.append(int(input()))

minv = x[0]
maxv = - 10**9

for i in range(1, n):
    maxv = max(maxv, x[i]-minv)
    minv = min(minv, x[i])

print(maxv)

Submission status: Accepted

Solution code:
N = int(input())
R = [int(input()) for i in range(N)]

min_R = R[0]
max_diff = R[1] - R[0]
for i in range(2, N):
    min_R = min(min_R, R[i-1])
    max_diff = max(max_diff, R[i]-min_R)
print(max_diff)

Submission status: Accepted

Solution code:
import sys
i = sys.stdin
_ = i.readline()
x = int(i.readline())
y = int(i.readline())
v = y-x
m = x if y>x else y

for s in map(int, sys.stdin):
    if s-m > v: v = s-m
    elif s < m: m = s

print(v)

Submission status: Accepted

Solution code:
import math


def ALDS1_1_D():
    """
    ＜問題＞
    FX取引では、異なる国の通貨を交換することで為替差の利益を得ることができます。
    例えば、１ドル100円の時に 1000ドル買い、価格変動により 1ドル 108円になった時に売ると、
    (108円 − 100円) × 1000ドル = 8000円の利益を得ることができます。

    ある通貨について、時刻 t における価格 Rt (t=0,1,2,,,n−1)が入力として与えられるので、
    価格の差 Rj−Ri (ただし、j>i とする) の最大値を求めてください。

    ＜入力＞
    最初の行に整数 n が与えられます。続く n 行に整数 Rt (t=0,1,2,,,n−1) が順番に与えられます。


    ＜出力＞
    最大値を１行に出力してください。

    ＜制約＞
    2 ≤ n ≤ 200,000
    1 ≤ Rt ≤ 10^9
    """
    n = int(input())
    r_list = [int(input()) for _ in range(n)]
    max_v, min_v = -math.inf, r_list[0]
    for j in range(1,n):
        max_v = max_v if max_v > (r_list[j]-min_v) else (r_list[j]-min_v)
        min_v = r_list[j] if min_v > r_list[j] else min_v
    print(max_v)

def main():
    ALDS1_1_D()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    # x,y = list(map(int, input().split()))
    inf=10**10
    n = int(input())
    ans = -inf
    min_r=inf
    for _ in range(n):
        r=int(input())
        ans=max(ans,r-min_r)
        min_r=min(min_r,r)
    print(ans)

main()

Submission status: Accepted

Solution code:
times = int(input())
values = [int(input()) for _ in range(times)]
min_v = values[0]
max_v = values[1] - values[0]
for j in range(1, times):
    if values[j] - min_v > max_v: max_v = values[j] - min_v
    if min_v > values[j]: min_v = values[j]
print(max_v)

Submission status: Accepted

Solution code:
n = int(input())
input_list = [int(input()) for i in range(n)]
max_value = -2000000000
min_value = input_list[0]

for j in input_list[1:]:
    max_value = max(max_value, j - min_value)
    min_value = min(min_value, j)
print(max_value)

Submission status: Accepted

Solution code:
import sys

def main():
    n = int(sys.stdin.readline())
    r = [int(sys.stdin.readline()) for x in range(n)]
    m = r[0]
    result = -(10**9)
    for i in range(1, n):
        m = min(m, r[i -1])
        d = r[i] - m
        if d > result:
            result = d
    print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for _  in range(n)]

maxv = -10**12
minv = R[0]

for i in range(1, n):
    maxv = max(maxv, R[i]-minv)
    minv = min(minv, R[i])
    
print(maxv)

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())
R = [int(input()) for _ in range(N)]

R_min = list(accumulate(R, min))
R_max = accumulate(reversed(R), max)
R_max = list(reversed(list(R_max)))

max_profit = -10**10
for i in range(N-1):
    profit = R_max[i+1] - R_min[i]
    if profit > max_profit:
        max_profit = profit

print(max_profit)

Submission status: Accepted

Solution code:
def max_profit(n, values):
    min_val = values[0]
    profit = -(10**9)

    for i in range(1, n):
        if profit < values[i] - min_val:
            profit = values[i] - min_val
        if min_val > values[i]:
            min_val = values[i]

    return profit


def main():
    n = int(input())
    values = [int(input()) for i in range(n)]
    print(max_profit(n, values))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
numbers = []
for i in range(N):
  numbers.append(int(input()))

maxv = -1 *10**9
minv = numbers[0]

for i in range(1,N):
  maxv = max(maxv,numbers[i]-minv)
  minv = min(minv,numbers[i])

print(maxv)

Submission status: Accepted

Solution code:
import math
N = int(input())
R = []
inf = float("inf")
MAX = -inf
MIN = inf
R = [inf]
for i in range(N):
    r = int(input())
    MAX = max(r-MIN,MAX)
    MIN = min(MIN,r)
    R.append(r)

print(MAX)

Submission status: Accepted

Solution code:
n=int(input())
t=int(input())
min_t=t
t1 = int(input())
if min_t > t1:
    min_t = t1
maxdt=t1-t
for i in range(2,n):
    t=int(input())
    if maxdt < t-min_t:
        maxdt = t-min_t
    if t<min_t:
        min_t=t
print(maxdt)

Submission status: Accepted

Solution code:
N = int(input())
value = []
for _ in range(N):
    value.append(int(input()))
minv = value[0]
maxv = value[1] - minv
minv = min([minv,value[1]])
for i in range(2,N):
    maxv = max([maxv,value[i]-minv])
    minv = min([minv,value[i]])
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

maxv = -1000000000
minv = int(input())

for i in range(n-1):
    val = int(input())
    
    maxv = max(maxv, val-minv)
    minv = min(minv, val)

print(maxv)

Submission status: Accepted

Solution code:

# print('hello world')

n = int(input())
R = []
for i in range(n):
    R.append(int(input()))

# max_profit = None
# min_price = R[0]
#
# for i in range(1, n, 1):
#     if R[i] > min_price:
#         profit = R[i] - min_price
#         if profit > max_profit:
#             max_profit = profit
#     elif R[i] < min_price:
#         min_price = R[i]
#
# print(min_price)

min_value_tmp = R[0]
max_profit_tmp = - 10 ** 9

for i in range(1, n, 1):
    max_profit_tmp = max(max_profit_tmp, R[i]-min_value_tmp)
    min_value_tmp = min(min_value_tmp, R[i])
    # print(i, max_profit_tmp, min_value_tmp)

print(max_profit_tmp)

Submission status: Accepted

Solution code:
import sys

data = sys.stdin.readlines()
f = lambda x:int(x.rstrip())
data = [f(x) for x in data][1:]

vmin = data[0]
out = data[1] - data[0]

for x in data[1:]:
  out = max(out,x-vmin)
  vmin = min(x,vmin)

print(int(out))

Submission status: Accepted

Solution code:
n = int(input())
minv = int(input())
maxv = -float('inf')

for i in range(n-1):
    cur = int(input())
    maxv = max(maxv, cur - minv)
    minv = min(minv, cur)

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

R = list()
for i in range(n):
    R.append(int(input()))

differ_max = -2*10**9
rate_min = R[0]

for R_i in range(1, len(R)):
    differ_max = max(differ_max, R[R_i] - rate_min)
    rate_min = min(rate_min, R[R_i])

print(differ_max)

Submission status: Accepted

Solution code:
n = int(input())
min_val = int(input())
maxdif = -10**9

for i in range(n-1):
  val = int(input())
  dif = val - min_val
  maxdif = max(maxdif, dif)
  min_val = min(min_val, val)

print(maxdif)

Submission status: Accepted

Solution code:
n = int(input())
R_list = []

for i in range(n):
    Ri = int(input())
    R_list.append(Ri)

maxv = (-1)*10**9
minv = R_list[0]

for j in range(1, n):
    maxv = max(maxv, R_list[j] - minv)
    minv = min(minv, R_list[j])
    
print(maxv)

Submission status: Accepted

Solution code:
N=int(input())
R=int(input())
maxv=-1000000000
minv=R
for n in range(N-1):
    R=int(input())
    maxv=max(maxv,R-minv)
    minv=min(minv,R)
print(maxv)

Submission status: Accepted

Solution code:
N = int(input())
R = [int(input()) for _ in range(N)]
maxi = -(10**10)
mini = R[0]
for i in range(1,N):
    maxi = max(maxi,R[i]-mini)
    mini = min(mini,R[i])
print(maxi)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    prices = []
    # get price
    for i in range(n):
        prices.append(int(input()))
    maxp = prices[1] - prices[0]
    minp = prices[0]
    for i in range(1, n):
        maxp = max(maxp, prices[i] - minp)
        minp = min(minp, prices[i])
    print(maxp)

Submission status: Accepted

Solution code:
diff_max = -10**10
n = int(input())
num_min = int(input())
for i in range(n-1):
    temp = int(input())
    if temp - num_min > diff_max:
        diff_max = temp - num_min
    if temp < num_min:
        num_min = temp
    
print(diff_max)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    assert n >= 2, '输入至少包含两个数'
    a = int(input())
    b = int(input())
    res = b - a
    min_value = min(a, b)
    for _ in range(n - 2):
        tmp = int(input())
        res = max(tmp - min_value, res)
        min_value = min(tmp, min_value)
    print(res)

Submission status: Accepted

Solution code:
n = int(input())

minvalue = 2000000000
maxvalue = -2000000000

for i in range(n):
    r = int(input())
    maxvalue = max(maxvalue, r - minvalue)
    minvalue = min(minvalue, r)
print(maxvalue)

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input())  for i in range(n)]

maxv = r[1] - r[0]
minv = r[0]
for i in range(1,n):
    maxv = max(maxv, r[i]-minv)
    minv = min(minv, r[i])

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for i in range(n)]

minv = R[0]
maxv = - 10**10
for i in range(1,n):
    maxv = max(maxv,R[i]-minv)
    minv = min(minv,R[i])

print(maxv)

Submission status: Accepted

Solution code:
import sys
n = int(input())
A =  [int(input()) for i in range(n)]

max_benefit = -sys.maxsize
min_value = A[0]
for i in range(1,n):
    max_benefit = max(max_benefit, A[i]-min_value)
    min_value = min(min_value, A[i])
print(max_benefit)

Submission status: Accepted

Solution code:
num = int(input())

val0 = int(input())

minv = val0
maxv = -200000000000000

for i in range(1,num):
    value = int(input())
    maxv = max([value-minv,maxv])
        
    minv = min([value,minv])

print(maxv)

Submission status: Accepted

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

N=int(input())
a = [int(input()) for _ in range(N)]
max_a=[0]*N
tmp_max=0
for i in reversed(range(N)):
    tmp_max=max(tmp_max,a[i])
    max_a[i]=tmp_max
ans=-1e+9
for i in range(N-1):
    ans=max(ans,max_a[i+1]-a[i])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

r=[0]*n
for i in range(0,n):
    r[i]=int(input())

maxv=r[1]-r[0]
minv=r[0]

for j in range(1,n):
    maxv=max(maxv,r[j]-minv)
    minv=min(minv,r[j])

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
maxv = -1000000000
minv = int(input())
for i in range(1,n):
    r = int(input())
    maxv = max(maxv, r - minv)
    minv = min(minv, r)
print(maxv)

Submission status: Accepted

Solution code:
n=int(input())
A=[]
for i in range(0,n):
    v=int(input())
    A.append(v)
max=A[1]-A[0]
min=A[0]
for i in range(1,n):
    k=A[i]-min
    if max<k:
        max=k
    if min>A[i]:
        min=A[i]
print(max)

Submission status: Accepted

Solution code:
def func(n, r_t):
    minV = r_t[0]
    maxV = r_t[1] - minV
    for i in range(1, n):
        maxV = max(maxV, r_t[i] - minV)
        minV = min(minV, r_t[i])
    return maxV


n = int(input())
r_t = [int(input()) for _ in range(n)]
print(func(n, r_t))

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    x=int(input())
    a.append(x)
maxa=a[1]-a[0]
mina=a[0]
for i in range(1,n):
    if a[i]-mina>maxa:
        maxa=a[i]-mina    
    if a[i]<mina:
        mina=a[i]
print(maxa)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline
n = int(input())

minv = int(input())
max = -1000000001
for _ in range(n-1):
    val = int(input())
    if val - minv > max:
        max = val - minv
    if val < minv:
        minv = val

print(max)

Submission status: Accepted

Solution code:
import sys
n = int(input())
# r = [int(i.strip()) for i in sys.stdin.readlines()]

d = -1e10
a = int(input())
for i in range(n-1):
    b = sys.stdin.readline()
    b = int(b)
    d = max(b - a, d)
    if b < a:a = b

print(d)

Submission status: Accepted

Solution code:
n = int(input())
v_0 = int(input())
minv = v_0
v_1 = int(input())
if v_1 < v_0:
	minv = v_1
maxv = v_1 - v_0

for _ in range(2,n):
	value = int(input())
	maxv = max(maxv, value - minv)
	minv = min(minv, value)

print(maxv)

Submission status: Accepted

Solution code:
def main():
    N = int(input())

    ans = - (10**9 + 7)
    minv = int(input())
    for i in range(1,N):
        r = int(input())
        ans = max(ans, r - minv)
        minv = min(minv, r)
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
p=10**9+7
ans=-10**9-7
for i in range(n):
    r=int(input())
    if i:
        ans=max(ans,r-p)
    p=min(p,r)
print(ans)

Submission status: Accepted

Solution code:
"""
17D8101031C 杉山　祥吾 ID:keisei334 python
"""

n=int(input())
a=[int(input()) for i in range(n)]

maximum=a[1]-a[0]
minimum=a[0]
for j in range(1,n):
    if(a[j]-minimum>maximum):
        maximum=a[j]-minimum
    if(minimum>a[j]):
        minimum=a[j]

print(maximum)

"""実行結果
6
5
3
1
3
4
3
3

3
4
3 
2
-1
"""

Submission status: Accepted

Solution code:

n=int(input())
R=list()
for i in range(n):
    R.append(int(input()))

minv=R[0]
maxv=R[1]-R[0]
for i in range(1,n):
    if maxv<R[i]-minv:
        maxv=R[i]-minv
    if minv>R[i]:
        minv=R[i]
print(maxv)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
min = A[0]
max = A[1]-A[0]
for i in range(1,N):
    if max < A[i]-min:
        max = A[i]-min
    if min > A[i]:
        min = A[i]

print(max)

Submission status: Accepted

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


minv=R[0]
maxv=R[1]-R[0]
for i in range(1,n):

    maxv=max(maxv,R[i]-minv)
    minv=min(minv,R[i])

print(maxv)

Submission status: Accepted

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

mini = a[0]
max_sub = -float("inf")
for i in range(1, n):
    if a[i] - mini > max_sub:
        max_sub = a[i] - mini
    
    if a[i] < mini: mini = a[i]

print(max_sub)

Submission status: Accepted

Solution code:
prices = [int(input()) for _ in range(int(input()))]

max_profit = -2000000000
min_price = prices[0]

for price in prices[1:]:
    cur_profit = price - min_price
    max_profit = max(cur_profit, max_profit)
    min_price = min(min_price, price)

print(max_profit)

Submission status: Accepted

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

minv=a[0]
maxv=a[1]-a[0]

for i in range(1,n):
    maxv=max(maxv,a[i]-minv)
    minv=min(minv,a[i])

print(maxv)

Submission status: Accepted

Solution code:
def Maximum_Profit():
    n = int(input())
    R=[]
    for i in range(n):
        R.append(int(input()))
    minv = R[0]
    for i in range(1,n):
        if i==1:
            maxv=R[1]-R[0]
        elif maxv<R[i]-minv:
            maxv=R[i]-minv
        if minv>R[i]:
            minv=R[i]
    return maxv

maxv = Maximum_Profit()
print(maxv)

Submission status: Accepted

Solution code:
R = []
n = int(input())
for i in range(n):
    R.append(int(input()))
maxv = -20000000000
minv = R[0]

for i in range(1, n):
    if R[i] - minv > maxv:
        maxv = R[i] - minv
    if R[i] < minv:
        minv = R[i]

print(maxv)

Submission status: Accepted

Solution code:
# https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/1/ALDS1_1_D
# O(n**2)のプログラムを書かないように注意

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


n = read_a_int()
R = read_col(n, 1)[0]

# dp的な考え方？
# 各時刻における損益の最大値は、前の時刻における損益の最大値と現時刻の最小値から計算可能
# 最小値にぶち当たるたびに、そこにひっつきながら収益がもっと上がるか見張る感じ

r_min = R[0]
ans = -10**9
for r in R[1:]:
    ans = max(ans, r-r_min)
    r_min = min(r, r_min)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

min = int(input())
max_profit = 0 - min

for i in range(1, n):
    v = int(input())
    
    if (v-min) > max_profit:
        max_profit = (v-min)
    
    if v < min:
        min = v

print(max_profit)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for i in range(n)]

maxv = R[1] - R[0]
minv = R[0]

for j in range(1, n):
  if maxv < R[j] - minv:
    maxv = R[j] - minv
  
  if minv > R[j]:
    minv = R[j]

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
r = [0] * n
for i in range(n):
    r[i] = int(input())
    
minr = r[0]
r[0] = r[1] - r[0]
for i in range(1,n):
    x = r[i] - minr
    if r[i] < minr:
        minr = r[i]
    r[i] = x
    
s = max(r)
print(s)

Submission status: Accepted

Solution code:
n = int(input())
R_list = [int(input()) for i in range(n)]

R_i = 10**10
R_t = -10**10
for R_j in R_list:
    R_t = max(R_t, R_j - R_i)
    if R_j < R_i:
        R_i = R_j
print(R_t)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for _ in range(n)]

maxv = -10 ** 9
minv = R[0]

for i in range(1, n):
    maxv = max(maxv, R[i] - minv)
    minv = min(minv, R[i])
print(maxv)

Submission status: Accepted

Solution code:
def find_max_profit(collection):
    min_currency = collection[0]
    max_profit = -(10**9)
    for currency in collection[1:]:
        if max_profit < currency - min_currency:
            max_profit = currency - min_currency
        if min_currency > currency:
            min_currency = currency
            
    return max_profit
    
nums_currency = int(input())
collection = [int(input()) for _ in range(nums_currency)]

print(find_max_profit(collection))

Submission status: Accepted

Solution code:
n=int(input())
a=int(input())
b=int(input())
nowmin=min(a,b)
ans=b-a
for _ in range(n-2):
  new=int(input())
  if new<nowmin:
    nowmin=new
  else:
    ans=max(ans,new-nowmin)
print(ans)

Submission status: Accepted

Solution code:
#価格の差の最大値を求める
#2重ループを防ぐためにループを一つにまとめる
n =int(input())
r = [0]*n
for i in range(n):
    r[i] = int(input())

maxv = -200000000000000000
minv = r[0]
ans = 0 
#最小値を保持しておく
for i in range(1,len(r)):
    maxv = max(maxv,r[i] - minv)
    minv = min(minv,r[i])

print(maxv)

Submission status: Accepted

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

N = int(input())

min_price = None
max_profit = None
for i in range(N):

    price = int(input())
    if min_price is not None and (max_profit is None or max_profit < (price - min_price)):
        max_profit = price - min_price

    if min_price is None or min_price > price:
        min_price = price

print(max_profit)

Submission status: Accepted

Solution code:
n=int(input())
mina=999999999999999999999999
m=-99999999999999999999999
for i in range(n):
    a = int(input())
    if a - mina > m:
        m=a-mina
    if a<mina:
        mina=a
print(m)

Submission status: Accepted

Solution code:
n = int(input())

Max = -10**12
Min = 10**12

for i in range(n):
    tmp = int(input())
    Max = max(Max, tmp - Min)
    Min = min(Min, tmp)

print(Max)

Submission status: Accepted

Solution code:
N = int(input())
r1 = int(input())
r2 = int(input())
R = [int(input()) for i in range(N-2)]

res = r2 - r1
min_r = min(r1, r2)

for r in R:
  res = max(res, r - min_r)
  min_r = min(min_r, r)

print(res)

Submission status: Accepted

Solution code:
n=int(input())
k1=int(input())
for _ in range(n-1):
    k2=int(input())
    if _==0:
        ma=k2-k1
    if k1>k2:
        k1=k2
    else:
        ma=max([ma,k2-k1])

print(ma)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip()) for _ in range(n)]


mins = li[0]
maxs = -100000000000

for i in range(1,n):
    maxs = max(maxs,li[i]-mins)
    mins = min(mins,li[i])
print(maxs)

Submission status: Accepted

Solution code:
n=int(input())
r=[int(input()) for i in range(n)]
ans=-(10**10)
m=r[0]
for i in range(n-1):
	m=min(m,r[i])
	ans=max(ans,r[i+1]-m)
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    r = [int(input()) for _ in range(n)]

    maxv = -1000000000
    minv = r[0]

    for i in range(1,n):
        maxv = max(maxv, r[i] - minv)
        minv = min(minv, r[i])

    print(maxv)
resolve()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n = int(input())

numList = []

for i in range(n):
    a = int(input())
    numList.append(a)


min = numList[0]
max = numList[1]
profit = max - min

for i in range(1,len(numList)):
    if min > numList[i]:
        min = numList[i]
        max = numList[i]
        continue
    if max <= numList[i]:
        max = numList[i]
        if profit < max- min:
            profit = max - min
    
print(profit)

Submission status: Accepted

Solution code:
n=int(input())

ex=[]
for i in range(n):
    x=int(input())
    ex.append(x)

maxv=(-1)*10**10
minv=ex[0]

for i in range(1,n):
    maxv=max([maxv,ex[i]-minv])    
    minv=min([minv,ex[i]])

print(maxv)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    R = [int(input()) for i in range(n)]
    
    min_r = R[0]
    ans = -float('inf')
    
    for r in R[1:]:
        ans = max(ans, r-min_r)
        min_r = min(min_r, r)
        
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

n = int(input())
R = []

for i in range(n):
    r = int(input())
    R.append(r)

maxv = R[1] - R[0]
minv = R[0]

for r in R[1:]:
    maxv = max(maxv, r - minv)
    minv = min(minv, r)

print(maxv)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    max_diff = 1 - pow(10, 9)
    min_rate = int(input())
    # t > 0 以降の場合
    for t in range(n-1):
        rate = int(input())
        diff = rate - min_rate
        max_diff = max(max_diff, diff)
        min_rate = min(min_rate, rate)
    print(max_diff)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#coding: utf-8

n = int(input())
for i in range(n):
    R = int(input())
    if i == 0:
        minR = R
    elif i == 1:
        maxP = R - minR
    if i >= 1:
        maxP = max(maxP, R - minR)
    minR = min(minR, R)
print(maxP)

Submission status: Accepted

Solution code:
n = int(input())
nums = []
for i in range(n):
    nums.append(int(input()))

maxv = nums[1] - nums[0]
minv = nums[0]
for j in range(1, n):
    maxv = max(maxv, nums[j]-minv)
    minv = min(minv, nums[j])

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
a = []

r = int(input())
g = 0
m = 1000000001
s = False
d = -1000000001
for i in range(1, n):
    m = min(m, r)
    r_save = r
    r = int(input())
    if r == r_save:
        s = True
        continue
    g_save = g
    if r > r_save:
        g = 1
    elif r < r_save:
        if not s and not a:
            d = max(d, r - r_save)
        g = -1
    if g_save == 1 and g == -1:
        a.append((r_save, m))
    if i == n - 1 and g == 1:
        a.append((r, m))

if not a:
    if s:
        print(0)
    else:
        print(d)
else:
    print(max([v[0] - v[1] for v in a]))

Submission status: Accepted

Solution code:
def main(n):
    mn = 10**10
    ans = -1 * 10**10
    for i in range(n):
        temp = int(input())
        if temp - mn > ans:
            ans = temp - mn
        if temp < mn:
            mn = temp
    print(ans)

main(int(input()))

Submission status: Accepted

Solution code:
n = int(input())
maxv = -(10 ** 10)
minv = 10 ** 10

for j in range(n):
    Rj = int(input())
    maxv = max(maxv, Rj - minv)
    minv = min(minv,Rj)
    
print(maxv)

Submission status: Accepted

Solution code:
n=int(input())
R=[0]*n
for i in range(n):
  R[i]=int(input())
max_r=-10000000000
min_r=10000000000
benefit=[-60000000000]*2*n

for x in R:
    if max_r<x-min_r:
        max_r=x-min_r
    if min_r>x:
        min_r=x

print(max_r)

Submission status: Accepted

Solution code:
# coding: utf-8

def maximum_profit(A, N):
    min_x = 10e9
    max_benefit = -10e9
    for i in range(N):
        x = A[i]
        
        current_benefit = x - min_x
        if current_benefit > max_benefit:
            max_benefit = current_benefit
            
        if x < min_x:
            min_x = x


    print(max_benefit)


if __name__ == "__main__":
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    maximum_profit(A, N)

Submission status: Accepted

Solution code:
import sys

max_profit = -1 * (10 ** 9)

n = int(input())
rt = [int(input()) for i in range(n)]
min_r = rt[0]

for r in rt[1:]:
    profit = r - min_r
    min_r = min(min_r, r)
    max_profit = max(max_profit, profit)

print(max_profit)

Submission status: Accepted

Solution code:
box=[int(input()) for i in range(int(input()))]

max_point=-10**15
min_point=box[0]
for i in range(1,len(box)):
    max_point=max(max_point,box[i]-min_point)
    min_point=min(min_point,box[i])
    
print(max_point)

Submission status: Accepted

Solution code:
n=int(input())

minR=int(input())
maxP=-1000*1000*1000

for i in range(n-1):
    R=int(input())
    maxP=max(maxP,R-minR)
    minR=min(minR,R)

print(maxP)

Submission status: Accepted

Solution code:
N = int(input())

R0 = int(input())
R1 = int(input())

minR = min(R0, R1)
maxv = R1 - R0

for i in range(N-2):
    Ri = int(input())
    if Ri < minR:
        minR = Ri
    elif maxv < Ri - minR:
        maxv = Ri - minR

print(maxv)

Submission status: Accepted

Solution code:
N = int(input())
d = [int(input()) for i in range(N)]

maxv = -1 * (10 ** 9)
minv = d[0]

for i in range(N - 1):
    minv = min(d[i], minv)
    maxv = max(d[i + 1] - minv, maxv)

print(maxv)

Submission status: Accepted

Solution code:
m=int(input())
n=[]
for i in range(m):
  n.append(int(input()))
maxv=-1*10**9
minv=n[0]
for i in range(1,len(n)):
  maxv=max(maxv,n[i]-minv)
  minv=min(minv,n[i])

print(maxv)

Submission status: Accepted

Solution code:
from array import array

n = eval(input())
arr = array('i')

while n > 0:
    arr.append(eval(input()))
    n -= 1

max_number = arr[1] - arr[0]
min_number = arr[0]
length = len(arr)
for i in range(1, length):
    max_number = max(arr[i] - min_number, max_number)
    min_number = min(arr[i], min_number)

print(max_number)

Submission status: Accepted

Solution code:
n = int(input())

p_max = -float('inf')
t_min = float('inf')

for i in range(0, n):
    t = int(input())
    p_max = max(p_max, t - t_min)
    t_min = min(t_min, t)

print(p_max)

Submission status: Accepted

Solution code:
# ALDS1_1_D - Maximum Profit
def main():
    N, *A = map(int, open(0).read().split())
    ans, cur = -float("inf"), float("inf")
    for i in A:
        ans = max(ans, i - cur)
        if i < cur:
            cur = i
    print(ans)


if __name__ == "__main__":
    main()