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:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n = I()
    R = [I() for _ in range(n)]

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

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
v = int(input())
maxv = v
minv = v
rieki = -(10**9)
for i in range(n-1) :
    v = int(input())
    if rieki < v - minv :
        rieki = v - minv
    if minv > v :
        minv = v
print(rieki)

Submission status: Accepted

Solution code:
n = int(input())

maxv = -2000000000000
minv = int(input())

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

print(maxv)

Submission status: Accepted

Solution code:
N = int(input())

ans = -(10**9+1)
for i in range(N):
    if i == 0:
        minnum = int(input())
        continue
    num = int(input())
    ans = max(ans,num-minnum)
    minnum = min(minnum,num)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
max_sub = -1145148101919#とりうる最小の値
min_value = int(input())
for i in range(1,N):
    r=int(input())
    max_sub=max(max_sub,r-min_value)#最小値
    min_value=min(min_value,r)#最小値の保持、毎回更新する
print(max_sub)

Submission status: Accepted

Solution code:
N = int(input())
t = int(input())
t1 = int(input())
mint = t
if t > t1:
  mint = t1
maxt = t1 -t
  
for i in range(2,N):
  a = int(input())
  if a - mint > maxt:
    maxt = a - mint
  if a < mint:
    mint = a
print(maxt)

Submission status: Accepted

Solution code:
n = int(input())

max_v = -1 * 10**10
min_v = int(input())

for i in range(n-1):
    num = int(input())
    max_v = max(max_v, num-min_v)
    min_v = min(min_v, num)

print(max_v)

Submission status: Accepted

Solution code:
n = int(input())
minv = 10**18
maxv = -10**18

for _ in range(n):
    new = int(input())
    maxv = max(maxv, new - minv)
    minv = min(minv, new)
    
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
minv = int(input())
maxv = -float("inf")
for i in range(1,n):
    s = int(input())
    maxv = max(maxv,s-minv)
    minv = min(minv,s)
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
minv = 0
maxv = -2000000000
a = int(input())
minv = a
for i in range(n-1):
    a = int(input())
    maxv = max(maxv,a-minv)
    minv = min(minv,a)

print(maxv)

Submission status: Accepted

Solution code:

import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines
MOD = 10 ** 9 + 7
INF = 1 << 60

# ---------------------------------------------------
# ---------------------------------------------------
# ---------------------------------------------------

def resolve():
    N = int(inp())
    A = [int(inp()) for _ in range(N)]
    minV = A[0]
    maxV = -(1<<60)
    for i in range(1, N):
        maxV = max(maxV, A[i]-minV)
        minV = min(minV, A[i])
    print(maxV)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
# 走査した部分列の最小値を覚えておけば、それ以降の走査においてはその最小値との差のみ調べればいい

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

tmp = -1000000000
minv = A[0]
for i in range(1, n):
    if tmp < A[i] - minv:
        tmp = A[i] - minv
    if A[i] < minv:
        minv = A[i]

print(tmp)

Submission status: Accepted

Solution code:
n = int(input())
diff = -100000000000
minv = int(input())
for i in range(1,n):
    a = int(input())
    diff = max(diff,a-minv)
    minv = min(a,minv)
print(diff)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
mina =a[0]
maxa=a[1]-a[0]
for i in range(1,n):
    maxa = max(a[i]-mina,maxa)
    mina = min(mina,a[i])
print(maxa)

Submission status: Accepted

Solution code:
n = int(input())

x = [int(input()) for i in range(n)]
minx = x[0]
maxx = -10000000000

for i in range(1,n):
    r = x[i] - x[i-1]
    maxx = max(maxx,x[i] - minx)
    minx = min(minx,x[i])

print(maxx)

Submission status: Accepted

Solution code:
def isPrime(x):
    if x > 2 and x % 2 == 0:
        return False
    if x == 1:
        return True
    for a in range(3,round(x**(1/2))+1,2):
        if x % a == 0:
            return False
    return True

n = int(input())

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


# pattern1
min_R = []
min = 10**9
for a in R:
    if a < min:
        min = a
    min_R.append(min)
#print(min_R)
#print(R)
R.pop(0)
calculated = [a-b for (a,b) in zip(R,min_R)]

