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

# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

for _ in range(Q):

    q = tuple(map(str, input().split()))

    if int(q[0]) == 1:
        num_reverse += 1

    else:
        if (num_reverse + int(q[1])) % 2 == 0:
            S = S + q[2]
        else:
            S = q[2] + S

if num_reverse % 2 == 1:
    S = S[::-1]

print(S)


### FIXED CODE ###

# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

L = ''
R = ''

rev = False

for _ in range(Q):

    q = tuple(map(str, input().split()))

    if q[0] == '1':
        rev = (not rev)

    else:
        if (rev and q[1] == '1') or (not rev and q[1] == '2'):
            R = R + q[2]
        else:
            L = q[2] + L 

if rev:
    print( R[::-1] + S[::-1] + L[::-1])
else:
    print( L + S + R )
### BUGGY CODE ###

#ナップザック問題
import numpy as np

N,W=map(int,input().split())
wv = np.array([list(map(int, input().split())) for i in range(0, N)])  


dp=np.zeros((N+1,W+1),dtype=int)
for n in range(0, N):  
    for w in range(0, W + 1):  
        if w < wv[n, 0]:  
            dp[n+1, w] = dp[n, w]  
        else:  
            dp[n+1, w] = max(dp[n, w], dp[n, w - wv[n, 0]] + wv[n, 1])  
print(dp[N,W])  

### FIXED CODE ###

N,W=map(int,input().split())
DP=[[0 for j in range(W+1)]for i in range(N+1)]
for i in range(N):
    w,v=map(int,input().split())
    for j in range(1,W+1):
        if j>=w:
            DP[i+1][j]=max(DP[i][j-w]+v,DP[i][j])
        else:
            DP[i+1][j]=DP[i][j]
print(DP[N][W])
### BUGGY CODE ###

N,M = (int(i) for i in input().split())
AB = []
values = []

for i in range(N):
  A,B = (int(i) for i in input().split())
  values.extend([A]*B)

values.sort()

count = 0
ans = 0

for i in range(len(values)):
  if count < M:
    count +=1
    ans += values[i]
  else:
    break



print(ans)


### FIXED CODE ###

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

for i in range(N):
  AB.append([int(i) for i in input().split()])

AB = sorted(AB,key=lambda x:x[0])

count = 0
ans = 0

for A,B in AB:
  if count + B > M:
    ans += A*(M-count)
    break
  else:
    ans += A*B
    count += B


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

N=int(input())
a=[int(i) for i in input().split()]
ans=0
while True:
  f=0
  for i in range(N):
    if a[i]>=N:
      d=a[i]//N
      ans+=d
      a[i]%=N
      for j in range(N):
        if i!=j:
          a[j]+=d
      f=1
      break
  if f==0:
    break
print(ans)
      
  
        

### FIXED CODE ###

N=int(input())
a=[int(i) for i in input().split()]
ans=0
while True:
  f=0
  for i in range(N):
    if a[i]>=N:
      d=a[i]//N
      ans+=d
      a[i]%=N
      for j in range(N):
        if i!=j:
          a[j]+=d
    else:
      f+=1
  if f==N:
    break
print(ans)
### BUGGY CODE ###

n=int(input())
a=[]
for i in range(n):
    b=int(input())
    if b in a:
        a.remove(b)
    else:
        a.append(b)
print(len(a))

### FIXED CODE ###

from collections import Counter
n=int(input())
a=[int(input()) for i in range(n)]
ans=0
for k,v in Counter(a).items():
    if v%2!=0:
        ans+=1
print(ans)
### BUGGY CODE ###

import string
letters=string.ascii_lowercase

s = input()
K = int(input())

ans = []
for i in range(26):
    for j in range(len(s)):
        if letters[i] == s[j]:
            for k in range(1, len(s)-j+1):
                if s[j:j+k] not in ans:
                    ans.append(s[j:j+k])
    ans = sorted(set(ans))
    if len(ans) >= K:
        break

print(ans[K-1])

### FIXED CODE ###

