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


cur_min = int(input())
max_diff = -1 * (10**9) - 1

for i in range(n - 1) :
    num = int(input())
    
    max_diff = max(max_diff, num - cur_min)
    
    if num < cur_min :
        cur_min = num
    
print(max_diff)

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

result = []
biggest = 0
for i in range(n-1):
    last = ls.pop()
    if last > biggest:
        biggest = last
        result.append(last - min(ls))
print(max(result))

Solution (Python):
n = int(input())
maxt = -float("inf")
for i in range(n):
    t = int(input())
    if i == 0:
        mint = t
    else:
        if t - mint > maxt:
            maxt = t - mint
        if t < mint:
            mint = t
print(maxt)

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

Solution (Python):
def max_profit(A, n):
    minv = A[0]
    for j in range(1, n):
        if j == 1:
            maxv = A[j] - A[j-1]
        if maxv < A[j] - minv:
            maxv = A[j] - minv
        if minv > A[j]:
            minv = A[j]
    print(maxv)

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

max_profit(A, n)

Solution (Python):
def main():
    n = int(input())
    t0 = int(input())
    t1 = int(input())
    tmpmin = t0
    tmpmax = t1 - t0
    tmpmin = min(t0, t1)

    for _ in range(2,n):
        t = int(input())
        if t - tmpmin > tmpmax:
            tmpmax = t - tmpmin 
        if t < tmpmin:
            tmpmin = t
    print(tmpmax)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    t0 = int(input())
    t1 = int(input())

    tmpmax = t1 - t0
    tmpmin = min(t0, t1)

    for _ in range(2,n):
        t = int(input())
        if t - tmpmin > tmpmax:
            tmpmax = t - tmpmin 
        if t < tmpmin:
            tmpmin = t
    print(tmpmax)


if __name__ == '__main__':
    main()

Solution (Python):



n = int(input())

min_r = int(input())
max_margin = -1000000001

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

print(max_margin)

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

max_j = n - 1
max_diff = r[max_j] - r[max_j - 1]

for i in range(n-3, -1, -1):
    if r[i+1] > r[max_j]:
        max_j = i + 1
    tmp_diff = r[max_j] - r[i]
    if max_diff < tmp_diff:
        max_diff = tmp_diff

print(max_diff)

Solution (Python):
inputs = []
while True:
    try:
        inputs.append(int(input()))
    except(EOFError):
        break

inputs.pop(0)

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

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

print(maxv)

Solution (Python):


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

MIN = R[0]
MAXdiff = R[1]-R[0]

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

print(MAXdiff)

Solution (Python):
N = int(input())
R = [int(input()) for _ 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):
n=int(input())
R=[int(input()) for i in range(n)]
mini=10**10
maxi=-10**10
for r in R:
    maxi=max([maxi,r-mini])
    mini=min([mini,r])
print(maxi)

Solution (Python):
cnt = 0
for i in range(int(input())):
    c_num = int(input())
    if cnt == 0:
        r_min = c_num
    elif cnt == 1:
        p_max = c_num - r_min
        if c_num < r_min:
            r_min = c_num
    else:
        if p_max < c_num - r_min:
            p_max = c_num - r_min
        if c_num < r_min:
            r_min = c_num
    cnt += 1
print(p_max)

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

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

purchase_price = price_list[0]
profit = price_list[1] - purchase_price
for price in price_list[1:]:
    if profit < price - purchase_price:
        profit = price - purchase_price
    if price < purchase_price:
        purchase_price = price

print(profit)

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

purchase_price = price_list[0]
profit = price_list[1] - purchase_price
for price in price_list[1:]:
    profit = max(profit, (price-purchase_price))
    purchase_price = min(price, purchase_price)

print(profit)

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

purchase_price = int(input())
profit = -2000000000
for _ in range(N-1):
    price = int(input())
    profit = max(profit, (price-purchase_price))
    purchase_price = min(price, purchase_price)

print(profit)

