Problem:
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) × 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

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

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

Solution (Python):


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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):




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

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

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

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

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

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

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

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

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

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

Solution (Python):


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

    maxp = max([R[i+1]-mini,maxp])
    mini = min([R[i+1],mini])
print(maxp)

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

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

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

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

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

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

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

Solution (Python):


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)

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

Solution (Python):
n = int(input())

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

for _ in range(1, n):

    R = int(input())

    maxv = max(maxv, R - minv)   
    minv = min(minv, R)          


print(maxv)

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

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

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

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

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

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

Solution (Python):

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)

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

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

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

Solution (Python):

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)

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

Solution (Python):
n = int(input())
s = []
while(n>0):
    s.append(int(input()))
    n -= 1

mi = 10e9
ma = -10e9

for i in range(len(s)-1):
    mi = min(mi,s[i])
    ma = max(ma,s[i+1] - mi)
    
print(ma)

Solution (Python):
N = int(input())

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

Solution (Python):
a = int(input())

N = []

for i in range(a):
    b = int(input())
    N.append(b)

max_n = -10000000000000000
min_n = N[0]

for i in range(1,a):
    max_n = max(max_n, N[i]-min_n)
    min_n = min(min_n, N[i])

print(max_n)

Solution (Python):
n = int(input())
ma, mi = -10**10, 10**10

for _ in range(n):
    x = int(input())
    ma, mi = max(ma, x - mi), min(mi, x)
print(ma)

Solution (Python):
n = int(input())
rList = [int(input()) for i in range(n)]
minR = rList[0]
maxDiff = -10**9
for i in range(1, n):
    maxDiff = max(maxDiff, rList[i] - minR)
    minR = min(minR, rList[i])
print(maxDiff)

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


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

Solution (Python):
n = int(input())

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

maxv = R[1]-R[0]
minv = 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)

Solution (Python):
n = int(input())
max_profit = -float('inf')
min_price = float('inf')
for _ in range(n):
    price = int(input())
    if max_profit < price - min_price:
        max_profit = price - min_price
    if min_price > price:
        min_price = price
print(max_profit)

Solution (Python):
num=[]
for i in range(int(input())):
	num.append(int(input()))
	if i==0:
		min=num[0]
	elif i==1:
		max=num[1]-num[0]
		if num[1]<min:
			min=num[1]
	else:
		if num[i]-min>max:
			max=num[i]-min
		elif num[i]<min:
			min=num[i]
print(max)

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

tmp_max = r[1] - r[0]
tmp_min = r[0]

for i in range(N-1):
  if(r[i+1] - tmp_min > tmp_max):
    tmp_max = r[i+1] - tmp_min
  if(r[i+1] < tmp_min):
    tmp_min = r[i+1]

print(tmp_max)

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

Solution (Python):
n = int(input())
Min = int(input())
Max = -10**9
for i in range(n):
    if i > 0:
        Num = int(input())
        if Max < Num - Min:
            Max = Num - Min
        
        if Min > Num:
            Min = Num
        
print(Max)

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

Solution (Python):
b = int(input())
n = []
ans = -2**100

for i in range(b):
    a = int(input())
    n.append(a)
minn = n[0]

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

Solution (Python):
n=int(input())
m=int(input())
l=int(input())
ma=l-m
mi=min(l,m)
for i in range(n-2):
    j=int(input())
    ma=max(ma,j-mi)
    mi=min(mi,j)
print(ma)

Solution (Python):


def main():
    n = int(input())
    N = [int(input()) for _ in range(n)]
    
    
    minv = N[0]
    maxv = -10000000000000000
    for i in range(1, len(N)):
        maxv = max(maxv, N[i] - minv)
        minv = min(minv, N[i])
    print(maxv)

main()

Solution (Python):
import sys






"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        
        
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    n = ip.I()
    ma = -1e11
    mi = 1e11
    for _ in r(n):
        r = ip.I()
        ma, mi = max(ma, r - mi), min(mi, r)
    p(ma)


main()

Solution (Python):
n = int(input())

num = []
max = -9999999999999
minv = 10000000000

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

    if i >= 1:
        if max <= num[i] - minv:
            max = num[i] - minv

    if num[i] <= minv:
         minv = num[i]

print(max)

Solution (Python):
n = int(input())
r = [int(input()) for i in range(n)]
minp = r[0]
pft = r[1] - r[0]
for i in range(1, n):
    pft = max(pft, r[i] - minp)
    minp = min(minp, r[i])

print(pft)

Solution (Python):
n = int(input())
high, low , max = 0, 1000000000, -1000000000
for i in range(n):
    a = int(input())
    if high < a and i > 0:
        high = a
    if max < high - low:
        max = high - low
    if low > a:
        low, high = a, 0
print(max)

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

min_R = R[0]
profit = -2000000000

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

print(profit)

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

maxv = -2 * 10**9
minv = r[0]

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