s = input()
k = int(input())
n = len(list(s))
kouho = set()
for i in range(n):
    for d in range(1, k + 1):
        if i + d <= n:
            kouho.add(s[i : i + d])
ans = list(kouho)
ans.sort()
# print(ans)
print(ans[k - 1])
### BUGGY CODE ###

from itertools import product
H, W, K = map(int, input().split())
G = [list(map(int, list(input()))) for _ in range(H)]

ans = float("inf")

for pattern in product([0, 1], repeat = H - 1):
    div = [0] + [i for i, p in enumerate(pattern, 1) if p == 1] + [H]
    rows = []
    for i in range(len(div) - 1):
        row = []
        for j in range(W):
            tmp = 0
            for k in range(div[i], div[i + 1]):
                tmp += G[k][j]
            row.append(tmp)
        rows.append(row)
        
    flag = True
    for row in rows:
        if [r for r in row if r > K]:
            flag = False
            break
    if not flag:
        continue

    tmp_ans = 0
    cnt = [0] * len(rows)
    w = 0

    while w < W:
        for r in range(len(rows)):
            cnt[r] += rows[r][w]
        if [c for c in cnt if c > K]:
            cnt = [0] * len(rows)
            tmp_ans += 1
            w -= 1
        w += 1
    
    tmp_ans += pattern.count(1)
    ans = min(ans, tmp_ans)

print(ans)

### FIXED CODE ###

from itertools import product
H, W, K = map(int, input().split())
G = [list(map(int, list(input()))) for _ in range(H)]

ans = float("inf")

for pattern in product([0, 1], repeat = H - 1):
    div = [0] + [i for i, p in enumerate(pattern, 1) if p == 1] + [H]
    rows = []
    for i in range(len(div) - 1):
        row = []
        for j in range(W):
            tmp = 0
            for k in range(div[i], div[i + 1]):
                tmp += G[k][j]
            row.append(tmp)
        rows.append(row)
        
    flag = True
    for row in rows:
        if [r for r in row if r > K]:
            flag = False
            break
    if not flag:
        continue

    tmp_ans = 0
    cnt = [0] * len(rows)
    w = 0

    while w < W:
        for r in range(len(rows)):
            cnt[r] += rows[r][w]
        if [c for c in cnt if c > K]:
            cnt = [0] * len(rows)
            tmp_ans += 1
            w -= 1
        w += 1
    
    tmp_ans += pattern.count(1)
    ans = min(ans, tmp_ans)

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

a, b, c, d = map(int, input().split())

dp = [[1 for i in range(d+1)] for j in range(c+1)]

mod = 998244353

for y in range(c+1):
    if y < a:
        continue
    for x in range(d+1):
        if x < b:
            continue
        if y == a and x == b:
            continue
        py = 0
        px = 0
        m = 0
        if y > a:
            py = dp[y-1][x] * x
        if x > b:
            px = dp[y][x-1] * y
        if y > a and x > b:
            m = dp[y-1][x-1] * (y-1) * (x-1)
        dp[y][x] = py + px - m
        dp[y][x] %= mod


print(dp[c][d])


### FIXED CODE ###

a, b, c, d = map(int, input().split())

dp = [[1 for i in range(d+1)] for j in range(c+1)]

mod = 998244353

for y in range(c+1):
    if y < a:
        continue
    for x in range(d+1):
        if x < b:
            continue
        if y == a and x == b:
            continue
        py = 0
        px = 0
        m = 0
        if y > a:
            py = dp[y-1][x] * x
        if x > b:
            px = dp[y][x-1] * y
        if y > a and x > b:
            m = dp[y-1][x-1] * (y-1) * (x-1)
        dp[y][x] = py + px - m
        dp[y][x] %= mod


print(dp[c][d])
### BUGGY CODE ###

N, M = map(int, input().split(' '))
A = []
B = []
for n in range(N):
  A_n, B_n = map(int, input().split(' '))
  A.append(A_n)
  B.append(B_n)
  
