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

Solution (Python):
import sys

def solution(numbers):
    profit = float("-inf")
    
    minv = numbers[1]
    
    for i in range(2, len(numbers)):
        n = numbers[i]
        profit = max(profit, n - minv)
        minv = min(minv, n)
        
    return profit

def main():
    first = True
    numbers = []
    for line in sys.stdin:
        if first:
            first = False
        numbers.append(int(line))

    print(solution(numbers))

if __name__ == '__main__':
    main()

Solution (Python):
a = int(input())
R = [int(input()) for _ in range(a)]

minv = R[0]

maxv = R[1]-R[0] 

for j in range(len(R)-1):
    maxv = max(maxv,R[j+1]-minv)
    minv = min(minv,R[j+1])
    
print(maxv)

Solution (Python):





def MaximumProfit(val_list, N):
   maxv = -10e10
   minv = val_list[0]
   for i in range(1, N):
      maxv = max(maxv, val_list[i]-minv)
      minv = min(minv, val_list[i])
   return maxv

N = int(input())
val_list = [int(input()) for _ in range(N)] 
ans = MaximumProfit(val_list, N)
print(ans)

Solution (Python):
n=int(input())
minv=int(input())
maxv=-10**10
for i in range(n-1):
    r=int(input())
    if r-minv>maxv:
        maxv=r-minv
    if r<minv:
        minv=r
print(maxv)

Solution (Python):
n = int(input())
Rlist = [int(input()) for _ in range(n)]
min_val = Rlist[0]
ans = -10**10
for i in range(1, n):
    ans = max(ans, Rlist[i]-min_val)
    min_val = min(min_val, Rlist[i])
print(ans)

Solution (Python):

r = int(input())










    


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

ans = R[1] - R[0]
prev = R[0]
for j in range(1, len(R)):
  cur = R[j]
  ans = max(ans, cur - prev)
  prev = min(cur, prev)

print(ans)

Solution (Python):
n = int(input())

min_r = int(input())
maxv = -float('inf')

for _ in range(n-1):
    r = int(input())
    maxv = max(maxv, r - min_r)
    min_r = min(min_r, r)

print(maxv)

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

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)

Solution (Python):
import math
import string
import itertools
import fractions
import heapq
import collections
import re
import array
import bisect
import sys
import random
import time
inf = 10**9


def main():
    n = int(input())
    minv = int(input())
    maxv = -inf

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

    print(maxv)


if __name__ == '__main__':
    main()

Solution (Python):

input_number = int(input())
input_list = list()


for i in range(input_number):
    input_data = int(input())
    if i == 0:
        difference_mix = input_data
    elif i == 1:
        difference_max = input_data - difference_mix
        if difference_mix > input_data:
            difference_mix = input_data
    else:
        if difference_max < input_data - difference_mix:
            difference_max = input_data - difference_mix
        if difference_mix > input_data:
            difference_mix = input_data


print(difference_max)

Solution (Python):
n = int(input())
main = [0 for i in range(n)]

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

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

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

print(maxv)

Solution (Python):

n = int(input().rstrip())


r = int(input().rstrip())
min = r

for i in range(n-1):
    r = int(input().rstrip())
    if i == 0:
        profit = r - min
    else:
        if r - min > profit:
            profit = r - min
    if min > r:
        min = r

print(profit)

Solution (Python):
data = []
for _ in range(int(input())) :
    data.append(int(input()))
    
max_diff = -1000000000
min_data = data[0]

for value in data[1:] :
    max_diff = max(max_diff, value - min_data)
    min_data = min(min_data, value)

print(max_diff)

Solution (Python):
import sys

n=int(sys.stdin.readline())

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


maxv=int(-20000000000)
minv=r[0]

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

print(maxv)

Solution (Python):
n = int(input())
for index, i in enumerate([int(input()) for _ in range(n)]):
    if index != 0:
        tmp = i - min_val
        if index == 1 or answer < tmp:
            answer = tmp
    if index == 0 or i < min_val:
        min_val = i
