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:
if __name__ == '__main__':
    N = int(input())
    A = [int(input()) for _ in range(N)]
    minv = A[0]
    maxv = A[1] - 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())
max_val = -1 * 10 ** 9
min_r = int(input())

for _ in  range(N - 1):
  R = int(input())
  val = R - min_r
  max_val = max(val, max_val)
  min_r = min(R, min_r)

print(max_val)

Submission status: Accepted

Solution code:
n = int(input())

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

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
R =[0 for i in range(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:
num = int(input())


for i in range(num):
    v = int(input())
    if i == 0:
        minv =v
        maxv = -1000000000
        continue
    maxv  = max(maxv, v-minv)
    minv  = min(minv, v)
    
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
def test():

	n = int(input())
	
	max = -10000000000
	m_min = int(input())

	for i in range(n-1):

		next = int(input())

		if max < (next - m_min):
			max = next - m_min

		if m_min > next:
			m_min = next

	print(max)


if __name__ == "__main__":
	test()

Submission status: Accepted

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

profit = -1e10 # 利益
min_val = int(input())

for i in range(1, n):
    val = int(input())
    diff = val - min_val
    if (diff > profit):
        profit = diff
    if (val < min_val):
        min_val = val

print(profit)

Submission status: Accepted

Solution code:
#import pysnooper
#import os,re,sys,operator,math,heapq,string
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy
setrecursionlimit(10**6)

#@pysnooper.snoop()
n=int(stdin.readline().rstrip())
a=[int(stdin.readline().rstrip()) for _ in range(n)]
ans=0
mi=a[0]
ma=-(10**10)
for i in range(1,n):
    ma=max(ma,a[i]-mi)
    mi=min(mi,a[i])
print(ma)

Submission status: Accepted

Solution code:
n=int(input())
R=[]
for i in range(n):
    R.append(int(input()))
Rmin=R[0]
Rmax=-2000000000
for i in range(1,n):
    Rmax=max(Rmax,R[i]-Rmin)
    Rmin=min(Rmin,R[i])
print(Rmax)

Submission status: Accepted

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

tempmax = rs[-1]    
rmaxs = [0]*(n-1)
rmaxs[-1] = tempmax

for i in range(n-2,0,-1):
    if rs[i] > tempmax:
        tempmax = rs[i]
    rmaxs[i-1] = tempmax

ans = rmaxs[0] - rs[0]

for i in range(1,n-1):
    if rmaxs[i] - rs[i] > ans:
        ans = rmaxs[i] - rs[i]

print(ans)

Submission status: Accepted

Solution code:
n  = int(input())
R = [int(input()) for _ in range(n)]
ans = -1e10
minv = R[0]
for j in range(1,n):
    ans = max(ans, R[j] - minv)
    minv = min(minv, R[j])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
INF = 2000000000

N = int(raw_input())
p = int(raw_input())
minimum = p
maximum = -INF
for _ in range(N-1):
    t = int(raw_input())
    maximum = max(maximum, t-minimum)
    minimum = min(minimum, t)
print maximum

Submission status: Accepted

Solution code:
n = int(input())
max_revenue = -1000000000
min_price = 1000000000
min_price = int(input())
for _ in range(n-1):
    price = int(input())
    if max_revenue < price - min_price: max_revenue = price - min_price
    if min_price > price: min_price = price
print(max_revenue)

Submission status: Accepted

Solution code:
n=int(input())
R =[int(input()) for i in range(n)]
r=R[0]
maxV=-float('inf')
for i in range(n-1):
	if R[i]<r:
		r=R[i]
	if maxV<R[i+1]-r:
		maxV=R[i+1]-r
print(maxV)

Submission status: Accepted

Solution code:
n = int(input())
max_revenue = -1000000000
min_price = 1000000000
min_price = int(input())
for _ in range(n-1):
    price = int(input())
    if max_revenue < price - min_price: max_revenue = price - min_price
    if min_price > price: min_price = price
print(max_revenue)

Submission status: Accepted

Solution code:
n=int(input())

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

maxv=-1000000000
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:

n = int(input())

maxi = 0
mini = float("inf")


lmin = []
rmax = []

R = []

for i in range(n):

    r = int(input())
    R.append(r)

    if len(lmin) == 0:
        lmin.append(r)

    else:
        lmin.append(min(lmin[-1],r))

for i in range(n):

    i = n-1-i

    if len(rmax) == 0:
        rmax.append(R[i])

    else:
        rmax.append(max(R[i],rmax[-1]))

rmax.reverse()
lmin.insert(0,float("inf"))

ans = -1 * float("inf")

for i in range(n):

    ans = max(ans,rmax[i]-lmin[i])

#print (lmin)
#print (rmax)

print (ans)

Submission status: Accepted

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

def maxR(R):
    n = len(R)
    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])
    return maxv

