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 math
 
n = int(input())
tmp = int(input())
tmpmin = tmp
t1 = int(input())
if t1 < tmp: tmpmin = t1
ans = t1 - tmp
 
 
for i in range(2,n):
    tmp = int(input())
  
    if tmp - tmpmin > ans: ans = tmp - tmpmin
    if tmp < tmpmin: tmpmin = tmp
     
print(int(ans))

Solution (Python):
maxv = -10000000000
minv = 10000000000

for _ in range(int(input())) :
    num	= int(input())
    maxv = max(maxv,num	- minv)
    minv = min(minv,num)
print(maxv)

Solution (Python):
n=input()
l=[input() for _ in range(n)]
chk,ans=l[0],-1000000000
for i in range(1,n):
    chk=min(chk,l[i-1])
    if l[i]-chk>ans:
        ans=l[i]-chk
print ans

Solution (Python):
mini = pre = float("inf")
def f(n):
    global mini, pre
    mini = min(mini, pre)
    pre = n
    return n - mini
print max(f(input()) for _ in xrange(input()))

Solution (Python):
mini = pre = float("inf")
def f(n):
    global mini, pre
    mini = min(mini, pre)
    pre = n
    return n - mini
print max(f(input()) for _ in xrange(input()))

Solution (Python):
maxv = -2*10**9
minv = 2*10**9
n = int(input())
for i in range(n):
    x = int(input())
    maxv = max(x - minv, maxv)
    minv = min(x, minv)
print(maxv)

Solution (Python):
n = int(input())
co = 10**9
ma = -(10**9)
rr = int(input())
if rr < co:
	co = rr
for i in range(n-1):
	r = int(input())
	k = co
	if r < co:
		co = r
	if r - k > ma:
		ma = r - k
print(ma)

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

max_dif = -(10**9)
min_rate = rates[0]

for i in range(1, n):
    max_dif = max(max_dif, rates[i]-min_rate)
    min_rate= min(min_rate, rates[i])

print(max_dif)

Solution (Python):
from sys import stdin


def main():
    n = int(stdin.readline().rstrip())
    

    ans = None
    tmp = None
    for a in range(n):
        i = int(input())
        
        if tmp is None:
            
            tmp = i
        elif ans is None:
            ans = i - tmp
        elif i - tmp > ans:
            ans = i - tmp

        
        if tmp == 0:
            tmp = i
        elif tmp > i:
            tmp = i
        else:
            pass

    return ans


if __name__ == '__main__':
    print(main())

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

R = list()
mini = 0
for i in range(2):
    R.append(int(input()))
mini = min(R)

sub = R[1]-R[0]

for k in range(n-2):
    x = int(input())
    tmp = mini
    R.append(x)
    if (x < mini):
        mini = x

    if (x - tmp > sub):
        sub = x - tmp

print(sub)

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

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

print(maxv)

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


minv = int(input())
maxv = -10**12


for j in range(1, N):
    r = int(input())
    maxv = max(maxv, r - minv)
    minv = min(minv, r)
    
print(maxv)

Solution (Python):
n = int(input())
l = []
for _ in range(n):
    x = int(input())
    l.append(x)
min_value = min(l[0],l[1])
max_difference = l[1]-l[0]
for i in range(2,n):
    max_difference = max(max_difference,l[i]-min_value)
    min_value = min(min_value,l[i])
print(max_difference)

Solution (Python):
N = int(input())
curmin = 10**10
ans = -10**10
for i in range(N):
	x = int(input())
	ans = max(ans, x - curmin)
	curmin = min(curmin, x)
print(ans)

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

maxv = -2000000000
minv = r[0]

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

Solution (Python):
num = int(input())
maxv = -20000000000
minv = int(input())

for i in range(num - 1):
    a = int(input())
    b = a - minv
    maxv = max(maxv, b)
    minv = min(minv, a)
print(maxv)

Solution (Python):
'''
chapter2_3
ALDS1_1_D Maximum Profit
'''
N = int(input())
R = [int(input()) for i in range(N)]
minr = R[0]
maxr = -10**9
for i in range(1,N):
    maxr = max(maxr, R[i]-minr)
    minr = min(minr,R[i])
