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

import bisect

N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)

ans = 0
dif = 0
count = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        dif = L[i] - L[j] + 1
        L_descend = sorted(L[j+1:])
        # print(L_descend, bisect.bisect_left(L_descend, dif))
        index = bisect.bisect_left(L_descend, dif)
        count = len(L_descend) - index
        ans += count
        # print("ans = ", ans)
print(ans)


### FIXED CODE ###

N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)

def f(j, max1, max2):
    L_temp = L[j+1:]
    # print("max1 = ", max1, "max2 = ", max2, L_temp)
    dif = max1 - max2
    left = -1
    right = len(L_temp)
    while right - left != 1:
        temp = (left+right)//2
        if dif < L_temp[temp]:
            left = temp
        else:
            right = temp
    if left != N:
        return left+1
    else:
        return left


ans = 0
max1 = 0
max2 = 0
for i in range(N):
    max1 = L[i]
    for j in range(i+1, N-1):
        max2 = L[j]
        ans += f(j, max1, max2)
        # print("ans = ", ans)
print(ans)
### BUGGY CODE ###

n = int(input())
a =list(map(int,input().split()))

an=([a.count(i) for i in range(1,n+1)])
for i in range(n):
    print(an[i])

### FIXED CODE ###

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

for i in a:
    al[i-1] += 1
for i in range(n):
    print(al[i])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from itertools import product
    n=int(input())
    A=[list(map(int,list(input()))) for _ in range(n)]
    for i,j in product(range(n),repeat=2):
        if(i==j): continue
        if(A[i][j]==0): A[i][j]=INF
    B=[a[:] for a in A] # deep copy
    E=[[] for _ in range(n)]
    for i,j in product(range(n),repeat=2):
        if(i==j): continue
        if(A[i][j]==1): E[i].append(j)

    # bipartile check
    d=[-1]*n # -1: unvisited
    def dfs(v,p=-1)->bool:
        # initialize
        if(p==-1):
            if(d[v]!=-1): return True
            else: d[v]=0
        # iterate
        res=True
        for nv in E[v]:
            if(nv==p): continue
            if(d[nv]==-1):
                d[nv]=d[v]^1
                dfs(nv,v)
            else:
                res=min(res,d[nv]^d[v])
        return res

    for v in range(n):
        if(not dfs(v)):
            print(-1)
            return

    # Warshall-Floyd
    for k,i,j in product(range(n),repeat=3):
        B[i][j]=min(B[i][j],B[i][k]+B[k][j])

    # output
    diam=0
    for i,j in product(range(n),repeat=2):
        if(B[i][j]==INF): continue # connectedなのｄ必要ない
        diam=max(diam,B[i][j])
    print(diam+1)
resolve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=[[int(s) for s in input()] for _ in range(n)]
    B=[a[:] for a in A]
    from itertools import product
    # 対角以外の0をINF
    for i,j in product(range(n),repeat=2):
        if i==j: continue
        if B[i][j]==0: B[i][j]=INF
    # Warshall-Floyd
    for k,i,j in product(range(n),repeat=3):
        B[i][j]=min(B[i][j],B[i][k]+B[k][j])
    # 直径となる始点を1つ取得
    dia=0; p=0
    for i,j in product(range(n),repeat=2):
        if dia<B[i][j]:
            dia=B[i][j]
            p=i
    for i,j in product(range(n),repeat=2):
        if i>=j: continue # i<j
        if A[i][j]:
            if abs(B[i][p]-B[j][p])!=1:
                print(-1)
                return
    print(dia+1)
resolve()
### BUGGY CODE ###

N,M = (int(x) for x in input().split())
A_arr = [int(x) for x in input().split()]
BC_arr = [[0,0] for _ in range(M)]
for i in range(M):
    b,c = (int(x) for x in input().split())
    BC_arr[i][0] = b
    BC_arr[i][1] = c

A_arr.sort()

for BC in BC_arr:
    Bi = BC[0]
    Ci = BC[1]
    for j in range(Bi):
        if A_arr[j] < Ci:
            A_arr[j] = Ci
        else:
            break
    A_arr.sort()

print(sum(A_arr))


### FIXED CODE ###

N,M = (int(x) for x in input().split())
A_arr = [int(x) for x in input().split()]
BC_arr = [[0,0] for _ in range(M)]
for i in range(M):
    b,c = (int(x) for x in input().split())
    BC_arr[i][0] = b
    BC_arr[i][1] = c

