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:
#inputの取得
n = int(input())
R = [0 for i in range(n)]
for i in range(n):
    R[i] = int(input())

#配列を逆順にする
R.reverse()

#最大値のindexを基点として、右方向と左方向に対して確認を行う。
j = R.index(max(R[0:n-1]))
i = R.index(min(R[j+1:n]))
str_max = R[j] - R[i]

#最大値index +1 が最終index以上の場合は右方向の検索を実施しない。
if j+1 >= n-1:
    pass
else:
    #右方向確認
    while 1:
        #前回の最大値より後の最大値のindexを取得（右方向）
        j = R.index(max(R[j+1:n-1])) + j + 1
    
        #最大値index +1 が最終index以上になったらbreak
        if j+1 >= n-1:
            break
        
        #直前に取得した最大値indexより後の最小値を取得
        i = R.index(min(R[j+1:n]))
        
        #最大値 - 最小値を算出
        tmp = R[j] - R[i]
        if str_max < tmp:
            str_max = tmp

#左方向確認
j = R.index(max(R[0:n-1]))
while 1:
    if j <= 0:
        break
    #前回の最大値より前の最大値のindexを取得（左方向）
    j = R.index(max(R[0:j]))
    i = R.index(min(R[j+1:n]))
    
    #最大値 - 最小値を算出
    tmp = R[j] - R[i]
    if str_max < tmp:
        str_max = tmp

print(str_max)

Submission status: Accepted

Solution code:
import sys
times = int(input())
minv = int(input())
maxv = -sys.float_info.max
for i in range(1,times):
    r = int(input())
    maxv = max(maxv,r - minv)
    minv = min(minv,r)
print(maxv)

Submission status: Accepted

Solution code:
import sys


def main():
    array = []
    input_num = int(input())
    for v in range(input_num):
        array.append(int(input()))
    min_value = array[0]
    max_diff = array[1] - array[0]
    array = array[1:]
    for v in array:
        max_diff = max(max_diff, v - min_value)
        min_value = min(min_value, v)
    print(max_diff)


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

Submission status: Accepted

Solution code:
t = int(input())
minv = int(input())
maxv = -float('inf')

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

print(maxv)

Submission status: Accepted

Solution code:
t = int(input())
minv = int(input())
maxv = -float('inf')

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
L = [0] * N
L[0] = A[0]
for i in range(1, N):
    L[i] = min(L[i - 1], A[i])
R = [0] * N
R[-1] = A[-1]
for i in range(N - 2, -1, -1):
    R[i] = max(R[i + 1], A[i])
ans = R[-1] - L[0]
for i in range(N - 1):
    ans = max(ans, R[i + 1] - L[i])
print(ans)

Submission status: Accepted

Solution code:
def main(n,r):
    _dp = [-float('INF')]*n
    _dp[0]=r[1]-r[0]
    _min =r[0]
    for i in range(1,n):
        _min = min(_min, r[i-1])
        _dp[i] = max(_dp[i-1], r[i]-_min)
    print(_dp[-1])

n = int(input())
r = [int(input()) for _ in range(n)]
main(n,r)

Submission status: Accepted

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

_min = l[0]
_max = 0
ans = -float("inf")

#print(l)
dp=[-float("inf")]*(N)

for i in range(1,N):
    pass
    dp[i] = max(dp[i-1]+l[i]-l[i-1],l[i]-l[i-1])
print(max(dp))

Submission status: Accepted

Solution code:
N=int(input())

base=int(input())
ans=-1000000000
for i in range(1,N):
    now=int(input())
    ans=max(ans,now-base)
    if now<base:
        base=now
    
print(ans)

Submission status: Accepted

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

maxv = -float("inf")
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:
import sys
import math
input = sys.stdin.readline

def get_max(vec: list) -> int:
    reference = vec[0]
    diffs = -1 * 1e+9
    for x in vec[1:]:
        _diffs = reference - x
        if diffs < _diffs:
            diffs = _diffs
        if reference < x:
            reference = x
    return diffs

if __name__ == "__main__":
    n = int(input())
    res = 0
    vec = []
    for _ in range(n):
        vec.append(int(input()))
    print(get_max(list(reversed(vec))))

Submission status: Accepted

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

