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())
datas = [int(input()) for _ in range(N)]

now_max = max(datas[1:])
max_index = datas.index(now_max)
diff = now_max - datas[0]

for i in range(1,N-1):
    if i == max_index:
        now_max = max(datas[i+1:])
        max_index = datas.index(now_max,i+1)
    new_diff = now_max - datas[i]
    if diff < new_diff:
        diff = new_diff

print(diff)

Solution (Python):
n = int(input())
min_r, max_d = int(input()), - 10**9
for _ in range(n - 1):
    r = int(input())
    max_d = max(max_d, r - min_r)
    min_r = min(min_r, r)
print(max_d)

Solution (Python):
N = int(input())
datas = [int(input()) for _ in range(N)]
now_max = max(datas[1:])
max_index = (N-1) - datas[::-1].index(now_max)
_min = min(datas)
min_index = datas.index(_min)

if max_index > min_index:
    diff = now_max - _min
else:
    diff = now_max - datas[0]
    
    for i in range(1,N-1):
        if i == max_index:
            now_max = max(datas[i+1:])
            max_index = datas.index(now_max,i+1)
        new_diff = now_max - datas[i]
        if diff < new_diff:
            diff = new_diff

print(diff)

Solution (Python):
from sys import stdin

N,*datas = [int(i) for i in stdin.readlines()]
now_max = max(datas[1:])
max_index = (N-1) - datas[::-1].index(now_max)
_min = min(datas)
min_index = datas.index(_min)

if max_index > min_index:
    diff = now_max - _min
else:
    diff = now_max - datas[0]
    
    for i in range(1,N-1):
        if i == max_index:
            now_max = max(datas[i+1:])
            max_index = datas.index(now_max,i+1)
        new_diff = now_max - datas[i]
        if diff < new_diff:
            diff = new_diff

print(diff)

Solution (Python):
from sys import stdin

N,*datas = [int(i) for i in stdin.readlines()]
now_max = max(datas[1:])
max_index = (N-1) - list(reversed(datas)).index(now_max)
_min = min(datas)
min_index = datas.index(_min)

if max_index > min_index:
    diff = now_max - _min
else:
    diff = now_max - datas[0]
    
    for i in range(1,N-1):
        if i == max_index:
            now_max = max(datas[i+1:])
            max_index = datas.index(now_max,i+1)
        new_diff = now_max - datas[i]
        if diff < new_diff:
            diff = new_diff

print(diff)

Solution (Python):
from sys import stdin

N,*datas = [int(i) for i in stdin.readlines()]
now_max = max(datas[1:])
max_index = datas.index(now_max)
diff = now_max - datas[0]

for i in range(1,N-1):
    if i == max_index:
        now_max = max(datas[i+1:])
        max_index = datas.index(now_max,i+1)
    new_diff = now_max - datas[i]
    if diff < new_diff:
        diff = new_diff

print(diff)

Solution (Python):
from sys import stdin

N,*datas = [int(i) for i in stdin.readlines()]
now_max = max(datas[1:])
max_index = datas.index(now_max)
diff = now_max - datas[0]

for i in range(1,N-1):
    if i == max_index:
        now_max = max(datas[i+1:])
        max_index = datas.index(now_max,i+1)
    new_diff = now_max - datas[i]
    if diff < new_diff:
        diff = new_diff

print(diff)

Solution (Python):

n = int(input())
M = 10**9

R = int(input())
minval = R
maxval = -M
for i in range(1,n):
  R = int(input())
  maxval = max(maxval,R-minval)
  minval = min(minval,R)

print(maxval)

Solution (Python):



n = int(raw_input())
R = [int(raw_input()) for i in range(n)]

maxv = -2000000000
minv = R[0]

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

print maxv