A_arr.sort()
BC_arr.sort(key=lambda x:x[1], reverse=True)

break_th = False
j_count = 0
for BC in BC_arr:
    Bi = BC[0]
    Ci = BC[1]
    for j in range(Bi):
        if A_arr[j_count] >= Ci:
            break_th = True
            break
        A_arr[j_count] = Ci
        j_count += 1
        if j_count == N:
            break_th = True
            break
    if break_th:
        break
print(sum(A_arr))
### BUGGY CODE ###

S, T = input(), input()

def func(S, T):

    S_2 = S+S
    len_s = len(S)
    merge_S = list(set(S))
    merge_T = list(set(T))

    for t in merge_T:
        if t not in merge_S:
            return -1

    alp2num = {t: merge_T.index(t) for t in merge_T}
    dis_S = [[-1]*len(merge_T) for _ in range(len_s)]
    pt = S.index(T[0])
    ans = pt+1

    for t in T[1:]:
        it = alp2num[t]
        if dis_S[pt][it] == -1:
            dis_S[pt][it] = S_2[pt+1:].index(t) + 1
        ans += dis_S[pt][it]
        pt = (pt + dis_S[pt][it]) % len_s

    return ans

print(func(S,T))

### FIXED CODE ###

S, T = input(), input()

def func(S, T):

    merge_S = list(set(S))
    merge_T = list(set(T))
    for t in merge_T:
        if t not in merge_S:
            return -1

    S_2 = S+S
    len_s = len(S)

    ans = -1

    for t in T:
        ans += S_2[(ans+1)%len_s:].index(t) + 1

    return ans + 1

print(func(S,T))
### BUGGY CODE ###

from collections import deque

N, M = [int(x) for x in input().split()]

friends_map = {}

for m in range(M):
    a, b = [int(x) - 1 for x in input().split()]

    if a in friends_map:
        friends_map[a].add(b)
    else:
        friends_map[a] = {b}

    if b in friends_map:
        friends_map[b].add(a)
    else:
        friends_map[b] = {a}

stack = deque()
stack.appendleft(0)

remain_user = {i for i in range(N)}

max_group = 0
watch = set()
while stack:
    now = stack.popleft()
    remain_user.discard(now)
    watch.add(now)

    if now in friends_map:
        for next_node in friends_map[now]:
            if not next_node in watch:
                stack.appendleft(next_node)
                remain_user.discard(next_node)

    if not stack:
        if len(watch) > max_group:
            max_group = len(watch)

        if remain_user:
            stack.appendleft(remain_user.pop())
            watch = set()

print(max_group)


### FIXED CODE ###

from collections import deque

N, M = [int(x) for x in input().split()]

friends_map = {n : set() for n in range(N)}
remain_user = set()
for m in range(M):
    a, b = [int(x) - 1 for x in input().split()]

    friends_map[a].add(b)
    friends_map[b].add(a)
    remain_user.add(a)
    remain_user.add(b)

stack = deque()
stack.appendleft(0)

max_group = 1
watch = [0] * N
count = 0
while stack:
    now = stack.popleft()

    if watch[now] == 0:
        remain_user.discard(now)
        watch[now] = 1
        count += 1

        for next_node in friends_map[now]:
            if watch[next_node] == 0:
                stack.appendleft(next_node)

    if not stack:
        max_group = max(count, max_group)

        if remain_user:
            stack.appendleft(remain_user.pop())
            count = 0

print(max_group)
### BUGGY CODE ###

from functools import lru_cache

N = int(input())

@lru_cache(maxsize=None)
def gcd2(a, b):
  if b == 0:
    return a
  return gcd2(b, a % b)

def gcd(a, b, c):
  return gcd2(gcd2(a, b), c)

res = 0

for i in range(1, N + 1):
  for j in range(1, N + 1):
    for k in range(1, N + 1):
      res += gcd(i, j, k)

print(res)

### FIXED CODE ###

from functools import lru_cache

N = int(input())

@lru_cache(maxsize=None)
def gcd2(a, b):
  while b != 0:
    a, b = b, a % b
  return a

def gcd(a, b, c):
  return gcd2(gcd2(a, b), c)

res = 0

for i in range(1, N + 1):
  for j in range(i, N + 1):
    for k in range(j, N + 1):
      if i == j == k:
        res += gcd(k, j, i)
      elif i != j != k:
        res += gcd(k, j, i) * 6
      else:
        res += gcd(k, j, i) * 3

print(res)
### BUGGY CODE ###

