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



def get_maximum_profit(value_list, n_list):
    minv = pow(10,10) + 1
    profit = -minv
    for v in value_list:
        profit = max(profit, v - minv) 
        minv = min(minv, v)
    return profit

def main():
    n_list = int(input())
    target_list = [int(input()) for i in range(n_list)]
    print(get_maximum_profit(target_list, n_list))
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys

def solve():
    n = int(raw_input())
    R = []

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

    
    

    max_profit = -sys.maxint
    min_R = R[0]

    for i in xrange(1, n):
        if R[i] - min_R > max_profit:
            max_profit = R[i] - min_R
        if min_R > R[i]:
            min_R = R[i]

    print max_profit


if __name__ == "__main__":
    solve()

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

r0 = int(input())
r1 = int(input())
max = r1-r0
min = min(r0,r1)
for i in range(n-2):
    r = int(input())
    if max < r-min:
        max = r-min
    if min > r:
        min = r
print(max)

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

R = [0 for i in range(n)]

ans = 1 - 10**9
for i in range(n):
    R[i] = int(raw_input())
    if i == 0:
        min_R = R[0]
        continue
    if min_R < R[i]:
        if ans < R[i]-min_R:
            ans = R[i] - min_R
    elif min_R > R[i]:
        if ans < R[i]-min_R:
            ans = R[i] - min_R
        min_R = R[i]
    else:
        if ans < 0:
            ans = 0
        
print ans

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

R = [0 for i in range(n)]

ans = 1 - 10**9
for i in range(n):
    R[i] = int(raw_input())
    if i == 0:
        min_R = R[0]
        continue
    if R[i] - min_R > ans:
        ans = R[i] - min_R
    if R[i] < min_R:
        min_R = R[i]
        
print ans

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

R = [0 for i in range(n)]

ans = 1 - 10**9
min_R = int(raw_input())
for i in range(n-1):
    R[i] = int(raw_input())
    if R[i] - min_R > ans:
        ans = R[i] - min_R
    if R[i] < min_R:
        min_R = R[i]
        
print ans

Solution (Python):
import sys

n = int(raw_input())

min_R = int(raw_input())
nums = map(int, sys.stdin.readlines())

ans = -10**9

for num in nums:
    if num - min_R > ans:
        ans = num - min_R
    if num < min_R:
        min_R = num
        
print ans

Solution (Python):
def calc_max_profit(data):
    """
    http://judge.u-aizu.ac.jp/onlinejudge/commentary.jsp?id=ALDS1_1_D
    :param data: ??????????????????
    :return:
    """
    maxv = data[1] - data[0]
    minv = data[0]
    for j in range(1, len(data)):
        maxv = max(maxv, data[j] - minv)
        minv = min(minv, data[j])
    return maxv


if __name__ == '__main__':
    
    num = int(input())
    data = []
    for i in range(num):
        data.append(int(input()))
    
    

    
    result = calc_max_profit(data)

    
    print('{0}'.format(result))

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

max_diff = -float('inf')
min_n = p[0]

for i in range(1, n):
  max_diff = max(max_diff, p[i] - min_n)
  min_n = min(min_n, p[i])

print(max_diff)

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

max_diff = - 10 ** 9
min_n = int(input())

for i in range(1, n):
  n = int(input())
  max_diff = max(max_diff, n - min_n)
  min_n = min(min_n, n)

print(max_diff)

Solution (Python):
import sys

n = int(input())

max_diff = - 10 ** 9
min_n = int(input())

for n in map(int, sys.stdin.readlines()):
  max_diff = max(max_diff, n - min_n)
  min_n = min(min_n, n)

print(max_diff)

Solution (Python):



"""
input:
6
5
3
1
3
4
3

output:
3
"""

import sys


def solve():
    
    max_profit = -1 * float('inf')
    min_stock = prices[0]
    for price in prices[1:]:
        max_profit = max(max_profit, price - min_stock)
        min_stock = min(min_stock, price)
    return max_profit


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    p_num = int(_input[0])
    prices = list(map(int, _input[1:]))
    print(solve())

Solution (Python):

num = int(input())
max_value = float("-inf")
min_value = float("inf")

for i in [int(input()) for x in range(num)]:

    if max_value < (i - min_value):
        max_value = i - min_value

    if min_value > i:
        min_value = i

print(max_value)

Solution (Python):