Solution (Python):
import sys
b,s=-1e10,1e10
input()
for r in map(int,sys.stdin):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):
import sys
b,s=-1e10,1e10
input()
for r in map(int,sys.stdin):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):
import sys
s=1e10;b=-s
input()
for r in map(int,sys.stdin):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):
import sys
s=1e10;b=-s
input()
for r in map(int,sys.stdin):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):
import sys
s=1e10;b=-s
input()
for r in map(int,sys.stdin):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):
import sys
s=1e10;b=-s
input()
for r in map(int,sys.stdin.readlines()):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):
import sys
s=1e10;b=-s
input()
for r in map(int,sys.stdin.readlines()):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):
import sys
s=1e10;b=-s
input()
for r in map(int,sys.stdin.readlines()):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):



import sys

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

max_diff = min(r) - max(r)
max_v = r[0]

for i in range(n-1):
    if r[i] == max_v:
        max_v = max(r[i+1:])
    diff = max_v - r[i]
    if diff > max_diff:
        max_diff = diff

print(max_diff)

Solution (Python):
import sys
  
input()
minv = int(input())
maxv = -1000000000
  
for r in map(int,sys.stdin.readlines()):
  m = r-minv
  if maxv < m: 
    maxv = m
    if m < 0: minv = r
  elif m < 0: minv = r
  
print(maxv)

Solution (Python):
def maximum_profit(n):
    maxv = min(n) - max(n)
    minv = n[0]
    for i in range(1, len(n)):
        maxv = n[i] - minv if maxv < n[i] - minv else maxv
        minv = n[i] if minv > n[i] else minv
    return maxv

if __name__ == '__main__':
    n = []
    for _ in range(int(input())):
        n.append(int(input()))
    print(maximum_profit(n))

Solution (Python):
row_num = int(input())
nums = []
for row in range(row_num):
    nums.append(int(input()))

min_diff = nums[0]
max_diff = nums[1] - nums[0]
for i in range(1, len(nums)):
    if max_diff < nums[i] - min_diff:
        max_diff = nums[i] - min_diff
    if min_diff > nums[i]:
        min_diff = nums[i]

print(max_diff)

Solution (Python):
import sys
def m():
 s=1e10;b=-s
 input()
 for r in map(int,sys.stdin):
  if b<r-s:b=r-s
  if s>r:s=r
 print(b)
if'__main__'==__name__:m()

Solution (Python):
import sys
def m():
 s=1e10;b=-s
 input()
 for r in map(int,sys.stdin):
  if b<r-s:b=r-s
  if s>r:s=r
 print(b)
if'__main__'==__name__:m()

Solution (Python):
import sys
def m():
 s=1e10;b=-s
 input()
 for r in map(int,sys.stdin):
  if b<r-s:b=r-s
  if s>r:s=r
 print(b)
m()

Solution (Python):
import sys
def m():
 s=1e10;b=-s
 input()
 for r in map(int,sys.stdin):
  if b<r-s:b=r-s
  if s>r:s=r
 print(b)
m()

Solution (Python):
import sys
def m():
 s=1e10;b=-s
 input()
 for r in map(int,sys.stdin):
  if b<r-s:b=r-s
  if s>r:s=r
 print(b)
if'__main__'==__name__:m()

Solution (Python):
import sys

length = int(sys.stdin.readline())
value =[ int(num) for num in sys.stdin.readlines() ]
minv = value[0]
maxv = value[1] - value[0]

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

print(maxv)

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

    n = len(r)
    maxv = -20000000000
    minv = r[0]

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


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
 
R = [int(input()) for i in range(N)]
 
maxv = -2000000000
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 sys
if False:

    fh=open('ALDS1_1_D1.txt','r')
else:
    fh=sys.stdin
input=fh.readline

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

    pmax=a[1]-a[0]
    pmin=a[0]
    for j in range(1, n):
        pmax = max(a[j]-pmin,pmax)
        pmin = min(pmin,a[j])

    print(pmax)
    
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
y0 = int(input())
y1 = int(input())
m = min(y0, y1)
M = y1 - y0
i = 2
while i < n:
    y = int(input())
    M = max(M, y - m)
    m = min(m, y)
    i += 1
print(M)

