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



Sample input:
3 2
100 50 200


Sample output:
1

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

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 = [INF] + list(map(int, readline().split())) + [-INF]

    L = []
    for i in range(1,N+1):
        if A[i-1]>A[i] and A[i]<A[i+1]:
            L.append(A[i])
        elif A[i-1]<A[i] and A[i]>A[i+1]:
            L.append(A[i])

    diff = defaultdict(int)
    if L[1]>L[0]:
        for i in range(1,len(L),2):
            diff[L[i]-L[i-1]] += 1
    else:
        for i in range(2,len(L),2):
            diff[L[i] - L[i-1]] += 1

    print(max(diff.values()))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

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 = [INF] + list(map(int, readline().split())) + [-INF]

    L = []
    for i in range(1,N+1):
        if A[i-1]>A[i] and A[i]<A[i+1]:
            L.append(A[i])
        if A[i-1]<A[i] and A[i]>A[i+1]:
            L.append(A[i])

    diff = defaultdict(int)
    for i in range(0,len(L),2):
        diff[L[i+1]-L[i]] += 1

    print(max(diff.values()))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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)
print(l.count(max))

Submission status: Wrong Answer

Solution code:
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)
answer = min(T//2,answer)
print(answer)

Submission status: Wrong Answer

Solution code:
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)
answer = min(T,answer)
print(answer)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

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:
            current_min = (A[i], i)
            if A[i] < A[i+1]:
                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()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

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]:
                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[-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()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
# d = defaultdict(int)で0で初期化
# d = defaultdict(lambda: 100)で100で初期化
n, t = map(int, input().split())
a = list(map(int, input().split()))
eqmx = 1
eqmn = 1
i = 0
d = defaultdict(int)
while i < n - 1:
    if a[i + 1] == a[i]:
        while i < n - 1 and a[i] == a[i + 1]:
            eqmn += 1
            i += 1

    elif a[i] < a[i + 1]:
        mn = a[i]
        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())])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
# d = defaultdict(int)で0で初期化
# d = defaultdict(lambda: 100)で100で初期化
n, t = map(int, input().split())
a = list(map(int, input().split()))
eqmx = 0
eqmn = 1
i = 0
d = defaultdict(int)
while i < n - 1:
    if a[i + 1] == a[i]:
        while i < n - 1 and a[i] == a[i + 1]:
            eqmn += 1
            i += 1

    if a[i] < a[i + 1]:
        mn = a[i]
        while i < n - 1 and a[i] <= a[i + 1]:
            if a[i] == a[i + 1]:
                eqmx += 1
            else:
                eqmx = 0
            i += 1
        mx = a[i]
        d[mx - mn] += eqmx + eqmn

    else:
        i += 1

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

Submission status: Wrong Answer

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 #998244353
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()]
    d = []
    u = []
    if a[0] < a[1]:
        d.append(a[0])
    for i in range(1,n-1):
        if a[i-1] > a[i] < a[i+1]:
            d.append(a[i])
        elif a[i-1] < a[i] > a[i+1]:
            u.append(a[i])

    if a[-2] < a[-1]:
        u.append(a[-1])

    ds = []
    nu = len(u)
    for i in range(nu):
        mau = 0
        if mau < u[nu-1-i]:
            mau = u[nu-1-i]
        ds.append(mau-d[nu-1-i])
    dma = max(ds)
    cnt = 0
    for i in ds:
        if dma == i:
            cnt += 1
    print(cnt)
    return None

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 #998244353
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()]
    d = []
    u = []
    pv = 10000000000
    for i in range(n-1):
        if pv > a[i] < a[i+1]:
            d.append(a[i])
        elif pv < a[i] > a[i+1]:
            u.append(a[i])
        if a[i] != a[i+1]:
            pv = a[i]

    if pv < a[-1]:
        u.append(a[-1])

    ds = []
    if len(u) != len(d):
        print(asfasfa)
    for ui,di in zip(u,d):
        ds.append(ui-di)
    dma = max(ds)
    cnt = 0
    for i in ds:
        if dma == i:
            cnt += 1
    print(cnt)
    return None

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 #998244353
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()]
    d = []
    u = []
    pv = 10000000000
    for i in range(n-1):
        if pv > a[i] < a[i+1]:
            d.append(a[i])
        elif pv < a[i] > a[i+1]:
            u.append(a[i])
        if a[i] != a[i+1]:
            pv = a[i]

    if pv < a[-1]:
        u.append(a[-1])

    ds = []
    for ui,di in zip(u,d):
        ds.append(ui-di)
    dma = max(ds)
    cnt = 0
    for i in ds:
        if dma == i:
            cnt += 1
    print(cnt)
    return None

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 #998244353
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()]
    d = []
    u = []
    pv = 1000000
    for i in range(n-1):
        if pv > a[i] < a[i+1]:
            d.append(a[i])
        elif pv < a[i] > a[i+1]:
            u.append(a[i])
        if a[i] != a[i+1]:
            pv = a[i]

    if pv < a[-1]:
        u.append(a[-1])

    ds = []
    for ui,di in zip(u,d):
        ds.append(ui-di)
    dma = max(ds)
    cnt = 0
    for i in ds:
        if dma == i:
            cnt += 1
    print(cnt)
    return None

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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 highest <= a[i]:
        highest = a[i]
    if lowest >= a[i]:
        lowest = a[i]
        highest = a[i]
    #print(highest, lowest)
    baibai.append(highest-lowest)

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

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
A = list(map(int,input().split()))
ans = 0

