Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

#<127>

N, M = map(int, input().split())
gate = [0] * N
#rang = []
for m in range(M):
    L, R = map(int, input().split())
    for x in range(L, R + 1):
        gate[x - 1] += 1
        #print(gate)

print(gate.count(M))

### FIXED CODE ###

N, M = map(int, input().split())
L, R = [0] * M, [0] * M
for m in range(M):
    L[m], R[m] = map(int, input().split())
print(max(min(R) - max(L) + 1, 0))
### BUGGY CODE ###

import sys
from array import array
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))


def solve():
    n, k =read(); arr = read()
    dp = array("i",[-1 for i in range(n)]); dp[0] = 0
    for i in range(n):
    	for j in range(1, k+1):
    		if i-j >-1:
    			tem = dp[i-j]+abs(arr[i-j] - arr[i])
    			if dp[i] == -1:
    				dp[i] = tem
    			else:
    				dp[i] = min(dp[i], tem)
    print(dp[-1])

    

if __name__ == "__main__":
	solve()

### FIXED CODE ###

import sys
# import numpy as np
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))
# import profile

def solve():
    n, k =read(); arr = read()
    dp = [-1]*n; dp[0] = 0
    for i in range(n):
    	for j in range(1, k+1):
    		if i-j >-1:
    			tem = dp[i-j]+abs(arr[i-j] - arr[i])
    			if dp[i] == -1:
    				dp[i] = tem
    			elif dp[i] > tem:
    				dp[i] = tem
    				# dp[i] = min(dp[i], tem)
    print(dp[-1])

def one():
	l = [0]*10**7
def two():
	l = [0 for i in range(10**7)]

if __name__ == "__main__":
	solve()
	# cProfile.run(solve())
	# profile.run(one())
	# cProfile.run()
### BUGGY CODE ###

N=int(input())
A=sorted(map(int,input().split()))[::-1]
ans=A[0]
while True:
    M=A[0]
    m=A[1]
    del A[0]
    if m==0:break
    A.append(M%m)
    A=sorted(A)[::-1]
    ans=min(ans,A[0])
print(ans)

### FIXED CODE ###

import heapq

N=int(input())
A=sorted(map(int,input().split()))[::-1]
que = []
for x in A:
    heapq.heappush(que, -x)

ans=A[0]
while len(que) > 1:
    v1 = abs(heapq.heappop(que))
    v2 = abs(heapq.heappop(que))
    if v2 == 0:
        print (v1)
        break
    heapq.heappush(que, -(v1 % v2))
    heapq.heappush(que, -(v2))
### BUGGY CODE ###

"""
    Author: _YL_
"""
s = input()
n = int(input())

i = 0
while i < n:
    lst = input().split()

    if lst[0] == "1":
        temp = ""
        for letter in s:
            temp = letter + temp
        s = temp
    elif lst[1] == "1":
        s = lst[2] + s
    else:
        s += lst[2]

    i += 1

print(s)


### FIXED CODE ###

"""
    Author: _YL_
"""
s = input()
n = int(input())

flip = 0
left = 0
right = 1
strs = ["", ""]

i = 0
while i < n:
    lst = input().split()

    if lst[0] == "1":
        flip += 1
        left = 0 if left == 1 else 1
        right = 0 if right == 1 else 1
    elif lst[1] == "1":
        strs[left] += lst[2]
    else:
        strs[right] += lst[2]
    i += 1

if flip % 2 == 1:
    s = s[::-1]
    
s = strs[left][::-1] + s + strs[right]

print(s)
### BUGGY CODE ###

from copy import deepcopy
n = int(input())
s = input().rstrip()

def solve1(n, s):
    # 全探索
    cnt = 0
    for v in range(0, 1000):
        v = str(v)
        v = "0" * (3 - len(v)) + v
        idx = 0
        for i in range(len(s)):
            if s[i] == v[idx]:
                idx += 1
            if idx == 3:
                cnt += 1
                break
    print(cnt)