print(maxR(R))

Submission status: Accepted

Solution code:
N = int(input())
args = [int(input()) for i in range(N)]
minargs = args[0]
result = - 10 ** 10
for i in range(1,N):
    minargs = min(minargs,args[i-1])
    result = max(result, args[i] - minargs)
print(result)

Submission status: Accepted

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

# 数字入力　行数＋数字
n=int(input()) #行数

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

# print(num_list)

# Derive the maximum profit under the condition j > i
# max_profit = num_list[1] - num_list[0]
# min_value = num_list[0]
# max_value = num_list[1]
# i, j = 0, 0
# # Before
# for j in range(1, n):
#     for i in range(0, j):
#         max_profit = max(max_profit, num_list[j]-num_list[i])
# print("max_profit with algorithm 1 is "+ str(max_profit))

min_value = num_list[0]
max_profit = -10**9
# After
for i in range(1,n):
        max_profit = max(max_profit, num_list[i] - min_value)
        min_value = min(num_list[i], min_value)

# print("max profit with algorithm 2 is " + str(max_profit))
print(max_profit)

Submission status: Accepted

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

min_i = N[0]
max_d = -9999999999
for j in range(1, r):
    diff = N[j] - min_i
    if diff > max_d:
        max_d = diff
    if N[j] < min_i:
        min_i = N[j]
print(max_d)

Submission status: Accepted

Solution code:
valuenum = int(input())

values = [int(input()) for _ in range(valuenum)]

Minv = values.pop(0)
ans = -(10 ** 9)

for Rj in values:
  temp_ans = Rj - Minv
  if temp_ans > ans:
    ans = temp_ans
  if Rj < Minv:
    Minv = Rj

print(ans)

Submission status: Accepted

Solution code:
def maxProf2(n, a):
    init = True
    for i in range(1, n):
        if init:
            vmin = min(a[:i])
            vmax = a[i]
            diffmax = vmax - vmin
            init = False
            continue
        vmin = min(vmin, a[i-1])
        diffmax = max(diffmax, a[i]-vmin)
    print(diffmax)
    return

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

Submission status: Accepted

Solution code:
def maxProfit(list):
    mini = 0
    ans = list[1] - list[0]
    for i in range(1, len(list)):
        if ans < list[i] - list[mini]:
            ans = list[i] - list[mini]
        if list[mini] > list[i]:
            mini = i
    return ans

N = int(input())
prolist = []
for i in range(N):
    prolist.append(int(input()))
print(maxProfit(prolist))

Submission status: Accepted

Solution code:
def MaximumProfit():
    N = int(input())
    num = int(input())
    _min = num

    num2 = int(input())
    if num2 < num : _min = num2
    _maxn2n = num2 - num
    
    for i in range(2, N):
        num = int(input())
        if _maxn2n < num - _min : _maxn2n = num - _min
        if _min > num : _min = num

    return _maxn2n

print(MaximumProfit())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
max_value = -20000000000
min_value = int(input())

for i in range(num-1):
    value = int(input())
    max_value = max([max_value, value - min_value])
    min_value = min([min_value, value])
    
print(max_value)

Submission status: Accepted

Solution code:
N = int(input())
lst = []
  
  
for i in range(N):
  lst.append(int(input()))
  
M = lst[1] - lst[0]
m = lst[0]
  
for j in range(1, N):
    M = max([M, lst[j] - m])
    m = min([m, lst[j]])
              
print(M)

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:
num_inputs = int(input())

for i in range(num_inputs):
    current_rate = int(input())
    if i == 0:
        minimum_rate = current_rate
        continue
    
    profit = current_rate - minimum_rate
    if i == 1:
        max_profit = profit
    elif profit > max_profit:
        max_profit = profit
    
    if current_rate < minimum_rate:
        minimum_rate = current_rate

print(max_profit)

Submission status: Accepted

Solution code:
n = int(input())
prices = [int(input()) for _ in range(n)]
minv = prices[0]
maxv = prices[1]-minv
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())
r=[int(input()) for _ in range(n)]
b=r[0]
h=[]
for i in range(1,n):
    h.append(r[i]-b)
    b=min(b,r[i])
