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):
n = int(input())
li = [int(input()) for _ in range(n)]
minv = li[0]
maxv = 10**9*(-1)

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

print(maxv)

Solution (Python):
import sys
mx = -sys.maxsize - 1
mi = sys.maxsize
for i in range(int(input())):
    x1 = int(input())
    mx = max(mx, x1 - mi)
    mi = min(mi, x1)
print(mx)

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

minv = int(input())
maxp = -1 * 10 ** 9
for _ in range(n - 1):
    tmp = int(input())
    maxp = max(maxp, tmp - minv)
    minv = min(minv, tmp)

print(maxp)

Solution (Python):
import sys
import math

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

profit = -math.inf
lowest = math.inf
for _ in range(n):
    r = int(sys.stdin.readline())
    profit = max(profit, r - lowest)
    lowest = min(lowest, r)

print(profit)

Solution (Python):
if __name__ == '__main__':
	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):
n = int(input())
R, maxv = [], None
for _ in range(n):
    R.append(int(input()))
minv = R[0]
for j in range(1,n):
    maxv = max(maxv, R[j]-minv) if maxv else R[j]-minv
    minv = min(minv, R[j])
print(maxv)

Solution (Python):
n = int(input())
min_number = int(input())
max_profit = -2000000000000000000000000000
for i in range(n-1):
    number = int(input())    
    if max_profit < number-min_number:
        max_profit = number - min_number
    if number < min_number:
        min_number = number
print(max_profit)

Solution (Python):

if __name__ == "__main__":
    n = int(input())
    R = []
    for i in range(0,n,1):
        R.append(int(input()))
    maxv = R[1] - R[0]
    minv = R[0]
    for i in range(1,n,1):
        maxv = max(maxv, R[i]-minv)
        minv = min(minv,R[i])
    print(maxv)

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

maxv = -float('inf')
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 = -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().strip())
r = [int(input().strip()) for _ in range(n)]
minv = r.pop(0)
maxv = -10**9
for ri in r:
    maxv = max(maxv, ri-minv)
    minv = min(minv, ri)
print(maxv)

Solution (Python):
def main():
	n = int(input())
	r = [int(input()) for _ in range(n)]
	rmax = r[1] - r[0]
	rmin = r[0]
	for i in range(1, n):
		rmax = max(rmax, r[i] - rmin)
		rmin = min(r[i], rmin)
	print(rmax)

main()

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

Solution (Python):
n = int(input())
count = 0
l = []

for i in range(0, n):
    b = int(input())
    l.append(b)

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

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

print(maxv)

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

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

sabun=0
for i in range(n):
    r=int(input())
    if i>=1:
        sabun=r-min_r
        if i==1:
            saidai=sabun
        if sabun>saidai:
            saidai=sabun
    if i==0:
        min_r=r
    elif r<min_r:
        min_r=r

print(saidai)

Solution (Python):
n=int(input())
t=int(input())
t1=int(input())
maxim=t1-t
if t<=t1:
    max_i=t1
    min_i=t
else:
    min_i=t1
    max_i=t
if n>2:
    for i in range(2,n):
        t=int(input())
        if t-min_i>maxim:
            maxim=t-min_i
        if t>max_i:
            max_i=t
        elif t<min_i:
            min_i=t
print(maxim)

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

min_num = int(input())
max_num = 0
profit = -999999999

for i in range(0, n - 1):
    num = int(input())
    temp = num - min_num
    if temp > profit:
        profit = temp
    if num < min_num:  
        min_num = num
        max_num = num
    if num > max_num:
        max_num = num

print(profit)

Solution (Python):
n = int(input())
r0 = int(input())
r1 = int(input())
mx = r1 - r0
mn = min(r0, r1)

for i in range(2, n):
    r = int(input())
    mx = max(mx, r - mn)
    mn = min(r, mn)

print(mx)

Solution (Python):
def solve():
    loop = int(input())
    profit = 1 - 10 ** 9

    current_min = int(input())
    for _ in range(loop-1):
        now = int(input())
        if now - current_min > profit:
            profit = now - current_min

        if current_min > now:
            current_min = now
    print(profit)


if __name__ == '__main__':
    solve()

Solution (Python):


N = int(input())
aN = [int(input()) for i in range(N)]