Solution (Python):
n=int(raw_input().strip())
lst=[]
minval=0x3f3f3f3f
ans=-0x3f3f3f3f
for i in xrange(n):
    val=int(raw_input().strip())
    if(i>=1):
        
        ans=max(ans,val-minval)
    minval = min(minval, val)
print ans

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

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

Solution (Python):
N=int(input())
R=[int(input()) for i in range(N)]
S=[0 for i in range(N)]
S[0]=R[0]
for i in range(1,N):
    S[i]=min(S[i-1],R[i])
T=[R[i+1]-S[i] for i in range(N-1)]
print(max(T))

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

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

print(gap)

Solution (Python):
def max_benefit(num_list):
    max_num = 10**9*-1
    min_num = num_list[0]

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

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

ans = max_benefit(num_list)
print(ans)

Solution (Python):
def main():
    n = int(input()) 
    min_value = int(input())
    max_profit = - 10 ** 9
    for i in range(n-1):
        now_value = int(input())
        tmp_profit = now_value - min_value

        if now_value < min_value:
            min_value = now_value
        if max_profit < tmp_profit:
            max_profit = tmp_profit

        
    print(max_profit)
    return


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):
n = int(input())
s = []
for i in range(n):
    s.append(int(input()))
max_s = -10**9
min_s = s[0]
for i in range(1, n):
    max_s = max(max_s, s[i] - min_s)
    min_s = min(min_s, s[i])

print(max_s)

Solution (Python):
n = int(input()) 
price = [int(input()) for i in range(n)]
maxp = price[1] - price[0]
minp = price[0]
for j in range(1, n):
    maxp = max([maxp, price[j] - minp])
    minp = min([minp, price[j]])
print(maxp)

Solution (Python):
n = int(input())
ns = []
max_profit = -1000000000
min_value = 1000000000
for i in range(n):
    num = int(input())
    if i > 0:
        max_profit = max(max_profit, num-min_value)
    min_value = min(num, min_value)
print(max_profit)

Solution (Python):
n = int(input())
ns = []
max_profit = -1000000000
min_value = int(input())
for i in range(1,n):
    num = int(input())
    max_profit = max(max_profit, num-min_value)
    min_value = min(num, min_value)
print(max_profit)

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())
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())
r_min = int(input())
r_ji = - float('inf')
for t in range(n - 1):
    r_t = float(input())
    if r_ji < r_t - r_min:
        r_ji = r_t - r_min
    if r_t < r_min:
        r_min = r_t
print(int(r_ji))

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

maxv = -2 * 10**9
minv = r[0]

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

print(maxv)

Solution (Python):
'''
Rjの最小値を保持することで最大利益の更新判定をn回で終わらせる
'''

r = []

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

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

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

print(maxv)

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

minval = l[0]
maxp = -1000000000
for val in l[1:]:
    if maxp < val - minval:
        maxp = val - minval
    if minval > val:
        minval = val

print(maxp)

Solution (Python):
import math

N = int(input())
R = [int(input().strip()) for _ in range(N)]

maxv = -math.inf
minv = R[0]
for i in range(1, N):

    tmp_maxv = R[i] - minv 
    
    if maxv < tmp_maxv:
        maxv = tmp_maxv

    
    if R[i] < minv:
        minv = R[i]

print(maxv)

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

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

print(margin)

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

min_v = R[0]
max_v = R[1] - R[0]
for i in range(1, len(R)):
    if min_v > R[i - 1]:
        min_v = R[i - 1]
    if max_v < R[i] - min_v:
        max_v = R[i] - min_v
print(max_v)

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

min_v = int(input())
r = int(input())
max_v = r - min_v
if min_v > r:
    min_v = r

for _n in range(2, N):
    r = int(input())
    if max_v < r - min_v:
        max_v = r - min_v
    if min_v > r:
        min_v = r
print(max_v)

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

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

print(ans)

Solution (Python):
s = int(input())
maxv = -100000000000000000
minv = 10000000000000000

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



if x >= 0:
    print(maxv)
else:
    print("-1")

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