# calc answer
drink = 0
ans = 0
while(drink < M):
  min_i = A.index(min(A))  # min shop
  if B[min_i] <= M-drink:  # buy all drinks
    drink += B[min_i]
    ans += A[min_i]*B[min_i]
    del A[min_i]
    del B[min_i]
  else:  # buy some drinks
    ans += A[min_i]*(M-drink)
    drink = M
  
print(ans)


### FIXED CODE ###

N, M = map(int, input().split(' '))
data = []
for n in range(N):
  A_n, B_n = map(int, input().split(' '))
  data.append([A_n,B_n])
data = sorted(data)
  
# calc answer
drink = 0
ans = 0
for d in data:
  a, b = d
  drink += b
  ans += a*b
  if drink > M:
    ans -= a*(drink-M)
    break
  
print(ans)
### BUGGY CODE ###

import sys
read = sys.stdin.read
def main():
    def warshall_floyd(d):
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j])
        # 更新したdを返す。d[i][j]はノードiからノードjまでの最短距離
        # 行きつけないノードはinfになる。
        # 負閉路があるなら「ノードｖからノードｖへのコスト」、つまりd[x][x]がマイナス値になる。
        return d

    # 入力
    n, x, y = map(int, input().split())
    # ワーシャルフロイドで更新する変数dをつくる
    d = [[float("inf")] * n for _ in range(n)]
    d[x - 1][y - 1] = 1
    d[y - 1][x - 1] = 1
    for i1 in range(n-1):
        d[i1][i1+1] = 1
        d[i1+1][i1] = 1
    for j1 in range(n):
        d[j1][j1] = 0

    # ワーシャルフロイド実行。
    res = warshall_floyd(d)
    r = [0] * (n - 1)
    for i1 in range(n-1):
        for i2 in range(i1 + 1, n):
            r[res[i1][i2] - 1] += 1
    print(*r, sep= '\n')

if __name__ == '__main__':
    main()

### FIXED CODE ###


import sys
input = sys.stdin.readline
from collections import deque
from collections import Counter
def main():
    # 入力
    n, x, y = map(int, input().split())
    x -= 1
    y -= 1

    # 処理
    dis = deque()
    for i1 in range(n):
        if x < i1:
            right = i1 + 1
            while right < n:
                dis_now = min(right - i1, (i1 - x + abs(right - y) + 1))
                dis.append(dis_now)
                right += 1
        else:
            right = i1 + 1
            while right < n:
                if right < y:
                    dis_now = min(right - i1,(x - i1 + abs(y - right) + 1))
                    dis.append(dis_now)
                else:
                    dis.append(right - i1 - (y - x) + 1)
                right += 1
    dis_c = Counter(dis)
    dis_list = [0] * (n - 1)
    for i2 in range(1, n):
        dis_list[i2 - 1] = dis_c[i2]

    # 出力

    for edis in dis_list:
        print(edis)

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

MOD = 10**9 + 7
N = int(input())
*A, = map(int, input().split())
ans = 0
for i in range(len(format(max(A), 'b'))):
    bit_0, bit_1 = 0, 0
    for a in A:
        if (a >> i) & 1: 
            bit_1 += 1
        else:
            bit_0 += 1
    ans += bit_1 * bit_0 * pow(2, i, MOD)
    ans %= MOD
print(ans)

### FIXED CODE ###

MOD = 10**9 + 7
N = int(input())
*A, = map(int, input().split())
ans = 0
for i in range(60):
    bit_0, bit_1 = 0, 0
    for a in A:
        if (a >> i) & 1: 
            bit_1 += 1
        else:
            bit_0 += 1
    ans += bit_1 * bit_0 * pow(2, i, MOD)
    ans %= MOD
print(ans)
### BUGGY CODE ###

import copy
n,k,*A = map(int,open(0).read().split())

for _ in range(k):
    C = [0] * n
    for i,d in enumerate(A):
        for j in range(i-d,i+d+1):
            if 0<=j<n:
                C[j] += 1
    A = copy.deepcopy(C)

    up = True
    for c in C:
        if c != n:
            up = False          
    if up:
        break

print(*C)


### FIXED CODE ###