start = R[0]
max = R[1] - R[0]
for j in range(1, n):
    if (max < R[j] - start):
        max = R[j] - start
    if (R[j] < start):
        start = R[j]

print(max)

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

min_v = 10**10+1
max_p = -10**10+1
for i in range(n):
    val = int(input())
    p = val - min_v
    if p>max_p:
        max_p = p
    
    if val<min_v:
        min_v = val

print(max_p)

Submission status: Accepted

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

maxv = -(10**9)
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:
import math

def get_input():
    return int(input())

def max_profit(inputs):
  a = inputs[0]
  l = inputs[1:]
  r = 10 ** 10 * -1
  for i in l:
    p = i - a
    if p > r:
      r = p
    if i < a:
      a = i
  
  return r

n = get_input()
inputs = []
for _ in range(n):
  inputs.append(get_input())

# print(*inputs, sep=',')

print(max_profit(inputs))

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input())]
min = R[0]
x = -1000000000
for i in range(1,n):
    R.append(int(input()))
    if R[i] - min > x:
        x = R[i] - min
    if R[i] < min:
        min = R[i]
print(x)

Submission status: Accepted

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

temp_max_val = -1 * 10**9
temp_min = num_list[0]

for i in range(N-1):
    temp_max_val = max(temp_max_val, num_list[i+1] - temp_min)
    temp_min = min(temp_min, num_list[i+1])
    # print(temp_min)
    # print(temp_max_val)

print(temp_max_val)

Submission status: Accepted

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

def get_max_ret(n, r):
    max_ret = r[1] - r[0]
    min_val = r[0]
    for i in range(1, n):
        ret = r[i] - min_val
        max_ret = max(max_ret, ret)
        min_val = min(r[i], min_val)
    return max_ret
    
max_return = get_max_ret(n, r)
print(max_return)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    n = int(input().strip())
    a = []
    for i in range(n):
        a.append(int(input().strip()))
    m = INF
    ans = -INF
    for i in range(n):
        ans = max(ans, a[i] - m)
        if m > a[i]:
            m = a[i]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

minv = r[0]
result = -float("inf")
for  i in range(1, n, 1):
    result = max(result, r[i] - minv)
    minv = min(minv, r[i])
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, current_min, *R = map(int, open(0).read().split())

ret = -float("inf")
for r in R:
    ret = max(ret, r - current_min)
    current_min = min(current_min, r)
    
print(ret)

Submission status: Accepted

Solution code:
n = int(input())
m = 0
d = -1000000000
for i in range(n):
    v = int(input())
    if n == i + 1 and d == 0: d = v - m
    if m == 0:
        m = v
        continue
    d = d if d > v - m else v - m
    m = m if m < v else v
print(d)

Submission status: Accepted

Solution code:
import sys

N=int(input())

min=int(input())
max = None

for r in map(int, sys.stdin):
    if max is None or max < r - min:
        max = r-min
    if min > r:
        min = r

print(max)

Submission status: Accepted

Solution code:
n = int(input())
R = [int(input()) for i in range(n)]
res = -10e9
for i in range(n):
    if i == 0:
        minv = R[i]
    else:
        if res < R[i] - minv:
            res = R[i] - minv
        if R[i] < minv:
            minv = R[i]
print(res)

Submission status: Accepted

Solution code:
n = int(input())

t0 = int(input())
t1 = int(input())
minT = min(t0, t1)
answer = t1 - t0

for i in range(2, n):
    target = int(input())
    answer = max(answer, target - minT)
    minT = min(target, minT)

print(answer)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    l = list()

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

    minv = l[0]
    maxv = -1000000000

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

    print(maxv)

main()

Submission status: Accepted

Solution code:
import sys

def solution(numbers):
    profit = float("-inf")
    
    minv = numbers[1]
    
    for i in range(2, len(numbers)):
        n = numbers[i]
        profit = max(profit, n - minv)
        minv = min(minv, n)
        
    return profit

def main():
    first = True
    numbers = []
    for line in sys.stdin:
        if first:
            first = False
        numbers.append(int(line))

    print(solution(numbers))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