print(maxr)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
R = [0] * N
for i in range(N):
    R[i] = I()
Min = R[0]
Max = R[1] - R[0]

for i in range(1, N):
    Max = max(Max, R[i] - Min)
    Min = min(Min, R[i])

print(Max)

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

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

Solution (Python):
def main():
    n = int(input())
    result = -10**10

    r = int(input())
    for t in range(1, n):
        r_next = int(input())

        if (r_next - r) > result:
            result = r_next-r

        if r > r_next:
            r = r_next

    print(result)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
rmin = int(input())
d = -(10**9)
for i in range(1,n):
    r = int(input())
    if d < r-rmin:
        d = r-rmin
    if r < rmin:
        rmin = r
print(d)

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

def max_profit(lst):
    minv = lst[0]
    maxv = lst[1] - lst[0]
    for j in range(1,len(lst)):
        if maxv < lst[j] - minv:
            maxv = lst[j] - minv
        if minv > lst[j]:
            minv = lst[j]
    return maxv

print(max_profit(lst))

Solution (Python):
from sys import maxsize
n = int(input())
L = []
for _ in range(n):
    L.append(int(input()))
minv = L[0] 
maxv = -maxsize
for i in range(1, n):
    maxv = max(maxv, L[i]-minv)
    minv = min(minv, L[i])
print(maxv)

Solution (Python):
N = int(input())
Prices = [int(input()) for n in range(N)]

ans =  -float("inf")
minprice = Prices[0]

for i in range(1,N):
    p = Prices[i] - minprice
    ans = max(ans,p)
    minprice = min(minprice, Prices[i])

print(ans)

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

ans = R[1] - R[0]

M = R[-1]
for i in range(n-2,-1,-1):
    s = R[i]
    if M - s > ans:
        ans = M-s
    if M < s:
        M = s

print(ans)

Solution (Python):
R = [int(input()) for i in range(int(input()))]
dfmx = -10 ** 10
mn = 10 ** 10
for i in range(len(R)):
    dfmx = max(R[i] - mn, dfmx)
    mn = min(R[i], mn)
print(dfmx)

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

fx = []

for _ in range(n):
    fx.append(int(input()))
    
maxv = -1000000000
minv = fx[0]
for j in range(1, n):
    maxv = max(maxv, fx[j] - minv)
    minv = min(minv, fx[j])
print(maxv)

Solution (Python):
def profit(R, n):
    max_profit = R[1] - R[0]
    min_n = R[0]
    for i in range(1, n-1):
        if min_n > R[i]:
            min_n = R[i]
        if max_profit < R[i+1] - min_n:
            max_profit = R[i+1] - min_n
    return max_profit


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

    print(profit(R, n))


if __name__ == "__main__":
    main()

Solution (Python):
R_min, dR_max = float("inf"), -float("inf")
for _ in range(int(input())):
    R = int(input())
    dR_max = max(dR_max, R-R_min)
    R_min = min(R_min, R)
print(dR_max)

Solution (Python):
R_min, dR_max = float("inf"), -float("inf")
for _ in range(int(input())):
    R = int(input())
    dR_max = max(dR_max, R-R_min)
    R_min = min(R_min, R)
print(dR_max)

Solution (Python):
maxv = -float('inf')
N = int(input())
R_list = [int(input()) for _ in range(N)]
minv = R_list[0]
for j in range(1, N):
    if R_list[j] - minv > maxv:
        maxv = R_list[j] - minv
    if minv > R_list[j]:
        minv = R_list[j]
      
print(str(maxv))

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

def main():
  max_profit = -10 ** 9
  min_value = R[0]

  for i in range(1, n):
    if (max_profit < R[i] - min_value):
      max_profit = R[i] - min_value
    if (R[i] < min_value):
      min_value = R[i]

  return(max_profit)

print(main())

Solution (Python):

import sys
input=sys.stdin.readline

