Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

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) &times; 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



Sample input:
6
5
3
1
3
4
3


Sample output:
3

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input()) # 2 >= n
    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(i+1,now_value,max_profit)
    print(max_profit)
    return


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- Coding: utf-8 -*-

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ALDS_1_1_D Maximum Profit

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

  future_max = max(numbers[i+1:])
  #print(numbers[i+1:])
  t = numbers[i+1:]
  #print((max(t)))
  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)
        #print("temp: " + str(temp))
        if temp > m:
            m = temp
            ti = numbers[i]

print (m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ALDS1_1_D

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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#coding:utf-8

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
#coding:utf-8
#1_1_D
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# ?§£??¬??????????????????????????¨????????¨??¬??¬????????§??????

# input & calc
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)

# output
print(max_diff)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# coding: utf-8
# ?§£??¬??????????????????????????¨????????¨??¬??¬????????§??????

# input & calc
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)

# output
print(max_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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 "i = %d, tmp = %d, maxv = %d, minv = %d" % (i, tmp, maxv, minv)

print maxv

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# encoding=utf-8
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

Submission status: Accepted

Solution code:
# encoding=utf-8
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

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

for v in range(1, n):
   if minv > R[v]:
      minv = R[v]
   elif maxv < R[v] - minv:
      maxv = R[v] - minv
print maxv

Submission status: Accepted

Solution code:
n = input()
R = []
for i in range(0, n):
   R.append(int(raw_input()))

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

for v in range(1, n):
   if minv > R[v]:
      minv = R[v]
   elif maxv < R[v] - minv:
      maxv = R[v] - minv
print maxv

Submission status: Accepted

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

minv = float('inf')
maxv = -float('inf')
for r in R:
    maxv = max(maxv, r-minv)
    minv = min(minv, r)
print(maxv)

Submission status: Accepted

Solution code:
n = int( raw_input( ) )
minv = int( raw_input( ) )
maxv = 0 - 1000000000
 
for j in range( n-1 ):
  num = int( raw_input( ) )  
  diff = num - minv
  if maxv < diff:
    maxv = diff
  if num < minv:
    minv = num
 
print maxv

Submission status: Accepted

Solution code:
n = raw_input()
n = int(n)
minv = raw_input()
minv = int(minv)
maxv = -1000000000
  
for j in range(n-1):
  num = raw_input()
  num = int(num)  
  diff = num - minv
  if maxv < diff:
    maxv = diff
  if num < minv:
    minv = num
  
print maxv

Submission status: Accepted

Solution code:
n = int(input())

min_price = int(input())
max_diff = -1e9

for _ in range(n - 1):
    p = int(input())
    max_diff = max(p - min_price, max_diff)
    min_price = min(p, min_price)

print(max_diff)

Submission status: Accepted

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

def smax(N,a):
  smin = a[0]
  smax = -10**9
  for j in range(1,N):
   smax = max(smax,a[j]-smin)
   smin = min(smin,a[j])
  return smax

print smax(N,a)

Submission status: Accepted

Solution code:
N = int(raw_input())
min = int(raw_input())
max = int(raw_input())
dif = max - min
if max < min:
    min = max

for i in range(N - 2):
    R = int(raw_input())
    if R - min > dif:
        dif = R - min
    if R < min:
        min = R

print dif