Solution (Python):
n = eval(input())
R = [None]*n
for i in range(n):
    R[i] = eval(input())
maxv = -2e9
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 = input()
prices = list()
for idx in range(int(n)):
    tmp = input()
    prices.append(int(tmp))

minp = prices[0]
maxv = prices[1] - prices[0]

for price in prices[1:]:
    maxv = max(maxv, price - minp)
    minp = min(minp, price)

print(maxv)

Solution (Python):
def MaximumProfit(l, N):
    maxv = -1000000000
    minv = l[0]
    for i in range(1,N):
        maxv = max(maxv, l[i] - minv)
        minv = min(minv, l[i])
    print(maxv)

def main():
    
    N = int(input())
    l = [int(input()) for i in range(N)]
    
    MaximumProfit(l, N)

if __name__ == '__main__': main()

Solution (Python):
import sys

line_num = int(sys.stdin.readline().rstrip())
maxv = -2000000000
minv = int(sys.stdin.readline().rstrip())

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

print(maxv)

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

maxv = -2000000000

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

minv = lst[0]

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

print(maxv)

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

Solution (Python):

import sys



min_num = -sys.maxsize - 1

max_diff = -sys.maxsize - 1
for idx_i in range(int(input())):
    Rt = int(input())
    if(not idx_i):
        min_num = Rt
    else:
        tmp = Rt - min_num
        if(tmp > max_diff):
            max_diff = tmp
        if(Rt < min_num):
            min_num = Rt
print(max_diff)

Solution (Python):
import sys

max_diff = -sys.maxsize - 1
for idx in range(int(input())):
    Rt = int(input())
    if(idx):
        tmp = Rt - min_num
        if(tmp > max_diff):
            max_diff = tmp
        if(Rt < min_num):
            min_num = Rt
    else:
        min_num = Rt
print(max_diff)

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

minv = R[0]
max_profit = -1 * 10**9

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

print(max_profit)

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

minv = R[0]
max_profit = -1 * 10**9

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

print(max_profit)

Solution (Python):
from collections import defaultdict
import math




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

Solution (Python):
n = int(input())
a = int(input())
b = int(input())
max_value = b - a
min_number = min(a, b)
for j in range(n-2):
  r = int(input())
  max_value = max(max_value, r - min_number)
  min_number = min(min_number, r)
print(max_value)

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())
min_v = int(input())
ans = 1 - min_v
for i in range(1, n):
    j = int(input())
    ans = max(ans, j - min_v)
    min_v = min(min_v, j)
print(ans)

Solution (Python):
def max_profit(prices):
    """returns max(prices[j] - prices[i]) while j > i


    >>> max_profit([5, 3, 1, 3, 4, 3])
    3
    >>> max_profit([4, 3, 2])
    -1
    """
    minprice = prices[0]
    maxprofit = -prices[0]

    for p in prices[1:]:
        if maxprofit < p - minprice:
            maxprofit = p - minprice
        if minprice > p:
            minprice = p

    return maxprofit


def run():
    count = int(input())
    prices = []

    for i in range(count):
        prices.append(int(input()))

    print(max_profit(prices))


if __name__ == '__main__':
    run()

Solution (Python):
def set_values():
	values = []
	for i in range(num):
		values.append(int(input()))
	return values

num = int(input())
values = set_values()
max_value = values[1] - values[0]
min_value = values[0]

for i in range(1, num):
	max_value = max(max_value, (values[i] - min_value))
	min_value = min(min_value, values[i])

print(max_value)

Solution (Python):
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
nowmin = a[0]
nowmax = max(a[1:])
ans = nowmax - nowmin
for i in range(n-1):
  if a[i] == nowmax:
    nowmax = max(a[i+1:])
  if a[i] < nowmin:
    k = nowmax - a[i]
    if k > ans:
      ans =  k
      nowmin = a[i]


print(ans)

Solution (Python):
N = int(input())
a = []

for i in range(N):
	a.append(int(input()))