print(max(calculated))
'''
minv = 999999999999
maxv = R[1]-R[0]
for a in range(len(R)):
    maxv = max(maxv, R[a]-minv)
    minv = min(minv, R[a])
    # print(f'{maxv} {minv}')
print(maxv)
'''

Submission status: Accepted

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

r_min = r[0]
ans = -2000000000
for i in range(1, n):
    ans = max(ans, r[i] - r_min)
    r_min = min(r[i], r_min) 
    
print(ans)

Submission status: Accepted

Solution code:
count = int(input())
l = []

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

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

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

print(maxv)

Submission status: Accepted

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

rmax = r[1] - r[0]  # 初期値は0番目と1番目の差分
imin = 0 # 最低値のindex

for i in range(1, len(r)):
	if r[i]-r[imin] > rmax:
		rmax = r[i]-r[imin]
	if r[i] < r[imin]:
		imin = i

print(rmax)

Submission status: Accepted

Solution code:
import math
import itertools

def main():
  n = int(input())
  r0 = int(input())
  minR = r0
  r1 = int(input())
  if minR > r1:
    minR = r1
  maxDiff = r1 - r0

  for _ in range(2,n):
    r = int(input())

    diff = r - minR

    if maxDiff < diff:
      maxDiff = diff

    if minR > r:
      minR = r
  
  print(maxDiff)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
# region
import sys
import itertools

DEBUG = True if (len(sys.argv) > 1 and sys.argv[1] == "DEBUG") else False


def dp(value):
    if DEBUG:
        print(value)
    return


def input_text(split=" "):
    # 入力
    result = []
    while True:
        try:
            row = list(
                map(lambda x: int(x) if x.isdecimal() else x, input().split(split))
            )
            result.append(row)
        except EOFError:
            break
    return result


# endregion
it = input_text()

r = list(itertools.chain.from_iterable(it[1:]))
r_min = sys.maxsize
diff_max = -sys.maxsize

if len(r) == 0:
    print(0)
    sys.exit()

for rj in r:
    dif = rj - r_min
    if diff_max < dif:
        diff_max = dif
    if rj < r_min:
        r_min = rj

print(diff_max)

Submission status: Accepted

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

maxv = -20000000000
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:
num = int(input())
a = []
[a.append(int(input())) for _ in range(num)]
minn = a[0]
maxn = (10 ** 9) * -1
for i in range(1,len(a)):
  maxn = max(maxn,a[i] - minn)
  minn = min(minn,a[i])
print(maxn)

Submission status: Accepted

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

r_min = R[0]
ans = -10**9
for r in R[1:]:
    ans = max(ans, r-r_min)
    r_min = min(r, r_min)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline())

minprice = 2000000000
maxdifference = -1000000000
for _ in range(n):
    price = int(stdin.readline())
    if price - minprice > maxdifference:
        maxdifference = price - minprice
    if price < minprice:
        minprice = price
print(maxdifference)

Submission status: Accepted

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

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

minimum = R[0]
sa = R[1] - R[0]

for i in range(1, n):
    if minimum > R[i-1]:
        minimum = R[i-1]

    if sa < R[i] - minimum:
        sa = R[i] - minimum

print(sa)

Submission status: Accepted

Solution code:
n = int(input())
ans = -10**10
a = []
for i in range(n):
    a.append(int(input()))
min_ = a[0]
for i in range(1,n):
    ans = max(ans, a[i]-min_)
    min_ = min(min_,a[i])
print(ans)

Submission status: Accepted

Solution code:
num = int(input())
t_list = []
for i in range(num):
    t = int(input())
    t_list.append(t)

minv = t_list[0]
maxv = t_list[1] - t_list[0]
for j in range(1,len(t_list)):
    m_buf = t_list[j] - minv
    if m_buf > maxv:
        maxv = m_buf
    
    if minv > t_list[j]:
        minv = t_list[j]

print('{}'.format(maxv))

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for _ in range(n)]
maxv = R[1] - R[0]
minv = R[0]

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

print(maxv)

Submission status: Accepted

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

mn = r[0]
ans = - 10 ** 10
for i in r[1:]:
    ans = max(ans, i - mn)
    mn = min(mn, i)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for _ in range(n)]
mn = r[0]
mx = -1000000001
for i in range(1, n) :
    mx = max(mx, r[i] - mn)
    mn = min(mn, r[i])