print(max(h))

Submission status: Accepted

Solution code:
N = int(input())
k = 0
ans = -(10**10)
for i in range(N):
    R = int(input())
    if i==0:k=R;continue
    ans = max(ans,R-k)
    k = min(k,R)
print(ans)

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())
r=[]
for _ in range(n):
    r.append(int(input()))
maxv=r[1]-r[0]
minv=r[0]
for j in range(1,n):
    maxv=maxv if maxv>r[j]-minv else r[j]-minv
    minv=minv if minv<r[j] else r[j]
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

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

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 i in range(n)]

maxv = -20000000000000000
minv = r[0]
for rt in range(1,len(r)):
    maxv = max(maxv,r[rt] - minv)
    minv = min(minv,r[rt])

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 = int(input())
c = []
max = -(10)**12
min = (10)**12

for i in range(n):
  m = int(input())
  
  if(max < m - min):
    max = m - min
  if min > m:
    min = m
  c.append(m)

print(max)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
min_r = int(input())
max_val = 1 - min_r
for _ in range(n - 1):
    r = int(input())
    diff_r = r - min_r
    if diff_r >= max_val:
        max_val = diff_r
    if r <= min_r:
        min_r = r
print(max_val)

Submission status: Accepted

Solution code:
n = int(input())

in_list = [int(input()) for _ in range(n)]
min_val = in_list[0]
max_sub = -1e9

for i in range(1, n):
    if in_list[i] - min_val > max_sub:
        max_sub = in_list[i] - min_val

    if in_list[i] < min_val:
        min_val = in_list[i]

print(max_sub)

Submission status: Accepted

Solution code:
N = int(input())
lst = []
 
 
for i in range(N):
  lst.append(int(input()))
 
M = lst[1] - lst[0]
m = lst[0]
 
for j in range(1, N):
    M = max([M, lst[j] - m])
    m = min([m, lst[j]])
             
print(M)

Submission status: Accepted

Solution code:
class inte:
	n = 0
	def __init__(self, n):
		self.n = n
	def chmax(self, n):
		self.n = max(self.n, n)
	def chmin(self, n):
		self.n = min(self.n, n)

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

INF = 10 ** 12

min_v = inte(A[0])
max_o = inte(-INF)

for a in A[1:]:
	max_o.chmax(a - min_v.n)
	min_v.chmin(a)

print(max_o.n)

Submission status: Accepted

Solution code:
import math

n=int(input())

t=int(input())
mint=t
t1=int(input())
mint = min(t, t1)
maxtt=t1-t


for i in range(2,n):
    t=int(input())
 
    maxtt = max(t-mint, maxtt)
    mint = min(t, mint)
    # if t-mint>maxtt: maxtt=t-mint
    # if t<mint: mint=t
    
print(int(maxtt))

Submission status: Accepted

Solution code:
n = int(input())
maxv = -10**9
minv = int(input())
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:
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:
n = input()
Rt = []
for i in range(int(n)):
    Rt.append(int(input()))   ####入力をinputで取得!!!!
R  = []
maxv = -10**9
minv = Rt[0]

for i in range(1,len(Rt)):
    if maxv < Rt[i]-minv:
        maxv = Rt[i]-minv
    if minv > Rt[i]:
        minv = Rt[i]
    #print(maxv,minv)
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

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

maxv = -(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())
saitei=10**9
result=-(10**9)
for i in range(N):
  R=int(input())
  if i > 0 and result < R-saitei:
    result = R-saitei
  if saitei > R:
    saitei = R
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input()) #1行目のNを取得する
    minv = int(input())
    maxv = 10**9*(-1)
    for j in range(N-1):
        r = int(input())
        if maxv < r-minv: maxv = r-minv
        if minv >r: minv = r
    print(str(maxv))

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for i in range(n)]
ans = R[1] - R[0]
R_i = R[0]
for j in range(1,n):
    ans = max(ans, R[j] - R_i)
    R_i = min(R_i, R[j])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for a in range(n) ]
minA = A[0]
maxsa = A[1] - A[0]
for i in range(1, n):
    s = A[i] - minA
    minA = min(minA, A[i])
    maxsa = max(maxsa, s)

print(maxsa)

Submission status: Accepted