def main():
    numbers=[]
    number = int(input())
    numbers=[int(input()) for _ in range(number)]
    answers=[-10**9]*number
    buy_point=10**9

    if len(numbers)==2:
        print(numbers[1]-numbers[0])
        exit()

    for idx,value in enumerate(numbers):
        if buy_point==10**9:
            buy_point=value
            continue
        else:
            answers[idx]=value-buy_point
            if buy_point > value:
                buy_point=value

    print(max(answers))
            
if __name__=="__main__":
    main()

Solution (Python):
ans = -10e+10
mn = 10e+10

n = int(input())
for i in range(n):
    x=int(input())
    ans = max(ans,x-mn)
    mn = min (mn,x)

print(ans)

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

max_n = -float('inf')
for i in range(n):
    num = int(input())
    if i == 0:
        min_n = num
    else:
        if num - min_n > max_n:
            max_n = num - min_n
        if num < min_n:
            min_n = num
print(max_n)

Solution (Python):
n = int(input())
R = []
for _ in range(n):
    R.append(int(input()))
M = - 10 ** 9 - 5
m = R[0]
for i in range(1, n):
    M = max(M, R[i] - m)
    m = min(m, R[i])
print(M)

Solution (Python):
import math
N =int(input())
min = math.inf
max_diff = -math.inf
for i in range(N):
    num = int(input())
    if min == math.inf:
        min = num
        continue
    if num - min > max_diff:
        max_diff = num - min
    if min > num:
        min = num
print(max_diff)

Solution (Python):
n = int(input())
r = [int(input()) for i in range(n)]
start = r[0]
score = r[1] - r[0]
for i in range(1,n):
    if r[i] - start > score: 
        score = r[i] - start
    if r[i] < start: 
        start = r[i]
print(score)

Solution (Python):
R = [int(input()) for i in range(int(input()))]
dfmx = -10 ** 10
mn = 10 ** 10
for i in range(len(R)):
    dfmx = max(R[i] - mn, dfmx)
    mn = min(R[i], mn)
print(dfmx)

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

if Rt1 < Rt2:
    min_Rt = Rt1
    
else:
    min_Rt = Rt2
    
max_prf = Rt2 - Rt1


for i in range(2,n):
    Rt = int(input())
    tmp_prf = Rt - min_Rt

    if tmp_prf > max_prf:
        max_prf = tmp_prf
    
    if Rt < min_Rt:
        min_Rt = Rt
    
print(str(max_prf))

Solution (Python):
n = int(input())
maxv = -2000000000
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())

lst=[int(input()) for _ in range(n)]
work_mx=10**10

mx=-10**16
for i in range(n-1):
    if lst[i]>=work_mx : continue
    work_mx=lst[i]
    sm=max(lst[i+1:])-lst[i]
    mx=max(sm,mx)

print(mx)

Solution (Python):
import sys
max_value = 1e10
min_value = -max_value
input()
for v in map(int, sys.stdin):
    if min_value < v - max_value:
        min_value = v - max_value
    if v < max_value:
        max_value = v
print(min_value)

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

a = R[0]
b = R[1] - R[0]

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

Solution (Python):
size = int(int(input()))
num_list = [int(input()) for _ in range(size)]
maxv = num_list[1] - num_list[0]
minv = num_list[0]
for i in range(1, size):
    if minv > num_list[i]:
        minv = num_list[i]
        continue
    elif num_list[i] - minv > maxv:
        maxv = num_list[i] - minv


print(maxv)

Solution (Python):

li=[int(input()) for i in range(int(input()))]

max_val=-10**15
min_val=li[0]
for i in range(1,len(li)):
    max_val=max(max_val,li[i]-min_val)
    min_val=min(min_val,li[i])
    
print(max_val)

Solution (Python):

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







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

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

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)

Solution (Python):
def find_max_profit(prices:list) -> int:
    maxp = -10000000000
    minp = prices[0]
    for i in range(1,len(prices)):
        if prices[i] - minp > maxp:
            maxp = prices[i] - minp
        if prices[i] < minp:
            minp = prices[i]
    return maxp            
    