maxv = -10e10
minv = a[0]

for i in range(1, N):
	maxv = a[i] - minv if maxv < a[i] - minv else maxv
	minv = a[i] if minv > a[i] else minv

print(maxv)

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

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

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

print(maxv)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
ma = a[1]-a[0]
mi = a[0]
for i in range(1,n):
    ma = max(ma, a[i] - mi)
    mi = min(mi, a[i])
    
print(ma)

Solution (Python):
n = int(input())
x = []
for i in range(n):
    x.append(int(input()))
mx = float("inf") * (-1)
mn = x[0]
for i in range(1,n):
    if mx < (x[i] - mn):
        mx = x[i] - mn
    if mn > x[i]:
        mn = x[i]
print(mx)

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

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

for i in range(n): R.append(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())
R = [int(input()) for _ in range(n)]
minv, maxv = R[0], 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):
values = [int(input()) for _ in range(int(input()))]
maxv = values[1] - values[0]
minv = values[0]

for val in values[1:]:
    maxv = max(maxv, val - minv)
    minv = min(minv, val)
print(maxv)

Solution (Python):
values = [int(input()) for _ in range(int(input()))]
maxv = values[1] - values[0]
minv = values[0]

for val in values[1:]:
    maxv = max(maxv, val - minv)
    minv = min(minv, val)         
print(maxv)

Solution (Python):


def Maximum_Profit(n,A):
    Max=A[1]-A[0]
    Min=A[0]
    for j in range(1,n):
        Max=max(Max,A[j]-Min)
        Min=min(Min,A[j])
    return Max

if __name__ == "__main__":
    n=int(input())
    A=[]
    for i in range(n):
        A.append(int(input()))
    Max=Maximum_Profit(n,A)
    print(Max)

Solution (Python):
def parse_stdin(input_lines: int):
    data_list = []
    for i in range(input_lines):
        data_list.append(input().rstrip())
    return data_list


def find_max_interest(rates):
    min_value = rates[0]
    max_interest = -20000000000
    for i, rate in enumerate(rates[1:]):
        max_interest = max(max_interest, rate - min_value)
        min_value = min(min_value, rate)
    return max_interest


def main():
    input_lists = parse_stdin(int(input()))
    rates = list(map(int, input_lists))
    print(find_max_interest(rates))


if __name__ == '__main__':
    main()

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

max = a[1]-a[0]
min = 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):

def Maximum(R,n):
	Max=-10**9
	Min=R[0]
	for i in range(1,n):
		tmp=R[i]-Min
		if Max<tmp:
			Max=tmp
		if Min>R[i]:
			Min=R[i]
	
	return Max

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

print(Max)

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

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

R_distance_max = -1000000000
R_min = R[0]

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

print(R_distance_max)

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
max = A[1]-A[0]
min = 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):
    R.append(int(input()))

minv = R[0]
maxv = -1.0 * 10**9
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())
maxn=-10**12
minn=int(input())

for j in range(1, n):
    r = int(input())
    maxn = max(maxn, r - minn)
    minn = min(minn, r)
    
print(maxn)

Solution (Python):
n = int(input())
max_value = -10**9
min_value = int(input())
for i in range(1,n):
    r = int(input())
    max_value = max(max_value,r-min_value)
    min_value = min(min_value, r)

print(max_value)

Solution (Python):
import sys

line_num = int(sys.stdin.readline().rstrip())
maxv = -2000000000000000
minv = int(sys.stdin.readline().rstrip())

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

print(maxv)

Solution (Python):



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

min_i = 0
max_val = R[1]-R[0]
max_j = -1
for i in range(n-1):
    if R[min_i] < R[i]:
        continue
    elif R[min_i] > R[i]:
        min_i = i

    if max_j <= i:
        max_j = i+1
        for j in range(i+1, n):
            if R[max_j] < R[j]:
                max_j = j

    if max_val < R[max_j] - R[min_i]:
        max_val = R[max_j] - R[min_i]

print(max_val)