minr = float('inf')
maxd = -float('inf')
for _ in range(n):
    r = int(input())
    maxd = max(maxd, r - minr)

    minr = min(minr, r)

print(maxd)

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

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

Solution (Python):


nums = int(input())
a = [int(input()) for i in range(nums)]

minv = a[0]
maxv = -2000000000

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

print(maxv)

Solution (Python):
n = int(input())
inf = 10 ** 10
buy = inf
ans = -inf
for i in range(n):
    price = int(input())
    ans = max(ans, price - buy)
    buy = min(buy, price)
print(ans)

Solution (Python):
s_max = -float('inf')
s = 0
for i in range(int(input())):
	r = int(input())
	if i:
		d = r - prev
		s = max(s, 0) + d
		s_max = max(s, s_max)
	prev = r
print(s_max)

Solution (Python):
s_max = -float('inf')
s = 0
for i in range(int(input())):
	r = int(input())
	if i:
		s = max(s, 0) + r - prev
		s_max = max(s, s_max)
	prev = r
print(s_max)

Solution (Python):
import sys
s_max = -float('inf')
s = 0
n = int(input())
r = list(map(int, sys.stdin.read().split()))
for i in range(1, n):
	s = max(s, 0) + r[i] - r[i-1]
	s_max = max(s, s_max)
print(s_max)

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

Solution (Python):
import sys
d = -float('inf')
n = int(input())
l = int(input())
for s in sys.stdin:
	r = int(s)
	d = max(d, r-l)
	l = min(l, r)
print(d)

Solution (Python):
n=int(input())
minv=int(input())
maxv=-float('inf')
for _ in range(n-1):
  tmp=int(input())
  maxv=max(maxv,tmp-minv)
  minv=min(minv,tmp)
print(maxv)

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

for _ in [0]*n:
    current = int(input())
    result = max(result, current-min_value)
    min_value = min(min_value, current)

print(result)

Solution (Python):


N=int(input())
A=[]
for i in range(N):
    A.append(int(input())) 
MIN= A[0]
MAX= A[1]-A[0]
for i in range(1,N):
    MAX=max(MAX,A[i]-MIN)
    MIN=min(MIN,A[i])
print(MAX)

Solution (Python):
import sys
n = int(input())
r = [int(input()) for x in range(n)]
max_val = sys.maxsize * -1
min_val = r[0]
for i in range(1,n):
  max_val = max(max_val, r[i] - min_val)
  min_val = min(min_val, r[i])
print(max_val)

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

Solution (Python):
def calcMax(i):
  now = numbers[i]

  future_max = max(numbers[i+1:])
  
  t = numbers[i+1:]
  
  return future_max - now

n = int(input())

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

m = -1000000001
ti = 1000000001
for i in range(len(numbers) - 1):
    if numbers[i] < ti:
      if (i == 0 or i == len(numbers) - 1 or (numbers[i-1] >= numbers[i] and numbers[i] <= numbers[i+1])):
        temp = calcMax(i)
        
        if temp > m:
            m = temp
            ti = numbers[i]

print (m)

Solution (Python):
def main():
    n = int(input())
    ans = 0
    minr = int(input())
    ans = -1 * 10**9
    for i in range(n-1):
        r = int(input())
        ans = max(ans, r-minr)
        minr = min(minr, r)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
maxv = -2*10**10
minv = int(input())
for i in range(N-1):
    _ = int(input())
    maxv = max(maxv,_-minv)
    minv = min(minv,_)
print(maxv)

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

dp = [-(10**10)]*N
dp[0] = A[1]-A[0]
for i in range(1,N-1):
    dp[i] = max(A[i+1]-A[i]+dp[i-1], A[i+1]-A[i])

print(max(dp))

Solution (Python):
from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

Given_num=int(input())
tmp_num=int(input())
minv=tmp_num
maxv=-BIG_NUM
for i in range(1,Given_num):
    tmp_num=int(input())
    maxv=max(maxv,tmp_num-minv)
    minv=min(minv,tmp_num)
