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())
R = []
for i in range(n):
    r = int(input())
    R.append(r)

mins = [0] * n
mins[0] = float('inf')
for i in range(1, n):
    # the minimum value of R[:i]
    mins[i] = min(mins[i-1], R[i-1])

print(max(R[i] - mins[i] for i in range(n)))

Submission status: Accepted

Solution code:
n = int(input())
min = int(input())
current = int(input())
diff = current - min
for _ in range(n-2):
    if current < min:
        min = current
    current = int(input())
    if current - min > diff:
        diff = current - min
print(diff)

Submission status: Accepted

Solution code:
import math
 
n = int(input())
tmp = int(input())
tmpmin = tmp
t1 = int(input())
if t1 < tmp: tmpmin = t1
ans = t1 - tmp
 
 
for i in range(2,n):
    tmp = int(input())
  
    if tmp - tmpmin > ans: ans = tmp - tmpmin
    if tmp < tmpmin: tmpmin = tmp
     
print(int(ans))

Submission status: Accepted

Solution code:
maxv = -10000000000
minv = 10000000000

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

Submission status: Accepted

Solution code:
n=input()
l=[input() for _ in range(n)]
chk,ans=l[0],-1000000000
for i in range(1,n):
    chk=min(chk,l[i-1])
    if l[i]-chk>ans:
        ans=l[i]-chk
print ans

Submission status: Accepted

Solution code:
mini = pre = float("inf")
def f(n):
    global mini, pre
    mini = min(mini, pre)
    pre = n
    return n - mini
print max(f(input()) for _ in xrange(input()))

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())
co = 10**9
ma = -(10**9)
rr = int(input())
if rr < co:
	co = rr
for i in range(n-1):
	r = int(input())
	k = co
	if r < co:
		co = r
	if r - k > ma:
		ma = r - k
print(ma)

Submission status: Accepted

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

max_dif = -(10**9)
min_rate = rates[0]

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

print(max_dif)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    n = int(stdin.readline().rstrip())
    #a = [int(input()) for i in range(n)]

    ans = None
    tmp = None
    for a in range(n):
        i = int(input())
        # 最大値の計算
        if tmp is None:
            # tmp未定義の場合は定義するだけ
            tmp = i
        elif ans is None:
            ans = i - tmp
        elif i - tmp > ans:
            ans = i - tmp

        # 時系列で最も安い価格の保存
        if tmp == 0:
            tmp = i
        elif tmp > i:
            tmp = i
        else:
            pass

    return ans


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

Submission status: Accepted

Solution code:
n = int(input())

R = list()
mini = 0
for i in range(2):
    R.append(int(input()))
mini = min(R)

sub = R[1]-R[0]

for k in range(n-2):
    x = int(input())
    tmp = mini
    R.append(x)
    if (x < mini):
        mini = x

    if (x - tmp > sub):
        sub = x - tmp

print(sub)

Submission status: Accepted

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

minv = prices[0]
maxv = -10**9
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[0]
minv = int(input())
maxv = -10**12

#R[1..N-1]
for j 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())
l = []
for _ in range(n):
    x = int(input())
    l.append(x)
min_value = min(l[0],l[1])
max_difference = l[1]-l[0]
for i in range(2,n):
    max_difference = max(max_difference,l[i]-min_value)
    min_value = min(min_value,l[i])
print(max_difference)

Submission status: Accepted

Solution code:
N = int(input())
curmin = 10**10
ans = -10**10
for i in range(N):
	x = int(input())
	ans = max(ans, x - curmin)
	curmin = min(curmin, x)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
r = [0] * 200000
for i in range(n):
    r[i] = int(input())
#最大の利益はマイナスのこともあるため、初期値に十分マイナスの値を設定しておく
maxv = -2000000000
minv = r[0]

for j in range(1, n):
    #　現在のmaxvよりも大きな利益が出たら更新
    maxv = max(maxv, r[j] - minv)
    # 今よりも低い価格がでたらminvを更新する
    minv = min(minv, r[j])
print(maxv)

Submission status: Accepted

Solution code:
num = int(input())
maxv = -20000000000
minv = int(input())

for i in range(num - 1):
    a = int(input())
    b = a - minv
    maxv = max(maxv, b)
    minv = min(minv, a)