Solution code:
if __name__=='__main__':
    N = int(input())
    s = -1000000000
    a = int(input())
    for i in range(1, N):
        b = int(input())
        s = b - a if s < b - a else s
        a = b if b < a else a
    print(s)

Submission status: Accepted

Solution code:
n = int(input())

r = -10**9
m = int(input())
inp = m

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

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    time_interval = int(input())
    t0_num = int(input())
    min_num = t0_num
    t1_num = int(input())
    if t1_num < t0_num:
        min_num = t1_num
    max_diff = t1_num - t0_num

    for _ in range(2, time_interval):
        t_num = int(input())
        if max_diff < t_num - min_num:
            max_diff = t_num - min_num
        if t_num < min_num:
            min_num = t_num

    print(f"{max_diff}")

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for _ in range(n)]
maxv = 10 ** 9 * (-2)
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 main():
    n = int(input())
    rs = [int(input()) for _ in range(n)]
    result = -2000000000000
    history_min = rs[0]
    for r in rs[1:]:
        result = max(result, r - history_min)
        history_min = min(history_min, r)
    print(result)
        


if __name__ == '__main__':
    main()

Submission status: Accepted

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


print(maxv)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
nums = [int(stdin.readline().rstrip()) for _ in range(N)]

minimum = nums[0]
ans = (10**9)*-1
for i in range(1, N):
    ans = max(ans, nums[i]-minimum)
    minimum = min(minimum, nums[i])
print(ans)

Submission status: Accepted

Solution code:
def max_crossing_subarray(A, low, mid, high):
    s = 0
    left_max = -10**9
    left_idx = mid
    for i in range(mid, low - 1, -1):
        s += A[i]
        if s > left_max:
            left_max = s
            left_idx = i

    s = 0
    right_max = -10**9
    right_idx = mid + 1
    for i in range(mid + 1, high + 1):
        s += A[i]
        if s > right_max:
            right_max = s
            right_idx = i
    return left_idx, right_idx, left_max + right_max


def max_subarray(A, low, high):
    #print ">> max_subarray(A, %d, %d)" % (low, high)
    if low == high:
        return low, low, A[low]
    mid = (low + high) / 2
    left_ret = max_subarray(A, low, mid)
    right_ret = max_subarray(A, mid + 1, high)
    cross_ret = max_crossing_subarray(A, low, mid, high)
    if left_ret[2] >= right_ret[2] and left_ret[2] >= cross_ret[2]:
        return left_ret
    elif right_ret[2] >= left_ret[2] and right_ret[2] >= cross_ret[2]:
        return right_ret
    else:
        return cross_ret


if __name__ == '__main__':
    n = int(raw_input())
    R = []
    for i in range(n):
        R.append(int(raw_input()))
    minv = R[0]
    maxp = -10**9
    for j in range(1, n):
        maxp = max(maxp, R[j] - minv)
        minv = min(minv, R[j])
    print maxp
    #A = [R[i] - R[i - 1] for i in range(1, n)]
    #print max_subarray(A, 0, len(A) - 1)[2]

Submission status: Accepted

Solution code:
N = int(input())

maxv = -1 * 10 ** 9

minv = int(input())

for i in range(N-1):
    R = int(input())
    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)]

max_diff = -1 * 10**9
min_value = R[0]
for j in range(1, N):
    # 差分のうち最大値
    max_diff = max(max_diff, R[j] - min_value)
    # 過去の最小値
    min_value = min(min_value, R[j])
print(max_diff)

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
import sys
current_min=1e10;
max_profit=-current_min
N = int(input())
for rate in [int(input()) for _ in range(N)]:
    if max_profit < rate - current_min:
        max_profit = rate - current_min
    if current_min > rate:
        current_min = rate
print(max_profit)

Submission status: Accepted

Solution code:
n=int(input())
r=[int(input()) for _ in range(n)]
maxr=-(10**10)
minr=r[0]
for i in range(1,n):
    maxr=max(maxr,r[i]-minr)
    minr=min(minr,r[i])
print(maxr)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
max_v = -10**9 * 2
min_v = int(input())
for i in range(1, n):
    R = int(input())
    max_v = max(max_v, R - min_v)
    min_v = min(min_v, R)
print(max_v)

Submission status: Accepted

Solution code:
n = int(input())

minv=999999999999
ans=-999999999999

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

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



print(ans)

Submission status: Accepted

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