print(mx)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(input()) for i in range(n)]
ans = -2 * 10 ** 9 
minv = l[0]
for i in range(1, n):
    ans = max(ans, l[i] - minv)
    minv = min(minv, l[i])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[]*6
[a.append(int(input())) for x in range(n)]
minimam_num=a[0]
abs_num = -1001001001
for i in range(1,n):
    if minimam_num > a[i-1]:
        minimam_num=a[i-1]
    if abs_num < (a[i]-minimam_num):
        abs_num=(a[i]-minimam_num)
print(abs_num)

Submission status: Accepted

Solution code:
n = int(input())
r = []

for i in range(n):
    r.append(int(input()))
#
# max = r[1]-r[0]
#
# for j in range(n):
#     for k in range(j+1,n):
#         if k > j and r[k]-r[j] > max:
#             max = r[k]-r[j]
#
# print(max)

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

for i in range(n):
    Rt = int(input())
    if i == 0:
        min_Rt = Rt
    elif i == 1:
        max_p = Rt - min_Rt
        min_Rt = min(min_Rt, Rt)
    else:
        max_p = max(max_p, Rt - min_Rt)
        min_Rt = min(min_Rt, Rt)


print(max_p)

Submission status: Accepted

Solution code:
n = int(input())
l = list(int(input()) for _ in range(n))
ma = -10**9
mi = l[0]
for j in range(1,n):
    ma = max(ma,l[j]-mi)
    mi = min(mi,l[j])
print(ma)

Submission status: Accepted

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

rmin = r[0]
dmax = -10**9

for i in range(1, n):
    d = r[i] - rmin
    if (d > dmax):
        dmax = d
    if (r[i]<rmin):
        rmin = r[i]

print(dmax)

Submission status: Accepted

Solution code:
import sys

input()
a = []
for s in sys.stdin:
    a.append(int(s))

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

print(maxv)

Submission status: Accepted

Solution code:
def maxV(R, n):
    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])
    return maxv

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

Submission status: Accepted

Solution code:
n = int(input())
R = []
ans = -(float('inf'))

for i in range(n):
    r = int(input())
    if i == 0:
        min_r = r
        continue
    else:
        ans = max(ans,r-min_r)
        if r < min_r: min_r = r

print (ans)

Submission status: Accepted

Solution code:
N = int(input())
minv = int(input())
k = int(input())
maxv = k - minv
minv = min(minv,k)

for i in range(N - 2):
  k = int(input())
  maxv = max(maxv,k - minv)
  minv = min(minv,k)
    
print(maxv)

Submission status: Accepted

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

ans = -float("inf")
mini = R[0]
for i in range(1,N):
    ans = max(R[i]-mini,ans)
    mini = min(R[i],mini)
    #print(ans)
    #print(mini)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p_list = [int(input()) for i in range(n)]
maxval = -20000000000000
minval = p_list[0]
for i in range(n - 1):
  maxval = max(maxval, p_list[i + 1] - minval)
  minval = min(minval, p_list[i + 1])
print(maxval)

Submission status: Accepted

Solution code:
l = []
n=int(input())
for i in range(n):
    l.append(input())
l = list(map(int,l))
result_max = l[1] - l[0]
value_min = l[0]
for i in range(1,n):
    result_max = max(result_max,l[i]-value_min)
    value_min = min(value_min,l[i])                              
print(result_max)

Submission status: Accepted

Solution code:
n = int(input())
nums = [int(input()) for _ in range(n)]

temp_min = nums[0]
max_profit = nums[1] - nums[0]

for num in nums[1:]:
    max_profit = max(max_profit, num-temp_min)
    temp_min = min(temp_min, num)

print(max_profit)

Submission status: Accepted

Solution code:
N = int(input())
L = [int(input()) for _ in range(N)]

tmp_min = 10 ** 11
ans = -10 ** 11
for i in range(N):
    ans = max(L[i] - tmp_min, ans)
    tmp_min = min(L[i], tmp_min)
    
print(ans)

Submission status: Accepted

Solution code:
min_r = 10 ** 9
max_raise = (10 ** 9 * -1) + 1
n = int(input())
r = int(input())
min_r = min(min_r, r)
for n in range(n - 1):
    r = int(input())
    max_raise = max(max_raise, (r - min_r))
    min_r = min(min_r, r)