import copy
n,k,*A = map(int,open(0).read().split())
for _ in range(min(50,k)):
    B = [0] * (n+1)
    for i,d in enumerate(A):
        start = max(0,i-d)
        B[start] += 1
        end = min(n,i+d+1)
        B[end] -= 1
    light = 0 
    for i,b in enumerate(B[:n]):
        light += b
        A[i] = light

print(*A)
### BUGGY CODE ###

import bisect
N, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
INF = 10**11+1
dp = [[-1]*(W+1) for i in range(N+1)]
dp[0][0] = 0
for i in range(1,N+1):
  w,v = A[i-1]
  for j in range(W+1):
    if dp[i-1][j] != -1:
      dp[i][j] = dp[i-1][j]
    if j >= w and dp[i-1][j-w] != -1:
      dp[i][j] = max(dp[i-1][j-w]+v,dp[i][j])
print(max(dp[-1]))

### FIXED CODE ###

N, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
dp = [[-1]*(W+1) for i in range(N+1)]
dp[0][0] = 0
for i in range(1,N+1):
  w,v = A[i-1]
  for j in range(W+1):
    dp[i][j] = dp[i-1][j]
    if j >= w and dp[i-1][j-w] != -1:
      dp[i][j] = max(dp[i-1][j-w]+v,dp[i][j])
print(max(dp[-1]))
### BUGGY CODE ###

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

A = []
for i in range(Q):
    A.append(int(input()))

#print(N,K,Q)
#print(A)

P = [K for _ in range(N)]
#print(P)
for a in A:
    for n in range(N):
        if (n==(a-1)):
            P[n] += 1

#print(P)

for n in range(N):
    if(P[n]-Q>0):
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

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

A = []
for i in range(Q):
    A.append(int(input()))

#print(N,K,Q)
#print(A)

P = [K for _ in range(N)]
#print(P)
for a in A:
    P[a-1] += 1

#print(P)

for n in range(N):
    if(P[n]-Q>0):
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

import collections
N = int(input())
s = [[collections.Counter(input())] for _ in range(N)]
ans = 0
for i in range(N):
    ans += s[:i].count(s[i])

print(ans)


### FIXED CODE ###

import collections

from operator import mul
from functools import reduce


def cmb(n, r):
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


N = int(input())
s = [" ".join(sorted(list(input()))) for _ in range(N)]
c = collections.Counter(s)
ans = 0
for i in c.values():
    if i > 1:
        ans += cmb(i, 2)
print(ans)
### BUGGY CODE ###

x=int(input())
c=1
for b in range(1,x):
    for p in range(2,x):
        if b**p<=x:c=max(c,b**p)
print(c)

### FIXED CODE ###

x=int(input())
c=1

for b in range(1,x):
  for p in range(2,x):
    if b**p<=x:c=max(c,b**p)
    else:break

print(c)
### BUGGY CODE ###

import sys
input=sys.stdin.readline
from collections import deque
#class ctmp():
#    def __init__(self,ij,dis):
#        self.ij=ij
#        self.dis=dis
H,W=map(int,input().split())
ls=["#"*(W+2)]
for i in range(H):
    ls.append("#"+input()+"#")
ls.append("#"*(W+2))
q=deque([])
qd=deque([])
#tmp=ctmp((1,1),0)
q.append((1,1))
qd.append(0)
df={}
while q:
    tmpij=q.popleft()
    tmpd=qd.popleft()
    df[tmpij]=1
    if tmpij==(H,W):
        break
    for i in range(4):
        ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
        #print(ntmpij)
        if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            #c_tmp=ctmp(ntmpij,tmp.dis+1)
            #q.append(c_tmp)
            q.append(ntmpij)
            qd.append(tmpd+1)
if tmpij==(H,W):
    nw=0
    for i in range(1,H+1):
        #for j in range(1,W+1):
         #   if ls[i][j]==".":
          #      nw+=1
        nw+=ls[i].count(".")
    print(nw-tmpd-1)
else:
    print(-1)


### FIXED CODE ###