def profit(R):
    minv = R[0]
    maxv = min(R) - max(R)
    for i in range(1, len(R)):
        if maxv < R[i] - minv:
            maxv = R[i] - minv
        if minv > R[i]:
            minv = R[i]
    return maxv

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

Submission status: Accepted

Solution code:
n = int(input())
n_min = int(input())
diff_max = -10**9-7
for i in range(n-1):
    num = int(input())
    diff_max = max(diff_max, num-n_min)
    n_min = min(num, n_min)
print(diff_max)

Submission status: Accepted

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

minv = r[0]
maxv = -float("inf")
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())
price0 = 0
minp = 0
maxv = 0

for i in range(n):
    if i == 0:
        price0 = int(input())
        continue
    if i == 1:
        price1 = int(input())
        minp = min(price1, price0)
        maxv = price1 - price0
        continue
    price = int(input())
    value = price - minp
    minp = min(minp, price)
    maxv = max(maxv, value)
    
print(maxv)

Submission status: Accepted

Solution code:
n=int(input())
data=[]
for i in range(n):
    data.append(int(input()))
leftmin=data[0]
for j in range(1,n):
    if data[j-1]<leftmin:
        leftmin=data[j-1]
    ansj=data[j]-leftmin
    if j==1:
        ans=ansj
    else:
        if ansj>ans:
            ans=ansj

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
mi=1000000000000000
ma=-10000000000000000
  
for i in range(n):
    r=int(input())
    ma=max(ma,r-mi)
    mi=min(mi,r)
  
print(ma)

Submission status: Accepted

Solution code:
n = int(input())

min_x = float("inf")
max_diff = float("-inf")
for _ in range(n):
    x = int(input())
    diff = x - min_x
    max_diff = max(max_diff, diff)
    min_x = min(x, min_x)

print(max_diff)

Submission status: Accepted

Solution code:
N = int(input())

R = []
for _ in range(N):
    r = int(input())
    R.append(r)

ri = R[0]

import sys
INT_MIN = sys.maxsize * -1

ans = INT_MIN

for rj in R[1:]:
    ans = max(ans, rj-ri)
    ri = min(ri, rj)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
for i in range(n):
    cur = int(input())
    if i == 0:
        ans = -10**9
        m = cur
    else:
        ans = max(ans,cur-m)
        m = min(m,cur)
print(ans)

Submission status: Accepted

Solution code:
def solve():
    input_line = input()
    N = int(input_line)
    for i in range(N):
        input_line = input()
        num = int(input_line)
        if i == 0:
            min_value = num
        elif i == 1:
            max_value = num-min_value
            min_value = min(num, min_value)
        else:
            max_value = max(max_value, num-min_value)
            min_value = min(num, min_value)
    print(max_value)

solve()

Submission status: Accepted

Solution code:
#ヒントで計算量の指摘あったので今後は計算量も考慮、ヒントのママ
#i>jのみ。つまり空売りなし
import math
n=int(input())
i=0
R=[0 for i in range(n)]
while i<n:
    R[i]=int(input())
    i+=1
maxp=R[1]-R[0]
mini=R[0]#たぶんminではいけない
for i in range(n-1):
#    print(maxp,mini)
    maxp = max([R[i+1]-mini,maxp])#逆だとi=jが含まれてしまう
    mini = min([R[i+1],mini])
print(maxp)
#print(mini)

Submission status: Accepted

Solution code:
n=int(input())
da=[int(input()) for i in range(n)]
ma=[0 for i in range(n-1)]
mi=[0 for i in range(n-1)]
MA=da[n-1]
MI=da[0]
ma[n-2]=MA
mi[0]=MI
for i in range(1,n-1):
    MA=max(MA,da[n-i-1])
    MI=min(MI,da[i])
    ma[n-i-2]=MA
    mi[i]=MI
ans=-10e9
for i in range(n-1):
    ans=max(ans,ma[i]-mi[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
r = [int(input()) for i in range(n)]
minv=r[0]
maxv=-1000000000
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)]

maxv = float('-inf')
minr = r[0]
for j in range(1, n):
    maxv = max(maxv, r[j] - minr)
    minr = min(minr, r[j])

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

min_v = 2000000000
max_v = -2000000000

for _ in range(n):
    x = int(input())
    max_v = max(max_v, x - min_v)
    min_v = min(min_v, x)

print(max_v)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for _ in range(n)]
max_dif = - 10 ** 9
min_r = R[0]
l = len(R)
for i in range(1, l):
    max_dif = max(max_dif, R[i] - min_r)
    min_r = min(min_r, R[i])

