Problem:
 Score : 400 points    Problem Statement There are N towns located in a line, conveniently numbered 1 through N . Takahashi the merchant is going on a travel from town 1 to town N , buying and selling apples.  Takahashi will begin the travel at town 1 , with no apple in his possession. The actions that can be performed during the travel are as follows:   Move : When at town i ( i < N ), move to town i + 1 .  Merchandise : Buy or sell an arbitrary number of apples at the current town. Here, it is assumed that one apple can always be bought and sold for A_i yen (the currency of Japan) at town i ( 1 ≦ i ≦ N ), where A_i are distinct integers. Also, you can assume that he has an infinite supply of money.   For some reason, there is a constraint on merchandising apple during the travel: the sum of the number of apples bought and the number of apples sold during the whole travel, must be at most T . (Note that a single apple can be counted in both.)  During the travel, Takahashi will perform actions so that the profit of the travel is maximized. Here, the profit of the travel is the amount of money that is gained by selling apples, minus the amount of money that is spent on buying apples. Note that we are not interested in apples in his possession at the end of the travel.  Aoki, a business rival of Takahashi, wants to trouble Takahashi by manipulating the market price of apples. Prior to the beginning of Takahashi's travel, Aoki can change A_i into another arbitrary non-negative integer A_i' for any town i , any number of times. The cost of performing this operation is |A_i - A_i'| . After performing this operation, different towns may have equal values of A_i .  Aoki's objective is to decrease Takahashi's expected profit by at least 1 yen. Find the minimum total cost to achieve it. You may assume that Takahashi's expected profit is initially at least 1 yen.      Constraints  1 ≦ N ≦ 10^5  1 ≦ A_i ≦ 10^9 ( 1 ≦ i ≦ N )  A_i are distinct.  2 ≦ T ≦ 10^9  In the initial state, Takahashi's expected profit is at least 1 yen.         Input The input is given from Standard Input in the following format:  N  T  A_1  A_2  ...  A_N       Output Print the minimum total cost to decrease Takahashi's expected profit by at least 1 yen.        Sample Input 1 3 2
100 50 200      Sample Output 1 1  In the initial state, Takahashi can achieve the maximum profit of 150 yen as follows:   Move from town 1 to town 2 .  Buy one apple for 50 yen at town 2 .  Move from town 2 to town 3 .  Sell one apple for 200 yen at town 3 .   If, for example, Aoki changes the price of an apple at town 2 from 50 yen to 51 yen, Takahashi will not be able to achieve the profit of 150 yen. The cost of performing this operation is 1 , thus the answer is 1 .  There are other ways to decrease Takahashi's expected profit, such as changing the price of an apple at town 3 from 200 yen to 199 yen.       Sample Input 2 5 8
50 30 40 10 20      Sample Output 2 2       Sample Input 3 10 100
7 10 4 5 9 3 6 8 2 1      Sample Output 3 2

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N, T = map(int, readline().split())
    A = list(map(int, readline().split()))

    L = []
    Max = 0
    Min = A[0]

    for i in range(1, N):
        if A[i] < Min:
            Min = A[i]
        if A[i] - Min > Max:
            Max = A[i] - Min
        L.append(A[i] - Min)
    print(L.count(Max))

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N, T = map(int, input().split())
    A = list(map(int, input().split()))
    min_A = float('inf')
    cnt = 0
    profit = 0
    for i in range(N):
        min_A = min(min_A,A[i])
        diff_A = A[i] - min_A
        if profit == diff_A:
            cnt += 1
        elif profit < diff_A:
            cnt = 1
            profit = diff_A
    print(cnt)



if __name__ == "__main__":
    main()

Solution (Python):
n,t = map(int,input().split())
A = list(map(int,input().split()))
b = float('inf')
B = []
for a in A:
    b = min(b,a)
    B.append(a-b)
B.sort()
cnt = B.count(B[-1])
print(cnt)

Solution (Python):
n, t = map(int, input().split())
a = list(map(int, input().split()))
count = 0
l = []
max = 0
min = a[0]
for i in range(1,n):
    if a[i] < min:
        min = a[i]
    if a[i] - min > max:
        max = a[i] - min
    l.append(a[i] - min)
print(l.count(max))