Solution (Python):
import sys
s=1e10;b=-s
input()
for r in map(int,sys.stdin):
 if b<r-s:b=r-s
 if s>r:s=r
print(b)

Solution (Python):
import sys
def f():
 s=1e10;b=-s
 input()
 for r in map(int,sys.stdin):
  if b<r-s:b=r-s
  if s>r:s=r
 print(b)
f()

Solution (Python):
n = int(input())
maxp = int(-1e10)
minv = int(1e10)
for i in range(n):
    p = int(input())
    maxp = max(maxp, p - minv)
    minv = min(minv, p)
print(maxp)

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

maxv = -2000000000
minv = R[0]

for r in R[1:]:

    maxv = max(maxv, r - minv)
    minv = min(minv, r)

print(maxv)

Solution (Python):

price = [int(input()) for p in range(int(input()))]










min_ = price[0]
max_ = -1000000000000
for i in range(1, len(price)):
    if price[i] - min_ > max_:
        max_ = price[i] - min_

    if price[i] < min_:
        min_ = price[i]

print(max_)

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

mi = 10**10
ans = -(10**10)
for r in rli:
    ans = max(ans, r - mi)
    if r < mi:
        mi = r
print(ans)

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

maxv = -float("inf")
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())

    min = int(input())
    ans = -2 * int(1e9+1)

    for _ in range(n-1):
        x = int(input())
        if x - min > ans:
            ans = x - min

        if x < min:
            min = x

    print(ans)

main()

Solution (Python):
import sys

count=int(input())
input_list=[ int(i) for i in sys.stdin.readlines() ]

first=0
second=1
maxv=-1000000000
minv=input_list[first]

for i in input_list[second:]:
    if i-minv > maxv:
        maxv=i-minv
    if i < minv:
        minv=i

print(maxv)

Solution (Python):


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

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

max_num = 1 - 10 ** 9
min_num = R[0]

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

print(max_num)

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())
    r0 = int(input())
    r1 = int(input())
    minv = r0 if r0 <= r1 else r1
    maxv = r1 - r0
    for i in range(2, n):
        r = int(input())
        maxv = max(maxv, (r-minv))
        minv = min(minv, r)
    print(maxv)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    maxv = -10 ** 9
    minv = int(input())

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

    print(maxv)

Solution (Python):
n = int(input())
data = list(map(int, [input() for i in range(n)]))
inf = -float("inf")
dp = [inf for i in range(n)]
tsum = 0
for i in range(1, n):
    t = data[i]-data[i-1]
    tsum += t
    if(dp[i] < tsum):
        dp[i] = tsum
    else:
        dp[i] = dp[i-1]
    if tsum < 0:
        tsum = 0
print(max(dp))

Solution (Python):
def maximum_profit(n):
    maxv = min(n) - max(n)
    minv = n[0]
    for i in range(1, len(n)):
        maxv = n[i] - minv if maxv < n[i] - minv else maxv
        minv = n[i]        if minv > n[i]        else minv
    return maxv
if __name__ == '__main__':
    n = int(input())
    numbers = [int(input()) for i in range(0, n)]
    print(maximum_profit(numbers))

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

diff = -10**9 - 1
mi = 10**9 + 1

for i in range(n-1):
    mi = min(r[i], mi)
    diff = max(r[i+1]-mi, diff)

print(diff)

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

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

print(maxv)

Solution (Python):

from math import sqrt, floor

def intinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def main():
    n = int(input())
    R = []
    for i in range(n): R.append(int(input()))
    
    
    
    
    
    Maxprof = R[1] - R[0]
    currentMin = R[0]
    for i in range(2, n):
        currentMin = min(currentMin, R[i - 1])
        Maxprof = max(Maxprof, R[i] - currentMin)
    print(Maxprof)        

if __name__ == "__main__":
    main()

Solution (Python):


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

print(maxv)

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