print(maxv)

Submission status: Accepted

Solution code:
'''
chapter2_3
ALDS1_1_D Maximum Profit
'''
N = int(input())
R = [int(input()) for i in range(N)]
minr = R[0]
maxr = -10**9
for i in range(1,N):
    maxr = max(maxr, R[i]-minr)
    minr = min(minr,R[i])
print(maxr)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
R = [0] * N
for i in range(N):
    R[i] = I()
Min = R[0]
Max = R[1] - R[0]

for i in range(1, N):
    Max = max(Max, R[i] - Min)
    Min = min(Min, R[i])

print(Max)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    result = -10**10

    r = int(input())
    for t in range(1, n):
        r_next = int(input())

        if (r_next - r) > result:
            result = r_next-r

        if r > r_next:
            r = r_next

    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
rmin = int(input())
d = -(10**9)
for i in range(1,n):
    r = int(input())
    if d < r-rmin:
        d = r-rmin
    if r < rmin:
        rmin = r
print(d)

Submission status: Accepted

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

def max_profit(lst):
    minv = lst[0]
    maxv = lst[1] - lst[0]
    for j in range(1,len(lst)):
        if maxv < lst[j] - minv:
            maxv = lst[j] - minv
        if minv > lst[j]:
            minv = lst[j]
    return maxv

print(max_profit(lst))

Submission status: Accepted

Solution code:
from sys import maxsize
n = int(input())
L = []
for _ in range(n):
    L.append(int(input()))
minv = L[0] 
maxv = -maxsize
for i in range(1, n):
    maxv = max(maxv, L[i]-minv)
    minv = min(minv, L[i])
print(maxv)

Submission status: Accepted

Solution code:
N = int(input())
Prices = [int(input()) for n in range(N)]

ans =  -float("inf")
minprice = Prices[0]

for i in range(1,N):
    p = Prices[i] - minprice
    ans = max(ans,p)
    minprice = min(minprice, Prices[i])

print(ans)

Submission status: Accepted

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

ans = R[1] - R[0]

M = R[-1]
for i in range(n-2,-1,-1):
    s = R[i]
    if M - s > ans:
        ans = M-s
    if M < s:
        M = s

print(ans)

Submission status: Accepted

Solution code:
R = [int(input()) for i in range(int(input()))]
dfmx = -10 ** 10
mn = 10 ** 10
for i in range(len(R)):
    dfmx = max(R[i] - mn, dfmx)
    mn = min(R[i], mn)
print(dfmx)

Submission status: Accepted

Solution code:
n = int(input())

fx = []

for _ in range(n):
    fx.append(int(input()))
    
maxv = -1000000000
minv = fx[0]
for j in range(1, n):
    maxv = max(maxv, fx[j] - minv)
    minv = min(minv, fx[j])
print(maxv)

Submission status: Accepted

Solution code:
def profit(R, n):
    max_profit = R[1] - R[0]
    min_n = R[0]
    for i in range(1, n-1):
        if min_n > R[i]:
            min_n = R[i]
        if max_profit < R[i+1] - min_n:
            max_profit = R[i+1] - min_n
    return max_profit


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

    print(profit(R, n))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
R_min, dR_max = float("inf"), -float("inf")
for _ in range(int(input())):
    R = int(input())
    dR_max = max(dR_max, R-R_min)
    R_min = min(R_min, R)
print(dR_max)

Submission status: Accepted

Solution code:
maxv = -float('inf')
N = int(input())
R_list = [int(input()) for _ in range(N)]
minv = R_list[0]
for j in range(1, N):
    if R_list[j] - minv > maxv:
        maxv = R_list[j] - minv
    if minv > R_list[j]:
        minv = R_list[j]
      
print(str(maxv))

Submission status: Accepted

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

def main():
  max_profit = -10 ** 9
  min_value = R[0]

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

  return(max_profit)

print(main())

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    numbers=[]
    number = int(input())
    numbers=[int(input()) for _ in range(number)]
    answers=[-10**9]*number
    buy_point=10**9

    if len(numbers)==2:
        print(numbers[1]-numbers[0])
        exit()

    for idx,value in enumerate(numbers):
        if buy_point==10**9:
            buy_point=value
            continue
        else:
            answers[idx]=value-buy_point
            if buy_point > value:
                buy_point=value