Solution (Python):
n, t = map(int, input().split())
a = [int(x) for x in input().split()]
lst = [0]
mina = a[0]
for i in range(1, n):
    mina = min(mina, a[i-1])
    lst.append(max(0, a[i]-mina))

lst.sort(reverse=True)
cnt = 1
for i in range(1, n):
    if lst[0] == lst[i]: cnt += 1
    else: break

print(cnt)

Solution (Python):
n, t = map(int,input().split())
a = list(map(int,input().split()))
amax = [a[-1]] * n

for i in range(n-2, -1, -1):
    amax[i] = max(amax[i+1], a[i])

cnt = 0
v = -10**10
for i in range(n-1):
    b = amax[i+1] - a[i]
    if b > v:
        v = b
        cnt = 1
    elif b == v:
        cnt += 1
print(cnt)

Solution (Python):
class Combination:
    def __init__(self, n, mod):
        self.n = n
        self.mod = mod
        self.fac = [1 for i in range(self.n + 1)]
        self.finv = [1 for i in range(self.n + 1)]
        for i in range(2, self.n+1):
            self.fac[i] = (self.fac[i - 1] * i) % self.mod
            self.finv[i] = (self.finv[i - 1] * pow(i, -1, self.mod)) % self.mod

    def comb(self, n, m):
        return self.fac[n] * (self.finv[n - m] * self.finv[m] % self.mod) % self.mod
        
def iparse():
    return list(map(int, input().split()))

def RLE(s):
    res = []
    prev = ""
    cnt = 0
    for e in s:
        if prev == "":
            prev = e
            cnt += 1
        elif prev == e:
            cnt += 1
        else:
            res.append((cnt,prev))
            prev = e
            cnt = 1
    res.append((cnt,prev))
    return res


if __name__ == "__main__":
    n, t = iparse()
    a = iparse()
    a.reverse()
    s = [0 for i in range(n + 1)]
    for i in range(n):
        s[i + 1] = max(s[i], a[i])
    
    res = []
    a.reverse()
    for i in range(n):
        res.append(s[n - 1 - i] - a[i])
    res.sort()
    res.reverse()
    cnt = 1
    prev = res[0]
    for i in range(1,n):
        if prev == res[i]:
            cnt += 1
    print(cnt)

Solution (Python):
N, T = map(int, input().split())
A = list(map(int, input().split()))
dictA = {}
minA = 10**13
maxD = 0
for ai in A:
    minA = min(minA, ai)
    maxD = max(ai-minA, maxD)
    dictA[ai] = True
ans = 0
for ai in A:
    dictA[ai] = False
    if dictA.get(ai + maxD, False):
        ans += 1
print(ans)

Solution (Python):
N, T = map(int, input().split())
A = list(map(int, input().split()))

min_num = 10 ** 9 + 7
diff = 0
count = 0
for i in range(N):
    min_num = min(min_num, A[i])
    if diff == A[i] - min_num:
        count += 1
    elif A[i] - min_num > diff:
        count = 1
        diff = A[i] - min_num
    else:
        continue

print(count)

Solution (Python):
N, T = map(int, input().split())
A = list(map(int, input().split()))
Max = [0] * N
revenue = 0
for i in range(N)[::-1]:
    if i == N-1:
        Max[i] = (A[i], 1)
        continue
    ma, cnt = Max[i+1]
    if ma > A[i]:
        Max[i] = Max[i+1]
    elif ma == A[i]:
        Max[i] = (ma, cnt + 1)
    else:
        Max[i] = (A[i], 1)
    revenue = max(revenue, Max[i][0] - A[i])

D = dict()

for a, (m, c) in zip(A, Max):
    if m - a == revenue:
        if (m, a) in D:
            x, y = D[(m, a)]
            D[(m, a)] = (x + 1, max(y, c))
        else:
            D[(m, a)] = (1, c)