if __name__ == '__main__':
    n = int(input())
    prices = []
    for i in range(n):
        prices.append(int(input()))
    print(find_max_profit(prices))

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

min_r = int(input())
score = -float('inf')
for i in range(n-1):
    r = int(input())
    score = max(score, r - min_r)
    min_r = min(r, min_r)

print(score)

Solution (Python):


n = int(input().split()[0])
max_diff = -float("inf")
min_value = float("inf")

for i in range(n):
    r = int(input().split()[0])
    if i != 0:
        diff = r - min_value
        max_diff = max(max_diff, diff)
    min_value = min(min_value, r)

ans = max_diff
print(max_diff)

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



max_val = -(10**9 + 1)
min_num = num_list[0]
for j, num in enumerate(num_list[1:N]):
    
    max_val = max(max_val, num - min_num) 
    min_num = min(min_num, num)

print(max_val)

Solution (Python):
n = int(input())
minimum = 10000000000
maxprofit = -1000000000
for _ in range(n):
    rt = int(input())
    profit = rt - minimum
    minimum = min(rt, minimum)
    maxprofit = max(profit, maxprofit)

print(maxprofit)

Solution (Python):

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


max_v = -100000000000000 
min_v = elements.pop(0)

'''
for j が 1 から n-1 まで
  maxv = (maxv と R[j]-minv のうち大きい方)
  minv = (minv と R[j] のうち小さい方)
'''
for element in elements :
    max_v = max(element-min_v,max_v)
    min_v = min(element,min_v)
print(max_v)

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

res = r1 - r0
r_min = r0 if r0 < r1 else r1

for i in range(2, N):
    r = int(input())

    if res < r - r_min:
        res = r - r_min
    elif r < r_min:
        r_min = r

print(res)

Solution (Python):
min_r = 0
profit = 0

n = int(input())
for i in range(n):
    r = int(input())
    if i == 0:
        min_r = r
    elif i == 1:
        profit = r - min_r
    else:
        profit = max(profit, r - min_r)
    min_r = min(min_r, r)
print(profit)

Solution (Python):
n, *aa = map(int, open(0).read().split())

bb = []
mn = 1<<31

for a in aa:
    mn = min(mn, a)
    bb.append(mn)

print(max(aa[i] - bb[i-1] for i in range(1,n)))

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


N = int(input())

a = int(input())
b = int(input())
ans = b-a
_min = min(a, b)

for i in range(2, N):
    tmp = int(input())
    ans = max(ans, tmp - _min)
    _min = min(_min, tmp)

print(ans)

Solution (Python):
n = int(input())
input_list = [int(input()) for i in range(n)]
min_value = input_list[0]
profit = -1000000000
for i in input_list[1:]:
    if(profit < (i - min_value)):
        profit = i - min_value
    
    if (min_value > i):
        min_value = i
print(profit)

Solution (Python):
n = int(input())
r = [int(input()) for i in range(n)]
start = r[0]
score = r[1] - r[0]
for i in range(1,n):
    if r[i] - start > score: score = r[i] - start
    if r[i] < start: start = r[i]
print(score)

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

mins = float("inf")
profit = float("-inf")
for index in range(n):
    a = int(input())
    profit = max(profit, a - mins)
    mins = min(mins, a)
print(profit)

Solution (Python):
n = int(input())
R = [int(input()) for _ in range(n)]
maxv = 10**9 * (-1)
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())
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 minv > A[j]:
        minv = A[j]
print(maxv)

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

Solution (Python):
cnt=int(input())
A=[int(input()) for i in range(cnt)]


mn=1e10
ans=A[1]-A[0]

for i in A:
    ans=max(ans,i-mn)
    mn=min(mn,i)
    
print(ans)

Solution (Python):


"""
Created on Tue Jun 30 00:17:54 2020

@author: yukio
"""


n=int(input())
lst=[int(input())for _ in range(n)]
minv=lst[0]
maxv=-float("inf")
for i in range(1,n):
    maxv=max(maxv,lst[i]-minv)
    minv=min(minv,lst[i])

print(maxv)