def main():
    import sys
    input=sys.stdin.readline
    from collections import deque
    #class ctmp():
    #    def __init__(self,ij,dis):
    #        self.ij=ij
    #        self.dis=dis
    H,W=map(int,input().split())
    ls=["#"*(W+2)]#
    for i in range(H):
        ls.append("#"+input()+"#")
    ls.append("#"*(W+2))#
    q=deque([])
    #qd=deque([])
    #tmp=ctmp((1,1),0)
    q.append((1,1))
    #qd.append(0)
    #df={}
    lf=[[float("inf") for _ in range(W+2)] for _ in range(H+2)]#
    #これでfとdの両方の役割をこなす
    lf[1][1]=0
    while q:
        tmpij=q.popleft()
        #tmpd=qd.popleft()
        #df[tmpij]=1
        if tmpij==(H,W):
            break
        lind=[(-1,0),(1,0),(0,-1),(0,1)]#
        for i in range(4):
            #ntmpij=(tmpij[0]+(i<2)*(2*i-1),tmpij[1]+(i>1)*(2*i-5))
            ntmpij=(tmpij[0]+lind[i][0],tmpij[1]+lind[i][1])
            #print(ntmpij)
            #if ntmpij not in df and ls[ntmpij[0]][ntmpij[1]]==".":
            if lf[ntmpij[0]][ntmpij[1]]==float("inf") and ls[ntmpij[0]][ntmpij[1]]==".":
                #c_tmp=ctmp(ntmpij,tmp.dis+1)
                #q.append(c_tmp)
                q.append(ntmpij)
                #qd.append(tmpd+1)
                lf[ntmpij[0]][ntmpij[1]]=lf[tmpij[0]][tmpij[1]]+1
                #dは訪れる前に書き込んでしまう
    if tmpij==(H,W):
        nw=0
        for i in range(1,H+1):
            #for j in range(1,W+1):
             #   if ls[i][j]==".":
              #      nw+=1
            nw+=ls[i].count(".")
        #print(nw-tmpd-1)
        print(nw-lf[H][W]-1)
    else:
        print(-1)

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

import numpy as np

def warshall_floyd(d):
  n = len(d)
  for i in range(n):
    for j in range(n):
      for k in range(n):
        d[j, k] = min(d[j, k], d[j, i]+d[k, i])
  
  return d


N, M, L = map(int, input().split())
d = np.ones([N, N]) * np.inf

for _ in range(M):
  a, b, c = map(int, input().split())
  d[a-1, b-1] = c
  d[b-1, a-1] = c
  
for i in range(N):
  d[i, i] = 0

d = warshall_floyd(d)
d2 = (d <= L)*1.0
d2[d2 == 0] = np.inf
d2 = warshall_floyd(d2)

Q = int(input())
for _ in range(Q):
  s, t = map(int, input().split())
  out = d2[s-1, t-1]
  print(-1 if out == np.inf else int(out-1))
  

### FIXED CODE ###

from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())

D = np.ones([N, N]) * np.inf
D[np.eye(N, dtype=bool)] = 0

for _ in range(M):
  A, B, C = map(int, input().split())
  D[A-1, B-1] = C
  D[B-1, A-1] = C

D = floyd_warshall(D)
D2 = D <= L

D2 = floyd_warshall(D2)

Q = int(input())
for _ in range(Q):
  s, t = map(int, input().split())
  r = D2[s-1, t-1] - 1
  if r < np.inf:
    print(int(r))
  else:
    print(-1)
### BUGGY CODE ###