import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
s = 0
for i in range(M):
  b, c = map(int, input().split())
  b = min(b, N)
  A += [c for j in range(b)]
  s += b
heapq.heapify(A)
for i in range(s):
  heapq.heappop(A)
print(sum(A))

### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
exc = []

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))
### BUGGY CODE ###

N,K = list(map(int,input().split(" ")))
hs = list(map(int,input().split(" ")))
dp = [9999999999999] * N

dp[0] = 0
for i in range(1,N):
    temp = []
    stepLim = min(K,i)
#     stepLim = min(K,i)  #stepLim→上限

    for j in range(1,stepLim + 1):
        c = dp[i-j]+abs(hs[i]-hs[i-j])
        temp.append(c)
    dp[i] = min(temp)
print(dp[-1])

### FIXED CODE ###

N,K = list(map(int,input().split(" ")))
hs = list(map(int,input().split(" ")))
dp = [9999999999999] * N

dp[0] = 0
for i in range(1,N):
#ind → i-1 ~ i-stepLim -1   [i-stepLim -1:i]
    stepLim = min(K,i)
    temp = 9999999999999999999
    dp[i] = min([d+abs(hs[i]-h) for d,h in zip(dp[i-stepLim:i],hs[i-stepLim:i])])
print(dp[-1])
### BUGGY CODE ###

def exp_val(n):
    return (n+1)/2

def dice_in_line():
    K,N=map(int,input().split())
    p_n=list(map(int,input().split()))
    exp=[]
    for i in p_n:
        exp.append(exp_val(i))
    exp_sum=[]
    for i in range(K-N+1):
        exp_sum.append(sum(exp[i:i+N]))
    print(max(exp_sum))

dice_in_line()

### FIXED CODE ###

def dice_in_line():
    N,K=map(int,input().split())
    p_n=list(map(int,input().split()))
    max=x=sum(p_n[:K])
    for i in range(N-K):
        x+=(p_n[K+i]-p_n[i])
        if max<x:
            max=x
    print((max+K)/2)

dice_in_line()
### BUGGY CODE ###

S=str(input())
N=len(S)
s=[0]*(N+1)
s[0]=0
counter=[0]*2019
counter[0]=1
ans=0
for i in range(1,N+1):
    s[i]=S[-i:]
    r=int(s[i])%2019
    counter[r]+=1
for i in counter:
    ans += i*(i-1)//2
print(ans)

### FIXED CODE ###

#https://mirucacule.hatenablog.com/entry/2020/04/27/090908
#https://drken1215.hatenablog.com/entry/2020/04/29/171300

S=str(input())[::-1]#逆順で格納
N=len(S)
counter=[0]*2019
counter[0]=1
ans=0
num,d=0,1
for c in S:
    num += int(c) * d
    num %= 2019
    d *= 10
    d %= 2019
    counter[num]+=1
for i in counter:
    ans += i*(i-1)//2
print(ans)
### BUGGY CODE ###

N, K = map(int, input().split())
P = list(map(int, input().split()))


ans = 0
for i in range(0, N-K+1):
    tmp = sum(P[i:i+K])
    if tmp > ans:
        ans = tmp
    
print((ans+K)/2)


### FIXED CODE ###

N, K = map(int, input().split())
P = list(map(int, input().split()))

acc = [0] * (N+1)
for i in range(0, N):
    acc[i+1] = acc[i] + P[i]

ans = 0
for i in range(0, N-K+1):
    tmp = acc[i+K] - acc[i]
    if tmp > ans:
        ans = tmp
    
print((ans+K)/2)
### BUGGY CODE ###

import queue
while 1:
    n,k=map(int,input().split())
    if n==0:break
    a=queue.deque(int(input()) for _ in range(k))
    m=sum(a)
    for i in range(n-k):
        a.popleft()
        a.append(int(input()))
        m=max(m,sum(a))
    print(m)

### FIXED CODE ###

import queue
while 1:
    n,k=map(int,input().split())
    if n==0:break
    a=queue.deque(int(input()) for _ in range(k))
    m=b=sum(a)
    for i in range(n-k):
        c=a.popleft()
        d=int(input())
        a.append(d)
        b+=d-c
        m=max(b,m)        
    print(m)
### BUGGY CODE ###

#coding:utf-8
import bisect,math
A,B,Q=map(int,raw_input().split())
s=[]
t=[]
for a in range(A):
    s.append(input())
for a in range(B):
    t.append(input())
