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

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

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

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

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

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

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

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


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)

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

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

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

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

Solution (Python):

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)

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

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

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

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

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

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

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

Solution (Python):



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]





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

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

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

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

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

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

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

Solution (Python):


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)

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

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

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

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

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

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

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

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

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

Solution (Python):



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)

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

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

Solution (Python):

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)

Solution (Python):
def main():
    n = int(input())
    max_diff = 1 - pow(10, 9)
    min_rate = int(input())
    
    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()

Solution (Python):


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)

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

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

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

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

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

Solution (Python):


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)

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

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

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

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

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

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

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

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

Solution (Python):

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

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    A = [int(input()) for _ in range(N)]
    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)

Solution (Python):
N = int(input())
max_val = -1 * 10 ** 9
min_r = int(input())

for _ in  range(N - 1):
  R = int(input())
  val = R - min_r
  max_val = max(val, max_val)
  min_r = min(R, min_r)

print(max_val)

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

for i in range(n):
    r = int(input())
    if i==0:
        maxv = -2000000000
        minv = r
    else:
        maxv = max(maxv, r-minv)
        minv = min(minv, r)

print(maxv)

Solution (Python):
n = int(input())
R =[0 for i in range(n)]
for i in range(n):
	R[i] = 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):
num = int(input())


for i in range(num):
    v = int(input())
    if i == 0:
        minv =v
        maxv = -1000000000
        continue
    maxv  = max(maxv, v-minv)
    minv  = min(minv, v)
    
print(maxv)

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

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

Solution (Python):
def test():

	n = int(input())
	
	max = -10000000000
	m_min = int(input())

	for i in range(n-1):

		next = int(input())

		if max < (next - m_min):
			max = next - m_min

		if m_min > next:
			m_min = next

	print(max)


if __name__ == "__main__":
	test()

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


profit = -1e10 
min_val = int(input())

for i in range(1, n):
    val = int(input())
    diff = val - min_val
    if (diff > profit):
        profit = diff
    if (val < min_val):
        min_val = val

print(profit)

Solution (Python):





from sys import stdin,setrecursionlimit

setrecursionlimit(10**6)


n=int(stdin.readline().rstrip())
a=[int(stdin.readline().rstrip()) for _ in range(n)]
ans=0
mi=a[0]
ma=-(10**10)
for i in range(1,n):
    ma=max(ma,a[i]-mi)
    mi=min(mi,a[i])
print(ma)

Solution (Python):
n=int(input())
R=[]
for i in range(n):
    R.append(int(input()))
Rmin=R[0]
Rmax=-2000000000
for i in range(1,n):
    Rmax=max(Rmax,R[i]-Rmin)
    Rmin=min(Rmin,R[i])
print(Rmax)

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

tempmax = rs[-1]    
rmaxs = [0]*(n-1)
rmaxs[-1] = tempmax

for i in range(n-2,0,-1):
    if rs[i] > tempmax:
        tempmax = rs[i]
    rmaxs[i-1] = tempmax

ans = rmaxs[0] - rs[0]

for i in range(1,n-1):
    if rmaxs[i] - rs[i] > ans:
        ans = rmaxs[i] - rs[i]

print(ans)

Solution (Python):
n  = int(input())
R = [int(input()) for _ in range(n)]
ans = -1e10
minv = R[0]
for j in range(1,n):
    ans = max(ans, R[j] - minv)
    minv = min(minv, R[j])
print(ans)

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

Solution (Python):
INF = 2000000000

N = int(raw_input())
p = int(raw_input())
minimum = p
maximum = -INF
for _ in range(N-1):
    t = int(raw_input())
    maximum = max(maximum, t-minimum)
    minimum = min(minimum, t)
print maximum

Solution (Python):
n = int(input())
max_revenue = -1000000000
min_price = 1000000000
min_price = int(input())
for _ in range(n-1):
    price = int(input())
    if max_revenue < price - min_price: max_revenue = price - min_price
    if min_price > price: min_price = price
print(max_revenue)

Solution (Python):
n=int(input())
R =[int(input()) for i in range(n)]
r=R[0]
maxV=-float('inf')
for i in range(n-1):
	if R[i]<r:
		r=R[i]
	if maxV<R[i+1]-r:
		maxV=R[i+1]-r
print(maxV)

Solution (Python):
n = int(input())
max_revenue = -1000000000
min_price = 1000000000
min_price = int(input())
for _ in range(n-1):
    price = int(input())
    if max_revenue < price - min_price: max_revenue = price - min_price
    if min_price > price: min_price = price
print(max_revenue)

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

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

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

maxi = 0
mini = float("inf")


lmin = []
rmax = []

R = []

for i in range(n):

    r = int(input())
    R.append(r)

    if len(lmin) == 0:
        lmin.append(r)

    else:
        lmin.append(min(lmin[-1],r))