print("%d"%(maxv))

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


ans = float("-INF")
min_v = int(input())
for _ in range(n-1):
    r = int(input())
    ans = max(ans, r - min_v)
    min_v = min(min_v, r)


print(ans)

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

t = []

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

buy = t[0]
val = t[-1] - buy

for i in range(1, n):
    if t[i] - buy > val:
        val = t[i] - buy
    if t[i] < buy:
        buy = t[i]

print(val)

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

t = []

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

buy = t[0]
val = t[-1] - buy

for i in range(1, n):
    if t[i] - buy > val:
        val = t[i] - buy
    if t[i] < buy:
        buy = t[i]

print(val)

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


fin = sys.stdin.readline
n = int(fin())
price_list = [int(fin()) for _ in range(n)]
min_price_so_far = float('inf')
max_profit = float('-inf')
for price in price_list:
    profit = price - min_price_so_far
    max_profit = max(max_profit, profit)
    min_price_so_far = min(min_price_so_far, price)
print(max_profit)

Solution (Python):
n = int(input())
R = [0]*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):


N = int(input())

min,dif = 10000000000, -10000000000

for i in range(N):
    a = int(input())
    if (a - min) > dif:
        dif = a - min
    if a < min:
        min = a
print(dif)

Solution (Python):



n = int(input())
ans = -10000000000
mini = 10000000000

for i in range(n):
    x = int(input())
    ans = ans if (x-mini) < ans else x-mini
    mini = mini if x > mini else x

print(ans)

Solution (Python):
n = int(input())
i, j = int(input()), 0
nmin = i
a = -2 * 10 ** 9
for h in range(n - 1):
    j = int(input())
    a = max(a, j - nmin)
    if j < nmin:
        nmin = j

print(a)

Solution (Python):
def main():
    n = input()
    d = -999999999

    m = input()
    for i in range(n - 1):
        r = input()
        if (d < r - m):
            d = r - m
        if (m > r):
            m = r
    print(d)

main()

Solution (Python):
N = input()
d = -999999999
R = input()
for i in range(N-1):
	r = input()
	d = max(d,(r-R))
	R = min(R,r)
print d

Solution (Python):
l = 2*10**9
p = -2*10**9
n = int(input())
for _ in range(n):
	x = int(input())
	p = max(x-l,p)
	l = min(x,l)
print(p)

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

_min, dif = 10**10, -10**10
for x in xrange(n):
    a = int(raw_input())
    if a -_min > dif:
        dif = a - _min
    if a < _min:
        _min = a
print dif

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

p_buy = R[0]
p_sale = R[1]
buy = R[1]
sale = None
for i in range(2, N):
    if p_sale < R[i]:
        p_sale = R[i]
    if buy > R[i]:
        if sale is None:
            sale = R[i]
        if p_sale - p_buy < sale - buy:
            p_sale, p_buy = sale, buy 
        sale, buy = None, R[i]
    else:
        if sale is None or sale < R[i]:
            sale = R[i]

p_gain = p_sale - p_buy
print(p_gain if sale is None else max(p_gain, sale - buy))

Solution (Python):
R = [0 for i in xrange(0,200000)] 
n = int(raw_input())
for i in xrange(n):
	R[i] = int(input())
maxv = -2000000000
minv = R[0]
for j in xrange(1, n):
	maxv = max(maxv, R[j]-minv)
	minv = min(minv, R[j])
print maxv

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

Solution (Python):
n = int(raw_input())
R = [input() for i in range(n)]
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):
n = input()
minSeq = input()
maxv = -float("inf")
while True:
	try:
		R = input()
		if R - minSeq > maxv:
			maxv = R - minSeq
		if R < minSeq:
			minSeq = R
	except EOFError:
		break
print(maxv)

Solution (Python):
n = input()
minSeq = input()
maxv = -float("inf")
for i in range(n-1):
    R = input()
    if R - minSeq > maxv:
        maxv = R - minSeq
    if R < minSeq:
        minSeq = R