Solution (Python):
n = int(input())
r = [int(input().strip()) for _ in range(n)]
maxv = -1e9
minv = r[0]
for i in range(1,n):
    maxv = maxv if maxv > r[i]-minv else r[i]-minv
    minv = minv if minv < r[i] else r[i]
print(maxv)

Solution (Python):
from sys import maxsize

n, b = [int(input()) for i in range(2)]
p = -maxsize

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

print(p)

Solution (Python):
n = int(input())
a = []
mx = 0
mn = 0
for i in range(n):
    x = int(input())
    a.append(x)
    if i == 0:
        mn = a[i]
    elif i == 1:
        mx = a[i] - a[i-1]
        mn = a[i] if a[i] < a[i-1] else a[i-1]
    else:
        if mx < a[i] - mn:
            mx = a[i] - mn
        mn = a[i] if a[i] < mn else mn
print(str(mx))

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

r_list = []


min_r = 99999999999
max_profit = -999999999999

for n in range(N):
    temp_r = int(input().rstrip())
    if temp_r - min_r > max_profit:
        max_profit = temp_r - min_r
    if temp_r < min_r:
        min_r = temp_r

print(max_profit)

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

R = [0]*n

ans = -float('inf')

minimum = float('inf')
for i in range(n):
    R[i] = int(input())
    if i == 0:
        minimum = R[i]
        continue
    minimum = min(minimum, R[i-1])
    ans = max(ans, R[i] - minimum)
    
print(ans)

Solution (Python):
maxNum = -10 ** 10
minNum = 0
n = int(input())

line = []
for i in range(n):
    tmp = int(input())
    if i == 0:
        minNum = tmp
    line.append(tmp)
for i in range(1, len(line)):
    maxNum = max(maxNum, line[i] - minNum)
    minNum = min(minNum, line[i])

print(maxNum)

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

maxv = -10**10
minv = 10**10

for i in range(N):
    value = int(input())
    maxv = max(maxv, value-minv)
    minv = min(minv, value)
print(maxv)

Solution (Python):
N = int(input())
r = [int(input()) for _ in range(N)]
min_r = r[0]
max_diff = -2e9

for i in range(1, N):
    max_diff = max([max_diff, r[i]-min_r])
    min_r = min([min_r, r[i]])
print(max_diff)

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

maxv = -1e100
minv = R[0]

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

print(maxv)

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

maxv = -10**10
minv = 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())

min=10**10
ans=-10**10
for i in range(1,n+1):
    i=int(input())
    if i-min>ans:
        ans=i-min
    if i<min:
        min=i

print(ans)

Solution (Python):
n = int(input())
prev = int(input())
ans = -10**10

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

print(ans)

Solution (Python):
def main():
    N = int(input())
    r = [int(input()) for _ in range(N)]

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

    for j in range(1,N):
        temp = r[j] - minv
        if maxv < temp:
            maxv = temp

        if minv > r[j]:
            minv = r[j]

    return maxv

print(main())

Solution (Python):
import sys
inputs = list(map(lambda x: int(x.rstrip()), sys.stdin.readlines()))


r_min = inputs[1]
p_max = -10**9
for r in inputs[2:]:
    p_max = max(p_max, r-r_min)
    r_min = min(r_min, r)

print(p_max)

Solution (Python):
a=int(input())
b=[int(input()) for i in range(a)]

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

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    n = int(input())
    R = [0] * n
    for i in range(n):
        R[i] = int(input())

    min_R_front = [0] * n
    min_R_front[0] = R[0]
    for i in range(1, n):
        if R[i] < min_R_front[i - 1]:
            min_R_front[i] = R[i]
        else:
            min_R_front[i] = min_R_front[i - 1]

    max_R_back = [0] * n
    max_R_back[-1] = R[-1]
    for i in reversed(range(n - 1)):
        if R[i] > max_R_back[i + 1]:
            max_R_back[i] = R[i]
        else:
            max_R_back[i] = max_R_back[i + 1]

    ans = -float("inf")
    for i in range(n - 1):
        if ans < max_R_back[i + 1] - min_R_front[i]:
            ans = max_R_back[i + 1] - min_R_front[i]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin

read = stdin.readline

N = int(read())
data = [int(read()) for _ in range(N)]
min = data[0]
max_profit = data[1] - data[0]

for index in range(1,N):
    if max_profit < data[index] - min:
        max_profit = data[index] - min
    if data[index] < min:
        min = data[index]

print(max_profit)

Solution (Python):


n = int(input())
R = []
for _ in range(n):
    R.append(int(input()))
    
res = -1*float('inf')
minv = R[0]
for i in range(1, n):
    res = max(res, R[i]-minv)
    minv = min(minv, R[i])

print(res)

Solution (Python):
n = int(input())
R = []
for i in range(n):
    R.append(int(input()))
buying = R[0]  
maximumProfit = -10 ** 18  
for i in range(1, n):
    maximumProfit = max(maximumProfit, R[i] - buying)
    buying = min(buying, R[i])
print(maximumProfit)

Solution (Python):
n=int(input())
R=[int(input()) for _ in range(n)]
minR=10**9
ans=-10**9
for i in range(n):
  if i==0: 
    minR=min(minR,R[i])
    continue
  ans=max(ans,R[i]-minR)
  
  minR=min(minR,R[i])
print(ans)

Solution (Python):

from sys import stdin
import math

n = int(stdin.readline().rstrip())
ans = -(math.pow(10, 9))
tmp_min_val = int(stdin.readline().rstrip())
for i in range(n - 1):
  val = int(stdin.readline().rstrip())
  diff = val - tmp_min_val
  if ans < diff:
    ans = diff
  if val < tmp_min_val:
    tmp_min_val = val
print(ans)

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

mx = -2222222222222222
mi = lis[0]

for x in lis[1:]:
  mx = max(mx, x-mi)
  mi = min(mi, x)
  
print(mx)

Solution (Python):
N = int(input())
maxr = -10**10
minr = 10**10

for i in range(N):
  num = int(input())
  maxr = max(maxr, num-minr)
  minr = min(minr, num)
  
print(maxr)

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

maxDiff = R[1] - R[0]
minRi = R[0]

for r in R[1:]:
    if (r - minRi) > maxDiff:
        maxDiff = r - minRi
    if r < minRi:
        minRi = r

print(maxDiff)

Solution (Python):
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    min_A = A[0]
    diff = []
    for a in A[1:]:
        diff.append(a - min_A)
        min_A = min(a, min_A)

    print(max(diff))


if __name__ == '__main__':
    main()

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

pf = R[1] - R[0] 
minv = min(R[0],R[1]) 
maxv = R[1]

for i in range(2,N):
    newv = R[i]
    if (newv - minv) > pf:
        maxv = newv
        pf = newv - minv
    if newv < minv:
        minv = newv

print(pf)

Solution (Python):
def main():
    n = int(input())
    maxv = -(10 ** 9)
    minv = int(input())
    for _ in range(n-1):
        i = int(input())
        maxv = max(maxv, i - minv)
        minv = min(minv, i)
    print(maxv)
    
if __name__ == '__main__':
    main()

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

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

maximum = - 1e9
minimum = int(input())

for i in range(1,n):
    r = int(input())
    maximum = max(maximum, r - minimum)
    minimum = min(r, minimum)

print(maximum)

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


maximum_point=-10**10
minv=S[0]

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

print(maximum_point)

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


minv = R[0]

ans = R[1] - R[0]

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

Solution (Python):
def resolve():
    N = int(input())
    R = [int(input()) for _ in range(N)]
    minR, maxR = R[0], -1 * (10**9)
    for i in range(1, N):
        maxR = max(maxR, R[i] - minR)
        minR = min(minR, R[i])
    print(maxR)


resolve()

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

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

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

m = R[0]
M = R[1] - R[0]

for i in range(1,n):
    M = max(M,R[i] - m)
    m = min(m,R[i])

print(M)

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

n = int(input())

maxv = 10**9 * -1
min_R = int(input())
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 i in range(n)]