import sys
from operator import itemgetter
inputs = sys.stdin.readline
N, Q= map(int,inputs().split())
def main():
	E = [None]*2*N
	for n in range(N):
		a,b,c = map(int,inputs().split())
		E[2*n] = (a - c, 1, c)
		E[2*n + 1] = (b - c, -1, c)
	E.sort(key=itemgetter(0))
	tuple(tuple(E))
	mini = 1000000001
	kouho = {1000000001}
	my_add = kouho.add
	my_discard = kouho.discard
	q = 0
	d = int(inputs())
	flag = False
	cont = False
	for e in E:
		while(e[0] > d):
			if cont == True:
				if mini < 1000000001:
					print(mini)
				else:
					print(-1)
			else:
				mini = min(kouho)
				if mini < 1000000001:
					print(mini)
				else:
					print(-1)
			cont = True
			if q < Q-1:
				q = q + 1
				d = int(inputs())
			else:
				flag = True
				q = q + 1
				break
		if flag == True:
			break
		if e[1] == 1:
			my_add(e[2])
			if mini > e[2]:
				mini = e[2]
		elif e[1] == -1:
			my_discard(e[2])
			cont = False
	for t in [None]*(Q-q):
		print(-1)
		
def sub():
	V = [list(map(int,inputs().split())) for p in [0]*N]
	M = [int(inputs()) for p in [0]*Q]
	V.sort(key = lambda x:(x[2],x[0],x[1]))
	for v in V:
		v[0] -= v[2]
		v[1] -= v[2] + 1
	for m in M:
		for n in range(N):
			if V[n][0] <=m and m <= V[n][1]:
				print(V[n][2])
				break
		else:
			print(-1)


if N>199998:
	sub()
else:
	main()

### FIXED CODE ###