for i in range(n):

    i = n-1-i

    if len(rmax) == 0:
        rmax.append(R[i])

    else:
        rmax.append(max(R[i],rmax[-1]))

rmax.reverse()
lmin.insert(0,float("inf"))

ans = -1 * float("inf")

for i in range(n):

    ans = max(ans,rmax[i]-lmin[i])




print (ans)

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

def maxR(R):
    n = len(R)
    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])
    return maxv

print(maxR(R))

Solution (Python):
N = int(input())
args = [int(input()) for i in range(N)]
minargs = args[0]
result = - 10 ** 10
for i in range(1,N):
    minargs = min(minargs,args[i-1])
    result = max(result, args[i] - minargs)
print(result)

Solution (Python):




n=int(input()) 

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














min_value = num_list[0]
max_profit = -10**9

for i in range(1,n):
        max_profit = max(max_profit, num_list[i] - min_value)
        min_value = min(num_list[i], min_value)


print(max_profit)

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

min_i = N[0]
max_d = -9999999999
for j in range(1, r):
    diff = N[j] - min_i
    if diff > max_d:
        max_d = diff
    if N[j] < min_i:
        min_i = N[j]
print(max_d)

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

values = [int(input()) for _ in range(valuenum)]

Minv = values.pop(0)
ans = -(10 ** 9)

for Rj in values:
  temp_ans = Rj - Minv
  if temp_ans > ans:
    ans = temp_ans
  if Rj < Minv:
    Minv = Rj

print(ans)

Solution (Python):
def maxProf2(n, a):
    init = True
    for i in range(1, n):
        if init:
            vmin = min(a[:i])
            vmax = a[i]
            diffmax = vmax - vmin
            init = False
            continue
        vmin = min(vmin, a[i-1])
        diffmax = max(diffmax, a[i]-vmin)
    print(diffmax)
    return

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

Solution (Python):
def maxProfit(list):
    mini = 0
    ans = list[1] - list[0]
    for i in range(1, len(list)):
        if ans < list[i] - list[mini]:
            ans = list[i] - list[mini]
        if list[mini] > list[i]:
            mini = i
    return ans

N = int(input())
prolist = []
for i in range(N):
    prolist.append(int(input()))
print(maxProfit(prolist))

Solution (Python):
def MaximumProfit():
    N = int(input())
    num = int(input())
    _min = num

    num2 = int(input())
    if num2 < num : _min = num2
    _maxn2n = num2 - num
    
    for i in range(2, N):
        num = int(input())
        if _maxn2n < num - _min : _maxn2n = num - _min
        if _min > num : _min = num

    return _maxn2n

print(MaximumProfit())

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

Solution (Python):
num = int(input())
max_value = -20000000000
min_value = int(input())

for i in range(num-1):
    value = int(input())
    max_value = max([max_value, value - min_value])
    min_value = min([min_value, value])
    
print(max_value)

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())
lst = []
  
  
for i in range(N):
  lst.append(int(input()))
  
M = lst[1] - lst[0]
m = lst[0]
  
for j in range(1, N):
    M = max([M, lst[j] - m])
    m = min([m, lst[j]])
              
print(M)

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):
num_inputs = int(input())

for i in range(num_inputs):
    current_rate = int(input())
    if i == 0:
        minimum_rate = current_rate
        continue
    
    profit = current_rate - minimum_rate
    if i == 1:
        max_profit = profit
    elif profit > max_profit:
        max_profit = profit
    
    if current_rate < minimum_rate:
        minimum_rate = current_rate

print(max_profit)

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

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):
n=int(input())
r=[int(input()) for _ in range(n)]
b=r[0]
h=[]
for i in range(1,n):
    h.append(r[i]-b)
    b=min(b,r[i])
print(max(h))

Solution (Python):
N = int(input())
k = 0
ans = -(10**10)
for i in range(N):
    R = int(input())
    if i==0:k=R;continue
    ans = max(ans,R-k)
    k = min(k,R)
print(ans)

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):
n=int(input())
r=[]
for _ in range(n):
    r.append(int(input()))
maxv=r[1]-r[0]
minv=r[0]
for j in range(1,n):
    maxv=maxv if maxv>r[j]-minv else r[j]-minv
    minv=minv if minv<r[j] else r[j]
print(maxv)

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

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

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)

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

maxv = -20000000000000000
minv = r[0]
for rt in range(1,len(r)):
    maxv = max(maxv,r[rt] - minv)
    minv = min(minv,r[rt])

print(maxv)

Solution (Python):
n = int(input())
 
ans = -10000000000000000000
mn = 1000000000000000000
for i in range(n):
    x = int(input())
    ans = max(ans, x - mn)
    mn = min(mn, x)
 
print(ans)

Solution (Python):
n = int(input())
c = []
max = -(10)**12
min = (10)**12