print(answer)

Solution (Python):
def main2():
    N = int(input())
    R = []

    for _ in range(N):
        R.append(int(input()))
    
    maxv = -10**10 
    minv = R[0]
    for i in range(1, N):
        maxv = max(maxv, R[i]-minv)
        minv = min(minv, R[i])

    print(maxv)

main2()

Solution (Python):

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

for i in range(n):
    num = int(input())
    numbers.append(num)
max_value = -10**9
min_value = numbers[0]
for i in range(1, n):
    max_value = max(max_value, numbers[i] - min_value)
    min_value = min(min_value, numbers[i])
print(max_value)

Solution (Python):
n = int(input())
R = []

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

max_R, min_R = 10**9*(-1), R[0]
for j in range(1, n):
    max_R = max(max_R, R[j]-min_R)
    min_R = min(min_R, R[j])

print(max_R)

Solution (Python):


n = int(input())

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

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


print(maxv)

Solution (Python):
data = list()
for i in range(int(input())):
    data.append(int(input()))

max_diff = -1000000000
min_data = data[0]
for value in data[1:] :
    max_diff = max(max_diff, value - min_data)
    min_data = min(min_data, value)

print(max_diff)

Solution (Python):
N = int(input())
A = [int(input()) for n in range(N)]
B, C = [], []
m = 10 ** 9
M = 0
for n in range(N):
    if A[n] < m:
        m = A[n]
    if A[N - n - 1] > M:
        M = A[N - n - 1]
    B.append(m)
    C.append(M)
C.reverse()
D = [C[n+1] - B[n] for n in range(N-1)]
print(max(D))

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    A = []
    for _ in range(n):
        A.append(int(input()))
    min_price = A[0]
    max_profit = A[1] - A[0]
    for i in range(1, n):
        x = A[i]
        max_profit = max(max_profit, x - min_price)
        min_price = min(min_price, x)
        

    print(max_profit)

Solution (Python):
n = int(input())
r = [int(input()) for _  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)

Solution (Python):
n = int(input())
ans = -10**(19)
m = 10**19
for i in range(n):
    y = int(input())
    ans = max(ans,y-m)
    m = min(m,y)
print(ans)

Solution (Python):
n = int(input())
maxv = -2000000000
minv = int(input())

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

print(maxv)

Solution (Python):
n = int(input())

minv = int(input())
maxv = - 10 ** 9
for i in range(n-1):
    r_j = int(input())
    maxv = max(maxv, r_j - minv)
    minv = min(minv, r_j)
print(maxv)

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

max_profit = float('inf') * -1
min_v = R[0]
for i in range(1, len(R)):
    max_profit = max(max_profit, R[i]-min_v)
    min_v = min(min_v, R[i])

print(max_profit)

Solution (Python):
N = int(input())
R = []
for i in range(N):
    R.append(int(input()))
ans = -1 * (10 ** 9 + 7)
minv = R[0]
for i in range(1, N):
    ans = max(R[i] - minv, ans)
    minv = min(minv, R[i])
print(ans)

Solution (Python):
n = int(input())
max = -999999999
min = 0

for i in range(n):
    r = int(input())
    if i == 0:
        min = r
    else: 
        if max < r-min:
            max = r-min
        if min > r:
            min = r

print(max)

Solution (Python):
N = int(input())

for i in range(N):
    if i == 0:
        minimum = int(input())
    else:
        a = int(input())
        if i == 1 or maximum < a - minimum:
            maximum = a - minimum
        if a < minimum:
            minimum = a

print(maximum)

Solution (Python):
n = int(input())

maxp = 10**19 * -1
R = [int(input())]
min_R = R[0]

for i in range(1, n):
    R.append(int(input()))
    maxp = max(maxp, R[i]-min_R)
    min_R = min(min_R, R[i])