imin=0
imax=1
maxVal=x[imax]-x[imin]
for i in range(1,n):
    if x[imin] > x[i]:
        imin=i
        imax=i+1
        if imax > n-1:
            break
        dx = x[imax]-x[imin]
        if maxVal < dx:
            maxVal = dx
    elif x[imax] < x[i]:
        imax = i
        dx = x[imax]-x[imin]
        if maxVal < dx:
            maxVal = dx
print(maxVal)

Solution (Python):
n = int(input())
maxVal = -2000000000
minVal = int(input())
for i in range(1,n):
    x=int(input())
    dx = x-minVal
    if maxVal < dx:
        maxVal = dx
    if minVal > x:
        minVal = x
print(maxVal)

Solution (Python):
n = int(input())
maxVal = -2000000000
minVal = int(input())
for i in range(1,n):
    x=int(input())
    maxVal = max(maxVal,x-minVal)
    minVal = min(minVal,x)
print(maxVal)

Solution (Python):
n = int(input())
r0 = int(input())
r1 = int(input())
min_r = min(r0, r1)
ans = r1 - r0
for _ in range(2, n):
    r = int(input())
    ans = max(ans, r - min_r)
    min_r = min(r, min_r)
print(ans)

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

mini = a[0]
b = -10000000000
del a[0]
for i in a:
    if b < i - mini:
        b = i - mini
    if mini > i:
        mini = i

print(b)

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

min_rate = 10**15
max_prof = -10**15
for i in range(n):
    rate = int(input())
    prof = rate - min_rate
    if prof > max_prof:
        max_prof = prof
    if min_rate > rate:
        min_rate = rate

print(max_prof)

Solution (Python):
import sys

if __name__ == '__main__':
    l = map(int, sys.stdin)
    del l[0]
    maxp = -9999999999
    minv = l[0]
    for i in range(1, len(l)):
        maxp = max(maxp, l[i] - minv)
        minv = min(minv, l[i])
    print maxp

Solution (Python):
R = list()
n = int(input().rstrip())
for i in range(n):
    R.append(int(input().rstrip()))
maxv = R[1] - R[0]
minv = R[0]
for i in range(len(R) - 1):
    maxv = max(R[i + 1] - minv, maxv)
    minv = min(minv,R[i + 1])
print(maxv)

Solution (Python):
max_R = -10**9
for i in range(int(input())):
    if i == 0:
        min_R = int(input())
        continue
    R = int(input())
    if R - min_R > max_R:
        max_R = R - min_R
    if R < min_R:
        min_R = R
print(max_R)

Solution (Python):
n = int(input())
buy = 2000000001
sell = 0
max_profit = -1000000001
for _ in range(n):
    r = int(input())
    if r < buy:
        max_profit = max(max_profit, r - buy)
        buy = r
    else:
        sell = r
        max_profit = max(max_profit, sell - buy)

print(max_profit)

Solution (Python):
n = int(input())
mini = int(input())
ans = float('-inf')
for i in range(n-1):
    a = int(input())
    ans = max(a-mini, ans)
    mini = min(mini, a)
print(ans)

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

i = 0
while(i<n):
	x = input()
	x = int(x)
	list.append(x)
	i = i + 1

maxv = -2000000000
minv = list[0]
i = 1
while(i<n):
        maxv = max(maxv, list[i]-minv)
        minv = min(minv, list[i])
        i = i + 1
	
print(maxv)

Solution (Python):

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

max_profit = arr[1] - arr[0]
min_price = arr[0]
for i in range(1, n):
	current = arr[i]
	max_profit = max(max_profit, current - min_price)
	min_price = min(min_price, current)

print(max_profit)

Solution (Python):

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

max_profit = arr[1] - arr[0]
min_price = arr[0]
for i in range(1, n):
	current = arr[i]
	max_profit = max(max_profit, current - min_price)
	min_price = min(min_price, current)

print(max_profit)

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

n0 = int(input())
n1 = int(input())
max_profit = n1 - n0
min_price = min(n0, n1)
for i in range(n - 2):
	current = int(input())
	max_profit = max(max_profit, current - min_price)
	min_price = min(min_price, current)