print(max_dif)

Submission status: Accepted

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

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

for j in xrange(1,n):
    if( maxv < R[j] - minv):
        maxv = R[j] - minv
    if( R[j] < minv ):
        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 = R[1] - R[0]

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

Submission status: Accepted

Solution code:
#与えられる数列の中で、最大値と最小値のペアを返す

length = int(input())
List = [int(input()) for i in range(length)]

minimum = List[0]
maximum = List[1] - List[0]

for j in range(1,length):
    maximum = max(maximum, List[j]-minimum)
    minimum = min(minimum, List[j])

print(maximum)

Submission status: Accepted

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

min=2*10**9
ans=-2*10**9

for i in R:
  if i-min>ans:
    ans=i-min
  if min>i:
    min=i

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for _ in range(1, n):

    R = int(input())

    maxv = max(maxv, R - minv)   # Rtにおける最大のRj - Riを取得
    minv = min(minv, R)          # 入力されたRの中の最小を取得


print(maxv)

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 j in range(1,n):
        maxv = max(r[j] - minv, maxv)
        minv = min(r[j], minv)

print(maxv)

Submission status: Accepted

Solution code:
import sys

n = int(input())
nums = [int(input()) for i in range(n)]
maxnum = - sys.maxsize
prevmax = nums[n - 1]

for i in reversed(range(n - 1)):
    dif = prevmax - nums[i]

    if dif > maxnum:
        maxnum = dif

    prevmax = max(nums[i], prevmax)

print(maxnum)

Submission status: Accepted

Solution code:
n = int(input())
 
val_list = []
for _ in range(n):
    val_list.append(int(input()))
 
min_val = val_list[0]
 
diff_max = -200000000000000000
 
for i in range(1, len(val_list)):
    diff_max = max(diff_max, val_list[i]-min_val)
    min_val = min(min_val, val_list[i])
 
print(diff_max)

Submission status: Accepted

Solution code:
n = int(input())
R = []
for i in range(0,n):
    R.append(int(input()))
maxv = -1000000000
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)]

m = -10**9
minR = R[0]
for j in range(1, N):
    m = max(m, R[j] - minR)
    minR = min(minR, R[j])
print(m)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	n = int(input())
	R0 = int(input())
	R1 = int(input())
	difference = R1 - R0
	if R0 > R1:
		Rmin = R1
	else:
		Rmin = R0
	for i in range(n-2):
		Rn = int(input())
		if Rn - Rmin > difference:
			difference = Rn - Rmin
		if Rmin > Rn:
			Rmin = Rn
	print(difference)

Submission status: Accepted

Solution code:
# 第一行目の入力
n = int(input())
# 第２行目以降の入力
r_list = [int(input()) for i in range(n)]
score = -2000000000000000000000
minv = r_list[0]
"""
ループ内で
今まで出た最小値を持っておく
新しい値との差をとる
既存のスコアより大きければスコアを更新
"""
for r in r_list[1:]:
    score = max(score, r - minv) 
    minv = min(minv, r)        
print(score)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
  
maxv = -20**9
minv = A[0]
for i in range(1, N):
    maxv = max(maxv, A[i] - minv)
    minv = min(minv, A[i])
  
print(int(maxv))

Submission status: Accepted

Solution code:
num = int(input())
r0 = int(input())
min_r = r0
r1 = int(input())
if r1 < r0:
    min_r = r1
max_r = r1 - r0
for i in range(2,num):
    r = int(input())
    if (r - min_r) > max_r:
        max_r = (r - min_r)
    if r < min_r:
        min_r = r
print(max_r)

Submission status: Accepted

Solution code:
from sys import stdin

input = stdin.readline

n = int(input())

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

maxval = -2000000000
minval = A[0]
for i in range(1,len(A)):
	maxval = max(maxval,A[i]-minval)
	minval = min(minval,A[i])

print(maxval)

Submission status: Accepted

Solution code:
#ALDS1_1_D
n = int(input())
r_was = int(input())
lowest = r_was
r_is = int(input())
ans = r_is - lowest
r_was = r_is
for ni in range(n-2):
  r_is = int(input())
  if r_was < lowest:
    lowest = r_was
  if r_is - lowest > ans:
    ans = r_is - lowest
  r_was = r_is
print(ans)

Submission status: Accepted

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

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

print(maxv)