print(maxp)

Solution (Python):
def main():
    N = int(input())
    max_value = 10 ** 9 * -1
    min_value = int(input())
    for n in range(N - 1):
        now_value = int(input())
        if max_value < now_value - min_value:
            max_value = now_value - min_value
        if min_value > now_value:
            min_value = now_value
    print(max_value)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]

min_v = a[0]
max_p = a[1] - a[0]

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

print(max_p)

Solution (Python):
n = int(input())
a = []
minArray = []

m = int(1e10)
for _ in range(n):
	x = int(input())
	minArray.append(m)
	m = min([m,x])
	a.append(x)
	
t = 0
ans = -int(1e10)
for i in reversed(range(1,n)):
	t = max([t,a[i]])
	ans = max(ans, t-minArray[i])

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
R = [int(input()) for i in range(n)]

ans = -sys.maxsize
tmp = R[0]
for r in R[1:]:
    ans = max(r-tmp, ans)
    tmp = min(tmp, r)
print(ans)

Solution (Python):

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

rmin = r[0]
rmax = r[1]
delr = rmax - rmin

for i in range(n-1):
    delr = max(delr, r[i+1]-rmin)
    rmin = min(rmin, r[i+1])
print(delr)

Solution (Python):
N = int(input())
lst = [int(input()) for i in range(N)]

mi = lst[0]
ma = -float('inf')
for i in range(1,N):
    ma = max(ma, lst[i] - mi)
    mi = min(mi, lst[i])
print(ma)

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

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

print(maxv)

Solution (Python):
n = int(input())
minv = int(input())
maxv = -10000000000

for _ in range(1, n):
    r = int(input())
    if (maxv < r - minv):
        maxv = r - minv
    if (minv > r):
        minv = r

print(maxv)

Solution (Python):
n = int(input())
r_min = int(input())
r_t   = int(input())
g_max = r_t - r_min
r_min = min(r_t,r_min)
for _ in range(n-2):
    r_t = int(input())
    g_max = max(g_max, r_t-r_min)
    r_min = min(r_t,r_min)
print(g_max)

Solution (Python):
n=int(input())
A=[int(input()) for i in range(n)]
minv = A[0]
maxv = A[1]-A[0]
for j in range(1,n):
	if(maxv < A[j] - minv):
		maxv = A[j] - minv
	if(A[j] < minv):
		minv = A[j]
print(maxv)

Solution (Python):
MAX = 200000


def main():
  R = []

  n  = input()

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

  maxv = -20000000000;
  minv = R[0]

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

  print(maxv)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
dollar = [int(input()) for i in range(n)]
m = dollar[0]
max = dollar[1] - m

for i in range(1, n):
    if dollar[i] - m > max:
        max = dollar[i] - m
    if dollar[i] < m:
        m = dollar[i]
print(max)

Solution (Python):
N = int(input())
a= int(input())
min =a
max =a
ans =-999999999
for i in range(N-1):
    a= int(input())
    if ans < a - min :
        ans =a -min
    if min > a:
        min =a
    
print (ans)

Solution (Python):
N=int(input())
"""
R=[]

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

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

print(maxv)
"""

R=[int(input()) for i in range(N)]
minv=R[0]
maxv=R[1]-R[0]

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

print(maxv)

Solution (Python):
n = int(input()) 
t = [int(input()) for i in range(n)] 

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

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

print(maxv)

Solution (Python):
BIG_NUM = 10**10

N = int(input())

min_value = BIG_NUM
max_diff = -BIG_NUM
for i in range(N):
    num = int(input())
    
    diff = num - min_value
    if diff > max_diff:
        max_diff = diff
        
    if num < min_value:
        min_value = num

print(max_diff)

Solution (Python):
max_profit = -float('inf')
min_price = float('inf')

n = int(input())
prices = [int(input()) for _ in range(n)]