ans = 0
for key, value in D.items():
    x, y = value
    ans += min(x, y)
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return float(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




def main():
	n,t=MI()
	A=LI()

	B=[A[0]]
	for i in A[1:]:
		B.append(min(B[-1],i))
	

	C=[0]*n

	for i in range(n):
		C[i]=A[i]-B[i]

	c=max(C)
	print(C.count(c))

	
	



	
	
	









if __name__ == "__main__":
	main()

Solution (Python):

import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    N, T = map(int, input().split())
    A = list(map(int, input().split()))
    cost = [0]*N
    q = []
    heappush(q,-A[N-1])
    for i in range(N-2,-1,-1):
        v = heappop(q)
        cost[i] = -v-A[i]
        heappush(q,v)
        heappush(q,-A[i])
    
    print(cost.count(max(cost)))

if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import defaultdict
def input(): return sys.stdin.readline().strip()

def main():
    N, T = map(int, input().split())
    A = list(map(int, input().split()))
    M = [0] * (N - 1) + [A[-1]]
    for i in range(N - 2, -1, -1):
        M[i] = max(M[i + 1], A[i])
    cnt = 0
    maxA = 0
    for a, m in zip(A, M):
        if maxA < m - a:
            maxA = m - a
            cnt = 1
        elif maxA == m - a:
            cnt += 1
    print(cnt)






if __name__ == "__main__":
    main()

Solution (Python):
n, t = map(int, input().split())
A = tuple(map(int, input().split()))
count = 0
maxdiff = 0
mina = 10**9+1
for a in A:
    mina = min(mina, a)
    diff = a - mina
    if diff > maxdiff:
        count = 1
        maxdiff = diff
    elif diff == maxdiff:
        count += 1
print(count)

Solution (Python):
n, t = map(int, input().split())
A = tuple(map(int, input().split()))
AI = [(i, a) for i, a in enumerate(A)]
AI = sorted(AI, key=lambda x: x[1])


count = 0
maxdiff = 0
visited = [0] * n
for i, a in AI:
    if visited[i]:
        continue
    visited[i] = 1

    for j in range(i+1, n):
        if visited[j]:
            break
        visited[j] = 1
        diff = A[j] - a
        if diff > maxdiff:
            count = 1
            maxdiff = diff
        elif diff == maxdiff:
            count += 1
print(count)

Solution (Python):
n, t = map(int, input().split())
A = tuple(map(int, input().split()))
AI = [(i, a) for i, a in enumerate(A)]
AI = sorted(AI, key=lambda x: x[1])


count = 0
maxdiff = 0
visited = [0] * n
for i, a in AI:
    if visited[i]:
        continue
    visited[i] = 1

    for j in range(i+1, n):
        if visited[j]:
            break
        visited[j] = 1
        diff = A[j] - a
        if diff > maxdiff:
            count = 1
            maxdiff = diff
        elif diff == maxdiff:
            count += 1
print(count)

Solution (Python):
from collections import defaultdict
N,T = map(int,input().split())
a_ls = list(map(int,input().split()))

Min = float('inf')
max_profit = 0
for i in range(N):
    if a_ls[i] < Min:
        Min = a_ls[i]
    else:
        max_profit = max(max_profit,a_ls[i]-Min)



pair_dict = defaultdict(int)
Min = float('inf')
num_Min = 0
num_Max = 0
for i in range(N):
    
    
    
    if a_ls[i] < Min:
        if num_Max > 0:
            pair_dict[(Min,Min+max_profit)] = min(num_Max,num_Min)
        Min = a_ls[i]
        num_Min = 1
        num_Max = 0
    elif a_ls[i] == Min:
        num_Min += 1
    elif a_ls[i] - Min == max_profit:
        num_Max += 1

if num_Max > 0:
    pair_dict[(Min,Min+max_profit)] = min(num_Max,num_Min)

ans = 0

for v in pair_dict.values():
    ans += v
print(ans)

Solution (Python):
N, T = map(int, input().split())
A = [int(x) for x in input().split()]
cost, buy, mprofit = 1, 10e9, 0
for a in A:
    if a < buy:
        buy = a
    else:
        profit = a - buy
        if profit == mprofit:
            cost += 1
        if profit > mprofit:
            mprofit = profit
            cost = 1
print(cost)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



N, T = MAP()
A = LIST()

lis = []
tmp = A[0]
tmp_max = 0
for i in range(N):
	if A[i] < tmp:
		tmp = A[i]
		lis.append(tmp_max)
		tmp_max = 0
	else:
		tmp_max = max(tmp_max, A[i]-tmp)
lis.append(tmp_max)

print(lis.count(max(lis)))

Solution (Python):
N,T = map(int,input().split())
A_s = list(map(int,input().split()))
MIN = A_s[0]
DIVS = []
for A in A_s:
    if MIN > A:
        MIN = A
    DIVS.append(A-MIN)
MAX = max(DIVS)
answer = DIVS.count(MAX)
print(answer)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



N, T = MAP()
A = LIST()

min_ = INF
B = []
for i in A:
	B.append(i-min_)
	if i < min_:
		min_ = i

print(B.count(max(B)))

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


sys.setrecursionlimit(10**7)









INF = 1 << 50
EPS = 1e-8


def run():
    N, T , *A = map(int, read().split())
    max_gap = 0
    current_min = (INF, -1)
    pairs = set()
    if A[0] > A[1]:
        down = True
    else:
        down = False
        current_min = (A[0], 0)

    for i in range(N-1):
        if down:
            if A[i] < A[i+1]:
                if current_min[0] > A[i]:
                    current_min = (A[i], i)
                down = False
        else:
            if A[i] > A[i+1]:
                down = True
                gap = A[i] - current_min[0]
                if max_gap == gap:
                    pairs.add((current_min[1], i))
                elif max_gap < gap:
                    max_gap = gap
                    pairs = set()
                    pairs.add((current_min[1], i))
                else:
                    continue
    if not down:
        gap = A[N-1] - current_min[0]
        if max_gap == gap:
            pairs.add((current_min[1], N-1))
        elif max_gap < gap:
            max_gap = gap
            pairs = set()
            pairs.add((current_min[1], N-1))
    print(len(pairs))


if __name__ == "__main__":
    run()

Solution (Python):
n,t=map(int,input().split())
a=list(map(int,input().split()))
b=[0 for i in range(n)]
mi=a[0]
sa=0
for i in range(n):
    if a[i]<mi:
        mi=a[i]
    if a[i]-mi>=sa:
        sa=a[i]-mi
        b.append(sa)
ans=b.count(sa)
print(ans)

Solution (Python):
from collections import defaultdict


n, t = map(int, input().split())
a = list(map(int, input().split()))
mn = 1 << 32
i = 0
d = defaultdict(int)
while i < n - 1:
    if a[i] < mn:
        eqmn = 1
        mn = a[i]
        while i < n - 1 and a[i] == a[i + 1]:
            eqmn += 1
            i += 1

    elif a[i] < a[i + 1]:
        eqmx = 1
        while i < n - 1 and a[i] <= a[i + 1]:
            if a[i] == a[i + 1]:
                eqmx += 1
            else:
                eqmx = 1
            i += 1
        mx = a[i]
        d[mx - mn] += min(eqmx, eqmn)

    else:
        i += 1

print(d[max(d.keys())])

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,t = map(int,ipt().split())
    a = [int(i) for i in ipt().split()]
    bs = []
    mi = a[0]
    for i in a[1::]:
        if mi < i:
            bs.append(i-mi)
        else:
            mi = i
    ma = max(bs)
    cnt = 0
    for i in bs:
        if i == ma:
            cnt += 1

    print(cnt)
    return None

if __name__ == '__main__':
    main()

Solution (Python):
n, t = map(int, input().split())
a = list(map(int, input().split()))
lowest = a[0]
highest = a[0]
baibai = []
for i in range(n):
    if lowest > a[i]:
        lowest = a[i]
    
    baibai.append(a[i]-lowest)

max_baibai = max(baibai)
ans = baibai.count(max_baibai)

print(ans)

Solution (Python):
import numpy as np
N,T = map(int,input().split())
A = list(map(int,input().split()))
C = np.array(A)-np.minimum.accumulate(A)
print(sum(C==max(C)))

Solution (Python):
import sys
import math
import collections
import bisect
import itertools



sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))