for a,b,c in zip(A[0:],A[1:],A[2:]):
  if b<a and b<c:
    ans+=1

print(min(T,ans))

Submission status: Wrong Answer

Solution code:
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 highest < a[i]:
        highest = a[i]
    if lowest >= a[i]:
        lowest = a[i]
        highest = a[i]
    #print(highest, lowest)
    baibai.append(highest-lowest)

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

Submission status: Wrong Answer

Solution code:
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 highest < a[i]:
        highest = a[i]
    if lowest > a[i]:
        lowest = a[i]
        highest = a[i]
    #print(highest, lowest)
    baibai.append(highest-lowest)

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

Submission status: Wrong Answer

Solution code:
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 highest < a[i]:
        highest = a[i]
    if lowest > a[i]:
        lowest = a[i]
        highest = a[i]
    #print(highest, lowest)
    baibai.append(highest-lowest)

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

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

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


# ===CODE===

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

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

    ans = 0
    if len(res.keys()):
        ans = res[max(res.keys())]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

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

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


# ===CODE===

def main():
    n, t = ns()
    a = na()
    tmp = []
    prev = -1
    for ai in a:
        if prev != ai:
            tmp.append(ai)
        prev = ai

    tmp2 = [tmp[0]]
    prev = 0
    for i in range(1, len(tmp)):
        if prev * (tmp[i] - tmp[i - 1]) < 0:
            tmp2.append(tmp[i - 1])
        prev = tmp[i] - tmp[i - 1]
        if i == len(tmp) - 1:
            tmp2.append(tmp[i])

    ans = dict()
    for i in range(1, len(tmp2)):
        tmp = tmp2[i] - tmp2[i - 1]
        if tmp > 0:
            if tmp in ans.keys():
                ans[tmp] += 1
            else:
                ans[tmp] = 1
                
    mk = max(ans.keys())
    print(ans[mk] if n!=1 else 0)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

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

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


# ===CODE===