minv = R[0]
#print("最小値の初期値",minv)
maxv = R[1]-R[0] 
#print("最大値の初期値",maxv)
for j in range(len(R)-1):
    maxv = max(maxv,R[j+1]-minv)
    minv = min(minv,R[j+1])
    #print(maxv)
print(maxv)

Submission status: Accepted

Solution code:
# range(stop)は0<=i<stopの連番リストを返す．stopは含まれない．
# 0始まりで1ずつ増加する連番なので，負の値を指定すると空になる．
# range(start, stop)はstrat<=i<stopの連番リストを返す．stopは含まれないので注意．
# stepを負にすることで逆順の連番リストを作成できる．

def MaximumProfit(val_list, N):
   maxv = -10e10
   minv = val_list[0]
   for i in range(1, N):
      maxv = max(maxv, val_list[i]-minv)
      minv = min(minv, val_list[i])
   return maxv

N = int(input())
val_list = [int(input()) for _ in range(N)] # 1つの数字をN行入力
ans = MaximumProfit(val_list, N)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

r = int(input())
# ints = [int(input()) for _ in range(r)]

# maxv = None
# for i in range(len(ints)):
#   if i == len(ints)-1: break

#   if maxv is None:
#     maxv = max(ints[i+1:])-ints[i]
#   else:
#     maxv = max(maxv, max(ints[i+1:])-ints[i])
    
# print(maxv)

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

ans = R[1] - R[0]
prev = R[0]
for j in range(1, len(R)):
  cur = R[j]
  ans = max(ans, cur - prev)
  prev = min(cur, prev)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

min_r = int(input())
maxv = -float('inf')

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 _ 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:
import math
import string
import itertools
import fractions
import heapq
import collections
import re
import array
import bisect
import sys
import random
import time
inf = 10**9


def main():
    n = int(input())
    minv = int(input())
    maxv = -inf

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

    print(maxv)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#初期data
input_number = int(input())
input_list = list()

#アルゴリズム
for i in range(input_number):
    input_data = int(input())
    if i == 0:
        difference_mix = input_data
    elif i == 1:
        difference_max = input_data - difference_mix
        if difference_mix > input_data:
            difference_mix = input_data
    else:
        if difference_max < input_data - difference_mix:
            difference_max = input_data - difference_mix
        if difference_mix > input_data:
            difference_mix = input_data

#処理の実行
print(difference_max)

Submission status: Accepted

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

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

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

for i in range(1,n):
	if maxv < main[i] - minv:
		maxv = main[i] - minv
	if minv > main[i]:
		minv = main[i]

print(maxv)

Submission status: Accepted

Solution code:
# 入力
n = int(input().rstrip())

# 利益を算出
r = int(input().rstrip())
min = r

for i in range(n-1):
    r = int(input().rstrip())
    if i == 0:
        profit = r - min
    else:
        if r - min > profit:
            profit = r - min
    if min > r:
        min = r

print(profit)

Submission status: Accepted

Solution code:
data = []
for _ in range(int(input())) :
    data.append(int(input()))
    
max_diff = -1000000000
min_data = data[0]

for value in data[1:] :
    max_diff = max(max_diff, value - min_data)
    min_data = min(min_data, value)

print(max_diff)

Submission status: Accepted

Solution code:
import sys

n=int(sys.stdin.readline())

r=[int((sys.stdin.readline().rstrip())) for _ in range(n) ]


maxv=int(-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:
n = int(input())
for index, i in enumerate([int(input()) for _ in range(n)]):
    if index != 0:
        tmp = i - min_val
        if index == 1 or answer < tmp:
            answer = tmp
    if index == 0 or i < min_val:
        min_val = i
print(answer)

Submission status: Accepted

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

    for _ in range(N):
        R.append(int(input()))
    
    maxv = -10**10 
    minv = R[0]
    for i in range(1, N):
        maxv = max(maxv, R[i]-minv)
        minv = min(minv, R[i])

    print(maxv)

main2()

Submission status: Accepted

Solution code:

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

for i in range(n):
    num = int(input())
    numbers.append(num)
max_value = -10**9
min_value = numbers[0]
for i in range(1, n):
    max_value = max(max_value, numbers[i] - min_value)
    min_value = min(min_value, numbers[i])
print(max_value)

Submission status: Accepted

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

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

max_R, min_R = 10**9*(-1), R[0]
for j in range(1, n):
    max_R = max(max_R, R[j]-min_R)
    min_R = min(min_R, R[j])

print(max_R)

Submission status: Accepted

Solution code:
#Maximum Profit

n = int(input())

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

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


print(maxv)

Submission status: Accepted

Solution code:
data = list()
for i in range(int(input())):
    data.append(int(input()))

max_diff = -1000000000
min_data = data[0]
for value in data[1:] :
    max_diff = max(max_diff, value - min_data)
    min_data = min(min_data, value)

print(max_diff)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for n in range(N)]
B, C = [], []
m = 10 ** 9
M = 0
for n in range(N):
    if A[n] < m:
        m = A[n]
    if A[N - n - 1] > M:
        M = A[N - n - 1]
    B.append(m)
    C.append(M)