def maximum_profit(price_list):
    max = price_list[1] - price_list[0]
    min = price_list[0]
    for i in range(1, len(price_list)):
        if max < price_list[i] - min:
            max = price_list[i] - min
        if min > price_list[i]:
            min = price_list[i]
    return max

input_num = int(input())
price_list = list()
for i in range(input_num):
    input_price = int(input())
    price_list.append(input_price)
print(maximum_profit(price_list))

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

mi = r[0]
mx = -1e10
for j in range(1, len(r)):
    mx = max(mx, (r[j] - mi))
    mi = min(mi, r[j])
print(mx)

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

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

print(maxv)

Solution (Python):

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


max_profit = (-1) * (10 ** 9)
min_value = r[0]
for j in range(1, len(r)):
    max_profit = max(max_profit, r[j] - min_value)
    min_value = min(min_value, r[j])
print(max_profit)

Solution (Python):
n = int(input())
minv = int(input())
maxv = -1000000000

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

print(maxv)

Solution (Python):
n = int(input())
minv = int(input())
maxv = -1000000000

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

print(maxv)

Solution (Python):
n = int(input())
minv = int(input())
maxv = -1000000000

for i in range(1,n):
  r = int(input())
  m = r-minv
  if maxv < m < 0:
    maxv = m
    minv = r
  elif maxv < m:
    maxv = m
  elif m < 0:
    minv = r

print(maxv)

Solution (Python):
import sys

n = int(input())
minv = int(input())
maxv = -1000000000

for r in map(int,sys.stdin.readlines()):
  m = r-minv
  if maxv < m < 0:
    maxv = m
    minv = r
  elif maxv < m:
    maxv = m
  elif m < 0:
    minv = r

print(maxv)

Solution (Python):
import sys

n = int(input())
minv = int(input())
maxv = -1000000000

for r in map(int,sys.stdin.readlines()):
  m = r-minv
  if maxv < m < 0:
    maxv = m
    minv = r
  elif maxv < m:
    maxv = m
  elif m < 0:
    minv = r

print(maxv)

Solution (Python):
import sys

n = int(input())
minv = int(input())
v = int(input())
maxv = v-minv
if v < minv: minv = v

for r in map(int,sys.stdin.readlines()):
  m = r-minv
  if maxv < m < 0:
    maxv = m
    minv = r
  elif maxv < m:
    maxv = m
  elif m < 0:
    minv = r

print(maxv)

Solution (Python):
import sys

n = int(input())
minv = int(input())
v = int(input())
maxv = v-minv
if v < minv: minv = v

for r in map(int,sys.stdin.readlines()):
  m = r-minv
  if maxv < m < 0:
    maxv = m
    minv = r
  elif maxv < m:
    maxv = m
  elif m < 0:
    minv = r

print(maxv)

Solution (Python):
import sys

n = int(input())
minv = int(input())
maxv = -1000000000

for r in map(int,sys.stdin.readlines()):
  if maxv < r-minv < 0:
    maxv = r-minv
    minv = r
  elif maxv < r-minv:
    maxv = r-minv
  elif r-minv < 0:
    minv = r

print(maxv)

Solution (Python):
import sys

n = int(input())
minv = int(input())
maxv = -1000000000

for r in map(int,sys.stdin.readlines()):
  if maxv < r-minv < 0:
    maxv = r-minv
    minv = r
  elif maxv < r-minv:
    maxv = r-minv
  elif r-minv < 0:
    minv = r

print(maxv)

Solution (Python):
import sys
 
n = int(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):
import sys
 
n = int(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):
import sys
 
n = int(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):
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):
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):
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):
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):
import sys
 
l = sys.stdin.readlines()
minv = int(l[1])
maxv = -1000000000

for r in map(int,l[2:]):
  m = r-minv
  if maxv < m: 
    maxv = m
    if m < 0: minv = r
  elif m < 0: minv = r
 
print(maxv)

Solution (Python):
import sys
 
l = sys.stdin.readlines()
minv = int(l[1])
maxv = -1000000000

for r in map(int,l[2:]):
  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(profits):
    max_v = profits[1] - profits[0]
    min_v = profits[0]

    for j in range(1, len(profits)):
        max_v = max(max_v, profits[j]-min_v)
        min_v = min(min_v, profits[j])

    print(max_v)


def to_int(v):
    return int(v)


if __name__ == '__main__':
    l = to_int(input())
    profits = [to_int(input()) for i in range(l)]

    maximum_profit(profits)

Solution (Python):