print(maxv)

Solution (Python):
n = int(input())
min_v=int(input())
max_v=int(input())
hold_R=max_v-min_v
min_v=min(min_v, max_v)
for i in range(n-2):
    r_t=int(input())
    hold_R = max(hold_R, r_t-min_v)
    min_v = min(min_v, r_t)
print(hold_R)

Solution (Python):
n = int(input())
r = int(input())
rr = int(input())

ans = rr- r
ma, mi = max(r, rr), min(r, rr)

for _ in range(n - 2):
    R = int(input())
    ans = max(R - mi, ans)
    
    ma = max(ma, R)
    mi = min(mi, R)
    
    
print(ans)

Solution (Python):
n = int(input())
ans = -float('INF')
num = [int(input()) for _ in range(n)]
mini = float('INF')
for i in range(1, n):
    mini = min(mini, num[i-1])
    tmp = num[i] - mini
    ans = max(ans, tmp)
print(ans)

Solution (Python):
n = int(input())
rmax = -2000000000
rmin = int(input())
for _ in range(2,n+1):
    r = int(input())
    rmax = max(r-rmin, rmax)
    rmin = min(rmin, r)
print (rmax)

Solution (Python):
n = int(input())
r = [int(input()) for i in range(n)]
maxv = -(10**9)
minv = 10**9
for i in range(1,n):
    if minv > r[i-1]:
        minv = r[i-1]
    if r[i] - minv > maxv:
        maxv = r[i] - minv
print(maxv)

Solution (Python):
time = int(input())
min = int(input())
diff = -999999999
for i in range(time-1):
    value = int(input())
    math = value - min
    if math > diff:
        diff = math

    if value < min:
        min = value
print(diff)

Solution (Python):
n = int(input())
r = [int(input()) for i in range(n)]
min_r = r[0]
max_d = r[1]-r[0]
for i in range(1,n):
    max_d = max(max_d, r[i] - min_r)
    min_r = min(min_r, r[i])
print(max_d)

Solution (Python):
n = int(input())

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

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

print(maxv)

Solution (Python):
n,*a=map(int,open(0).read().split())
ans=-99999999999999999999
mini=a[0]
for i in range(n-1):
    if a[i]<mini:
        mini=a[i]
    if a[i+1]-mini>ans:
        ans=a[i+1]-mini
print(ans)

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

m = r[n-1]
ans = -10**10
for i in range(n-2, -1, -1):
    t = m-r[i]
    if t > ans:
        ans = t
    if r[i] > m:
        m = r[i]
print(ans)

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

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

for time in range(1, n):
    profit = nums[time] - min_price
    if profit > max_profit:
        max_profit = profit
    if nums[time] < min_price:
        min_price = nums[time]

print(max_profit)

Solution (Python):


N = int(input())

first_plice = int(input())
second_plice = int(input())
min_plice = min(first_plice, second_plice)
max_profit = second_plice - first_plice

for i in range(N-2):
    next_plice = int(input())
    max_profit = max(max_profit, next_plice - min_plice)
    min_plice = min(min_plice, next_plice)    

print(max_profit)

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

Solution (Python):
n = int(input())
x=[]
for i in range(n):
   x.append(int(input()))
maxv = x[1] - x[0]
minv = x[0]
for i in range(1, n):
    if maxv <= x[i] - minv:
        maxv = x[i] - minv
    if minv >= x[i]:
        minv = x[i]
print(maxv)

Solution (Python):
n = int(input())
min_r = pow(10, 10)
max_r = -pow(10,10)
for i in range(n):
    r = int(input())
    max_r = max(max_r,r - min_r)
    min_r = min(min_r, r)
print(max_r)

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

print(maxv)

Solution (Python):
n = int(input())
R = [int(input()) for _ in range(n)]
ans = R[1] - R[0]
bottom = min(R[0], R[1])
for r in R[2:]:
    ans = max(ans, r - bottom)
    if r < bottom:
        bottom = r
print(ans)

Solution (Python):
t = int(input())
early_value = int(input())
for i in range(t-1):
	value = int(input())
	if not i:
		later_value = value
		profit = later_value - early_value
		
		
	if value < early_value:
		early_value = value
		
	
		
			
			
			
	elif profit < value - early_value:
		later_value = value
		profit = later_value - early_value
		
print(profit)

Solution (Python):
def main():
    max_p = -1_000_000_000
    n = int(input())
    min_v = int(input())

    for i in range(1, n):
        now_v = int(input())
        now_p = now_v - min_v
        if max_p < now_p:
            max_p = now_p
        if now_v < min_v:
            min_v = now_v

    print(max_p)


if __name__ == '__main__':
    main()

Solution (Python):
N = input()
R_min = input()
R_max = 0
diff_max = R_max - R_min