C.reverse()
D = [C[n+1] - B[n] for n in range(N-1)]
print(max(D))

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    A = []
    for _ in range(n):
        A.append(int(input()))
    min_price = A[0]
    max_profit = A[1] - A[0]
    for i in range(1, n):
        x = A[i]
        max_profit = max(max_profit, x - min_price)
        min_price = min(min_price, x)
        # print('x {}, max {}, min {}'.format(x, max_profit, min_price))

    print(max_profit)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
maxv = -2000000000
minv = int(input())

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

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

minv = int(input())
maxv = - 10 ** 9
for i in range(n-1):
    r_j = int(input())
    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)]

max_profit = float('inf') * -1
min_v = R[0]
for i in range(1, len(R)):
    max_profit = max(max_profit, R[i]-min_v)
    min_v = min(min_v, R[i])

print(max_profit)

Submission status: Accepted

Solution code:
N = int(input())
R = []
for i in range(N):
    R.append(int(input()))
ans = -1 * (10 ** 9 + 7)
minv = R[0]
for i in range(1, N):
    ans = max(R[i] - minv, ans)
    minv = min(minv, R[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
max = -999999999
min = 0

for i in range(n):
    r = int(input())
    if i == 0:
        min = r
    else: 
        if max < r-min:
            max = r-min
        if min > r:
            min = r

print(max)

Submission status: Accepted

Solution code:
N = int(input())

for i in range(N):
    if i == 0:
        minimum = int(input())
    else:
        a = int(input())
        if i == 1 or maximum < a - minimum:
            maximum = a - minimum
        if a < minimum:
            minimum = a

print(maximum)

Submission status: Accepted

Solution code:
n = int(input())

maxp = 10**19 * -1
R = [int(input())]
min_R = R[0]

for i in range(1, n):
    R.append(int(input()))
    maxp = max(maxp, R[i]-min_R)
    min_R = min(min_R, R[i])

print(maxp)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    max_value = 10 ** 9 * -1
    min_value = int(input())
    for n in range(N - 1):
        now_value = int(input())
        if max_value < now_value - min_value:
            max_value = now_value - min_value
        if min_value > now_value:
            min_value = now_value
    print(max_value)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

min_v = a[0]
max_p = a[1] - a[0]

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

print(max_p)

Submission status: Accepted

Solution code:
n = int(input())
a = []
minArray = []

m = int(1e10)
for _ in range(n):
	x = int(input())
	minArray.append(m)
	m = min([m,x])
	a.append(x)
	
t = 0
ans = -int(1e10)
for i in reversed(range(1,n)):
	t = max([t,a[i]])
	ans = max(ans, t-minArray[i])

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
R = [int(input()) for i in range(n)]

ans = -sys.maxsize
tmp = R[0]
for r in R[1:]:
    ans = max(r-tmp, ans)
    tmp = min(tmp, r)
print(ans)

Submission status: Accepted

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

rmin = r[0]
rmax = r[1]
delr = rmax - rmin

for i in range(n-1):
    delr = max(delr, r[i+1]-rmin)
    rmin = min(rmin, r[i+1])
print(delr)

Submission status: Accepted

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

mi = lst[0]
ma = -float('inf')
for i in range(1,N):
    ma = max(ma, lst[i] - mi)
    mi = min(mi, lst[i])
print(ma)

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):
    maxv = max(maxv, R[j]-minv)
    minv = min(minv, R[j])

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())
minv = int(input())
maxv = -10000000000