def maximum_profit(profits):
    max_v = profits[1] - profits[0]
    min_v = profits[0]

    for j in range(1, len(profits)):
        max_v = max(max_v, profits[j]-min_v)
        min_v = min(min_v, profits[j])

    print(max_v)


def to_int(v):
    return int(v)


if __name__ == '__main__':
    l = to_int(input())
    profits = [to_int(input()) for i in range(l)]

    maximum_profit(profits)

Solution (Python):



def maximum_profit(profits):
    max_v = profits[1] - profits[0]
    min_v = profits[0]

    for j in range(1, len(profits)):
        max_v = max(max_v, profits[j]-min_v)
        min_v = min(min_v, profits[j])

    print(max_v)


def to_int(v):
    return int(v)


if __name__ == '__main__':
    l = to_int(input())
    profits = [to_int(input()) for i in range(l)]

    maximum_profit(profits)

Solution (Python):



def maximum_profit(profits):
    max_v = profits[1] - profits[0]
    min_v = profits[0]

    for j in range(1, len(profits)):
        max_v = max(max_v, profits[j]-min_v)
        min_v = min(min_v, profits[j])

    print(max_v)


def to_int(v):
    return int(v)


if __name__ == '__main__':
    l = to_int(input())
    profits = [to_int(input()) for i in range(l)]

    maximum_profit(profits)

Solution (Python):

n = int(input())

R = []

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

kouho = set()
diff_max = None

for (i, rt) in enumerate(R):
    if i == 0:
        rt_min = rt
    else:
        if rt < rt_min and not (diff_max is None):
            kouho.add(diff_max)
            diff_max = None
            rt_min = rt
        elif rt < rt_min and diff_max is None:
            rt_min = rt
        elif rt >= rt_min:
            if diff_max is None:
                diff_max = rt - rt_min
            else:
                diff_max = max(diff_max, rt - rt_min)

if not (diff_max is None):
    kouho.add(diff_max)



if kouho != set():
    print(max(kouho))
else:
    diff_tonari = {R[i + 1] - R[i] for i in range(n - 1)}
    print(max(diff_tonari))

Solution (Python):

n = int(input())

R = []

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

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

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

print(maxv)

Solution (Python):

n = int(input())

dif_max = - 10 ** 10
min_v = 10 ** 10

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

print(dif_max)

Solution (Python):

n = int(input())

dif_max = -float("inf")
min_v = float("inf")

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

print(dif_max)

Solution (Python):
import sys

n = int(input())

dif_max = -float("inf")
min_v = float("inf")

for i in range(n):
    r = int(sys.stdin.readline())
    dif_max = max(dif_max, r - min_v)
    min_v = min(min_v, r)

print(dif_max)

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):
debug = False

if debug:import time
if debug:start = time.time()

n = int(input())
R = [0]*n
R_min = 10**9

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

maximum_profit = -10**9
for i in range(1, n):
    R_min = R[i-1] if R[i-1] < R_min else R_min
    profit = R[i] - R_min
    maximum_profit = profit if profit > maximum_profit else maximum_profit
print(maximum_profit)

if debug:print(time.time() - start)

Solution (Python):
debug = False

if debug:import time
if debug:start = time.time()

n = int(input())
R = [0]*n
R_min = 10**9

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

maximum_profit = -10**9
for i in range(1, n):
    R_min = R[i-1] if R[i-1] < R_min else R_min
    profit = R[i] - R_min
    maximum_profit = profit if profit > maximum_profit else maximum_profit
print(maximum_profit)

if debug:print(time.time() - start)

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

maximum_profit = -10**9
r0 = int(input())
r_min = r0
for i in range(1, n):
    r1 = int(input())
    if r0 < r_min:
        r_min = r0
    profit = r1 - r_min
    if profit > maximum_profit:
        maximum_profit = profit
    r0 = r1
print(maximum_profit)

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

r0 = int(input())
r_min = r0
for i in range(1, n):
    r1 = int(input())
    r_min = min(r0, r_min)
    maximum_profit = max(maximum_profit, r1 - r_min)
    r0 = r1
print(maximum_profit)

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

Solution (Python):
def main():
    N = int(input())
    price = [int(input()) for i in range(N)]
    minv = float("inf")
    maxv = -float("inf")
    for p in price:
        maxv = max(maxv, p-minv)
        minv = min(minv, p)
    print(maxv)