start = aN[0]
ans = -float('inf')
for i in range(1,N):
    
    ans = max(aN[i] - start, ans)
    
    start = min(aN[i], start)
print(ans)

Solution (Python):
n = int(input())
num_list = [int(input()) for _ in range(n)]
max_num = 10**9*-1
min_num = num_list[0]

for i in range(1,len(num_list)):
    max_num = max(num_list[i]-min_num,max_num)
    min_num = min(num_list[i],min_num)

print(max_num)

Solution (Python):



n = int(input())
min = int(input())
profit = -999999999

for i in range(0,n-1):
    s = int(input())
    if (s - min > profit):
        profit = s - min
    if (min > s):
        min = s
print (profit)

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

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

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

if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
minR=int(input())
R=[int(input()) for i in range(n-1)]
maximum=R[0]-minR
for r in R:
    maximum=max(maximum, r-minR)
    minR=min(minR, r)
print(maximum)

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

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

ans = rlist[1] - rlist[0]
m = rlist[0]

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

print(ans)

Solution (Python):


















    
    
   
        
        
        
        
        

import sys

def main():
    inputs = int(sys.stdin.readline().rstrip())
    maxv = -100000000000
    minv = int(sys.stdin.readline().rstrip())

    
    for i in range(1, inputs):
        compv = int(sys.stdin.readline())
        if compv - minv > maxv:
            maxv = compv - minv
        if compv < minv:
            minv = compv
        

    bigprice = maxv
    print(bigprice)



if __name__ == '__main__':
    main()

Solution (Python):
n=[]
for i in range(int(input())):
	n.append(int(input()))
n_min=n[0]
n_max=n[1]-n_min
for i in range(1,len(n)):
	if n_max<n[i]-n_min:
		n_max=n[i]-n_min
	if n_min>n[i]:
		n_min=n[i]
print(n_max)

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

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

Solution (Python):
N = int(input())
r0, r1 = int(input()), int(input())
if r0 < r1:
    mint = r0
else:
    mint = r1
d = r1 - r0
for c in range(N-2):
    rj = int(input())
    dj = rj - mint
    if dj > d:
        d = dj
    if rj < mint:
        mint = rj
print(d)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    lst = [int(input()) for x in range(n)]
    max_lst = []
    tmpmax = 0
    for i in range(1, n + 1):
        tmpmax = max(tmpmax, lst[-i])
        max_lst.append(tmpmax)
    maxA = -(10 ** 9 + 1)
    for i in range(n - 1):
        maxA = max(maxA, max_lst[-i-2] - lst[i])
    print(maxA)

Solution (Python):
max_p=-10**11
n=int(input())
min_r=int(input())
for i in range(n-1):
    r = int(input())
    p = r-min_r
    if p>max_p:
        max_p = p
    if r<min_r:
        min_r = r
    print
print(max_p)

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

mx = [-1]*n
mn = [float('inf')]*n
val = [0]*n
for i in range(n):
    v = int(input())
    mx[i] = mn[i] = v

for i in range(1,n):
    mx[n-i-1] = max(mx[n-i-1],mx[n-i])
    mn[i] = min(mn[i],mn[i-1])

ans = -float('inf')
for i in range(n-1):
    ans = max(ans,mx[i+1]-mn[i])

print(ans)

Solution (Python):
n = int(input())
index=0
minL=[]
moneyL=[]
while index < n:
    inputN = int(input())
    moneyL.append(inputN)
    if index==0:
        minL.append(inputN)
    else:
        if minL[index-1] < inputN:
            minL.append(minL[index-1])
        else:
            minL.append(inputN)
    index+=1

index2=1
maxProfit = -10**9
while index2 < len(moneyL):
    if moneyL[index2] - minL[index2-1] > maxProfit:
        maxProfit = moneyL[index2] - minL[index2-1]
    else:
        pass
    index2+=1

print(maxProfit)

Solution (Python):
n=int(input())
min=10**10
max=-10**10
profit=max-min
for i in range(0,n):
    x=int(input())
    if(max < x and i!=0):
        max=x
        if(profit < max-min):
            profit=max-min
    if( x < min):
        min=x
        max=-10**10
print(profit)

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