for _ in xrange(Q):
    x=input()
    sl=bisect.bisect_left(s,x)-1
    sr=bisect.bisect_left(s,x)
    tl=bisect.bisect_left(t,x)-1
    tr=bisect.bisect_left(t,x)
    ans=float("inf")
    if sl!=-1 and tl!=-1:
        A=max(s[sl],t[tl])
        B=min(s[sl],t[tl])
        L=abs(x-A)+abs(A-B)
        ans=min(ans,L)
    if sl!=-1 and tr!=len(t):
        L=abs(x-s[sl])*2 + abs(t[tr]-x)        
        ans=min(ans,L)
        ans=min(ans,abs(x-s[sl])+abs(t[tr]-x)*2)
    if tl!=-1 and sr!=len(s):
        ans=min(ans,abs(x-t[tl])*2+abs(s[sr]-x))
        ans=min(ans,abs(x-t[tl])+abs(s[sr]-x)*2)
    if tr!=len(t) and sr!=len(s):
        A=min(s[sr],t[tr])
        B=max(s[sr],t[tr])
        L=abs(A-x)+abs(B-A)
        ans=min(ans,L)
    print(ans)
    
    




### FIXED CODE ###

#coding:utf-8
import bisect,sys
A,B,Q=map(int,raw_input().split())
s=[0]*A
t=[0]*B
for i in range(A):    
    s[i]=int(sys.stdin.readline())
for i in range(B):
    t[i]=int(sys.stdin.readline())
buf=[]
def f(x):
    sl=bisect.bisect_left(s,x)-1
    sr=sl+1
    tl=bisect.bisect_left(t,x)-1
    tr=tl+1
    ans=float("inf")
    if sl!=-1 and tl!=-1:
        A=max(s[sl],t[tl])
        B=min(s[sl],t[tl])
        L=abs(x-A)+abs(A-B)
        ans=min(ans,L)
    if sl!=-1 and tr!=len(t):
        L=abs(x-s[sl])*2 + abs(t[tr]-x)        
        ans=min(ans,L)
        ans=min(ans,abs(x-s[sl])+abs(t[tr]-x)*2)
    if tl!=-1 and sr!=len(s):
        ans=min(ans,abs(x-t[tl])*2+abs(s[sr]-x))
        ans=min(ans,abs(x-t[tl])+abs(s[sr]-x)*2)
    if tr!=len(t) and sr!=len(s):
        A=min(s[sr],t[tr])
        B=max(s[sr],t[tr])
        L=abs(A-x)+abs(B-A)
        ans=min(ans,L)
    return str(ans)

I=[0]*Q
for i in xrange(Q):
    I[i]=int(sys.stdin.readline())
r=map(f,I)
print("\n".join(r))
### BUGGY CODE ###

X = int(input())
s = t = 0
while s < X:
    s = 0
    t += 1
    for i in range(t+1):
        s += i
print(t)


### FIXED CODE ###

X = int(input())
s = 0
for t in range(10**9):
    if s >= X:
        print(t-1)
        break
    s += t
### BUGGY CODE ###

s=input()
k=int(input())
str=[]
p=1
for i in range(k):
    for j in range(0,len(s)-i):
        str.append(s[j:j+i+1])
        str=list(set(str))
str.sort()
print(str[k-1])

### FIXED CODE ###

s=input()
k=int(input())
str=[]
p=1
for i in range(k):
    for j in range(0,len(s)-i):
        str.append(s[j:j+i+1])
str=list(set(str))
str.sort()
print(str[k-1])
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
a = [0] + a + [0]
cost = []
for i in range(n + 1):
    cost.append(abs(a[i + 1] - a[i]))
for i in range(n):
    ans = abs(a[i + 2] - a[i])
    ans += sum(cost[:i]) + sum(cost[i + 2:])
    print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
a = [0] + a + [0]
cost = []
for i in range(n + 1):
    cost.append(abs(a[i + 1] - a[i]))
cost_t = [0]
for i in range(n + 1):
    cost_t.append(cost[i] + cost_t[i])
for i in range(n):
    ans = abs(a[i + 2] - a[i])
    ans += cost_t[i] + cost_t[-1] - cost_t[i + 2]
    print(ans)
### BUGGY CODE ###

import heapq
class pqheap:
    def __init__(self):
        self.p = list()
        self.q = list()

    def insert(self,x):
        heapq.heappush(self.p, x)
        return

    def erase(self,x):
        heapq.heappush(self.q, x)
        return

    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]