if __name__ == "__main__":
    import os
    import sys
    if len(sys.argv) > 1:
        if sys.argv[1] == "-d":
            fd = os.open("input.txt", os.O_RDONLY)
            os.dup2(fd, sys.stdin.fileno())
            main()
    else:
        main()

Solution (Python):
N = int(input())
M = -1000000000
L = int(input())
for i in range(1,N):
    C = int(input())
    M = max(C - L, M)
    L = min(C, L)
print(M)

Solution (Python):
import sys
input()
a,b=-1e11,1e11
for i in map(int,sys.stdin.readlines()):
  a=max(a,i-b)
  b=min(i,b)
print(a)

Solution (Python):
import sys
 
n = int(input())
r0 = int(input())
r1 = int(input())
mx = r1-r0
mn = min(r1,r0)
 
for i in map(int,sys.stdin.readlines()):
    if mx < i - mn:
        mx = i - mn
        if 0 > i-mn:
            mn = i
    elif mn > i:
        mn = i
 
print(mx)

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

min_v = r[0]
max_p = -1000000000
for i in range(1,n):
    max_p = max(max_p,r[i]-min_v)
    min_v = min(min_v,r[i])
print(max_p)

Solution (Python):
from sys import stdin
n = int(input())
r=[int(input()) for i in range(n)]
rv = r[::-1][:-1]
m = None
p_r_j = None
for j,r_j in enumerate(rv):
    if p_r_j == None or p_r_j < r_j:
        p_r_j = r_j
    if p_r_j > r_j:
        continue
    r_i = min(r[:-(j+1)])
    t = r_j - r_i
    if m == None or t > m:
        m = t
print(m)

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

Solution (Python):
n = int(input())
a = [0 for i in range(n)]
for i in range(n):
    a[i] = int(input())
minimum = a[0]
profit = a[1]-a[0]

for i in range(1,n):
    if profit < a[i]- minimum:
        profit = a[i] - minimum
    if minimum > a[i]:
        minimum = a[i]
        
print(profit)

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

R = []

for i in range(0,n):
    R.insert(i, int(input()))

m = min(R[0:2])
MAX = R[1] - R[0]
for r in range(2, n):
    if(R[r-1] < m):
        m = R[r-1]
    MAX2 = R[r] - m
    if(MAX2 > MAX):
        MAX = MAX2

print(MAX)

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

RMax = 0
R = []


for i in range(N):
	r = int(input())
	if i == 0:
		rmi = r
	if i == 1:
		RMax = r - R[0]
	if i > 0:		
		rdif = (r - rmi)
		if rdif >= RMax:
			RMax = rdif
	R.append(r)
	if r < rmi:
		rmi = r

print(RMax)

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

R = []

for i in range(0,n):
    R.insert(i, int(input()))

maxV = -2000000000  
minV = R[0]

for r in range(1, n):
    maxV = max(maxV, R[r] - minV)
    minV = min(minV, R[r])

print(maxV)

Solution (Python):

N=int(input())
p=[int(input()) for i in range(0,N)]
maxv=p[1]-p[0]
buy=p[0]
for i in range(1,N):
    if p[i]-buy>maxv:
        maxv=p[i]-buy
    if p[i]<buy:
        buy=p[i]
print(maxv)

Solution (Python):
n = int(input())
R = [int(input()) for i in range(n)]
profit= R[1] - R[0]
mn = R[0]
R.pop(0)
for i in R:
    if profit < i - mn:
        profit = i - mn
        if 0 > i - mn:
            mn = i
    elif mn > i:
        mn = i

print(profit)

Solution (Python):
N = int(input())
max_p = -1145148101919
min_v = int(input())
for i in range(1, N):
    R = int(input())
    max_p = max(R - min_v, max_p)
    min_v = min(R, min_v)

print(max_p)

Solution (Python):
import sys

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

for i in map(int, sys.stdin.readlines()):
    if mx < i - mn:
        mx = i - mn
    if mn > i:
        mn = i

print(mx)

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

maxv = -(10**9)
mina = a[0]

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

print(maxv)

Solution (Python):

num = int(raw_input())
a = int(raw_input())
b = int(raw_input())
diff = b - a
pre_min = min(a,b)

counter = 2
while counter < num:
    current_num = int(raw_input())
    if diff < current_num - pre_min:
        diff = current_num - pre_min
    if pre_min > current_num:
        pre_min = current_num
    counter += 1

print diff