print(max_profit)

Solution (Python):

n = int(input())
if n > 1:
	p1 = int(input())
	p2 = int(input())
	maxi = p2 - p1
	mini = min(p1, p2)
	for i in range(n - 2):
		current = int(input())
		maxi = max(maxi, current - mini)
		mini = min(mini,  current)
	print(maxi)

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

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

print(maxv)

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

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

print(maxv)

Solution (Python):
intMax = -10000000000
inputData = int(input())
inputList = list()
for i in range(inputData):
    inputList.append(int(input()))

inputDataMax = intMax
inputDataMin = inputList[0]
for i in range(inputData - 1):
    inputDataMax = max(inputDataMax,  inputList[i + 1] - inputDataMin)
    inputDataMin = min(inputDataMin, inputList[i + 1])

print(inputDataMax)

Solution (Python):
N = input()
N = int(N)
l = [int(input()) for i in range(N)]
flg = 1
ma = 0
mi = 0
for i in range(1,len(l)):
    if(mi > l[i-1] or flg ==1):
        mi = l[i-1]
    if(ma < l[i] - mi or flg ==1):
        ma = l[i] - mi
        flg = 0
print(ma)

Solution (Python):
N = int(input())
l = m = 0
for k in range(N):
    n = int(input())
    if k == 1 or m < n - l:
        m = n - l
    if k == 0 or n < l:
        l = n
print(m)

Solution (Python):
import math

class MaxPrice(object):
    
    def __init__(self):
        self.min_price = math.inf
        self.max_price = -math.inf
        self.max_diff = -math.inf

    def set_price(self, p):
        if self.max_price < p:
            self.max_price = p
        else:
            if self.max_diff < p - self.max_price:
                self.max_diff = p - self.max_price

        if p < self.min_price:
            self.min_price = p
        else:
            if self.max_diff < p - self.min_price:
                self.max_diff = p - self.min_price

def run():
    n = int(input())
    mp = MaxPrice()
    for i in range(n):
        p = int(input())
        mp.set_price(p)
    print(mp.max_diff)

if __name__ == '__main__':
    run()

Solution (Python):


n = int(input())

maximumProfit = - 10 ** 9
minimum = int(input())

i = 1
while i < n:
    a = int(input())
    maximumProfit = max(a - minimum, maximumProfit)
    minimum = min(a, minimum)
    i += 1

print(maximumProfit)

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

    for _ in range(N):
        R.append(int(input()))

    max_profit = -(10 ** 10)
    min_value = R[0]

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

    print(max_profit)

main()

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

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

minimum = []

m = arr[0]
for i in range(n):
	if arr[i] < m :
		m = arr[i]
		minimum.append(m)

	else:
		minimum.append(m)
ans = float("-inf")
for i in range(1,n):
	ans = max(ans,arr[i] - minimum[i-1])

print ans

Solution (Python):
n = int(input())
rlist = []
for i in range(n):
    r = int(input())
    rlist.append(r)
mlist = [rlist[1] - rlist[0]]
mini = rlist[0]
for i in range(1,n):
    if(rlist[i-1] < mini):
        mini = rlist[i-1]
    m = rlist[i] - mini
    if(m>mlist[-1]):
        mlist.append(m)
print(mlist[-1])

Solution (Python):
import sys

a = []
b = []
for line in sys.stdin:
    a.append(int(line))
    b.append(int(line))

del a[0]
del a[len(a)-1]
del b[0]
del b[0]

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

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

mins = [0] * n
mins[0] = float('inf')
for i in range(1, n):
    
    mins[i] = min(mins[i-1], R[i-1])

print(max(R[i] - mins[i] for i in range(n)))

Solution (Python):
n = int(input())
min = int(input())
current = int(input())
diff = current - min
for _ in range(n-2):
    if current < min:
        min = current
    current = int(input())
    if current - min > diff:
        diff = current - min
print(diff)