print(maxv)

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

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())
R = [int(input()) for _ in range(n)]
ans = R[1] - R[0]
bottom = min(R[0], R[1])
for i in range(2, n):
    ans = max(ans, R[i] - bottom)
    if R[i] < bottom:
        bottom = R[i]
print(ans)

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

Solution (Python):
n = int(input())
R = [int(input()) for _ in range(n)]
ans = R[1] - R[0]
bottom = min(R[0], R[1])
for r in R[2:]:
    ans = max(ans, r - bottom)
    if r < bottom:
        bottom = r
print(ans)

Solution (Python):
import sys

num_list = int(sys.stdin.readline())
lines = sys.stdin.readlines()
nums = map(int, lines)

max_diff = -sys.maxint - 1
min_val = nums.pop(0)
for num in nums :
    diff = num - min_val
    if diff > max_diff :
        max_diff = diff
    if num < min_val :
        min_val = num

print max_diff

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

mi = int( raw_input( ) )
profit = 0 - 1000000000

for i in range( n-1 ):
	num = int( raw_input( ) )
	
	diff = num - mi
	if profit < diff:
		profit = diff

	if num < mi:
		mi = num

print( profit )

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

mi = int( input( ) )
profit = 0 - 1000000000

for i in range( n-1 ):
	num = int( input( ) )
	
	diff = num - mi
	if profit < diff:
		profit = diff

	if num < mi:
		mi = num

print( profit )

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

mi = int( raw_input( ) )
profit = 0 - 1000000000
n -= 1
i = 0
while i < n:
	num = int( raw_input( ) )
	
	diff = num - mi
	if profit < diff:
		profit = diff

	if num < mi:
		mi = num
	i += 1

print( profit )

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

mi = int( input( ) )
profit = 0 - 1000000000
n -= 1
i = 0
while i < n:
	num = int( input( ) )
	
	diff = num - mi
	if profit < diff:
		profit = diff

	if num < mi:
		mi = num
	i += 1

print( profit )

Solution (Python):
n = int(raw_input())
R = [int(raw_input()) for i in xrange(n)]
mn = R[0]
ans = -1e10
for r in R[1:]:
    ans = max(ans, r - mn)
    mn = min(mn, r)
print ans

Solution (Python):

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

i = 0
j = 0
m = -float("inf")
while j < n - 1:
    j += 1
    if R[j] - R[i] > m:
        m = R[j] - R[i]
    if R[i] > R[j]:
        i = j
print(m)

Solution (Python):
n = input()
maxv = - 1 * pow(10, 9)
minv = input()
for _ in xrange(1, n):
    Rj = input()
    maxv = max(maxv, Rj - minv)
    minv = min(minv, Rj)

print maxv

Solution (Python):
import sys

n = input()
minv = input()
maxv = - 1000000000
nums = map(int, sys.stdin.readlines())
for num in nums:
    maxv = num - minv if num - minv > maxv else maxv
    minv = num if num < minv else minv

print maxv

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

Solution (Python):
n = int(input())
R = [int(input()) for _ in range(n)]
ret = -(10 ** 9)
mn = R[0]
for r in R[1:]:
    ret = max(ret, r - mn)
    mn = min(mn, r)
print(ret)

Solution (Python):
n = int(input())
R = (int(input()) for _ in range(n))
ret = -(10 ** 9)
mn = next(R)
for r in R:
    ret = max(ret, r - mn)
    mn = min(mn, r)
print(ret)

Solution (Python):
import itertools
n = int(input())
R = [int(input()) for _ in range(n)]
ret = -(10 ** 9)
mn = R[0]
for r in itertools.islice(R, 1, len(R)):
    ret = max(ret, r - mn)
    mn = min(mn, r)
print(ret)

Solution (Python):
n = int(raw_input())
R = []
R.append(int(raw_input()))
minv = R[0]
maxv = -1 * 10**9
for j in xrange(1,n):
    R.append(int(raw_input()))
    maxv = max(maxv,R[j]-minv)
    minv = min(minv,R[j])