Solution (Python):
N = int(input())
list_price = [int(input()) for i in range(N)]
minv = list_price[0]
maxv = -10**9
for x in list_price[1:]:
    maxv = max(maxv, x - minv)
    minv = min(minv, x)        
print(maxv)

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

Solution (Python):
N = int(input())
list_price = [int(input()) for i in range(N)]
minv = list_price[0]
maxv = list_price[1] - list_price[0]
for x in list_price[1:]:
    maxv = max(maxv, x - minv)
    minv = min(minv, x)        
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):
data_num = int(input())
cnt = 0
deff_price = -999999999
min_price = 0
tmp_price = 0

for i in range(data_num):
    price = int(input())
 
    if i ==0:
        min_price = price
    elif price - min_price > deff_price:
        deff_price = price - min_price
    
    if price < min_price:
        min_price = price

print(str(deff_price))

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

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


if __name__ == '__main__':
	n = input()
	R = []

	for i in xrange(n):
		R.append(input())
	maxv = -20000000000
	minv = R[0]

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

	print maxv

Solution (Python):


import math
prof = 0
d = int(input())
li =[int(input()) for i in range(d)]
max=float("-inf")
min=li[0]
for j in range(1,d):
    if li[j]-min>max:
        max=li[j]-min
    if li[j]<min:
        min=li[j]

print(max)

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






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

for j in range(1, line):
    maxd = max(maxd, nums[j] - minv)
    minv = min(minv, nums[j])


print(maxd)

Solution (Python):
n = int(input())
mi = int(input())
diff = -9999999999999999
for i in range(n-1):
    now = int(input())
    diff = max(diff, now-mi)
    mi = min(mi, now)
print(diff)

Solution (Python):
from sys import stdin

inf = 10**9 + 1

def solve():
    n = int(stdin.readline())
    R = [int(stdin.readline()) for i in range(n)]
    ans = max_profit(n, R)
    print(ans)

def max_profit(n, R):
    max_dif = -inf
    min_v = R[0]

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

    return max_dif

if __name__ == '__main__':
    solve()

Solution (Python):
from sys import stdin
 
inf = 10**9 + 1

def solve():
    n = int(stdin.readline())
    R = [int(stdin.readline()) for i in range(n)]

    ans = max_profit(n, R)

    print(ans)

def max_profit(n, R):
    max_d = -inf
    min_v = R[0]

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

    return 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):
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):
res = -10 ** 9
n   = int(input())
a   = int(input())
Min = int(a)

for mak in range(n - 1) :
    tem = 0
    b = int(input())
    Min = min(b, Min)
    
    if   b < a : tem = b - a
    else       : tem = b - Min

    if tem > res : res = int(tem)
    a   = int(b)
print(res)

Solution (Python):
res = -10 ** 9
n   = int(input())
a   = int(input())
Min = int(a)

for mak in range(n - 1) :
    tem = 0
    b = int(input())
    if b < Min : Min = int(b)
    
    if   b < a : tem = b - a
    else       : tem = b - Min

    if tem > res : res = int(tem)
    a   = int(b)
print(res)

Solution (Python):


import sys
import os
import math


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

max_profit = lst[1] - lst[0]
min_value = min(lst[0], lst[1])

for i in range(2, N):
    price = lst[i]
    max_profit = max(max_profit, price - min_value)
    min_value = min(min_value, price)

print(max_profit)

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

maxv = -20000000000

minv = int(input())

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

print(maxv)

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

maxv = a[-1] - a[0]
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())
R = list()

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

ans = R[1] - R[0]
min_R = R[0]

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

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

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

ans = R[1] - R[0]
min_R = R[0]

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

Solution (Python):
import sys

n = int(input())
r0 = int(input())
r1 = int(input())
p = r1 - r0
r = min(r0,r1)
for i in map(int, sys.stdin.readlines()):
    if p < i - r:
        p = i - r
    if r > i:
        r = i

print(p)

Solution (Python):
times = int(input())
element = []

for i in range(times):
    element.append(int(input()))

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

print(maxv)

Solution (Python):


def getint():
    return int(input().rstrip())

def main():
    n = getint()

    values = []
    for i in range(n):
        values.append(getint())

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

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

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

min = float('inf')
max_diff = -float('inf')

for _ in range(n):
    i = int(input())

    if max_diff < i - min:
        max_diff = i - min

    if i < min:
        min = i

print(max_diff)

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