def main():
    n, t = ns()
    a = na()

    res = dict()
    s_num = a[0]
    for i in range(1, n):
        tmp = a[i] - s_num
        if tmp > 0:
            if tmp in res.keys():
                res[tmp] += 1
            else:
                res[tmp] = 1

        s_num = min(s_num, a[i])

    print(res[max(res.keys())] if n != 1 else 0)


if __name__ == '__main__':
    main()

Solution (Python):

import sys 
from collections import deque
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, T = map(int, readline().split())
A = list(map(int,readline().split()))




N0 = 2**(N-1).bit_length()
data = [0]*(2*N0)


def init(A):
    for i in range(N):
        data[i+N0-1] = A[i]
    for i in range(N0-2,-1,-1):
        data[i] = max(data[2*i+1],data[2*i+2])


def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = max(data[2*k+1], data[2*k+2])


def query(l, r):
    L = l + N0; R = r + N0
    s = 0
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])

        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

init(A)
d = defaultdict(int)
for i in range(N-1):
    a = A[i]
    high = query(i+1,N+1)
    p = high - a
    d[p] += 1
d = sorted(d.items(),reverse=True)
print(d[0][1])

Solution (Python):
n, t = map(int, input().split())
A = list(map(int, input().split()))
from collections import defaultdict
prof = defaultdict(int)
low = 10**9
for a in A:
  if a > low: prof[a - low] += 1
  else: low = a