for t, price in enumerate(prices):
    if t > 0:
        max_profit = max(max_profit, price - min_price)
    min_price = min(min_price, price)

print(max_profit)

Solution (Python):
import math
n = int(input())

maxv = -10**12
minv = int(input())
for i in range(1,n):
    r = int(input())
    maxv = max(maxv,r - minv)
    minv = min(minv,r)
print(maxv)

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

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

print (maxv)

Solution (Python):
n=int(input())
ans=-9999999999999
buy=int(input())
sell=buy
for i in range(1,n):
	buy=min(sell,buy)
	sell=int(input())
	if(ans < sell-buy):
		ans=sell-buy
print(ans)

Solution (Python):
import sys
n = int(input())
mi = int(input())
mv = -10**9
for N in map(int, sys.stdin):
    
    if N - mi > mv:
        mv = N - mi
    if N < mi:
        mi = N
print(mv)

Solution (Python):
n = int(input())
r_min = int(input())
Rt = int(input())
g_max = Rt - r_min
r_min = min(Rt,r_min)

for _ in range(n - 2):
    Rt = int(input())
    g_max = max(g_max, Rt - r_min)
    r_min = min(Rt,r_min)
print(g_max)

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

rmin = r[0]
rmax = r[1] - r[0]
for i in range(1,len(r)):
    if rmin > r[i]:
        rmin = r[i]
    else:
        rmax = max(rmax,r[i] - rmin)

print(rmax)

Solution (Python):
n = int(input())
r = []
for i in range(0, n):
    r.append(int(input()))

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)

Solution (Python):
n = int(input())
r = [0] * (n)
for i in range(n):
    r[i] = int(input())
    
num_max = r[1] - r[0]
num_min = r[0]
min_idx = 0
for i in range(1, n):
    if num_max < r[i] - num_min:
        num_max = r[i] - num_min
    if r[i] < num_min:
        num_min = r[i]
print(str(num_max))

Solution (Python):
n = int(input())
r1 = int(input())
r2 = int(input())
m = r1 if r1 < r2 else r2
d = r2 - r1
for i in range(n-2):
    r = int(input())
    if r - m > d:
        d = r - m
    if r < m:
        m = r
print(d)

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

minv = r[0]
maxv = -2000000000

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

print(maxv)

Solution (Python):
n = int(input())
num = [int(input())for i in range(n)]
minv = num[0];maxv = -2000000000
for i in range(1,n):
    maxv = max(maxv,num[i]-minv) 
    minv = min(minv,num[i])
print(maxv)

Solution (Python):
fst = int(input())
lists = []
for i in range(fst):
    lists.append(int(input()))

maxv = -10 ** 9
minv = lists[0]
leng = len(lists)
for j in range(1, leng) :
    maxv = max(maxv, lists[j] - minv)
    minv = min(minv, lists[j])

print(maxv)

Solution (Python):
n = int(input())

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


v_max = r[1]
v_min = r[0]
profit_best = v_max - v_min

for j in range(2,n):
    v_max = r[j]
    v_min = min(v_min,r[j-1])
    profit_tmp = v_max - v_min
    profit_best = max(profit_best,profit_tmp)

print(profit_best)

Solution (Python):
first = int(input())
n = [int(input()) for i in range(first)]  
maxv = -200000000000
minv = n[0]
for i in range(1, len(n)):
    maxv = max(maxv, n[i] - minv)
    minv = min(minv, n[i])
print(maxv)

Solution (Python):
n = int(input())

min_r = pre_r = int(input())
max_diff = None

for _ in range(n-1):
    cur_r = int(input())
    min_r = pre_r if pre_r < min_r else min_r
    pre_r = cur_r

    diff = cur_r - min_r
    if max_diff is None or diff > max_diff:
        max_diff = diff