print(max_raise)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
maxv = -10000000000
minv = a[0]
for i in range(1,n):
  maxv = max(maxv,a[i] - minv)
  minv = min(minv,a[i])
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

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

ans = -float('inf')
min_r = R[0]

for i in range(1, n):
    if ans < R[i] - min_r:
        ans = R[i] - min_r
    if R[i] < min_r:
        min_r = R[i]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = []
for _ in range(n):
    S.append(int(input()))
mini = S[0]
ans = -10**9
for i in S[1:]:
    ans = max(ans,i-mini)
    mini = min(i,mini)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

price_list = list()

max_price = -20000000000
min_val = 2000000000

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

   if i == 0:
      min_val = price_list[0]
      continue

   min_val = price_list[i-1] if min_val > price_list[i-1] else min_val
   max_price = price_list[i] - min_val if price_list[i] - min_val > max_price else max_price

print(max_price)

Submission status: Accepted

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

sub = R[1] - R[0]
minimum = R[0]

for j in range(1,n):
    sub = max(sub, R[j] - minimum)
    minimum = min(minimum,R[j])
    
print(str(sub))

Submission status: Accepted

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

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

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())
r = [int(input()) for i in range(n)]
minv = r[0]
maxv = -999999999
for i in range(1,n):
    maxv = max(maxv,r[i]-minv)
    minv = min(minv,r[i])
print(maxv)

Submission status: Accepted

Solution code:
# Problem - 最大の利益

# input
N = int(input())
nums = [0]*N
for i in range(N):
    nums[i] = int(input())

# initialization
min_v = nums[0]
max_v = nums[1] - nums[0]
for i in range(1, N):
    max_v = max(max_v, nums[i] - min_v)
    min_v = min(min_v, nums[i])

# output
print(max_v)

Submission status: Accepted

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

mx = -1 * (10 ** 9)
mn = R[0]

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

print(mx)

Submission status: Accepted

Solution code:
N=int(input())
R=[]
for n 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)

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for i in range(n)]
maxv = -9999999999
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:
N=int(input())
R=[int(input()) for _ in range(N)]
maxv=-1*10**9
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:
n = int(input())
A = [int(input()) for _ in range(n)]
minv = A[0]
maxd = -1*(10**9)-1
for i in range(1, n):
    maxd = max(maxd, A[i]-minv)
    minv = min(minv, A[i])
print(maxd)

Submission status: Accepted

Solution code:
"""
#問題の意味わかってないね
#print(l)
mini = l[0] 
maxi = l[len(l)-1]
#print(id(mini))#idは有効期間が重ならない２つのオブジェクトについては同じ値を持つことがあるらしい
#print(id(maxi))
for i in range(1,len(l)):
    if l[i]<mini:
        mini=l[i]
        if(i == len(l)-1):
            print(-1)
            sys.exit(0)
    
if mini is maxi:
    print(-1)
    sys.exit(0)

for i in range(l.index(mini),len(l)):
    if l[i]>maxi:
        maxi=l[i]
print(maxi-mini)
"""

import sys
n = int(input())
#l = [int(input().rstrip()) for _ in range(n)]
mini = int(input())
t=int(input())
maxi=t-mini
if t<mini:
    mini=t
for i in range(n-2):
    n=int(input())
    if n-mini>maxi:
        maxi=n-mini
    if n<mini:
        mini=n
print(maxi)

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(input()) for _ in range(n)]
Min, ans = 2e9 + 10, -int(2e9 + 10)
for i in arr:
    ans = max(ans, i - Min)
    Min = min(Min, i)
print(ans)

Submission status: Accepted

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

maxv = -20000000000
minv = R[0]

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

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for _ in range(n)]
min = r[0]
max = r[1]-r[0]
for i in range(1,n):
    if max < r[i]-min:
        max = r[i]-min
    if min > r[i]:
        min = r[i]
print(max)

Submission status: Accepted

Solution code:
r = []

n = int(input())
for i in range(n):
    ri = int(input())
    r.append(ri)
    
minv = r[0]
maxv = -10**11

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

Submission status: Accepted

Solution code:
n = int(input())

mini = float('inf')
max_diff = -float('inf')
for _ in range(n):
    r = int(input())
    if r - mini > max_diff:
        max_diff = r - mini

    if r < mini:
        mini = r