print maxv

Solution (Python):
import sys
n = int(raw_input())
R = [int(raw_input()) for i in xrange(n)]
minimum = sys.maxint
maximum = -sys.maxint - 1
for x in R:
    p = x - minimum
    if maximum < p:
        maximum = p

    if minimum > x:
        minimum = x
print maximum

Solution (Python):


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

print(maxv)

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

a=int(input())
b=int(input())
min_number=min(a,b)
profit=b-a


for i in range(N-2):
	c=int(input())
	if profit<c-min_number:
		profit=c-min_number	
	if min_number>c:
		min_number=c


print(profit)

Solution (Python):
num = raw_input()
num_list = []
for i in range(int(num)):
        a = raw_input()
        num_list.append(int(a))

maxv = -10000000000000
minv = num_list[0]

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

print maxv

Solution (Python):
num = raw_input()
num_list = []
for i in range(int(num)):
        a = raw_input()
        num_list.append(int(a))

maxv = float("-inf")
minv = num_list[0]

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

print maxv

Solution (Python):
import sys

x = []

for line in sys.stdin.readlines():
  x.append(line.strip())

max = -(10**9)
min = int(x[1])

for i in range(2, int(x[0])+1):
  sa = int(x[i]) - min
  if max < sa:
    max = sa
  if min > int(x[i]):
    min = int(x[i])

  

print max

Solution (Python):
import sys

x = []

for line in sys.stdin.readlines():
    x.append(int(line.strip()))

length = int(x[0])

del x[0]

max = -(10 ** 9)
min =  x[0]

for i in range(1, length):
    sa = x[i] - min

    if sa > max:
        max = sa

    if x[i] < min:
        min = x[i]
   
print max

Solution (Python):
import sys

x = []

for line in sys.stdin.readlines():
    x.append(int(line.strip()))

length = int(x[0])

del x[0]

max = -(10 ** 9)
min =  x[0]

for i in range(1, length):
    sa = x[i] - min

    if sa > max:
        max = sa

    if x[i] < min:
        min = x[i]
   
print max

Solution (Python):
num = raw_input()
num_list = []
for i in range(int(num)):
    a = raw_input()
    num_list.append(int(a))

min_ans = num_list[0]
max_ans = float("-inf")
for i in num_list[1:]:
    if i - min_ans > max_ans:
        max_ans = i - min_ans
    if i < min_ans:
        min_ans = i

print max_ans

Solution (Python):
n = int(raw_input())
R = list()
max = -10000000000
min =  10000000000

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

    if max < R[i] - min:
        max = R[i] - min

    if min > R[i]:
        min = R[i]
print max

Solution (Python):
priceList = []
t = raw_input()


for i in range(int(t)):
    priceList.append(int(input()))

maxv = -2000000000
minv = priceList[0]

for i in range(1, int(t)):
	if maxv < priceList[i] - minv:
		maxv = priceList[i] - minv
	if minv > priceList[i]:
		minv = priceList[i]
    
print maxv

Solution (Python):
price_list = []
t = raw_input()
for i in range(int(t)):
    price_list.append(int(input()))
maxv = float("-inf")
minv = price_list[0]
for i in range(1, int(t)):
    maxv = max([maxv, price_list[i] - minv])
    minv = min([minv, price_list[i]])
print maxv

Solution (Python):
a=[]
n=int(input())
i=0
while(n>i):
    b=int(input())
    a.append(b)
    i+=1
score=-1000000000
mini = a[0]
for i in a[1:]:
    if(i-mini>score):
        score = i-mini
    if(mini>i):
        mini=i
    

print(score)

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

price = []
max_pro = -999999999

while(n>0):
    x = int(input())
    price.append(x)
    n -= 1
min = price[0]
for i in range(1,num):
    pro = price[i]-min

    if(max_pro < pro):
        max_pro = pro
    if(min > price[i]):
        min = price[i]
print ("{0}".format(max_pro))

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

x = input()
x = int(x)

present_min_value = x
max_profit_candidate = 1 - 10**9