def fx(raw):
    min_n = raw[0]
    max_n = raw[1] - raw[0]
    for i in raw[1:]:
        if i - min_n >= max_n:
            max_n = i - min_n
        if i < min_n:
            min_n = i
    return max_n


if __name__ == '__main__':
    print(fx(m))

Solution (Python):
R = []

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

maxv = 10**9 * (-1)
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 _ in range(n)]
minv = r[0]
maxv = -10000000000000000
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)]
minv = r[0]
maxv = -10000000000000000
for i in range(1, len(r)):
    maxv = max(maxv, r[i] - minv)
    minv = min(minv, r[i])
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):
R = [int(input()) for _ in [0]*int(input())]
minv = R[0]
maxv = -1e+10
for j in range(1, len(R)):
    maxv = max(maxv, R[j]-minv)
    minv = min(minv, R[j])
print(maxv)

Solution (Python):
n = int(input())
count = 0
list_prob = []
while count < n:
    list_prob.append(int(input()))
    count += 1
maxv = list_prob[1] - list_prob[0]
minv = list_prob[0]
for j in range(1,n):
    maxv = max(maxv, list_prob[j] - minv)
    minv = min(minv, list_prob[j])
print(maxv)

Solution (Python):
n = int(input())
rmin = int(input())
r=int(input())
dmax = r-rmin
if rmin>r: rmin=r
for _ in range(n-2):
    r = int(input())
    if dmax<r-rmin: dmax=r-rmin
    if rmin>r: rmin=r

print(dmax)

Solution (Python):
n=int(input())
list=[]
for i in range(0,n):
    a=int(input())
    list.append(a)
minv=list[0]
maxv=-10000000000
for i in range(1,n):
    maxv=max(maxv,list[i]-minv)
    minv=min(minv,list[i])
print(str(maxv))

Solution (Python):
n = int(input())
minr=10**10
maxr=-10**10
for i in range(n):
    r = int(input())
    maxr = max(maxr,r-minr)
    minr = min(minr, r)
    
print(maxr)

Solution (Python):

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

maxv = -2 * (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):
import math
n = int(input())
_min = _max = -1000000000
_min = int(input())
for i in range(1,n):
    tmp = int(input())
    _max = max(_max, tmp - _min)
    _min = min(_min, tmp)
print(_max)

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

    ans = R[1] - R[0]
    
    current_min = min(R[0], R[1])

    for i in range(2, N):
        ans = max(ans, R[i] - current_min)
        current_min = min(current_min, R[i])

    print(ans)

main()

Solution (Python):
N = int(input())
R = []
for _ in range(N):
    R.append(int(input()))
M = - 10 ** 100
m = 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

inf = 10**9 + 1

def solve():
    n = int(stdin.readline())
    max_d = -inf
    min_v = inf * 2

    for i in range(n):
        r = int(stdin.readline())

        max_d = max(max_d, r - min_v)
        min_v = min(min_v, r)

    print(max_d)


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

if __name__ == '__main__':
    solve()

Solution (Python):

n=int(input())
l=[int(input()) for _ in range(n)]
ans=l[1]-l[0]
bottom=min(l[0],l[1])

for i in l[2:]:
    ans=max(ans,i-bottom)
    bottom=min(i,bottom)

print(ans)

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

price = []

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

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

for i in range(1, n):
    if max < price[i] - min:
        max = price[i] - min

    if min > price[i]:
        min = price[i]


print(max)

Solution (Python):
n = int(input())
r = []
for i in range(n):
    r.append(int(input()))
min = r[0]
max_prof = -2000000000000
for i in range(1, n):
    if (r[i] - min) > max_prof:
        max_prof = r[i] - min
    if r[i] < min:
        min = r[i]
print(max_prof)

Solution (Python):
def main():
    profit, loss = -1e+10, -1e+10
    price, high, low, before = 0, 0, 1e+10, 1e+10
    win = False
    for _ in range(int(input())):
        price = int(input())
        if low > price:
            profit = max(profit, high - low)
            loss = max(loss, price - before)
            high, low, before = price, price, price
        else:
            high = max(high, price)
            before = price
            win = True
    print(max(profit, high - low) if win else loss)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
val = -float("inf")
minR = int(input())