def get_maximum_profit():
    element_number = int(input())
    v0 = int(input())
    v1 = int(input())

    min_v = min(v0, v1)
    max_profit = v1-v0

    for i in range(element_number-2):
        v = int(input())
        max_profit = max(max_profit, v-min_v)
        min_v = min(min_v, v)

    print(max_profit)

if __name__ == '__main__':
    get_maximum_profit()

Solution (Python):
import sys

def get_maximum_profit():
    element_number = int(input())
    v0 = int(input())
    v1 = int(input())

    min_v = min(v0, v1)
    max_profit = v1-v0

    for v in map(int, sys.stdin.readlines()):
        max_profit = max(max_profit, v-min_v)
        min_v = min(min_v, v)

    print(max_profit)

if __name__ == '__main__':
    get_maximum_profit()

Solution (Python):

n=int(input())
A=[]
max_dif=-1*10**9
for i in range(n):
    A.append(int(input()))
min=A[0]
for i in range(n-1):
    if(A[i+1]-min>max_dif):
        max_dif=A[i+1]-min
    if(min>A[i+1]):
        min=A[i+1]
print(max_dif)

Solution (Python):
def maximum_profit():
    n = int(input())
    numbers = [int(input()) for _ in range(n)]
    maxpro = numbers[1] - numbers[0]
    minnum = numbers[0]
    for i in range(1, len(numbers)):
        if maxpro < (numbers[i] - minnum):
            maxpro = (numbers[i] - minnum)
        if numbers[i] < minnum:
            minnum = numbers[i]
    print(maxpro)


if __name__ == "__main__":
    maximum_profit()

Solution (Python):


def main():
    n , data = input_data()
    vMax = float('-inf')
    vMin = data.pop(0)

    for Rj in data:
        if vMax < Rj - vMin:
            vMax = Rj - vMin
        if vMin > Rj:
            vMin = Rj

    print(vMax)

def input_data():
    n = int(input())
    lst = [int(input()) for i in range(n)]
    return (n , lst)

if __name__ == '__main__':
    main()

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

maxR = R[1]
minR = R[0]
maxB = R[1] - R[0]
for i in range(1,n):
 if R[i] < minR:
  minR = R[i]
  maxR = R[i] - 1
 elif minR <= R[i] and R[i] <= maxR:
  continue
 else:
  maxR = R[i]
  if maxR - minR > maxB:
   maxB = maxR - minR

print(str(maxB))

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

rt = [input() for i in range(n)]

minv = int(rt[0])
maxv = int(rt[1]) - int(rt[0])

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

print(maxv)

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

Solution (Python):
n = int(input())
minv = int(input())
maxv = (10**9)*(-1)
for j in range(1,n):
    r = int(input())
    if maxv < (r - minv):
        maxv = r - minv
    if minv > r:
        minv = r
print(maxv)

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

Solution (Python):


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


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

max_dif = -2000000000
min_value = R[0]

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

print(max_dif)

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

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

    rj = max(nums[1:])
    r  = rj - nums[0]

    for i in range(1, n-1):
        ri = nums[i]

        if ri == rj:
            rj = max(nums[i+1:])

        r = max(r, rj-ri)

    print(r)

if __name__ == '__main__':
    main()

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

    r  = 0
    ri = 0

    for i in range(n):
        m = int(input())

        if 0 == i:
            ri = m
            continue

        if 1 == i:
            r = m - ri

        r  = max(r, m - ri)
        ri = min(ri, m)

    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
maxi = -1000000000
mini = 1000000000
for i in range(n):
    m = int(input())
    if i > 0:
        if m - mini > maxi:
            maxi = m - mini
    if m < mini:
        mini = m
print(maxi)

Solution (Python):

"""
Created on Fri Jun 16 18:45:04 2017

@author: syaga
"""

if __name__ == "__main__":
    n = int(input())
    a = []
    for i in range(n):
        a.append(int(input()))
    max = a[1] - a[0]
    mini = a[0]
    for i in range(1, len(a)):
        diff = a[i] - mini
        if diff > max:
            max = diff
        if mini > a[i]:
            mini = a[i]
    print(max)

Solution (Python):
maxv = -1 * (10 ** 10) - 1
minv = 10 ** 10 + 1

n = int(input())

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

print(maxv)

Solution (Python):
max = -(10**9)
n = int(input())
r0 = int(input())
min = r0
for i in range(n-1):
    x = int(input())
    if max < x - min:
        max = x - min
    if x < min:
        min = x
print(max)