for _ in range(1, n):
    r = int(input())
    if (maxv < r - minv):
        maxv = r - minv
    if (minv > r):
        minv = r

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(A[j] < minv):
		minv = A[j]
print(maxv)

Submission status: Accepted

Solution code:
MAX = 200000


def main():
  R = []

  n  = input()

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

  maxv = -20000000000;
  minv = R[0]

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

  print(maxv)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
dollar = [int(input()) for i in range(n)]
m = dollar[0]
max = dollar[1] - m

for i in range(1, n):
    if dollar[i] - m > max:
        max = dollar[i] - m
    if dollar[i] < m:
        m = dollar[i]
print(max)

Submission status: Accepted

Solution code:
N = int(input())
a= int(input())
min =a
max =a
ans =-999999999
for i in range(N-1):
    a= int(input())
    if ans < a - min :
        ans =a -min
    if min > a:
        min =a
    
print (ans)

Submission status: Accepted

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

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

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

print(maxv)
"""

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

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

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

print(maxv)

Submission status: Accepted

Solution code:
BIG_NUM = 10**10

N = int(input())

min_value = BIG_NUM
max_diff = -BIG_NUM
for i in range(N):
    num = int(input())
    
    diff = num - min_value
    if diff > max_diff:
        max_diff = diff
        
    if num < min_value:
        min_value = num

print(max_diff)

Submission status: Accepted

Solution code:
max_profit = -float('inf')
min_price = float('inf')

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

for t, price in enumerate(prices):
    if t > 0:
        max_profit = max(max_profit, price - min_price)
    min_price = min(min_price, price)

print(max_profit)

Submission status: Accepted

Solution code:
import math
n = int(input())

maxv = -10**12
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())
arr = []
for i in range(n):
  arr.append(int(input()))

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

print (maxv)

Submission status: Accepted

Solution code:
n=int(input())
ans=-9999999999999
buy=int(input())
sell=buy
for i in range(1,n):
	buy=min(sell,buy)
	sell=int(input())
	if(ans < sell-buy):
		ans=sell-buy
print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(input())
mi = int(input())
mv = -10**9
for N in map(int, sys.stdin):
    # N = int(input())
    if N - mi > mv:
        mv = N - mi
    if N < mi:
        mi = N
print(mv)

Submission status: Accepted

Solution code:
n = int(input())
r_min = int(input())
Rt = int(input())
g_max = Rt - r_min
r_min = min(Rt,r_min)

for _ in range(n - 2):
    Rt = int(input())
    g_max = max(g_max, Rt - r_min)
    r_min = min(Rt,r_min)
print(g_max)

Submission status: Accepted

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

rmin = r[0]
rmax = r[1] - r[0]
for i in range(1,len(r)):
    if rmin > r[i]:
        rmin = r[i]
    else:
        rmax = max(rmax,r[i] - rmin)

print(rmax)

Submission status: Accepted

Solution code:
n = int(input())
r = []
for i in range(0, n):
    r.append(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:
n = int(input())
r = [0] * (n)
for i in range(n):
    r[i] = int(input())
    
num_max = r[1] - r[0]
num_min = r[0]
min_idx = 0
for i in range(1, n):
    if num_max < r[i] - num_min:
        num_max = r[i] - num_min
    if r[i] < num_min:
        num_min = r[i]
print(str(num_max))

Submission status: Accepted

Solution code:
n = int(input())
r1 = int(input())
r2 = int(input())
m = r1 if r1 < r2 else r2
d = r2 - r1
for i in range(n-2):
    r = int(input())
    if r - m > d:
        d = r - m
    if r < m:
        m = r
print(d)

Submission status: Accepted

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

minv = r[0]
maxv = -2000000000

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

print(maxv)


#ans = r[1] - r[0]
#
#for i in range(n):
#    ri =r[i]
#    for j in range(i+1, n):
#        rj = r[j]
#        ans = max(ans, rj - ri)
#
#print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

r = [ int(input()) for _ in range(n) ]
# print(r)

v_max = r[1]
v_min = r[0]
profit_best = v_max - v_min

for j in range(2,n):
    v_max = r[j]
    v_min = min(v_min,r[j-1])
    profit_tmp = v_max - v_min
    profit_best = max(profit_best,profit_tmp)

print(profit_best)

Submission status: Accepted

Solution code:
first = int(input())
n = [int(input()) for i in range(first)]  #n<200000
maxv = -200000000000
minv = n[0]
for i in range(1, len(n)):
    maxv = max(maxv, n[i] - minv)
    minv = min(minv, n[i])
print(maxv)

Submission status: Accepted

Solution code:
n = int(input())

min_r = pre_r = int(input())
max_diff = None

for _ in range(n-1):
    cur_r = int(input())
    min_r = pre_r if pre_r < min_r else min_r
    pre_r = cur_r

    diff = cur_r - min_r
    if max_diff is None or diff > max_diff:
        max_diff = diff

print(max_diff)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
maxi=a[1]-a[0]
mini=a[0]
for j in range(1,n):
    if a[j]-mini>maxi:
        maxi=a[j]-mini
    if a[j]<mini:
        mini=a[j]
print(maxi)

Submission status: Accepted

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

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

print(maxv)

Submission status: Accepted

Solution code:
times = int(input())

r_list = []
for i in range(times):
    r_list.append(int(input()))

"""
発想：今までで一番大きい差と、新しく追加される値を用いた一番大きい値の大きい方を取れば良い
一番大きい差を得るためには、そこまでの数列の一番小さい値を保持しておく必要があるので、それも保持しておく
"""
r_min = r_list[0]
r_max = r_list[1] - r_list[0]
for r in range(1, len(r_list)):
    r_max = max(r_max, r_list[r]-r_min)
    r_min = min(r_min, r_list[r])

print(r_max)

Submission status: Accepted

Solution code:
n = int(input())
a0 = int(input())
a1 = int(input())
max_profit = a1 - a0
min_i = min(a1, a0)
for i in range(n-2):
    c=int(input())
    if c - min_i > max_profit:
        max_profit = c - min_i
    min_i = min(min_i, c)
print(max_profit)

Submission status: Accepted

Solution code:
n = int(input())
rt = [int(input()) for v in range(n)]
max_diff = (10 ** 9) * -1
min_val = 10 ** 9
for j in range(1, n):
    min_val = min(rt[j-1], min_val)
    diff = rt[j] - min_val
    if max_diff < diff:
        max_diff = diff
print(max_diff)

Submission status: Accepted

Solution code:
n=int(input())

#初期値
ma=int(input())
result=-9999999999999999999999999

for i in range(n-1):
    value=int(input())
    if result<value-ma:
        result=value-ma

    # 最小値の更新
    if value < ma:
        ma=value

print(result)

Submission status: Accepted

Solution code:
n = int(input())
m = float('inf')
d = -float('inf')
for _ in range(n):
    r = int(input())
    d = max(d,r-m)
    m = min(m,r)
print(d)

Submission status: Accepted

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

s=[]
for i in range(n):
  l=int(input())
  s.append(l)
mi=s[0]
for i in range(1,n):
  ma=max(ma,s[i]-mi)
  mi=min(mi,s[i])
print(ma)

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:
#! /usr/bin/env python

def proc_inputs ():
    num_inputs = int(input())
    inputs = [int(input()) for i in range(num_inputs)]
    return inputs

def main ():
    inputs = proc_inputs()
    print(calc_prof(inputs))
    exit(0)

def calc_prof (inputs):
    maxnum = inputs[-1]
    # minnum = min(inputs[:-1])
    inputs.reverse()
    profs = []

    for i,n in enumerate(inputs[1:]):
        profs.append(maxnum - n)

        if n > maxnum and i != len(inputs) - 1:
            maxnum = n
            # print(maxnum)

        # if n >= maxnum and i != len(inputs) - 2:
        #     minnum = n
            # print("min changed at {} to {}".format(i, n))
            # maxnum = n
            # print(minnum)

    return max(profs)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

ans = -(10 ** 9)

N = int(input())
R_MIN = int(input())
for _ in range(N - 1):
    R = int(input())

    tmp = R - R_MIN
    if tmp > ans:
        ans = tmp
    if R_MIN > R:
        R_MIN = R
        continue

print(ans)