def main():
    n, t = ns()
    a = na()
    tmp = []
    prev = -1
    for ai in a:
        if prev != ai:
            tmp.append(ai)
        prev = ai

    tmp2 = [tmp[0]]
    prev = 0
    for i in range(1, len(tmp)):
        if prev * (tmp[i] - tmp[i - 1]) < 0:
            tmp2.append(tmp[i - 1])
        prev = tmp[i] - tmp[i - 1]
        if i == len(tmp) - 1:
            tmp2.append(tmp[i])

    ans = dict()
    for i in range(1, len(tmp2)):
        tmp = tmp2[i] - tmp2[i - 1]
        if tmp > 0:
            if tmp in ans.keys():
                ans[tmp] += 1
            else:
                ans[tmp] = 1

    mk = max(ans.keys())
    print(ans[mk])



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, t = map(int, input().split())
    a = list(map(int, input().split()))
    diff = 1 if a[0] < a[1] else -1  # 1:increasing, -1:decreasing
    now = a[0]
    buy = []
    cnt = 0
    for i in range(1, n):
        if a[i - 1] < a[i] and diff < 0:
            diff *= -1
            now = a[i - 1]
        elif a[i - 1] > a[i] and diff > 0:
            diff *= -1
            if True:
                buy.append([now, a[i - 1], a[i - 1] - now, cnt])
                cnt += 1
            now = a[i - 1]
    if diff > 0:
        buy.append([now, a[-1], a[-1] - now, cnt])
    ans = 1
    if 2 * len(buy) > t:
        buy.sort(key=lambda x: x[2], reverse=True)
        buy = buy[:t // 2]
        buy.sort(key=lambda x: x[3])
    f = False
    for i in range(len(buy) - 1):
        if buy[i][1] - 1 == buy[i + 1][1]:
            if f:
                ans += 1
                f = False
            else:
                f = True
                continue
        else:
            f = False
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, t = map(int, input().split())
    a = list(map(int, input().split()))
    diff = 1 if a[0] < a[1] else -1  # 1:increasing, -1:decreasing
    now = a[0]
    buy = []
    cnt = 0
    for i in range(1, n):
        if a[i - 1] < a[i] and diff < 0:
            diff *= -1
            now = a[i - 1]
        elif a[i - 1] > a[i] and diff > 0:
            diff *= -1
            if True:
                buy.append([now, a[i - 1], a[i - 1] - now, cnt])
                cnt += 1
            now = a[i - 1]
    if diff > 0:
        buy.append([now, a[-1], a[-1] - now, cnt])
    ans = 1
    if 2 * len(buy) > t:
        buy.sort(key=lambda x: x[2], reverse=True)
        buy = buy[:t // 2]
        buy.sort(key=lambda x: x[3])
    f = False
    for i in range(len(buy) - 1):
        if buy[i][1] - 1 == buy[i + 1][1]:
            if f:
                print(i)
                ans += 1
                f = False
            else:
                f = True
                continue
        else:
            f = False
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

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

    min_a = A[0]
    max_a = -1
    min_diff = 10**9
    benefit = []
    for i in range(N - 1):
        if A[i] < A[i + 1]:
            max_a = A[i + 1]
        else:
            if max_a != -1:
                benefit.append(max_a - min_a)
            min_a = A[i + 1]
            max_a = -1

        if A[i - 1] >= A[i]:
            min_diff = min(min_diff, A[i - 1] - A[i])
    else:
        if max_a != -1:
            benefit.append(max_a - min_a)

    if benefit:
        ans = benefit.count(max(benefit))
    else:
        exit()

    # print(benefit)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

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

    min_a = A[0]
    max_a = -1
    min_diff = 10**9
    benefit = []
    for i in range(N - 1):
        if A[i] <= A[i + 1]:
            max_a = A[i + 1]
        else:
            if max_a != -1:
                benefit.append(max_a - min_a)
            min_a = A[i + 1]
            max_a = -1

        if A[i - 1] >= A[i]:
            min_diff = min(min_diff, A[i - 1] - A[i])
    else:
        if max_a != -1:
            benefit.append(max_a - min_a)

    if benefit:
        ans = benefit.count(max(benefit))
    else:
        exit()

    # print(benefit)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

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

    min_a = A[0]
    max_a = -1
    min_diff = 10**9
    benefit = []
    for i in range(1, N):
        if min_a < A[i]:
            max_a = A[i]
        else:
            if max_a != -1:
                benefit.append(max_a - min_a)
            min_a = A[i]
            max_a = -1

        if A[i - 1] >= A[i]:
            min_diff = min(min_diff, A[i - 1] - A[i])
    else:
        if max_a != -1:
            benefit.append(max_a - min_a)

    if benefit:
        ans = benefit.count(max(benefit))
    else:
        exit()

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

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

    min_a = A[0]
    max_a = -1
    min_diff = 10**9
    benefit = []
    for i in range(1, N):
        if min_a < A[i]:
            max_a = A[i]
        else:
            if max_a != -1:
                benefit.append(max_a - min_a)
            min_a = A[i]
            max_a = -1

        if A[i - 1] >= A[i]:
            min_diff = min(min_diff, A[i - 1] - A[i])
    else:
        if max_a != -1:
            benefit.append(max_a - min_a)

    if benefit:
        ans = benefit.count(max(benefit))
    else:
        ans = min_diff + 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, t = map(int, input().split())
a = list(map(int, input().split()))



max_gain = 0
patrn = 0


for i in range(n-1):
    if i == 0:
        if a[1] > a[0]:
            max_gain = a[1] - a[0]
            patrn =1
    if a[i+1] - min(a[0:i+1]) > max_gain:
        max_gain = a[i+1] - min(a[0:i+1])
        patrn =1
    elif a[i+1] - min(a[0:i+1]) == max_gain:
        patrn +=1
    
print(patrn)

Submission status: Wrong Answer

Solution code:
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()
    prev = A[0]
    tmp_max = prev
    tmp_min = prev
    max_gap = 0
    cnt = 0
    for a in A:
        if a >= prev:
            tmp_max = a
        else:
            if prev - tmp_min > max_gap:
                cnt = 1
                max_gap = prev - tmp_min
            elif prev - tmp_min == max_gap:
                cnt += 1

            tmp_min = a
            tmp_max = a

        prev = a

    if prev - tmp_min > max_gap:
        cnt = 1
        max_gap = prev - tmp_min
    elif prev - tmp_min == max_gap:
        cnt += 1
    print(cnt)
    

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,T=map(int,input().split())
a=list(map(int,input().split()))
from collections import Counter
c=Counter()
f=False
t=0
for i in range(1,n):
  if f:
    if a[i]>=a[i-1]:
      t+=a[i]-a[i-1]
    else:
      c[t]+=1
      t=0
      f=False
  else:
    if a[i]>=a[i-1]:
      t=a[i]-a[i-1]
      f=True
if t!=0:
  c[t]+=1
ans=0
from math import floor
for k,v in sorted(c.items(),reverse=1):
  # print(k,v)
  ans=min(v,floor(T/2))
  break
print(ans)