n=int(input())
b=list(map(int,input().split()))
a=list(map(int,input().split()))
pq=pqheap()
[pq.insert([-x,i]) for i,x in enumerate(a)]
ans=0
while len(pq.p)>len(pq.q):
    m=pq.minimum()
    pq.erase(m)
    v,i=m
    v=-v
    if v==b[i]:continue
    v-=a[(i-1)%n]
    v-=a[(i+1)%n]
    if v<b[i]:
        print(-1)
        exit()
    pq.insert([-v,i])
    a[i]=v
    ans+=1
print(ans)





### FIXED CODE ###

def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)
### BUGGY CODE ###

import sys

def main():
    
    # sys.stdin = open('input_b.txt')

    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))

    dp = [0] + [float('inf')] * (n - 1)
    
    for i in range(1, n):
        for j in range(max(0, i - k), i):
            dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))

    print(dp[-1])



if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import numpy as np

def main():

    # sys.stdin = open('input_b.txt')
    input = sys.stdin.readline

    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))

    # dp = [0] + [float('inf')] * (n - 1)
    dp = np.zeros(n, dtype=int)
    h = np.array(h)


    for i in range(1, n):
        start = max(0, i-k)
        
        dp[i] = min(dp[start: i] + np.abs(h[i] - h[start : i]))

    print(dp[n-1])



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

N, M = map(int, input().split())
a = [0] * M
for i in range(M):
    a[i] = int(input())


dp = [0] * (N+1)

for i in range(N+1):
    if i == 0:
        dp[i] = 1
    elif i == 1:
        if i in a:
            dp[i] = 0
            a.remove(i)
        elif i not in a:
            dp[i] = 1
    else:
        if i in a:
            dp[i] = 0
            a.remove(i)
        else:
            dp[i] = dp[i-1] + dp[i-2]
            dp[i] = dp[i] % 1000000007

ans = dp[N]
print(ans)

### FIXED CODE ###

N, M = map(int, input().split())
a = [0] * M
for i in range(M):
    a[i] = int(input())


dp = [0] * (N+1)

for i in range(N+1):
    if i == 0:
        dp[i] = 1
    elif i == 1:
        if len(a) !=0:
            if i == a[0]:
                dp[i] = 0
                a.pop(0)

            else:
                dp[i] = 1
        else:
            dp[i] = 1
    else:
        if len(a) !=0:
            if i == a[0]:
                dp[i] = 0
                a.pop(0)

            else:
                dp[i] = dp[i-1] + dp[i-2]
                dp[i] = dp[i] % 1000000007
        else:
            dp[i] = dp[i-1] + dp[i-2]
            dp[i] = dp[i] % 1000000007

ans = dp[N]
print(ans)
### BUGGY CODE ###

N, M = [int(x) for x in input().split()]

AB = []
for i in range(M):
    a, b = [int(x) for x in input().split()]
    AB.append((a, b))

AB.sort(key=lambda x: x[1])

ans = 0
while len(AB) != 0:
    cut = AB[0][1]
    newAB = []
    ans += 1
    for a, b in AB:
        if a < cut:
            pass
        else:
            newAB.append((a, b))
    AB = newAB

print(ans)


### FIXED CODE ###

N, M = [int(x) for x in input().split()]

AB = []
for i in range(M):
    a, b = [int(x) for x in input().split()]
    AB.append((a, b))

AB.sort(key=lambda x: x[1])

ans = 0
while len(AB) != 0:
    cut = AB[0][1]
    newAB = []
    ans += 1
    for a, b in AB:
        if a < cut:
            pass
        else:
            newAB.append((a, b))
    AB = newAB

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

N, Q = map(int, input().split())
S = input()

AC_appearance = []
for i in range(len(S)-1):
    if S[i] == 'A' and S[i + 1] == 'C':
        AC_appearance.append(i)

for i in range(Q):
    l, r = map(int, input().split())
    l = l - 1
    r = r - 1
    print(str(len([x for x in AC_appearance if l <= x and x+1 <= r])))

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()

AC_appearance = [0] * len(S)
counter = 0
for i in range(1, len(S)):
    if S[i-1] == 'A' and S[i] == 'C':
        counter += 1
    AC_appearance[i] = counter 

for i in range(Q):
    l, r = map(int, input().split())
    l = l - 1
    r = r - 1
    print(AC_appearance[r] - AC_appearance[l])
### 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())
S=0

for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      S+=gcd(i,j,k)

print(S)

### FIXED CODE ###

from math import gcd 