kmax = max(prof.keys())
ans = prof[kmax]
print(ans)

Solution (Python):
n, t = map(int, input().split())
A = list(map(int, input().split()))
B = [0]

m = A[0]
for a in A[1:]:
  B.append(a - m)
  m = min(m, a)
  
mx = max(B)

ans = B.count(mx)
print(ans)

Solution (Python):
from collections import defaultdict


def main():
    n, t = map(int, input().split())
    a = list(map(int, input().split()))
    profit = defaultdict(int)
    now_min = a[0]
    for i in range(1, n):
        profit[a[i] - now_min] += 1
        now_min = min(now_min, a[i])
    profit = list(profit.items())
    profit.sort(key=lambda x: x[0], reverse=True)
    print(profit[0][1])


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
n, t = [int(x) for x in input().split()]
a_list = [int(x) for x in input().split()]
d = defaultdict(int)
a_min = a_list[0]
p_max = 0
for i in range(1, n):
    if a_list[i] < a_min:
        a_min = a_list[i]
        continue
    p = a_list[i] - a_min
    if p >= p_max:
        d[p] += 1
        p_max = p
print(d[p_max])

Solution (Python):
N, T = map(int, input().split())
A = [int(x) for x in input().split()]
buy = A[0]
max_profit = 0
ans = 1
for i in range(1,N):
    if A[i] > buy:
        if A[i] - buy > max_profit:
            max_profit = A[i] - buy
            ans = 1
        elif A[i] - buy == max_profit:
            ans += 1
    else:
        buy = A[i]
print(ans)

Solution (Python):
def main():
    n, t = map(int, input().split())
    a = list(map(int, input().split()))

    
    min_l = [a[0]]
    for i in range(1, n):
        if min_l[-1] < a[i]:
            min_l.append(min_l[-1])
        elif min_l[-1] == a[i]:
            min_l.append(min_l[-1])
        else:
            min_l.append(a[i])

    
    max_l = [0] * n
    max_l[n-1] = a[n-1]
    for i in range(n-2, -1, -1):
        if max_l[i+1] < a[i]:
            max_l[i] = a[i]
        else:
            max_l[i] = max_l[i+1]

    profit = []
    for i in range(n-1):
        profit.append(max_l[i+1] - min_l[i])

    max_profit = profit[0]
    for i in range(1, n-1):
        if max_profit < profit[i]:
            max_profit = profit[i]
    
    ans = 0
    for i in range(n-1):
        if max_l[i+1] - a[i] == max_profit:
            ans += 1
    print(ans)

main()

Solution (Python):
import sys
from heapq import heappush, heappop
from collections import defaultdict


class HeapSet:
    def __init__(self):
        self.minQue = []
        self.maxQue = []
        self.counter = defaultdict(lambda: 0)

    def insert(self, x):
        heappush(self.minQue, x)
        heappush(self.maxQue, -x)
        self.counter[x] += 1

    def delete(self, x):
        self.counter[x] = max(0, self.counter[x] - 1)

    def get_max(self):
        while self.maxQue and self.counter[-self.maxQue[0]] == 0:
            heappop(self.maxQue)
        return -self.maxQue[0] if self.maxQue else None

    def get_min(self):
        while self.minQue and self.counter[self.minQue[0]] == 0:
            heappop(self.minQue)
        return self.minQue[0] if self.minQue else None