for i in range(n):
  m = int(input())
  
  if(max < m - min):
    max = m - min
  if min > m:
    min = m
  c.append(m)

print(max)

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

min_x = r[0]
profit = -(10 ** 9)
for i in range(1, n):
  min_x = min(min_x, r[i-1])
  profit = max(profit, r[i] - min_x)
print(profit)

Solution (Python):
n = int(input())
min_r = int(input())
max_val = 1 - min_r
for _ in range(n - 1):
    r = int(input())
    diff_r = r - min_r
    if diff_r >= max_val:
        max_val = diff_r
    if r <= min_r:
        min_r = r
print(max_val)

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

in_list = [int(input()) for _ in range(n)]
min_val = in_list[0]
max_sub = -1e9

for i in range(1, n):
    if in_list[i] - min_val > max_sub:
        max_sub = in_list[i] - min_val

    if in_list[i] < min_val:
        min_val = in_list[i]

print(max_sub)

Solution (Python):
N = int(input())
lst = []
 
 
for i in range(N):
  lst.append(int(input()))
 
M = lst[1] - lst[0]
m = lst[0]
 
for j in range(1, N):
    M = max([M, lst[j] - m])
    m = min([m, lst[j]])
             
print(M)

Solution (Python):
class inte:
	n = 0
	def __init__(self, n):
		self.n = n
	def chmax(self, n):
		self.n = max(self.n, n)
	def chmin(self, n):
		self.n = min(self.n, n)

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

INF = 10 ** 12

min_v = inte(A[0])
max_o = inte(-INF)

for a in A[1:]:
	max_o.chmax(a - min_v.n)
	min_v.chmin(a)

print(max_o.n)

Solution (Python):
import math

n=int(input())

t=int(input())
mint=t
t1=int(input())
mint = min(t, t1)
maxtt=t1-t


for i in range(2,n):
    t=int(input())
 
    maxtt = max(t-mint, maxtt)
    mint = min(t, mint)
    
    
    
print(int(maxtt))

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

Solution (Python):
a,b=-1e11,1e11
for _ in range(int(input())):
    c=int(input())
    a,b=max(a,c-b),min(b,c)
print(a)

Solution (Python):
n = input()
Rt = []
for i in range(int(n)):
    Rt.append(int(input()))   
R  = []
maxv = -10**9
minv = Rt[0]

for i in range(1,len(Rt)):
    if maxv < Rt[i]-minv:
        maxv = Rt[i]-minv
    if minv > Rt[i]:
        minv = Rt[i]
    
print(maxv)

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

r = [int(input()) for i 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)

Solution (Python):
N=int(input())
saitei=10**9
result=-(10**9)
for i in range(N):
  R=int(input())
  if i > 0 and result < R-saitei:
    result = R-saitei
  if saitei > R:
    saitei = R
print(result)

Solution (Python):
n = int(input())
prev = 10**18
ans = -10**18
for i in range(n):
    r = int(input())
    if i:
        ans = max(ans, r - prev)
    prev = min(prev, r)
print(ans)

Solution (Python):
if __name__ == '__main__':
    N = int(input()) 
    minv = int(input())
    maxv = 10**9*(-1)
    for j in range(N-1):
        r = int(input())
        if maxv < r-minv: maxv = r-minv
        if minv >r: minv = r
    print(str(maxv))

Solution (Python):
n = int(input())
R = [int(input()) for i in range(n)]
ans = R[1] - R[0]
R_i = R[0]
for j in range(1,n):
    ans = max(ans, R[j] - R_i)
    R_i = min(R_i, R[j])
print(ans)

Solution (Python):
n = int(input())
A = [int(input()) for a in range(n) ]
minA = A[0]
maxsa = A[1] - A[0]
for i in range(1, n):
    s = A[i] - minA
    minA = min(minA, A[i])
    maxsa = max(maxsa, s)

print(maxsa)

Solution (Python):
if __name__=='__main__':
    N = int(input())
    s = -1000000000
    a = int(input())
    for i in range(1, N):
        b = int(input())
        s = b - a if s < b - a else s
        a = b if b < a else a
    print(s)

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

r = -10**9
m = int(input())
inp = m

for i in range(n-1):
    m = min(m, inp)
    inp = int(input())
    r = max(r, inp-m)
    
print(r)

Solution (Python):
if __name__ == "__main__":
    time_interval = int(input())
    t0_num = int(input())
    min_num = t0_num
    t1_num = int(input())
    if t1_num < t0_num:
        min_num = t1_num
    max_diff = t1_num - t0_num

    for _ in range(2, time_interval):
        t_num = int(input())
        if max_diff < t_num - min_num:
            max_diff = t_num - min_num
        if t_num < min_num:
            min_num = t_num

    print(f"{max_diff}")