print(max_diff)

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
maxi=a[1]-a[0]
mini=a[0]
for j in range(1,n):
    if a[j]-mini>maxi:
        maxi=a[j]-mini
    if a[j]<mini:
        mini=a[j]
print(maxi)

Solution (Python):
n = int(input())
r = []

for i in range(n):
    r.append(int(input()))
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)

Solution (Python):
times = int(input())

r_list = []
for i in range(times):
    r_list.append(int(input()))

"""
発想：今までで一番大きい差と、新しく追加される値を用いた一番大きい値の大きい方を取れば良い
一番大きい差を得るためには、そこまでの数列の一番小さい値を保持しておく必要があるので、それも保持しておく
"""
r_min = r_list[0]
r_max = r_list[1] - r_list[0]
for r in range(1, len(r_list)):
    r_max = max(r_max, r_list[r]-r_min)
    r_min = min(r_min, r_list[r])

print(r_max)

Solution (Python):
n = int(input())
a0 = int(input())
a1 = int(input())
max_profit = a1 - a0
min_i = min(a1, a0)
for i in range(n-2):
    c=int(input())
    if c - min_i > max_profit:
        max_profit = c - min_i
    min_i = min(min_i, c)
print(max_profit)

Solution (Python):
n = int(input())
rt = [int(input()) for v in range(n)]
max_diff = (10 ** 9) * -1
min_val = 10 ** 9
for j in range(1, n):
    min_val = min(rt[j-1], min_val)
    diff = rt[j] - min_val
    if max_diff < diff:
        max_diff = diff
print(max_diff)

Solution (Python):
n=int(input())


ma=int(input())
result=-9999999999999999999999999

for i in range(n-1):
    value=int(input())
    if result<value-ma:
        result=value-ma

    
    if value < ma:
        ma=value

print(result)

Solution (Python):
n = int(input())
m = float('inf')
d = -float('inf')
for _ in range(n):
    r = int(input())
    d = max(d,r-m)
    m = min(m,r)
print(d)

Solution (Python):
n=int(input())
ma=-10**9

s=[]
for i in range(n):
  l=int(input())
  s.append(l)
mi=s[0]
for i in range(1,n):
  ma=max(ma,s[i]-mi)
  mi=min(mi,s[i])
print(ma)

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

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

Solution (Python):


def proc_inputs ():
    num_inputs = int(input())
    inputs = [int(input()) for i in range(num_inputs)]
    return inputs

def main ():
    inputs = proc_inputs()
    print(calc_prof(inputs))
    exit(0)

def calc_prof (inputs):
    maxnum = inputs[-1]
    
    inputs.reverse()
    profs = []

    for i,n in enumerate(inputs[1:]):
        profs.append(maxnum - n)

        if n > maxnum and i != len(inputs) - 1:
            maxnum = n
            

        
        
            
            
            

    return max(profs)

if __name__ == "__main__":
    main()

Solution (Python):

ans = -(10 ** 9)

N = int(input())
R_MIN = int(input())
for _ in range(N - 1):
    R = int(input())

    tmp = R - R_MIN
    if tmp > ans:
        ans = tmp
    if R_MIN > R:
        R_MIN = R
        continue

print(ans)

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



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):
    
    if min_r > r[i]:
        min_r = r[i] 
    else:
        ans = max(r[i] - min_r, ans) 

print(ans)

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

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

Solution (Python):
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
    
    
    
    
print(maxv)

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

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

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

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

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

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

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

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

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

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

Solution (Python):
import sys

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

def main():
    
    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()

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):



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














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

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

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

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

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

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

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

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

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    prices = []
    
    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)

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

Solution (Python):
import math

n=int(input())
t=int(input())
mint=t
t1=int(input())
if t1<t: mint=t1
maxtt=t1-t


for i in range(2,n):
    t=int(input())
 
    if t-mint>maxtt: maxtt=t-mint
    if t<mint: mint=t
    
print(int(maxtt))

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

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

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

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

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

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