print(max_diff)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = []
    for _ in range(n):
        a.append(int(input()))
    lower = a[0]
    res = -10000000007
    for i in range(1,n):
        res = max(res,a[i]-lower)
        lower = min(lower,a[i])
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

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

ans = -10 ** 9

mi = R[0]

for i in range(1, n):
    ans = max(ans, R[i] - mi)
    if R[i] < mi:
        mi = R[i]

print(ans)

Submission status: Accepted

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

m = r[0]
ans = - float('inf')
for i in range(1, n):
    ans = max(ans, r[i] - m)
    if r[i] <= m:
        m = r[i]
    # print('i', i)
    # print('m', m)
    # print('r[]', r[i])
    # print('r[i] - m', r[i] - m)
    # print('ans', ans)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
max_diff = -10**10

current_min = int(input())

for _ in range(n-1):
    current = int(input())
    diff = current - current_min
    if diff > max_diff:
        max_diff = diff
    if current < current_min:
        current_min = current
print(max_diff)

Submission status: Accepted

Solution code:
N=int(input())
R=[int(input()) for i in range(N)]
max_prifit=R[1]-R[0]
minR=R[0]
for i in range(1,N):
    max_prifit=max(max_prifit,R[i]-minR)
    minR=min(minR, R[i])

print(max_prifit)

Submission status: Accepted

Solution code:
N = int(input())
Rt = []

for _ in range(N):

    # 価格
    Rt.append(int(input()))

minR = 10**9
profit = Rt[1]-Rt[0]

for i in range(0,N-1):
    minR = min(minR, Rt[i])
    profit = max(profit, Rt[i+1] - minR)

print(profit)

Submission status: Accepted

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

m = r[0]
M = -2000000000

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

print(M)

Submission status: Accepted

Solution code:
#!/usr/local/bin python
# -*- coding: utf-8 -*-
#
# ~/aizu/alds11d_maximumProfit.py
#
n = int(input())
r = []
for i in range(n):
    r.append(int(input()))

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

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


print(maxv)

Submission status: Accepted

Solution code:
# -*- coding:utf8 -*-

n = int(input())
l = [int(input()) for _ in range(n)]
minv = l[0]
maxv = l[1] - l[0]
for x in l[1:]:
    if x - minv > maxv:
        maxv = x - minv
    if x < minv:
        minv = x
print(maxv)

Submission status: Accepted

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

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


print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
minv = int(input())
maxv = -9999999999
for i in range(n-1):
    num = int(input())
    maxv = max(maxv, num - minv)
    minv = min(minv, num)
print(maxv)

Submission status: Accepted

Solution code:
N = int(input())
R = []

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

mins = [R[0]]
m = R[0]
for i in range(1,N):
    m = min(m,R[i])
    mins.append(m)

ans = -1e9
for i in range(1,N):
    ans = max(ans,R[i]-mins[i-1])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for i in range(n)]
maxv = -10**10
minv = A[0]

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

print(maxv)

Submission status: Accepted

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

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

max_profit = -1000000000
minmum_value = R[0]

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

print(max_profit)

Submission status: Accepted

Solution code:
N = int(input())
R =  [0]*N

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

m = R[0]
ans = -1*10**9

for i in range(1,N):
    if R[i] - m > ans:
        ans = R[i] - m
    if R[i] < m:
        m = R[i]
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(input()) for _ in range(n)]
minv, ans = p[0], -float('inf')

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for _ in range(n)]
minv = r[0]
maxv = r[1] - r[0]
for i in range(1,n):
    if maxv < r[i] -minv:
        maxv = r[i] -minv
    if minv > r[i]:
        minv = r[i]
    
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(input()) for i in range(n)]
vmin,vmax = lst.pop(0),-1000000000000
for i in lst:
    vmax = max(vmax,i-vmin)
    vmin = min(vmin,i)
print(vmax)

Submission status: Accepted

Solution code:
min_r = 100000000000
R = -100000000000

for i in range(int(input())):
    r = int(input())
    
    if i > 0:
        R = max(R, r - min_r)
        
    min_r = min(min_r, r)
    
print(R)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
# print(a)
maxv = -2*10**9
minv = 10000000000
for i in range(n):
  maxv = max(maxv,a[i]-minv)
  minv = min(a[i],minv)