K=int(input())
S=0

for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      S+=gcd(gcd(i,j),k)

print(S)
### BUGGY CODE ###

from collections import defaultdict
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()
q = []
for _ in range(Q):
  q.append(input().split())
q.reverse()

dp = [set() for _ in range(Q)]
for i in range(Q):
  t, d = q[i]
  if t == s[0] and d == 'L':
    dp[i].add(0)
  elif t == s[N-1] and d == 'R':
    dp[i].add(N-1)
  
  if i == 0:
    continue

  for p in dp[i-1]:
    dp[i].add(p)
    if p-1 >= 0 and t == s[p-1] and d == 'R':
      dp[i].add(p-1)
    elif p+1 <= N-1 and t == s[p+1] and d == 'L':
      dp[i].add(p+1)

print(N-len(dp[Q-1]))

### FIXED CODE ###

from collections import defaultdict
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()
q = []
for _ in range(Q):
  q.append(input().split())
q.reverse()

l = 0
r = N-1
for i in range(Q):
  t, d = q[i]
  if l-1 >= 0 and t == s[l-1] and d == 'R':
    l -= 1
  elif t == s[l] and d == 'L':
    l += 1

  if r+1 <= N-1 and t == s[r+1] and d == 'L':
    r += 1
  elif t == s[r] and d == 'R':
    r -= 1

print(max(0, r-l+1))
### BUGGY CODE ###

import sys
S = sys.stdin.readline().rstrip()
Q = int(sys.stdin.readline().rstrip())
flip = False

for i in range(Q):
    Que = sys.stdin.readline().rstrip()
    if Que == "1":
        flip = not(flip)
    else:
        Query = Que.split()
        F = Query[1]
        if (F == "1") ^ flip:
            S = "".join([Query[2], S])
        else:
            S = "".join([S, Query[2]])
print(S if not flip else S[::-1])


### FIXED CODE ###

import sys
from collections import deque

S = deque(sys.stdin.readline().rstrip())
Q = int(sys.stdin.readline().rstrip())
flip = False

for i in range(Q):
    Que = sys.stdin.readline().rstrip()
    if Que == "1":
        flip = not(flip)
    else:
        Query = Que.split()
        F = Query[1]
        if (F == "1") ^ flip:
            S.appendleft(Query[2])
        else:
            S.append(Query[2])
print("".join(S) if not flip else "".join(S)[::-1])
### BUGGY CODE ###

import itertools

n = int(input())
l = list(map(int, input().split()))

total = 0
for v in itertools.combinations(l, 3):
    for j in itertools.combinations([0, 1, 2], 3):
        a, b, c = j[0], j[1], j[2]
        if abs(v[a] - v[b]) < v[c] < (v[a] + v[b]):
            total += 1
            break

print(total)


### FIXED CODE ###

import bisect

n = int(input())
l = list(map(int, input().split()))
l.sort()

total = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        c = bisect.bisect_left(l, l[a] + l[b]) - b - 1
        total += c

print(total)
### BUGGY CODE ###

def main():
    N = int(input())
    a = []
    b = []
    for _ in range(N):
        ai, bi = map(int, input().split())
        a.append(ai)
        b.append(bi)
    ans = 0
    s = 1
    while True:
        if len(a) == 0:
            print(ans)
            exit()
        ma = max(a)
        mb = max(b)
        ans += ma * s
        if ma >= mb:
            i = a.index(ma)
        else:
            i = b.index(mb)
        a.pop(i)
        b.pop(i)
        ta = a
        a = b
        b = ta
        s *= -1


main()


### FIXED CODE ###

def main():
    N = int(input())
    ab = [[int(i) for i in input().split()] for _ in range(N)]
    ab = sorted(ab, key=lambda x: x[0] + x[1], reverse=True)
    ans = 0
    for i in range(N):
        if i % 2 == 0:
            ans += ab[i][0]
        else:
            ans -= ab[i][1]
    print(ans)


main()
### BUGGY CODE ###

import numpy as np 
n = int(input())
a = [int(input()) for i in range(n)]
aa = np.array(a)

for i in range(n):
    tmp = np.delete(aa,i)
    print(max(tmp))

### FIXED CODE ###

import numpy as np 
n = int(input())
a = [int(input()) for i in range(n)]
aa = np.array(a)
maxa = max(aa)
argmax = np.argmax(aa)

for i in range(n):
    if i == argmax:
        tmp = np.delete(aa,i)
        print(max(tmp))
    else:
        print(maxa)