for i in range(n - 1):
    a = input()
    a = int(a)
    diff = a - present_min_value
    if diff > max_profit_candidate:
        max_profit_candidate = diff
    if a < present_min_value:
        present_min_value = a

print(max_profit_candidate)

Solution (Python):
N = int(input())
R = [int(input()) for i in list(range(N))]
maxv = -1000000000
minr = R[0]

for i in list(range(1, N)):
    maxv = max(maxv, R[i] - minr)
    minr = min(minr, R[i])

print(maxv)

Solution (Python):




n = int(input())
max_diff = -20**9
minv = 20**9
for i in range(n):
    r = int(input())
    max_diff = max(max_diff, r - minv)
    minv = min(minv, r)


print(max_diff)

Solution (Python):





n = int(input())
max_diff = -20**9
minv = 20**9
for i in range(n):
    r = int(input())
    max_diff = max(max_diff, r - minv)
    minv = min(minv, r)


print(max_diff)

Solution (Python):
n=input()
for i in range(n):
	if i==0:
		num_1=input()
	elif i==1:
		num_2=input()
		maxv=num_2 - num_1
		MIN=min(num_1,num_2)
	else:
		num_3=input()
		if (num_3-MIN)>maxv:
			maxv=num_3-MIN
		if (num_3)<MIN:
			MIN=num_3
print(maxv)

Solution (Python):




def maxprofit(n, rs):
    maxp = rs[1] - rs[0]
    minp = rs[0]
    for i in range(1, n):
        maxp = max(maxp, rs[i] - minp)
        minp = min(minp, rs[i])
    return maxp


def main():
    n = int(input())
    rs = [int(input()) for i in range(n)]
    print(maxprofit(n, rs))

if __name__ == "__main__":
    main()

Solution (Python):
n = input()

a = input()
temp = a
b = input()
temp = b if b < temp else temp
for _ in xrange(n-2):
    now = input()
    if (b - a) < (now - temp):
        a, b = temp, now
    if now < temp:
        temp = now
    elif now > b:
        b = now
print b - a

Solution (Python):
n = int(raw_input())
num_list = [int(raw_input()) for i in xrange(n)]

minv = num_list[0]
maxv = -1000000000

for i in xrange(1, n):
  tmp = num_list[i] - minv
  maxv = tmp if tmp > maxv else maxv
  minv = num_list[i] if minv > num_list[i] else minv
  

print maxv

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

min_v = r[0]
max_profit = -1000000000000
for i in range(1,n):
    if max_profit < r[i] - min_v:
        max_profit = r[i]-min_v
    if r[i] < min_v:
        min_v = r[i]

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 i in range(1,len(R)):
    maxv = max(maxv,R[i]-minv)
    minv = min(minv,R[i])

print (maxv)

Solution (Python):

n = int(raw_input())

min = int(raw_input())  
keep = int(raw_input()) 
diff = keep - min       
if keep > min:
    keep = min
n = n - 2

for i in range(n):
    v = int(raw_input())
    if v - keep > diff:
        diff = v - keep
        min = keep
    elif keep > v:
        keep = v

print diff

Solution (Python):

n = int(raw_input())

min = int(raw_input())
diff = -1000000000
n = n - 1

for i in range(n):
    v = int(raw_input())

    if v - min > diff:
        diff = v - min
    if min > v:
        min = v

print diff

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
max_v = A[1] - A[0]

l = 0
r = 1
while r + 1 < N:
    if A[l] > A[r]:
        l = r
    elif A[r] > A[r + 1]:
        max_v = max(max_v, A[r] - A[l])
        if A[l] > A[r + 1]:
            l = r + 1
            r = l
    r += 1
if r<N:
    max_v = max(max_v, A[r] - A[l])
print(max_v)

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

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

Solution (Python):
n = input()

min_val = float("inf")
max_val = -float("inf")
for i in range(n):
  tmp = input()
  max_val = max(max_val, tmp - min_val)
  min_val = min(min_val, tmp)

print max_val