print(maxv)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
rt=[int(input()) for x in range(N)]
min=rt[0]
# if rt[0]>rt[1]:min=rt[1]
max=rt[1]-rt[0]
for j in range(1,N):
    if max < rt[j] - min: max = rt[j] - min
    if min> rt[j]:min=rt[j]

print(max)

Submission status: Accepted

Solution code:
n = int(input())
minvalue = int(input())
maxprofit = -2000000000
for i in range(1,n):
    Ri = int(input())
    maxprofit = max(Ri - minvalue, maxprofit)
    minvalue = min(minvalue, Ri)
print(maxprofit)

Submission status: Accepted

Solution code:
n=int(input())
a=0
table=[]
while a<n:
	s=int(input())
	table.append(s)
	a+=1

maxv=-10**9
minv=table[0]
for i in range(1,n,1):
	if maxv<table[i]-minv:
		maxv=table[i]-minv
	if minv>table[i]:
		minv=table[i]
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

ans = -10000000000000000000
mn = 1000000000000000000
for i in range(n):
    x = int(input())
    ans = max(ans, x - mn)
    mn = min(mn, x)

print(ans)

Submission status: Accepted

Solution code:
n, *A = map(int, open(0).read().split())
m = A[0]
M = -float('inf')
for a in A[1:]:
    M = max(M, a-m)
    m = min(m, a)
print(M)

Submission status: Accepted

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

# Input line num
num = int(input())
# Input rates
rates = [int(input()) for i in range(num)]

maxv = -100000000000
minv = 100000000000

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

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for _ in range(n)]
ans = -float("inf")
m = r[0]
for i in range(1,n):
    d = r[i]-m
    ans = max(d,ans)
    m = min(m,r[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
t = int(input())
mint = t
t1 = int(input())
if t1 < t:
    mint = t1
maxt = t1 - t

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

print(maxt)

Submission status: Accepted

Solution code:
N = int(input())
R = [int(input()) for _ in range(N)]
min_num = R[0]
now_profit = R[1] - R[0]
for i in range(1,N):
    now_profit = max(R[i]-min_num, now_profit)
    if min_num > R[i]:
        min_num = R[i]
print(now_profit)

Submission status: Accepted

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

min_r = 10**10
max_d = -10**10
for i in range(N):
    if i != 0:
        d = R[i] - min_r
        if d > max_d:
            max_d = d
    if R[i] < min_r:
        min_r = R[i]
print(max_d)

Submission status: Accepted

Solution code:
n = int(raw_input())
r = [int(raw_input()) for _ in range(n)]

mn = r[0]
ans = 1000000000 * (-1)

for i in range(1, len(r), 1):
  ans = max(ans, r[i] - mn)
  mn = min(mn, r[i])

print ans

Submission status: Accepted

Solution code:
n = int(input())
min_value = int(input())
diff = (-1) * min_value

for i in range(n - 1):
    rt = int(input())
    diff = max(diff, rt - min_value)
    min_value = min(rt, min_value)

print(diff)

Submission status: Accepted

Solution code:
n = int(input())
ans = -10**10
old = int(input())
for _ in range(n-1):
    num = int(input())
    if num - old > ans:
        ans = num - old
    if num < old:
        old = num
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
min_v = float("inf")
max_p = -float("inf")
for i in range(N):
    v = int(input())
    max_p = max(max_p,v-min_v)
    min_v = min(min_v,v)
print(max_p)

Submission status: Accepted

Solution code:
n=int(input())
min_val=10**10
ans=-(10**10)
for i in range(n):
    cu=int(input())
    ans=max(ans,cu-min_val)
    min_val=min(min_val,cu)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
minv = 10000000000
max_profit = -1 * minv
for i in range(n):
    r = int(input())
    max_profit = max(max_profit, r - minv)
    minv = min(minv, r)

print(max_profit)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())

l=[int(input()) for x in range(N)]

l_min=l[0]
profit_max=l[1]-l[0]

for i in l[1:]:
    profit_max=max(profit_max,i-l_min)
    l_min=min(i,l_min)

print(profit_max)

Submission status: Accepted

Solution code:
n = int(input())
ans = -(10**99)
l = []
for _ in range(n):
    x = int(input())
    l.append(x)
mini = l[0]
for i in range(1, n):
    t = l[i] - mini
    if t > ans:
        ans = t

    if mini > l[i]:
        mini = l[i]


print(ans)