#            print("buy_point={},value={}".format(buy_point,value))
    print(max(answers))
            
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
ans = -10e+10
mn = 10e+10

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

max_n = -float('inf')
for i in range(n):
    num = int(input())
    if i == 0:
        min_n = num
    else:
        if num - min_n > max_n:
            max_n = num - min_n
        if num < min_n:
            min_n = num
print(max_n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N =int(input())
min = math.inf
max_diff = -math.inf
for i in range(N):
    num = int(input())
    if min == math.inf:
        min = num
        continue
    if num - min > max_diff:
        max_diff = num - min
    if min > num:
        min = num
print(max_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
Rt1 =  int(input())
Rt2 =  int(input())

if Rt1 < Rt2:
    min_Rt = Rt1
    
else:
    min_Rt = Rt2
    
max_prf = Rt2 - Rt1


for i in range(2,n):
    Rt = int(input())
    tmp_prf = Rt - min_Rt

    if tmp_prf > max_prf:
        max_prf = tmp_prf
    
    if Rt < min_Rt:
        min_Rt = Rt
    
print(str(max_prf))

Submission status: Accepted

Solution code:
n = int(input())
maxv = -2000000000
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())

lst=[int(input()) for _ in range(n)]
work_mx=10**10

mx=-10**16
for i in range(n-1):
    if lst[i]>=work_mx : continue
    work_mx=lst[i]
    sm=max(lst[i+1:])-lst[i]
    mx=max(sm,mx)

print(mx)

Submission status: Accepted

Solution code:
import sys
max_value = 1e10
min_value = -max_value
input()
for v in map(int, sys.stdin):
    if min_value < v - max_value:
        min_value = v - max_value
    if v < max_value:
        max_value = v
print(min_value)

Submission status: Accepted

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

a = R[0]
b = R[1] - R[0]

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

Submission status: Accepted

Solution code:
size = int(int(input()))
num_list = [int(input()) for _ in range(size)]
maxv = num_list[1] - num_list[0]
minv = num_list[0]
for i in range(1, size):
    if minv > num_list[i]:
        minv = num_list[i]
        continue
    elif num_list[i] - minv > maxv:
        maxv = num_list[i] - minv


print(maxv)

Submission status: Accepted

Solution code:
#演習1-13 Max Prifit
li=[int(input()) for i in range(int(input()))]

max_val=-10**15
min_val=li[0]
for i in range(1,len(li)):
    max_val=max(max_val,li[i]-min_val)
    min_val=min(min_val,li[i])
    
print(max_val)

Submission status: Accepted

Solution code:

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

#ここから
#minv = R[0]
#for j が 1 から n-1 まで
#maxv = (maxv と R[j]-minv のうち大きい方) 
#minv = (minv と R[j] のうち小さい方)

maxv=list1[1]-list1[0]
minv=list1[0]                                    
for j in range(1,n):
  if maxv>=list1[j]-minv:
    maxv=maxv
  else:
    maxv=list1[j]-minv
  if minv<=list1[j]:
    minv=minv
  else:
    minv=list1[j]
print(maxv)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def find_max_profit(prices:list) -> int:
    maxp = -10000000000
    minp = prices[0]
    for i in range(1,len(prices)):
        if prices[i] - minp > maxp:
            maxp = prices[i] - minp
        if prices[i] < minp:
            minp = prices[i]
    return maxp            
    
if __name__ == '__main__':
    n = int(input())
    prices = []
    for i in range(n):
        prices.append(int(input()))
    print(find_max_profit(prices))

Submission status: Accepted

Solution code:
n = int(input())

min_r = int(input())
score = -float('inf')
for i in range(n-1):
    r = int(input())
    score = max(score, r - min_r)
    min_r = min(r, min_r)

print(score)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

n = int(input().split()[0])
max_diff = -float("inf")
min_value = float("inf")

for i in range(n):
    r = int(input().split()[0])
    if i != 0:
        diff = r - min_value
        max_diff = max(max_diff, diff)
    min_value = min(min_value, r)

ans = max_diff
print(max_diff)

Submission status: Accepted

Solution code:
N = int(input())
num_list = [int(input()) for _ in range(N)]
# print(num_list)

# i < j
max_val = -(10**9 + 1)
min_num = num_list[0]
for j, num in enumerate(num_list[1:N]):
    # print(f"num: {num}")
    max_val = max(max_val, num - min_num) 
    min_num = min(min_num, num)

print(max_val)

Submission status: Accepted

Solution code:
n = int(input())
minimum = 10000000000
maxprofit = -1000000000
for _ in range(n):
    rt = int(input())
    profit = rt - minimum
    minimum = min(rt, minimum)
    maxprofit = max(profit, maxprofit)

print(maxprofit)

Submission status: Accepted

Solution code:

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

#初期値
max_v = -100000000000000 #負の値をとる場合があるので0ではNG
min_v = elements.pop(0)

'''
for j が 1 から n-1 まで
  maxv = (maxv と R[j]-minv のうち大きい方)
  minv = (minv と R[j] のうち小さい方)
'''
for element in elements :
    max_v = max(element-min_v,max_v)
    min_v = min(element,min_v)
print(max_v)

Submission status: Accepted

Solution code:
N = int(input())
r0 = int(input())
r1 = int(input())

res = r1 - r0
r_min = r0 if r0 < r1 else r1

for i in range(2, N):
    r = int(input())

    if res < r - r_min:
        res = r - r_min
    elif r < r_min:
        r_min = r

print(res)

Submission status: Accepted

Solution code:
min_r = 0
profit = 0

n = int(input())
for i in range(n):
    r = int(input())
    if i == 0:
        min_r = r
    elif i == 1:
        profit = r - min_r
    else:
        profit = max(profit, r - min_r)
    min_r = min(min_r, r)
print(profit)

Submission status: Accepted

Solution code:
n, *aa = map(int, open(0).read().split())

bb = []
mn = 1<<31

for a in aa:
    mn = min(mn, a)
    bb.append(mn)

print(max(aa[i] - bb[i-1] for i in range(1,n)))

Submission status: Accepted

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


N = int(input())

a = int(input())
b = int(input())
ans = b-a
_min = min(a, b)

for i in range(2, N):
    tmp = int(input())
    ans = max(ans, tmp - _min)
    _min = min(_min, tmp)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
input_list = [int(input()) for i in range(n)]
min_value = input_list[0]
profit = -1000000000
for i in input_list[1:]:
    if(profit < (i - min_value)):
        profit = i - min_value
    # print(min_value, i, profit)
    if (min_value > i):
        min_value = i
print(profit)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

mins = float("inf")
profit = float("-inf")
for index in range(n):
    a = int(input())
    profit = max(profit, a - mins)
    mins = min(mins, a)
print(profit)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for _ in range(n)]
maxv = 10**9 * (-1)
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 i in range(n)]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
cnt=int(input())
A=[int(input()) for i in range(cnt)]