R = R_min
for i in range(0, N-1):
    R = input()
    a = R - R_min
    if diff_max < a:
        diff_max = a
    if R < R_min:
        R_min = R

print(diff_max)

Solution (Python):

import sys

n = int(input().rstrip())
ans = - sys.maxsize
minimum = sys.maxsize
for i in range(n):
    r = int(input().rstrip())
    if r - minimum > ans:
        ans = r - minimum
    if r < minimum:
        minimum = r

print(ans)

Solution (Python):
n = int(input())
minR = int(input())
maxVal = -10**9-1
for i in range(n-1):
    po = int(input())
    maxVal = max(maxVal, po - minR)
    minR = min(minR, po)
print(maxVal)

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

Solution (Python):
mn = 0
max_range = -1000000000
n = int(input())
mn = int(input())
for i in range(n-1):
    m = int(input())
    if m - mn > max_range:
      max_range = m - mn
    if m < mn:
      mn = m
print(max_range)

Solution (Python):


n = int(input())
maximum = -10e10
minimum = 10e10

maxmin = []
prev_num = 10e10
max_diff_between_two = -10e10
for i in range(n):
    in_num = int(input())
    if in_num - prev_num > max_diff_between_two and prev_num != 10e10:
        max_diff_between_two = in_num - prev_num
    if in_num < minimum:
        if maximum != -10e10 and minimum != 10e10:
            maxmin.append(maximum - minimum)
        minimum = in_num
        maximum = -10e10
    elif in_num > maximum:
        maximum = in_num
    prev_num = in_num
if maximum != -10e10 and minimum != 10e10:
    maxmin.append(maximum - minimum)

if len(maxmin) == 0:
    print(max_diff_between_two)
    quit()

max_diff = -10e10
for i in maxmin:
    if max_diff < i:
        max_diff = i

print(max_diff)

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

maxv = -20000000000000000
minv = list_R[0]

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

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

maxv = -1000000000
minv = R[0]

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

print(maxv)

Solution (Python):
n = int(input())
val = [0] * n
maxdiff = [0] * n
for i in range(n):
    val[i] = int(input())
maxdiff[0] = 0
maxdiff[1] = val[1] - val[0]
minval = min(val[0:2])
for i in range(2, n):
    maxdiff[i]= max(maxdiff[i - 1], val[i] - minval)
    minval = min(minval, val[i])
print(maxdiff[-1])

Solution (Python):
n = int(input())
R = [int(input()) for l in range(n)]

max_profit = -(10**10)
R_min = R[0]

for j in range(1, n):
    if R[j] - R_min > max_profit:
        max_profit = R[j] - R_min
    if R[j] < R_min:
        R_min = R[j]

print(max_profit)

Solution (Python):
num = int(input())
a = int(input())
b = int(input())
ans = b - a
temp = min(a, b)

for i in range(num - 2):
        num = int(input())
        if ans < num - temp:
                ans = num - temp
        if temp > num:
                temp = num

print(ans)

Solution (Python):
n = int(input())
rate = [int(input()) for _ in range(n)]
maxProfit = rate[1] - rate[0]
minRate = rate[0]  
maxRate = rate[0]  
for i in range(1, n):
    
    if rate[i] < minRate:
        minRate = rate[i]
        maxRate = rate[i]
    elif rate[i] >= maxRate and rate[i] - minRate > maxProfit:
        maxProfit = rate[i] - minRate
print(maxProfit)

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

max_res = -1e9
min_R = R[0]

for j in range(1, N):
    R_j = R[j]
    max_res = max(max_res, (R_j - min_R))
    min_R = min(min_R, R_j)

print(max_res)

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

Solution (Python):


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

d_MAX = -2*10**9
v_min = a[0]

for i in range(1, N):
    d_MAX = max(d_MAX, a[i]-v_min)
    v_min = min(v_min, a[i])

print(d_MAX)

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

min_R = R[0]
max_D = R[1] - R[0]
for i in range(1, n):
    max_D = max(max_D, R[i] - min_R)
    min_R = min(min_R, R[i])

print(max_D)

Solution (Python):
n=int(input())
mi=int(input())
ans=-float('inf')
for i in range(n-1):
    new=int(input())
    ans=max(ans,new-mi)
    mi=min(mi,new)
print(ans)

Solution (Python):
n=int(input())
r_min=int(input())
r_t=int(input())
g_max=r_t-r_min
r_min=min(r_t,r_min)

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

print(g_max)

Solution (Python):
import math

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

maxv = -math.inf
minv = R[0]
for i in range(1, n):
    diff = R[i] - minv
    maxv = diff if diff > maxv else maxv
    minv = R[i] if R[i] < minv else minv

print(maxv)

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
R = [int(stdin.readline().rstrip()) for i in range(n)]
minval = R[0]
diff = R[1]-minval
for i in range(1, n):
    diff = max(diff, R[i]-minval)
    minval = min(minval, R[i])
    
print(diff)