val_min  = R[0]
diff_max = -1e+9
for i in range(1,n):
    diff_max = max(diff_max, R[i]-val_min)
    val_min  = min(val_min, R[i])
    
print(diff_max)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
import functools

n = int(input())
A = [int(input()) for i in range(n)]
maxv = -2*10**9
minv = A[0]
for i in range(1,n):
    maxv = max(maxv,A[i] - minv)
    minv = min(minv,A[i])
print(maxv)

Solution (Python):
n=int(input())
ans = -10**10
mint = 10**10
for i in range(n):
    tmp = int(input())
    if i!=0:
        ans = max(ans, tmp - mint)
    mint = min(mint, tmp)
print(ans)

Solution (Python):
n = int(input())
minRate = int(input())
maxProf = -10 ** 9
for t in range(1, n):
    rt = int(input())
    if rt - minRate > maxProf:
        maxProf = rt - minRate
    if rt < minRate:
        minRate = rt
print(maxProf)

Solution (Python):
from typing import List


def get_max_v2(R: List[int]) -> int:
    max_diff: int = R[1] - R[0]
    min_r: int = min(R[0], R[1])

    for r in R[2:]:
        if r - min_r > max_diff:
            max_diff = r - min_r
        if r < min_r:
            min_r = r
    return max_diff


def main():
    n: int = int(input())
    R: List[int] = []
    for _ in range(n):
        R.append(int(input()))

    result = get_max_v2(R)
    print(result)


if __name__ == "__main__":
    main()

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):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n = I()
    R = [I() for _ in range(n)]

    ans = -INF
    min_v = INF
    for i in range(n):
        ans = max(R[i]-min_v, ans)
        min_v = min(R[i], min_v)

    print(ans)

if __name__ == '__main__':
    resolve()

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

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

maxv = -2000000000000
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())

ans = -(10**9+1)
for i in range(N):
    if i == 0:
        minnum = int(input())
        continue
    num = int(input())
    ans = max(ans,num-minnum)
    minnum = min(minnum,num)

print(ans)

Solution (Python):
N = int(input())
max_sub = -1145148101919
min_value = int(input())
for i in range(1,N):
    r=int(input())
    max_sub=max(max_sub,r-min_value)
    min_value=min(min_value,r)
print(max_sub)

Solution (Python):
N = int(input())
t = int(input())
t1 = int(input())
mint = t
if t > t1:
  mint = t1
maxt = t1 -t
  
for i in range(2,N):
  a = int(input())
  if a - mint > maxt:
    maxt = a - mint
  if a < mint:
    mint = a
print(maxt)

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

max_v = -1 * 10**10
min_v = int(input())

for i in range(n-1):
    num = int(input())
    max_v = max(max_v, num-min_v)
    min_v = min(min_v, num)

print(max_v)

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

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

Solution (Python):
n = int(input())
minv = int(input())
maxv = -float("inf")
for i in range(1,n):
    s = int(input())
    maxv = max(maxv,s-minv)
    minv = min(minv,s)
print(maxv)

Solution (Python):
n = int(input())
minv = 0
maxv = -2000000000
a = int(input())
minv = a
for i in range(n-1):
    a = int(input())
    maxv = max(maxv,a-minv)
    minv = min(minv,a)

print(maxv)

Solution (Python):

import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines
MOD = 10 ** 9 + 7
INF = 1 << 60





def resolve():
    N = int(inp())
    A = [int(inp()) for _ in range(N)]
    minV = A[0]
    maxV = -(1<<60)
    for i in range(1, N):
        maxV = max(maxV, A[i]-minV)
        minV = min(minV, A[i])
    print(maxV)

if __name__ == '__main__':
    resolve()

Solution (Python):


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

tmp = -1000000000
minv = A[0]
for i in range(1, n):
    if tmp < A[i] - minv:
        tmp = A[i] - minv
    if A[i] < minv:
        minv = A[i]

print(tmp)

Solution (Python):
n = int(input())
diff = -100000000000
minv = int(input())
for i in range(1,n):
    a = int(input())
    diff = max(diff,a-minv)
    minv = min(a,minv)
print(diff)