read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N, T = in_nn()
    A = in_nl()

    la = HeapSet()
    la.insert(A[0])

    min_sect = []
    ind_l = 0
    ind_r = 1
    pre_min = A[0]
    for i in range(N):
        min_a = la.get_min()
        if pre_min == min_a:
            ind_r = i + 1
        else:
            min_sect.append((pre_min, ind_l, ind_r))
            ind_l = i
            ind_r = i + 1

        pre_min = min_a
        if i + 1 < N:
            la.insert(A[i + 1])
    else:
        min_sect.append((min_a, ind_l, ind_r))

    max_benefit = -1
    ans = 0
    for min_a, l, r in min_sect:
        min_a = min(A[l:r])
        max_a = max(A[l:r])
        min_cnt = A[l:r].count(min_a)
        max_cnt = A[l:r].count(max_a)
        if max_a - min_a > max_benefit:
            max_benefit = max_a - min_a
            ans = min(max_cnt, min_cnt)
        elif max_a - min_a == max_benefit:
            ans += min(max_cnt, min_cnt)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n,t=map(int,input().split())
a=list(map(int,input().split()))
m=10**18
d={}
for i in a:
  if i<m:m=i
  if d.get(i-m):d[i-m]+=1
  else:d[i-m]=1
print(d[max(d)])

Solution (Python):
n, t = map(int, input().split())
aas = list(map(int, input().split()))
maxs = [0]*n
max_a = aas[-1]
for i in reversed(range(n)):
    maxs[i] = max_a
    if aas[i] > max_a:
        max_a = aas[i]
difs = [0]*n
for i in range(n):
    difs[i] = maxs[i] - aas[i]
print(difs.count(max(difs)))

Solution (Python):
N, T = map(int, input().split())
A = [int(i) for i in input().split()]

c = 0
MAX = 0
a1 = A[0]
for i in range(N-1):
    if A[i] <= a1:
        a1 = A[i]
    a2 = A[i+1]
    if MAX == a2 - a1:
        c += 1
    elif MAX < a2 - a1:
        MAX = a2 - a1
        c = 1
print(c)

Solution (Python):
N, T = map(int, input().split())
A = list(map(int, input().split()))

lo = 10**18
hi = 0
d = 0
res = 0


for i in range(N):
    if lo > A[i]:
        lo = A[i]
        hi = 0
    if hi < A[i]:
        hi = A[i]
        if d == hi - lo:
            res += 1
        elif d < hi - lo:
            d = hi - lo
            res = 1

print(res)

Solution (Python):
n,t=map(int,input().split())
a=list(map(int,input().split()))
min=[float("inf")]*n
min[0]=a[0]
m=0
for i in range(1,n):
    if min[i-1]>a[i]:
        min[i]=a[i]
    else:
        min[i]=min[i-1]
for i in range(n):
    r=a[i]-min[i]
    m=max(r,m)
ans=0
for i in range(n):
    r=a[i]-min[i]
    if r==m:
        ans+=1
print(ans)

Solution (Python):
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N, T = NMI()
    A = NLI()

    if N == 1:
        print(1)
        exit()

    A_min = []
    A_min.append(A[0])
    A_min.append(min(A[0], A[1]))
    for i, a in enumerate(A):
        if i < 2:
            continue
        A_min.append(min(A_min[-1], a))

    diff = [0] * N
    for i, am in enumerate(A_min):
        if i == 0:
            diff[0] = 0
            continue
        diff[i] = max(A[i] - A_min[i-1], 0)
    diff_max = max(diff)
    print(diff.count(diff_max))


if __name__ == "__main__":
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *A = map(int, read().split())

    B = [0] * N
    B[0] = INF
    for i in range(N - 1):
        if B[i] > A[i]:
            B[i + 1] = A[i]
        else:
            B[i + 1] = B[i]

    p = 0
    C = [0] * N
    for i in range(N):
        C[i] = A[i] - B[i]
        if p < C[i]:
            p = C[i]

    ans = C.count(p)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
N,T=map(int,input().split())
A=list(map(int,input().split()))

d = 0
cnt = 0
min_a = A[0]
for i in range(1, N):
  a = A[i]
  if min_a > a:
    min_a = a
    continue
 
  if d < a - min_a:
    d = a - min_a
    cnt = 1
  elif d == a - min_a:
    cnt += 1