import sys
from operator import itemgetter
def main():
	inputs = sys.stdin.readline
	N, Q= map(int,inputs().split())
	E = [None]*2*N
	for n in range(N):
		a,b,c = map(int,inputs().split())
		E[2*n] = (a - c, 1, c)
		E[2*n + 1] = (b - c, -1, c)
	E.sort(key=itemgetter(0))
	tuple(tuple(E))
	mini = 1000000001
	kouho = {1000000001}
	my_add = kouho.add
	my_discard = kouho.discard
	q = 0
	d = int(inputs())
	flag = False
	cont = False
	for e in E:
		while e[0] > d:
			if cont == True:
				print(mini-(mini//1000000001)*1000000002)
			else:
				mini = min(kouho)
				print(mini-(mini//1000000001)*1000000002)
			cont = True
			q = q + 1
			if q > Q-1:
				flag = True
				break
			d = int(inputs())
		if flag == True:
			break
		if e[1] == 1:
			my_add(e[2])
			if mini > e[2]:
				mini = e[2]
		elif e[1] == -1:
			my_discard(e[2])
			if mini == e[2]:
				cont = False
	for t in [None]*(Q-q):
		print(-1)
main()
### BUGGY CODE ###

n, p = map(int, input().split())
if n == 1:
    print(p)
else:
    ans = 1
    t = True
    for i in range(int(p ** (1 / n)) + 1, 2, -1):
        if p % (i ** n) == 0:
            ans = i
            t = False
            print(ans)
            break
    if t:
        if p % (2 ** n) == 0:
            print(2)
        else:
            print(1)




### FIXED CODE ###

import collections

def prime_return(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

n, p = map(int, input().split())
t = collections.Counter(prime_return(p))
ans = 1
for k, v in t.items():
    if v >= n:
        ans *= (k ** (v // n))
print(ans)
### BUGGY CODE ###

#python 3.7.1

a, b, c, d=map(int, input().split())

cnt=0

while(d!=0):

  

  if(a!=0 and d>0):

    d-=1

    a=a-1

    

    cnt=cnt+1

    

  elif b!=0 and d>0:

    d-=1

    b=b-1

    

    

  elif c!=0 and d>0:

    d-=1

    c=c-1

    

    cnt=cnt-1

    

print(cnt)

  



### FIXED CODE ###

#python 3.7.1

a, b, c, d=map(int, input().split())

k=d

if(a-k>0):

  print(k)

else:

  k=d-a

  if(b-k>0):

    print(a)

    

  else:

    k=d-a-b

    print(a-k)
### BUGGY CODE ###

n = int(input())
L, R = [0] * n, [0] * n
i = 0
for ipt in input().split():
    L[i] = i + int(ipt)
    R[i] = i - int(ipt)
    i += 1
def make_dic(L):
    L_set = set(L)
    return dict([(i, L.count(i)) for i in L_set])
L_dic, R_dic = make_dic(L), make_dic(R)
cnt = 0
for Lk, Lv in L_dic.items():
    try:
        cnt += R_dic[Lk] * Lv
    except KeyError:
        pass
print(cnt)

### FIXED CODE ###

n = int(input())
L_dict, R_dict = {}, {}
i = 0
for ipt in input().split():
    try:
        L_dict[i + int(ipt)] += 1
    except KeyError:
        L_dict[i + int(ipt)] = 1
    try:
        R_dict[i - int(ipt)] += 1
    except KeyError:
        R_dict[i - int(ipt)] = 1
    i += 1
cnt = 0
for Lk, Lv in L_dict.items():
    try:
        cnt += R_dict[Lk] * Lv
    except KeyError:
        pass
print(cnt)
### BUGGY CODE ###

N=int(input())
S=input()

pin={}

for i in range(10):
  for j in range(10):
    for k in range(10):
     pin['{}{}{}'.format(i,j,k)]='{}{}{}'.format(i,j,k)

ans=0
for key in pin:
  s=pin[key]
  isOne=0
  isTen=0
  isHundred=0
  for i in range(N) :
    if S[i]==s[0] and isOne==0:
      isOne=1
    elif S[i]==s[1] and isOne==1 and isTen==0:
      isTen=1
    elif S[i]==s[2] and isOne==1 and isTen==1 and isHundred==0:
      isHundred=1
  if isHundred==1:
    ans+=1

print(ans)

### FIXED CODE ###

N=int(input())
S=input()
S=list(map(int,S))
ans=0
for s in range(1000):
      A=s//100
      B=(s-A*100)//10
      C=s-A*100-B*10
      isOne=0
      isTen=0
      isHundred=0
      for i in range(N) :
        if isOne==0:
          if S[i]==A: 
            isOne=1
        elif isTen==0:
          if S[i]==B: 
            isTen=1
        elif isHundred==0:
          if S[i]==C:
            isHundred=1
            ans+=1
        else:
          break

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

N=int(input())
A = list(map(int,input().split()))
mod = 10**9+7
Ans = 0
for i in range(60):
  Po=0
  for j in A:
    if j>>i & 1:
      Po+=1
  Ans += Po*(N-Po)*(2**i)
  Ans = Ans % mod
print(Ans)

### FIXED CODE ###

N=int(input())
A = list(map(int,input().split()))
mod = 10**9+7
Ans = 0
for i in range(60):
  Po=sum([1 for j in A if j>>i & 1])
  Ans += Po*(N-Po)*(2**i)
  Ans = Ans % mod
print(Ans)
### BUGGY CODE ###

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

result = abs(a[0]-sum(a[1:]))
for i in range(1,N):
    result = min(result, abs(sum(a[:i]) - sum(a[i:])))

print(result)

### FIXED CODE ###

N = int(input())

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

sum = 0
for a_i in a:
    sum+=a_i

min = abs(a[0]-(sum-a[0]))
x=a[0]
for a_i in a[1:-1]:
    x +=a_i
    if (min>abs(x-(sum-x))):
        min=abs(x-(sum-x))

print(min)
### BUGGY CODE ###

import heapq

N, M = map(int, input().split())
jobs = [[] for i in range(M)]

for i in range(N):
    A, B = map(int, input().split())
    if A - 1 < M:
        jobs[A-1].append(B)

q = []
ans = 0
for i in range(M):
    for j in jobs[i]:
        q.append(-j)
    heapq.heapify(q)
    if len(q) != 0:
        ans += -heapq.heappop(q)

print(ans)

### FIXED CODE ###

import heapq

N, M = map(int, input().split())
jobs = [[] for i in range(M)]

for i in range(N):
    A, B = map(int, input().split())
    if A - 1 < M:
        jobs[A-1].append(B)

q = []
heapq.heapify(q)
ans = 0
for i in range(M):
    for j in jobs[i]:
        heapq.heappush(q, -j)
    if len(q) != 0:
        ans += -heapq.heappop(q)

print(ans)