mn=1e10
ans=A[1]-A[0]

for i in A:
    ans=max(ans,i-mn)
    mn=min(mn,i)
    
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 00:17:54 2020

@author: yukio
"""


n=int(input())
lst=[int(input())for _ in range(n)]
minv=lst[0]
maxv=-float("inf")
for i in range(1,n):
    maxv=max(maxv,lst[i]-minv)
    minv=min(minv,lst[i])

print(maxv)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import maxsize

n, b = [int(input()) for i in range(2)]
p = -maxsize

for i in range(n - 1):
    x = int(input())
    p = max(p, x - b)
    b = min(x, b)

print(p)

Submission status: Accepted

Solution code:
n = int(input())
a = []
mx = 0
mn = 0
for i in range(n):
    x = int(input())
    a.append(x)
    if i == 0:
        mn = a[i]
    elif i == 1:
        mx = a[i] - a[i-1]
        mn = a[i] if a[i] < a[i-1] else a[i-1]
    else:
        if mx < a[i] - mn:
            mx = a[i] - mn
        mn = a[i] if a[i] < mn else mn
print(str(mx))

Submission status: Accepted

Solution code:
N = int(input().rstrip())

r_list = []


min_r = 99999999999
max_profit = -999999999999

for n in range(N):
    temp_r = int(input().rstrip())
    if temp_r - min_r > max_profit:
        max_profit = temp_r - min_r
    if temp_r < min_r:
        min_r = temp_r

print(max_profit)

Submission status: Accepted

Solution code:
n = int(input())

R = [0]*n

ans = -float('inf')
#今見てるとこ以前の最小値
minimum = float('inf')
for i in range(n):
    R[i] = int(input())
    if i == 0:
        minimum = R[i]
        continue
    minimum = min(minimum, R[i-1])
    ans = max(ans, R[i] - minimum)
    
print(ans)

Submission status: Accepted

Solution code:
maxNum = -10 ** 10
minNum = 0
n = int(input())

line = []
for i in range(n):
    tmp = int(input())
    if i == 0:
        minNum = tmp
    line.append(tmp)
for i in range(1, len(line)):
    maxNum = max(maxNum, line[i] - minNum)
    minNum = min(minNum, line[i])

print(maxNum)

Submission status: Accepted

Solution code:
N = int(input())

maxv = -10**10
minv = 10**10

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

Submission status: Accepted

Solution code:
N = int(input())
r = [int(input()) for _ in range(N)]
min_r = r[0]
max_diff = -2e9

for i in range(1, N):
    max_diff = max([max_diff, r[i]-min_r])
    min_r = min([min_r, r[i]])
print(max_diff)

Submission status: Accepted

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

maxv = -1e100
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())
r = [int(input()) for _ in range(n)]

maxv = -10**10
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())

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    r = [int(input()) for _ in range(N)]

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

    for j in range(1,N):
        temp = r[j] - minv
        if maxv < temp:
            maxv = temp

        if minv > r[j]:
            minv = r[j]

    return maxv

print(main())

Submission status: Accepted

Solution code:
import sys
inputs = list(map(lambda x: int(x.rstrip()), sys.stdin.readlines()))

# n = inputs[0]
r_min = inputs[1]
p_max = -10**9
for r in inputs[2:]:
    p_max = max(p_max, r-r_min)
    r_min = min(r_min, r)

print(p_max)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    min_R_front = [0] * n
    min_R_front[0] = R[0]
    for i in range(1, n):
        if R[i] < min_R_front[i - 1]:
            min_R_front[i] = R[i]
        else:
            min_R_front[i] = min_R_front[i - 1]

    max_R_back = [0] * n
    max_R_back[-1] = R[-1]
    for i in reversed(range(n - 1)):
        if R[i] > max_R_back[i + 1]:
            max_R_back[i] = R[i]
        else:
            max_R_back[i] = max_R_back[i + 1]

    ans = -float("inf")
    for i in range(n - 1):
        if ans < max_R_back[i + 1] - min_R_front[i]:
            ans = max_R_back[i + 1] - min_R_front[i]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin

read = stdin.readline

N = int(read())
data = [int(read()) for _ in range(N)]
min = data[0]
max_profit = data[1] - data[0]

for index in range(1,N):
    if max_profit < data[index] - min:
        max_profit = data[index] - min
    if data[index] < min:
        min = data[index]

print(max_profit)

Submission status: Accepted

Solution code:
#ALDS1_1_D:   Maximum Profit

n = int(input())
R = []
for _ in range(n):
    R.append(int(input()))
    
res = -1*float('inf')
minv = R[0]
for i in range(1, n):
    res = max(res, R[i]-minv)
    minv = min(minv, R[i])

print(res)

Submission status: Accepted

Solution code:
n = int(input())
R = []
for i in range(n):
    R.append(int(input()))
buying = R[0]  # 初期買値
maximumProfit = -10 ** 18  # 初期利益
for i in range(1, n):
    maximumProfit = max(maximumProfit, R[i] - buying)
    buying = min(buying, R[i])
print(maximumProfit)

Submission status: Accepted

Solution code:
n=int(input())
R=[int(input()) for _ in range(n)]
minR=10**9
ans=-10**9
for i in range(n):
  if i==0: 
    minR=min(minR,R[i])
    continue
  ans=max(ans,R[i]-minR)
  #print(ans)
  minR=min(minR,R[i])
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
import math

n = int(stdin.readline().rstrip())
ans = -(math.pow(10, 9))
tmp_min_val = int(stdin.readline().rstrip())
for i in range(n - 1):
  val = int(stdin.readline().rstrip())
  diff = val - tmp_min_val
  if ans < diff:
    ans = diff
  if val < tmp_min_val:
    tmp_min_val = val
print(ans)

Submission status: Accepted

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

mx = -2222222222222222
mi = lis[0]

for x in lis[1:]:
  mx = max(mx, x-mi)
  mi = min(mi, x)
  
print(mx)

Submission status: Accepted

Solution code:
N = int(input())
maxr = -10**10
minr = 10**10

for i in range(N):
  num = int(input())
  maxr = max(maxr, num-minr)
  minr = min(minr, num)
  
print(maxr)

Submission status: Accepted

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

maxDiff = R[1] - R[0]
minRi = R[0]

for r in R[1:]:
    if (r - minRi) > maxDiff:
        maxDiff = r - minRi
    if r < minRi:
        minRi = r

print(maxDiff)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    min_A = A[0]
    diff = []
    for a in A[1:]:
        diff.append(a - min_A)
        min_A = min(a, min_A)

    print(max(diff))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

pf = R[1] - R[0] #利益の初期値代入
minv = min(R[0],R[1]) #最小値の初期値
maxv = R[1]

for i in range(2,N):
    newv = R[i]
    if (newv - minv) > pf:
        maxv = newv
        pf = newv - minv
    if newv < minv:
        minv = newv

print(pf)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    maxv = -(10 ** 9)
    minv = int(input())
    for _ in range(n-1):
        i = int(input())
        maxv = max(maxv, i - minv)
        minv = min(minv, i)
    print(maxv)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for _ in range(n)]
INF = 10**18
ans = -INF
minv = R[0]
for i in range(1, n):
    ans = max(ans, R[i]-minv)
    minv = min(minv, R[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

maximum = - 1e9
minimum = int(input())

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

print(maximum)

Submission status: Accepted

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


maximum_point=-10**10
minv=S[0]

for i in range(N-1):
    maximum_point=max(maximum_point,S[i+1]-minv)
    minv=min(minv,S[i+1])

print(maximum_point)

Submission status: Accepted

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


minv = R[0]

ans = R[1] - R[0]

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

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    R = [int(input()) for _ in range(N)]
    minR, maxR = R[0], -1 * (10**9)
    for i in range(1, N):
        maxR = max(maxR, R[i] - minR)
        minR = min(minR, R[i])
    print(maxR)


resolve()

Submission status: Accepted

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

minv = R[0]
maxv = -10**9
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 = []
for i in range(n):
    a = int(input())
    R.append(a)

m = R[0]
M = R[1] - R[0]

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

print(M)

Submission status: Accepted

Solution code:
from sys import stdin
input = lambda: stdin.readline().rstrip()

n = int(input())

maxv = 10**9 * -1
min_R = int(input())
for _ in range(n-1):
    r = int(input())
    maxv = max(maxv, r - min_R)
    min_R = min(min_R, r)

print(maxv)

Submission status: Accepted

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

val_min  = R[0]
diff_max = -1e+9
for i in range(1,n):
    diff_max = max(diff_max, R[i]-val_min)
    val_min  = min(val_min, R[i])
    
print(diff_max)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
import functools

n = int(input())
A = [int(input()) for i in range(n)]
maxv = -2*10**9
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())
ans = -10**10
mint = 10**10
for i in range(n):
    tmp = int(input())
    if i!=0:
        ans = max(ans, tmp - mint)
    mint = min(mint, tmp)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
minRate = int(input())
maxProf = -10 ** 9
for t in range(1, n):
    rt = int(input())
    if rt - minRate > maxProf:
        maxProf = rt - minRate
    if rt < minRate:
        minRate = rt
print(maxProf)

Submission status: Accepted

Solution code:
from typing import List


def get_max_v2(R: List[int]) -> int:
    max_diff: int = R[1] - R[0]
    min_r: int = min(R[0], R[1])

    for r in R[2:]:
        if r - min_r > max_diff:
            max_diff = r - min_r
        if r < min_r:
            min_r = r
    return max_diff


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

    result = get_max_v2(R)
    print(result)


if __name__ == "__main__":
    main()