print(cnt)

Solution (Python):
N,T=map(int,input().split())
A=list(map(int,input().split()))
ans=0
max_=0
B=[0]*N
for i in reversed(range(N)):
    B[i]=max(B[i],max_-A[i])
    max_=max(max_,A[i])
m=max(B)
print(B.count(m))

Solution (Python):
class SEGTree:
    def __init__(self,n):
        self.Unit=0
        i=1
        while(i<n):
            i*=2
        self.SEG=[self.Unit]*(2*i-1)
        self.d=i
    def update(self,i,x):
        i+=self.d-1
        self.SEG[i]=x
        while i>0:
            i=(i-1)//2
            self.SEG[i]=max(self.SEG[i*2+1],self.SEG[i*2+2])
    def find(self,a,b,k,l,r):
        if r<=a or b<=l:
            return self.Unit
        if a<=l and r<=b:
            return self.SEG[k]
        else:
            c1=self.find(a,b,2*k+1,l,(l+r)//2)
            c2=self.find(a,b,2*k+2,(l+r)//2,r)
            return max(c1,c2)
    def get(self,a,b):
        return self.find(a,b,0,0,self.d)

N,T=map(int,input().split())
A=list(map(int,input().split()))
SEG=SEGTree(N)
for i in range(N):
    SEG.update(i,A[i])
li=[0]*N
for i in range(N):
    li[i]=max(li[i],SEG.get(i+1,N)-A[i])
m=max(li)
ans=0
for i in range(N):
    if li[i]==m:
        ans+=1
print(ans)

Solution (Python):
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  

N,T = LI()
A = LI()

m = A[-1]
ans = []
for i in range(N-2,-1,-1):
    ans.append(m-A[i])
    m = max(m,A[i])

print(ans.count(max(ans)))

Solution (Python):
n,t=map(int,input().split())
a=list(map(int,input().split()))
accum=[0]*n
accum[n-1]=a[n-1]
for i in reversed(range(1,n)):
    accum[i-1]=max(accum[i],a[i-1])
maxi=0
for i in range(n-1):
    maxi=max(maxi,accum[i+1]-a[i])
ans=0
d={}
for i in range(n):
    if a[i]-maxi>0:
        if a[i]-maxi in d:
            if d[a[i]-maxi]>0:
                d[a[i]-maxi]-=1
                ans+=1
    if a[i] not in d:
        d[a[i]]=1
    else:
        d[a[i]]+=1
print(ans)

Solution (Python):
INF = 10**10
n,t = map(int,input().split())
A = list(map(int,input().split()))

ans = 0
cost = 0
l = 0
r = 1
while r<n:    
  dif = A[r] - A[l]
  if dif == cost:
    ans += 1
  elif dif > cost :
    cost = dif
    ans = 1 
  else:
    pass
  if dif <= 0:
    l = r
  r += 1
  
print(ans)

Solution (Python):


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
n,k=MI()
lis=LI()
mx=[0 for i in range(n)]
mx[-1]=lis[-1]
for i in range(n-1):
    j=n-i-1
    mx[j-1]=max(mx[j],lis[j-1])

difmx=0

for i in range(n):
    if mx[i]-lis[i]>difmx:
        difmx=mx[i]-lis[i]


new_mx=[[0,0] for i in range(n)]
new_mx[-1]=[lis[-1],1]
count=0
for i in range(n-1):
    j=n-i-1
    
    
    new_mx[j-1]=new_mx[j]
    if new_mx[j][0]-lis[j-1]==difmx:
        count+=new_mx[j][1]
    if new_mx[j][0]==lis[j-1]:
        new_mx[j][1]+=1
    if new_mx[j][0]<lis[j-1]:
        new_mx[j-1][0]=lis[j-1]
        new_mx[j-1][1]=1
print(count)

Solution (Python):
n , t = map(int,input().split())
a = list(map(int,input().split()))
k = [0 for i in range(n)]
nowmin = a[0]
for i in range(1,n):
    k[i] = a[i] - nowmin
    nowmin = min(nowmin,a[i])

maxri = max(k)
ans = 0
for i in range(n):
    if k[i] == maxri:
        ans += 1
print(ans)