def solve2(n, s):
    # DP
    dp = [[False for _ in range(1000)] for _ in range(4)]
    dp[0][0] = True

    for i in range(n):
        pre_dp = deepcopy(dp)
        for j in range(3):
            for k in range(100):
                if pre_dp[j][k]:
                    num = k*10+int(s[i])
                    dp[j+1][num] = True
    cnt = 0
    for i in range(1000):
        if dp[3][i]:
            cnt += 1
    print(cnt)
solve2(n, s)

### FIXED CODE ###

n = int(input())
s = input().rstrip()

def solve(n, s):
    # 全探索
    cnt = 0
    for v in range(0, 1000):
        v = str(v)
        v = "0" * (3 - len(v)) + v
        idx = 0
        for i in range(len(s)):
            if s[i] == v[idx]:
                idx += 1
            if idx == 3:
                cnt += 1
                break
    print(cnt)
solve(n, s)
### BUGGY CODE ###

n,k =map(int,input().split())
x = list(map(int, input().split()))
a = [0]*n
for i in range (1,n):
    a[i]=(min([abs(x[i]-x[j])+a[j] for j in range(max(0,i-k),i)]))
print (a[-1])

### FIXED CODE ###

def main():
    
    n,k =map(int,input().split())
    x = list(map(int, input().split()))
    a = [0]*n
    for i in range (1,n):
        a[i]=(min([abs(x[i]-x[j])+a[j] for j in range(max(0,i-k),i)]))
    print (a[-1])
if __name__ == "__main__":
    main()
### BUGGY CODE ###

K, S = map(int, input().split())
cnt = 0
for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if i+j+k == S:
                cnt += 1
print(cnt)

### FIXED CODE ###

K, S = map(int, input().split())
cnt = 0
for i in range(K+1):
    for j in range(K+1):
        Z = S - (i + j)
        if 0 <= Z <= K:
            cnt += 1
print(cnt)
### BUGGY CODE ###

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

for i in range(n):
    s_input = input().split()
    if s_input not in s:
        s[i] = s_input

print(n - s.count(0))

### FIXED CODE ###

#ABC164_C
n = int(input())

s = {input() for _ in range(n)}

print(len(s))
### BUGGY CODE ###

H, W = map(int, input().split())

graph = []
for i in range(H):
    graph.append(input())

L = [[0 for _ in range(W)] for _ in range(H)]
U = [[0 for _ in range(W)] for _ in range(H)]
D = [[0 for _ in range(W)] for _ in range(H)]
R = [[0 for _ in range(W)] for _ in range(H)]


for y in range(H):
    for x in range(W):
        if graph[y][x] == "#":
            L[y][x] = 0
        elif x == 0:
            L[y][x] = 1
        else:
            L[y][x] = L[y][x-1] + 1

for x in range(W):
    for y in range(H):
        if graph[y][x] == "#":
            U[y][x] = 0
        elif y == 0:
            U[y][x] = 1
        else:
            U[y][x] = U[y-1][x] + 1

for x in range(W):
    for y in range(H-1, -1, -1):
        if graph[y][x] == "#":
            D[y][x] = 0
        elif y == H-1:
            D[y][x] = 1
        else:
            D[y][x] = D[y+1][x] + 1

for y in range(H):
    for x in range(W-1, -1, -1):
        if graph[y][x] == "#":
            R[y][x] = 0
        elif x == W-1:
            R[y][x] = 1
        else:
            R[y][x] = R[y][x+1] + 1

ans = -1
for y in range(H):
    for x in range(W):
        ans = max(ans, L[y][x] + R[y][x] + U[y][x] + D[y][x] - 3)

print(ans)


### FIXED CODE ###

H, W = map(int, input().split())

graph = []
for i in range(H):
    graph.append(input())

L = [[0 for _ in range(W)] for _ in range(H)]
U = [[0 for _ in range(W)] for _ in range(H)]
D = [[0 for _ in range(W)] for _ in range(H)]
R = [[0 for _ in range(W)] for _ in range(H)]