for j in range(1,n):
    R = int(input())
    if R - minR > val:
        val = R - minR
    if R < minR:
        minR = R
        
print(val)

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

max=-10000000000
n1=int(input())
min=n1
for i in range(n-1):
    a=int(input())
    if max<a-min:
        max=a-min
    if a<min:
        min=a
print(max)

Solution (Python):
n = 0
dat = []


def input_file():
    global n, dat
    f = open('input', mode='r')
    n = int(f.readline())
    for _ in range(n):
        i = int(f.readline())
        dat.append(i)


def input_cmd():
    global n, dat
    n = int(input())
    for _ in range(n):
        dat.append(int(input()))


input_cmd()








minV = dat[0]
maxV = -2e10
for i in range(1, n):
    r = dat[i] - minV
    maxV = r if r > maxV else maxV
    
    minV = dat[i] if dat[i] < minV else minV
print(maxV)

Solution (Python):
n = 0
dat = []


def input_file():
    global n, dat
    f = open('input', mode='r')
    n = int(f.readline())
    
    
    
    for _ in range(n):
        i = int(f.readline())
        dat.append(i)

    


def input_cmd():
    global n, dat
    n = int(input())
    for _ in range(n):
        dat.append(int(input()))


input_cmd()








minV = dat[0]
maxV = -2e10
for i in range(1, n):
    r = dat[i] - minV
    maxV = r if r > maxV else maxV
    minV = dat[i] if dat[i] < minV else minV
print(maxV)

Solution (Python):
cnt = int(input())
concurrency_list = []

for _ in range(cnt):
    concurrency_list.append(int(input()))

minv = concurrency_list[0]
maxv = -100000000000

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

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

maxt = t - mint
if t < mint: mint = t

for _ in range(2, n):
    t = int(input())
    if t - mint > maxt : maxt = t - mint
    if t < mint : mint = t

print(maxt)

Solution (Python):
import sys
input_lines = [int(i) for i in sys.stdin.readlines()]
n = input_lines[0]
ans = input_lines[2] - input_lines[1]
if n > 2:
    min_num = min(input_lines[1], input_lines[2])
    for i in input_lines[3:]:
        ans = max(ans, i-min_num)
        min_num = min(i, min_num)
print(ans)

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

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

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

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())
prices = [int(input()) for _ in range(n)]
max_profit = prices[1] - prices[0]
min_price = prices[0]
for price in prices[1:]:
    max_profit = max(max_profit, price - min_price)
    min_price = min(min_price, price)

print(max_profit)

Solution (Python):

n=int(input())
a=[int(input()) for i in range(n)]
min1=a[0]
max1=-float('inf')
for i in range(1, n):
    max1=max(a[i]-min1,max1)
    min1=min(a[i],min1)
print(max1)

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 = int(input())
minv, maxv = 0, 0
for i in range(n):
    x = int(input())
    if i == 0: minv = x
    elif i == 1: maxv = x - minv 
    if maxv < x - minv: maxv = x - minv
    if minv > x: minv = x
print(maxv)

Solution (Python):
import math

n=int(input())
t=int(input())

mnt=t
t1=int(input())
if t1<t:
    mnt=t1
mxt=t1-t


for i in range(2,n):
    t=int(input())

    if t-mnt>mxt:
        mxt=t-mnt
    if t<mnt:
        mnt=t

print(int(mxt))

Solution (Python):
n = int(input())
min_ = float("INF")
max_ = float("-INF")
for i in range(n):
  r = int(input())
  max_ = max(max_, r-min_)
  min_ = min(min_, r)
print(max_)

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

Solution (Python):

maxv = -10**10
minimum = -10**10
for _ in range(int(input())):
    rt = int(input())
    if minimum == -10**10:
        minimum = rt
        continue
    if rt-minimum > maxv:
        maxv = rt-minimum
    if rt < minimum: minimum = rt
print(maxv)

Solution (Python):
n=int(input())
arr=[]
for i in range(n):
    arr.append(int(input()))
maxp=[]
max_=0
min_=20000000000
rooped=0
for j in arr:
    if max_<j:
        max_=j
        
        maxp.append(max_-min_)
    if min_>j:
        min_=j
        max_=0
maxp.sort()
maxp.reverse()
    
print(maxp[0])