for y in range(H):
    for x in range(W):
        if graph[y][x] == "#":
            L[y][x] = 0
        elif x == 0:
            L[y][x] = 1
        else:
            L[y][x] = L[y][x-1] + 1

for x in range(W):
    for y in range(H):
        if graph[y][x] == "#":
            U[y][x] = 0
        elif y == 0:
            U[y][x] = 1
        else:
            U[y][x] = U[y-1][x] + 1

for x in range(W):
    for y in range(H-1, -1, -1):
        if graph[y][x] == "#":
            D[y][x] = 0
        elif y == H-1:
            D[y][x] = 1
        else:
            D[y][x] = D[y+1][x] + 1

for y in range(H):
    for x in range(W-1, -1, -1):
        if graph[y][x] == "#":
            R[y][x] = 0
        elif x == W-1:
            R[y][x] = 1
        else:
            R[y][x] = R[y][x+1] + 1

ans = -1
for y in range(H):
    for x in range(W):
        ans = max(ans, L[y][x] + R[y][x] + U[y][x] + D[y][x] - 3)

print(ans)
### BUGGY CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
K=int(input())
tgt=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            tgt+=gcd(i, j, k)
print(tgt)

### FIXED CODE ###

from math import gcd
K=int(input())
K1=K+1
tgt=0
for i in range(1,K1):
    for j in range(1,K1):
        for k in range(1,K1):
            tgt+=gcd(gcd(i, j), k)
print(tgt)
### BUGGY CODE ###

import sys
n = int(sys.stdin.readline())
S = sys.stdin.readline().rstrip()


dp = [[float('inf') for i in range(n)] for j in range(n)]
#初期条件
for j in range(1, n):
    if S[0] == S[j]:
        dp[0][j] = 1
    else:
        dp[0][j] = 0

#print(dp)

for i in range(1, n-1):
    for j in range(i+1, n):
        if S[i] ==S[j]:
            dp[i][j] = min(dp[i-1][j-1] +1, n//2)
        else:
            dp[i][j] = 0

maxp = 0
for i in range(n-1):
    for j in range(i+1, n):
        if maxp < dp[i][j]:
            maxp = dp[i][j]
#print(dp)
print(maxp)

### FIXED CODE ###

import sys
n = int(sys.stdin.readline())
S = sys.stdin.readline().rstrip()


dp = [[0 for i in range(n)] for j in range(n)]
#初期条件
for j in range(1, n):
    if S[0] == S[j]:
        dp[0][j] = 1

#print(dp)

for i in range(1, n-1):
    for j in range(i+1, n):
        if S[i] ==S[j]:
            dp[i][j] = min(dp[i-1][j-1] +1, j-i)

maxp = 0
for i in range(n-1):
    for j in range(i+1, n):
        if maxp < dp[i][j]:
            maxp = dp[i][j]
#print(dp)
print(maxp)
### BUGGY CODE ###

n = int(input())
a = [list(map(int,input().split())) for i in range(n)]

def c(x,y):
    if y < x:
        x,y = y,x
    return x*n+y

e = [[] for i in range(n**2)]
count = [0]*(n**2)
for i in range(n):
    for j in range(n-2):
        y = c(i,a[i][j]-1)
        z = c(i,a[i][j+1]-1)
        count[z] += 1
        e[y].append(z)

q = []

for i in range(n-1):
    for j in range(i+1,n):
        x = c(i,j)
        if count[x] == 0:
            q.append(x)
ans = 0

while q:
    
    ans += 1
    old = q
    q = []
    for nowq in old:
        for nex in e[nowq]:
            count[nex] -= 1
            
            if count[nex] == 0:
                q.append(nex)
if sum(count) > 0:
    print(-1)
else:    
    print(ans)





### FIXED CODE ###

def main():
    import sys
    sys.setrecursionlimit(10**7)
    input = sys.stdin.readline
    n = int(input())
    l = [list(map(int,input().split())) for i in range(n)]
    ans = 1
    count = [0]*n
    check = set()
    game = []
    for i in range(n):
        if count[i] >= n-1 or i in check:
            continue
        ind = l[i][count[i]]-1
        if ind in check:
            continue
        if l[ind].index(i+1) == count[ind]:
            check |= set([i,ind])
            game.append([i,ind])
            count[i] += 1
            count[ind] += 1
    while True:
        gamepre = game
        check = set()
        game = []
        for i,j in gamepre:
            ci = True
            cj = True
            if count[i] >= n-1 or i in check:
                ci = False
            if count[j] >= n-1 or j in check:
                cj = False
            if ci:
                ci = True
                ind = l[i][count[i]]-1
                if ind in check:
                    ci = False
                if ci and l[ind].index(i+1) == count[ind]:
                    check |= set([i,ind])
                    game.append([i,ind])
                    count[i] += 1
                    count[ind] += 1
            if cj:
                cj = True
                ind = l[j][count[j]]-1
                if ind in check:
                    cj = False
                if cj and l[ind].index(j+1) == count[ind]:
                    check |= set([j,ind])
                    game.append([j,ind])
                    count[j] += 1
                    count[ind] += 1
        if not check:
            if sum(count) != n*(n-1):
                print(-1)
                exit()
            else:
                print(ans)
                exit()
        ans += 1
if __name__ == "__main__":
    main()
### BUGGY CODE ###

n,m = map(int,input().split())
g = 1
for i in range(1,m):
  if m%i == 0:
    if int(m/i) >= n:
      g = i
    else:
      break
      
print(g)
  

### FIXED CODE ###

n,m = map(int,input().split())
g = 1
def fact(k):
  a = []
  for i in range(1,int(k **(0.5))+1):
    if k%i ==0:
      a.append(i)
      a.append(int(k/i))
  b = list(set(a))
  b.sort()
  return b

g = fact(m)
for i in range(len(g)):
  if g[len(g)-i-1] <= m/n:
    print(g[len(g)-i-1])
    break
### BUGGY CODE ###

from collections import deque
n = int(input())
listx = [int(input()) for i in range(n)]
listx.sort()
listx = deque(listx)
listy = deque([])
ans=0

x=listx.popleft()
listy.appendleft(x)
x=listx.pop()
listy.append(x)
ans+=int(listy[1])-int(listy[0])

while len(listx)>=1:
    abs1=abs(listx[0]-listy[-1])
    abs2=abs(listx[-1]-listy[0])

    if abs1 < abs2:
        x=listx.pop()
        listy.appendleft(x)
        ans+=abs2

    else:
        x=listx.popleft()
        listy.append(x)
        ans+=abs1
        
    if listy[0] > listy[-1]:
        listy.reverse()

print(ans)


### FIXED CODE ###

import itertools
import numpy as np
data_num = int(input()) 
data = [int(input()) for i in range(data_num)] 
#data_num=5
#data=[6,8,1,2,3]

data.sort()
npdata=np.array(data)

parts1=[1]
parts2=[2 for i in range(data_num-2)]
parts1.extend(parts2)
parts1.append(1)
npparts1=np.array(parts1)

parts0=[(-1)**(i+1) for i in range(data_num)]
npparts0=np.array(parts0)

y=npparts1*npparts0

y=y.tolist()
y.sort()

y=np.array(y)
z1=sum(data*y)

parts01=[(-1)**i for i in range(data_num)]
npparts0=np.array(parts01)

y=npparts1*npparts0

y=y.tolist()
y.sort()
y=np.array(y)
z2=sum(data*y)

print(max(z1,z2))
### BUGGY CODE ###

A,B = (int(x) for x in input().split())

if A==B:
    print(A)
else:
    ans = A^(A+1)

    for i in range(A+2,B+1):
        ans = ans ^ i

    print(ans)

### FIXED CODE ###

A,B = map(int,input().split())

if A == B:
    print(A)
elif B - A == 1:
    print(A^B)
else:
    ans = 0
    if A % 2 != 0:
        ans ^= A
        A += 1
    if B % 2 == 0:
        ans ^= B
        B -= 1
    ans ^= ((B-A+1)//2)%2

    print(ans)
### BUGGY CODE ###

N, K, Q = map(int, input().split())
A, P = [], []
for q in range(Q):
  A.append(int(input()))

if K > len(A):
  for _ in range(N):
    print("Yes")
  exit()

for _ in range(N):
  P.append(K)

for a in A:
  for i, p in enumerate(P):
    if i != (a - 1):
      P[i] -= 1

for p in P:
  if p <= 0:
    print("No")
  else:
    print("Yes")

### FIXED CODE ###

N, K, Q = map(int, input().split())
A = []
for q in range(Q):
    A.append(int(input()))
score = [K - Q] * (N + 1)
for a in A:
    score[a] += 1
ans = "\n".join("No" if s <= 0 else "Yes" for s in score[1:])
print(ans)
### BUGGY CODE ###

s = input()
for _ in range(int(input())):
    q = input().split()
    if q[0] == '1':
        s = s[::-1]
    else:
        if q[1] == '1':
            s = ''.join([q[2], s])
        else:
            s += q[2]
print(s)

### FIXED CODE ###

def main():
    c = 0
    s = input()
    f = ''
    b = ''
    for _ in range(int(input())):
        q = input().split()
        if q[0] == '1':
            c += 1
        else:
            if (q[1] == '1' and c%2 == 0) or (q[1]=='2' and c%2==1):
                f = q[2] + f
            else:
                b += q[2]
    if c % 2 == 0:
        print(f+s+b)
    else:
        print(b[::-1]+s[::-1]+f[::-1])

if __name__ == '__main__':
    main()
### BUGGY CODE ###

def gcd(a,b):
    if a%b == 0:
        return b
    else :
        return gcd(b,a%b)

k = int(input())

ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for h in range(1,k+1):
            t = gcd(i,j)
            t = gcd(t,h)
            ans += t
print(ans)


### FIXED CODE ###

def gcd(a,b):
    if a%b == 0:
        return b
    else :
        return gcd(b,a%b)

k = int(input())

ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for h in range(1,k+1):
            t = gcd(i,j)
            t = gcd(t,h)
            ans += t
print(ans)
### BUGGY CODE ###

N,Q = map(int,input().split())
S = input()
buffer = []
lr =[]
for i in range(Q):
  buffer = list(map(int,input().split()))
  lr.append(buffer)

for q in range(Q):
  Ss = S[lr[q][0]-1:lr[q][1]]
  sums = 0
  for i in range(len(Ss)-1):
    if Ss[i:i+2] == "AC":
      sums += 1
  print(sums)

### FIXED CODE ###

N,Q = map(int,input().split())
S = input()
AC = [0] * N
buffer = []
lr =[]
for i in range(Q):
  buffer = list(map(int,input().split()))
  lr.append(buffer)
counts = 0
for i in range(N):
  if S[i:i+2] == "AC":
    counts +=1
  AC[i] =  counts
a =[0]
AC = a + AC
for q in range(Q):
  #print(str(lr[q][0])+" "+ str(lr[q][1]))
  print(AC[lr[q][1]-1] - AC[lr[q][0]-1])
### BUGGY CODE ###

H,W=map(int, input().split())

A=[]
for i in range(H):
  A.append(list(input()))
q=[]
for i in range(H):
  for j in range(W):
    if A[i][j] == '#':
      q.append((i, j))

cnt=0
while q:
  r=[]
  cnt += 1
  for i, j in q:
    if i-1 in range(H) and A[i-1][j] == '.':
      A[i-1][j] = '#'
      r += [(i-1, j)]
    if i+1 in range(H) and A[i+1][j] == '.':
      A[i+1][j] = '#'
      r.append((i+1, j))
    if j-1 in range(W) and A[i][j-1] == '.':
      A[i][j-1] = '#'
      q=r.append((i, j-1))
    if j+1 in range(W) and A[i][j+1] == '.':
      A[i][j+1] = '#'
      r.append((i, j+1))
  q=r
print(cnt-1)

### FIXED CODE ###

def f(h, w):
    dt = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    t = [[True] * (w + 2)]
    for _ in range(h):
        t += [[True] + [(c == "#") for c in input()] + [True]]
    t += [[True] * (w + 2)]
    kuro = []
    for i, ti in enumerate(t[1:-1]):
        for j, tij in enumerate(ti[1:-1]):
            if tij:
                kuro+=[(i + 1, j + 1)]
    cnt = -1
    while kuro:
        cnt += 1
        nxt = []
        for i, j in kuro:
            ti=t[i]
            if not ti[j+1]:
                ti[j + 1]=True
                nxt += [(i, j+1)]
            if not ti[j-1]:
                ti[j - 1]=True
                nxt += [(i, j-1)]
            if not t[i - 1][j]:
                t[i - 1][j]=True
                nxt += [(i - 1, j)]
            if not t[i + 1][j]:
                t[i + 1][j]=True
                nxt += [(i + 1, j)]
        kuro = nxt
    print(cnt)
 
 
h, w = map(int, input().split())
f(h, w)
### BUGGY CODE ###

from operator import itemgetter
from bisect import bisect,bisect_left,insort

def solve(n, ppp):
    q = sorted(enumerate(ppp),key=itemgetter(1),reverse=True)
    le = [-1,-1,n,n]
    ans = 0
    for i, p in q:
        l_i = bisect(le,i)
        LL = le[l_i-2]
        LR = le[l_i-1]
        RL = le[l_i]
        RR = le[l_i+1]
        ans += p*((LR-LL)*(RL-i)+(RR-RL)*(i-LR))
        insort(le,i)
    return ans


n = int(input())
ppp = list(map(int, input().split()))
print(solve(n, ppp))

### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from operator import itemgetter

n = int(readline())
ppp = list(map(int,readline().split()))

def BIT_add(i,x):
    while i <= n:
        tree[i] += x
        i += i&(-i)

def BIT_sum(i):
    s = 0
    while i:
        s += tree[i]
        i -= i&(-i)
    return s

def BIT_search(x):
    # 二分探索。和がx以上となる最小のインデックス(>= 1)を返す
      i = 0
      s = 0
      step = 1<<(n.bit_length()-1)
      while step:
          if i+step <= n and s + tree[i+step] < x:
              i += step
              s += tree[i]
          step >>= 1
      return i+1

q = sorted(enumerate(ppp,1),key=itemgetter(1),reverse=True)
tree = [0]*(n+1)

ans = 0
for i, p in q:
    L = BIT_sum(i) # 左にある既に書き込んだ数の個数
    BIT_add(i,1)
    R = n-p-L # 右にある既に書き込んだ数の個数
    LL = BIT_search(L-1) if L >= 2 else 0
    LR = BIT_search(L) if L >= 1 else 0
    RL = BIT_search(L+2) if R >= 1 else n+1
    RR = BIT_search(L+3) if R >= 2 else n+1
    ans += p*((LR-LL)*(RL-i)+(RR-RL)*(i-LR))
print(ans)
### BUGGY CODE ###

N=int(input())
S=[input() for _ in range(N)]
S.sort()
cnt=[0]*N
cnt[0]=1

for i in range(1,N):
    if S[i]==S[i-1]:
        cnt[i]=cnt[i-1]+1
    else:
        cnt[i]=1
cmax=max(cnt)
while cmax in cnt:
    A=cnt.index(cmax)
    print(S[A])
    cnt[A]=0

### FIXED CODE ###

N=int(input())
S=[input() for _ in range(N)]
S.sort()
cnt=1
sheet=[[],[]]

for i in range(1,N):
    if S[i]==S[i-1]:
        cnt+=1
        if i==N-1:
            sheet[0].append(S[i-1])
            sheet[1].append(cnt)
    else:
        sheet[0].append(S[i-1])
        sheet[1].append(cnt)
        cnt=1
        if i==N-1:
            sheet[0].append(S[i])
            sheet[1].append(cnt)

cmax=max(sheet[1])
for j in range(len(sheet[1])):
    if sheet[1][j]==cmax:
        print(sheet[0][j])
### BUGGY CODE ###

n,k=map(int,input().split());a=list(map(int,input().split()));dp=[0]*(k+1)
for i in range(1,k+1):
  for j in a:dp[i]=max((i-j>=0)*(dp[i-j]==0),dp[i])
print(["Second","First"][dp[-1]])

### FIXED CODE ###

n,k=map(int,input().split());a=list(map(int,input().split()));dp=[0]*(k+1)
for i in range(1,k+1):
  for j in a:dp[i]=max((i-j>=0)*(dp[i-j]==0),dp[i])
print(["Second","First"][dp[-1]])
### BUGGY CODE ###

n, m = list(map(int, input().split()))
a = []
for _ in range(m):
    a.append(int(input()))
cur = 0
dp = [0 for _ in range(n + 1)]
flags = [0 for _ in range(n + 1)]
for i in range(n + 1):
    if i in a:
        flags[i] = 1
dp[0], dp[1] = 1, 1
if 1 in a:
    dp[1] = 0
for i in range(2, n + 1):
    if flags[i]:
        continue
    dp[i] = (dp[i - 1] + dp[i - 2]) % (10 ** 9 + 7)
print(dp[n])


### FIXED CODE ###

n, m = list(map(int, input().split()))
a = []
for i in range(m):
    a.append(int(input()))
a = set(a)
dp = [0 for _ in range(n+1)]
dp[0] = 1
if 1 in a:
    dp[1] = 0
else:
    dp[1] = 1
for i in range(2, n+1):
    if not(i in a):
        dp[i] = dp[i - 2] + dp[i - 1]
print(dp[n] % 1000000007)
### BUGGY CODE ###

N = int(input())
A = [int(i) for i in input().split(' ')]

A.sort(reverse=True)
ring = []
max_value = A.pop(0)
ring.append(max_value)
ring.append(max_value)
diff = [max_value]
ans = 0
flag = True
for i in A:
    ans = ans + max(diff)
    n = diff.index(max(diff))
    ring.insert(n + 1, i)
    diff[n] = i
    diff.insert(n + 1, i)

print(ans)

### FIXED CODE ###

import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)
### BUGGY CODE ###

'''
Created on 2019/08/11

@author: remuo402
'''
n,m = map(int,input().split())
jobs = [[] for i in range(100001)]
for i in range(n):
    a1,b1 = map(int,input().split())
    jobs[a1 - 1].append(b1)
result = 0
for i in range(m):
    jobs_1 = 0
    jobs_2 = 0
    reward = 0
    for j in range(i+1):
        if len(jobs[j]) != 0:
            reward = max(reward,max(jobs[j]))
            jobs_1 = j
            jobs_2 = jobs[j].index(max(jobs[j]))
    if reward != 0:
        result += reward
        del jobs[jobs_1][jobs_2]
print(result)

### FIXED CODE ###

from heapq import heappush, heappop
n,m = map(int,input().split())
jobs = [[] for i in range(100001)]
for i in range(n):
    a1,b1 = map(int,input().split())
    jobs[a1].append(b1)

reward = 0
heapsort = []
for daysLeft in range(1,m+1):
    if len(jobs[daysLeft]) != 0:
        for i in jobs[daysLeft]:
            heappush(heapsort,-i)
    if len(heapsort) > 0:
        reward += -heappop(heapsort)
print(reward)
### BUGGY CODE ###

import copy
N = int(input())

A = [0]*N

for r in range(N):
  A[r] = int(input())
Elias_A = copy.copy(A)
A.sort()
Sorted_A = copy.copy(A)

  
for r in range(N):
  A.remove(Elias_A[r])
  print(A[N-2])
  A = copy.copy(Sorted_A)

### FIXED CODE ###

N = int(input())
A = [0]*N

for r in range(N): A[r] = int(input())
A_max = max(A)
A_sec = sorted(A)[-2]

for r in range(N):
  if A[r] == A_max: print(A_sec)
  elif A[r] == A_sec: print(A_max)
  else: print(A_max)