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

import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*m
c = [0]*m
sortsecond = lambda val: val[1]

x = [list(map(int, input().split())) for _ in range(m)]

x.sort(key=sortsecond)

a.sort(reverse=True)

for j in range(m):
    
    times = x[-(j+1)][0]
    number = x[-(j+1)][1]
    while times > 0 :
        if min(a) < number:
            a[a.index(min(a))] = number
        else:
            break
        times -= 1


print(sum(a))

### FIXED CODE ###

import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*m
c = [0]*m
sortsecond = lambda val: val[1]

x = [list(map(int, input().split())) for _ in range(m)]

x.sort(key=sortsecond)

a.sort()


i = 0
for j in range(m):
    times = x[-(j+1)][0]
    number = x[-(j+1)][1]
    while times > 0 :

        try:
            temp = a[i]
        except IndexError:
            print(sum(a))
            exit()
        
        if temp < number:
            a[i] = number
        else:
            print(sum(a))
            exit()
        times -= 1
        i += 1
print(sum(a))
### BUGGY CODE ###

h,n=map(int,input().split())
ma=[]
for _ in range(n):
  a=list(map(int,input().split()))
  ma.append(a)
  
inf=float('inf')
dp=[inf for _ in range(h+1) ]

dp[h]=0

for i in range(1,h+1):
  j=h+1-i
  for x in ma:
    p=x[0]
    q=x[1]
    if j-p>=1:
      dp[j-p]=min(dp[j-p],dp[j]+q)
    else:
      dp[0]=min(dp[0],dp[j]+q)
      
print(dp[0])
    

### FIXED CODE ###

h,n=map(int,input().split())
ma=[]
for _ in range(n):
  a=list(map(int,input().split()))
  ma.append(a)
  
inf=float('inf')
dp=[inf for _ in range(h+1) ]

dp[h]=0

for i in range(1,h+1):
  j=h+1-i
  for x in ma:
    p=x[0]
    q=x[1]
    if j-p>=1:
      dp[j-p]=min(dp[j-p],dp[j]+q)
    else:
      dp[0]=min(dp[0],dp[j]+q)
      
print(dp[0])
### BUGGY CODE ###

def main():
    N = int(input())
    S = input()

    ans = 0
    for k in range(N):
        for j in range(k):
            for i in range(j):
                if j-i != k-j:
                    if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                        ans+=1
    print(ans)
main()

### FIXED CODE ###

def main():
    N = int(input())
    S = input()

    ans = S.count('R') * S.count('G') * S.count('B')

    for i in range(N):
        for j in range(i+1, N):
            k = j + j - i
            if k < N:
                if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                    ans-=1
    print(ans)
main()
### BUGGY CODE ###


N = int(input())
A = list(map(int, input().split()))[::-1]

ans = []

for i in range(N):
  j = N - i
  cnt = 0 

  for m in [n for n in range(1, N+1) if (n % j == 0) and (n != j) ]:
    cnt += A[N - m]
  
  if (cnt + A[i]) % 2 == 1:
    ans.append(j)

len_ans = len(ans)
if len_ans == 0:
  print(0)
else:
  print(len_ans)
  print(*ans)

### FIXED CODE ###


N = int(input())
A = list(map(int, input().split()))
B = set()

for i in list(range(1, N+1))[::-1]:
  tmp = i
  cnt = 0
  while tmp <= N:
    if tmp in B:
      cnt += 1    
    tmp += i

  if cnt % 2 != A[i-1]:
    B.add(i)

len_b = len(B)
if len_b == 0:
  print(0)
else:
  print(len_b)
  print(*sorted(list(B)))
### BUGGY CODE ###

from collections import deque
N = int(input())
A = [list(map(lambda x:int(x)-1, input().split()))[::-1] for _ in range(N)]
q = deque([])

# 選手iが試合ができるかをチェックして、できるならばキューに入れる
def check(i, q):
  if len(A[i]) == 0:
    return
  j = A[i][-1]
  if len(A[j]) == 0:
    return
  if A[j][-1] == i:
    if i > j:
      i, j = j, i
    q.append((i, j))
  return 

for i in range(N):
  check(i, q)

day = 0
while q:
  day += 1
  q = set(sorted(q))
  prev = deque([])
  prev, q = q, prev
  for i, j in prev:
    A[i].pop(-1)
    A[j].pop(-1)
  for i, j in prev:
    check(i, q)
    check(j, q)
  
for i in range(N):
  if len(A[i]) != 0:
    print(-1)
    exit()

print(day)



### FIXED CODE ###

from collections import deque
N = int(input())
A = [list(map(lambda x:int(x)-1, input().split()))[::-1] for _ in range(N)]
q = deque([])

# 選手iが試合ができるかをチェックして、できるならばキューに入れる
def check(i, q):
  if len(A[i]) == 0:
    return
  j = A[i][-1]
  if len(A[j]) == 0:
    return
  if A[j][-1] == i:
    if i > j:
      i, j = j, i
    q.append((i, j))
  return 

for i in range(N):
  check(i, q)

day = 0
while q:
  day += 1
  q = set(sorted(q))
  prev = deque([])
  prev, q = q, prev
  for i, j in prev:
    A[i].pop(-1)
    A[j].pop(-1)
  for i, j in prev:
    check(i, q)
    check(j, q)
  
for i in range(N):
  if len(A[i]) != 0:
    print(-1)
    exit()

print(day)
### BUGGY CODE ###

n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(list(input()))))

b = [False] * n
ans = 0
count = 1

for i in range(n):
    if b[i]:
        continue
    for j in range(i + 1, n):
        if s[i] == s[j]:
            count += 1
            b[j] = True
    ans += int(count * (count - 1) / 2)
    count = 1
    b[i] = True

print(int(ans))

### FIXED CODE ###

n = int(input())
s = []
for i in range(n):
    s.append(''.join(sorted(list(input()))))
s.sort()

ans = 0
count = 1
s_pre = s[0]

for i in range(1, n):
    if s_pre == s[i]:
        count += 1
    else:
        ans += int(count * (count - 1) / 2)
        s_pre = s[i]
        count = 1

ans += int(count * (count - 1) / 2)
print(int(ans))
### BUGGY CODE ###

N = int(input())
S = str(input())
#N = 13
#S = "strangeorange"

lb = 0
ub = N //2 + 1
flag = False
while (ub - lb > 1):
    mid = (lb + ub) // 2
    s = []
    for i in range(N - 2*mid + 1):
        s.append(S[i:mid+i])
        if S[mid+i:i+2*mid] in s:
            flag = True
            break
        else:
            flag = False
    if flag == True:
        lb = mid
    else:
        ub = mid
print(lb)

### FIXED CODE ###

N = int(input())
S = str(input())
#N = 13
#S = "strangeorange"

lb = 0
ub = N //2 + 1
flag = False
while (ub - lb > 1):
    mid = (lb + ub) // 2
    s = set()
    for i in range(N - 2*mid + 1):
        s.add(S[i:mid+i])
        if S[mid+i:i+2*mid] in s:
            flag = True
            break
        else:
            flag = False
    if flag == True:
        lb = mid
    else:
        ub = mid
print(lb)
### BUGGY CODE ###

from collections import Counter
N = int(input())
S = input()
tmp = Counter(S)
# print(tmp)
r = tmp["R"]
g = tmp["G"]
b = tmp["B"]
ans = 0
# for s in S:
#     if s == "R":
#         r += 1
#     elif s == "G":
#         g += 1
#     else:
#         b += 1

for i in range(len(S) - 2):
    j = 1
    if S[i] == "R":
        ans += g * b
        r -= 1
    elif S[i] == "G":
        ans += r * b
        g -= 1
    else:
        ans += r * g
        b -= 1
    
    while not(i + j +j >= len(S)):
        if S[i] == "R":
            if S[i + j] == "G" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "G":
                ans -= 1
        if S[i] == "G":
            if S[i + j] == "R" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "R":
                ans -= 1
        if S[i] == "B":
            if S[i + j] == "G" and S[i + j + j] == "R" or S[i + j] == "R" and S[i + j + j] == "G":
                ans -= 1
        j += 1
print(ans)


### FIXED CODE ###

from collections import Counter
N = int(input())
S = input()
tmp = Counter(S)
# print(tmp)
r = tmp["R"]
g = tmp["G"]
b = tmp["B"]
ans = 0
# for s in S:
#     if s == "R":
#         r += 1
#     elif s == "G":
#         g += 1
#     else:
#         b += 1

for i in range(len(S) - 2):
    j = 1
    if S[i] == "R":
        ans += g * b
        r -= 1
    elif S[i] == "G":
        ans += r * b
        g -= 1
    else:
        ans += r * g
        b -= 1
    
    while not (i + j + j >= len(S)):
        if S[i] == "R":
            if S[i + j] == "R":
                j += 1
                continue
            if S[i + j] == "G" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "G":
                ans -= 1
        if S[i] == "G":
            if S[i + j] == "G":
                j += 1
                continue
            if S[i + j] == "R" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "R":
                ans -= 1
        if S[i] == "B":
            if S[i + j] == "B":
                j += 1
                continue
            if S[i + j] == "G" and S[i + j + j] == "R" or S[i + j] == "R" and S[i + j + j] == "G":
                ans -= 1
        j += 1
print(ans)
### BUGGY CODE ###

#import numpy as np
N, K= map(int,input().split())
A = list(map(int,input().split()))
p=0
while p<K:
  B=[0]*(N+1)
  for i in range(N):
    t = A[i]
    B[max(i-t,0)] +=1
    B[min(i+t+1,N)] -=1
  for i in range(N):
    B[i+1] += B[i]
    A=B[:N]
  p +=1        
  if min(A)==N:
    break
print(*A)

### FIXED CODE ###

N, K= map(int,input().split())
A = list(map(int,input().split()))
a=A[:]
for _ in range(K):
  B=[0]*(N+1)
  for i in range(N):
    t = a[i]
    B[max(i-t,0)] +=1
    B[min(i+t+1,N)] -=1
  for i in range(N):
    B[i+1] += B[i]
  a=B[:N]        
  if min(a)==N:
    break
print(*a)
### BUGGY CODE ###

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

bmax = max(A)
bmin = min(A)-N

def getSadNess(A,b):
    return sum([abs(A[i]-(b+i+1)) for i in range(N)]) 

def binSearch(A,bmin,bmax):
    b = (bmin+bmax)//2
    sadb = getSadNess(A,b)
    sadm = getSadNess(A,b-1)
    sadp = getSadNess(A,b+1)
    if sadb<=sadp and sadb<=sadm:
        return sadb
    elif sadb>sadm:
        return binSearch(A,bmin,b)
    else:
        return binSearch(A,b,bmax)
    
print(binSearch(A,bmin,bmax))

### FIXED CODE ###

N = int(input())
A = [int(i) for i in input().split()]
B = [A[i]-i-1 for i in range(N)]
B.sort()
b = N//2
ans = sum([abs(B[i]-B[b]) for i in range(N)])
print(ans)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
A_ = sorted(A)
for i in range(N):
  A_[i] = (str(A.index(A_[i])+1))
print(' '.join(A_))

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = ['']*N
for id,name in enumerate(A):
  ans[name-1] = str(id+1)
print(' '.join(ans))
### BUGGY CODE ###

A, B, C, D = map(int, input().split())
cnt = 0
for i in range(A, B+1):
    if i%C!=0 and i%D!=0:
        cnt += 1
        
print(cnt)
        

### FIXED CODE ###

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)

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

b_num = B//C + B//D - B//lcm(C, D)
a_num = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)

print((B-A+1) -(b_num - a_num))
### BUGGY CODE ###

import sys
from collections import deque
import fractions
from functools import reduce
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

from collections import deque


def bfs(sx, sy, gx, gy, H, W):
    d = [[-1 for i in range(W)] for j in range(H)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sy, sx))
    d[sy][sx] = 0

    while que:
        p = que.popleft()
        if p[0] == gy and p[1] == gx:
            break
        for i in range(4):
            ny = p[0] + dy[i]
            nx = p[1] + dx[i]
            #print(nx, ny)
            if 0 <= nx < W and 0 <= ny < H and maze[ny][nx] != "#" and d[ny][nx] == -1:
                que.append((ny, nx))
                #print(p[0], p[1], ny, nx)
                d[ny][nx] = d[p[0]][p[1]] + 1
    return d[gy][gx]


H, W = na()
ans = 0
maze = [input() for i in range(H)]
for sy in range(H):
    for sx in range(W):
        if maze[sy][sx] == '#':
            continue
        for gy in range(H):
            for gx in range(W):
                if maze[gy][gx] == '#':
                    continue
                #print(sx, sy, gx, gy)
                ans = max(ans, bfs(sx, sy, gx, gy, H, W))
                #print(ans)
print(ans)


### FIXED CODE ###

from collections import deque
H, W = map(int, input().split())
s = ['#'*(W+2)] + ['#'+input()+'#' for _ in range(H)] + ['#'*(W+2)]
ans = 0

for y in range(1, H+1):
    for x in range(1, W+1):
        if s[y][x] == '#':
            continue
        visited = [[0]*(W+2) for _ in range(H+2)]
        visited[y][x] = 1
        max_s = 0
        dq = deque([(y, x, 0)])

        while dq:
            cy, cx, steps = dq.popleft()
            if steps > max_s:
                max_s = steps
            steps += 1

            for dy, dx in ((cy+1, cx), (cy-1, cx), (cy, cx+1), (cy, cx-1)):
                if s[dy][dx] == '#' or visited[dy][dx]:
                    continue
                visited[dy][dx] = 1
                dq.append((dy, dx, steps))

        ans = max(ans, max_s)

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

import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

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

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = ""
right = ""
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right += c
            else:
                left += c
        elif f == 2:
            if is_reversed:
                left += c
            else:
                right += c
s = left[::-1] + s + right
if is_reversed:
    print(s[::-1])
else:
    print(s)

### FIXED CODE ###

import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

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

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = []
right = [] 
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right.append(c)
            else:
                left.append(c)
        elif f == 2:
            if is_reversed:
                left.append(c)
            else:
                right.append(c)

s = "".join(reversed(left)) + s + "".join(right)
if is_reversed:
    print(s[::-1])
else:
    print(s)
### BUGGY CODE ###

from collections import deque

n, m = map(int, input().split())

s = input()

def bfs(src):
	que = deque([[src, []]])
	visited = {src, }

	while que:
		u, path = que.popleft()
		if u == n:
			return path
		for i in range(1, m+1):
			next_pos = u + i
			if next_pos not in visited and next_pos <= n and s[next_pos]=='0':
				visited.add(next_pos)
				que.append((next_pos, path+[i]))
	return -1

ans = bfs(0)
if ans == -1:
	print(ans)
else:
	print(*ans)

### FIXED CODE ###

N, M = map(int, input().split())
S = input().strip()
T = S[::-1]

l = []
i = 0
while i < N:
  t = T[i+1: i+M+1][::-1]
  if '0' not in t:
    print(-1)
    exit()
  else:
    x = min(M - t.index('0'), len(t))
    l.append(x)
    i += x
print(*l[::-1])
### BUGGY CODE ###

n,Q=map(int,input().split())
g=[[] for i in range(n)]
for _ in range(n-1):
  a,b=map(int,input().split())
  g[a-1].append(b-1)
  g[b-1].append(a-1)
depth=[-1]*n
depth[0]=0
q=[0]
while q:
  x=q.pop()
  for t in g[x]:
    if depth[t]==-1:
      depth[t]=depth[x]+1
      q.append(t)
cnt=[0]*n
for _ in range(Q):
  p,x=map(int,input().split())
  q=[p-1]
  while q:
    y=q.pop()
    cnt[y]+=x
    for t in g[y]:
      if depth[t]>depth[y]:
        q.append(t)
print(*cnt)

### FIXED CODE ###

n,Q=map(int,input().split())
g=[[] for i in range(n)]
for _ in range(n-1):
  a,b=map(int,input().split())
  g[a-1].append(b-1)
  g[b-1].append(a-1)
depth=[-1]*n
depth[0]=0
q=[0]
while q:
  x=q.pop()
  for t in g[x]:
    if depth[t]==-1:
      depth[t]=depth[x]+1
      q.append(t)
cnt=[0]*n
for _ in range(Q):
  p,x=map(int,input().split())
  cnt[p-1]+=x
q=[0]
visited=[False]*n
while q:
  x=q.pop()
  visited[x]=True
  for t in g[x]:
    if not visited[t]:
      cnt[t]+=cnt[x]
      q.append(t)
print(*cnt)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
a = list(map(int,input().split()))
ls = [0 for i in range(n*n)]
for i in range(n):
  for j in range(n):
    ls[i*n+j] = min(i,j,n-1-i,n-1-j)
ans = 0
st = set()
for i in a:
  x,y = divmod(i-1,n)
  ans += ls[i-1]
  st.add((x,y))
  q = deque([i-1])
  while q:
    j = q.popleft()
    px,py = divmod(j,n)
    for nx,ny in (px-1,py),(px+1,py),(px,py+1),(px,py-1):
      if 0 <= nx < n and 0 <= ny < n:
        if (px,py) in st:
          if ls[px*n+py] < ls[nx*n+ny]:
            ls[nx*n+ny] = ls[px*n+py]
            q.append(nx*n+ny)
        elif ls[px*n+py]+1 < ls[nx*n+ny]:
          ls[nx*n+ny] = ls[px*n+py]+1
          q.append(nx*n+ny)
print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
a = list(map(int,input().split()))
n += 2
ls = [0 for i in range(n*n)]
for i in range(n):
  for j in range(n):
    ls[i*n+j] = min(i,j,n-1-i,n-1-j)-1
ans = 0
st = [0]*(n*n)
for i in a:
  x,y = divmod(i-1,n-2)
  i = (x+1)*n+(y+1)
  ans += ls[i]
  st[i]=1
  q = deque([i])
  while q:
    j = q.popleft()
    for k in (j-n,j+n,j+1,j-1):
      if st[j]:
        if ls[j] < ls[k]:
          ls[k] = ls[j]
          q.append(k)
      elif ls[j]+1 < ls[k]:
        ls[k] = ls[j]+1
        q.append(k)
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from collections import Counter
# input
n = int(input())
s = []
for i in range(n):
    s.append(input())
c = Counter(s)
# output
#ma = c.most_common()[0][1]
o = list(k for k, v in c.items() if v == c.most_common()[0][1])
#o = [k for k, v in c.items() if v == ma]
o.sort()
print('\n'.join(o))


### FIXED CODE ###

# -*- coding: utf-8 -*-
from collections import Counter
# input
n = int(input())
s = []
for i in range(n):
    s.append(input())
c = Counter(s)
# output
ma = c.most_common()[0][1]
o = [k for k, v in c.items() if v == ma]
o.sort()
print('\n'.join(o))
### BUGGY CODE ###

N,Q=map(int,input().split())
S=list(input())
count_list=[]

flag=0
for i in range((len(S)+1)//2):
    if S[i*2]=='A':
        if i*2!=len(S)-1:
            if S[(i*2)+1]=='C': count_list.append(i*2+1)
    elif S[i*2]=='C':
        if i*2!=0:
            if S[(i*2)-1]=='A': count_list.append(i*2)

for i in range(Q):
    l,r=map(int,input().split())
    l,r=l-1,r-1 #リストのインデックスとの整合
    n=sum(l+1<=x<= r for x in count_list)
    print(max(n,0))

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
    t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])
### BUGGY CODE ###

from collections import Counter

n = int(input())
s = input()
q = int(input())
Q = [list(map(str,input().split())) for _ in range(q)]

for x,y,z in Q:
    if int(x) == 1:
        s = s[:int(y)-1]+z+s[int(y):]
    else:
        c = Counter(s[int(y)-1:int(z)])
        print(len(c.keys()))

### FIXED CODE ###

import bisect

n = int(input())
s = list(input())
q = int(input())

alpha_map = {chr(i):[] for i in range(97,123)}
for i,c in enumerate(s):
    bisect.insort(alpha_map[c],i+1)
    
for _ in range(q):
    x,y,z = map(str,input().split())
    if int(x) == 1:
        y = int(y)
        bef = s[y-1]
        if bef == z:
            continue
        alpha_map[bef].pop(bisect.bisect_left(alpha_map[bef],y))
        bisect.insort(alpha_map[z],y)
        s[y-1] = z
    else:
        count = 0
        for i,j in alpha_map.items():
            idx = bisect.bisect_left(j,int(y))
            if idx < len(j) and j[idx] <= int(z):
                    count += 1
        print(count)
### BUGGY CODE ###

x, y, z, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
t = []
for i in set(range(x)):
    for j in set(range(y)):
        for l in set(range(z)):
            if (i+1) * (j+1) * (l+1) <= k:
                t.append(a[i]+b[j]+c[l])
            else:
                break
t.sort(reverse=True)
for i in range(k):
    print(t[i])

### FIXED CODE ###

x,y,z,K=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans=[]
for i in range(x):
  for j in range(y):
    for k in range(z):
      if (i+1)*(j+1)*(k+1)<=K:
        ans.append(a[i]+b[j]+c[k])
      else:
        break
ans.sort(reverse=True)
for i in range(K):
  print(ans[i])
### BUGGY CODE ###

cnt = 0
N = int(input())
A = list(map(int, input().split()))
add_list = [A[i] + i for i in range(N)]
minus_list = [j - A[j] for j in range(N)]
for x in add_list:
    cnt += minus_list.count(x)
print(cnt)

### FIXED CODE ###

from collections import Counter
cnt = 0
N = int(input())
A = list(map(int, input().split()))
minus_list = [j - A[j] for j in range(N)]
counter = Counter(minus_list)
for i in range(N):
    add = A[i] + i
    cnt += counter[add]
print(cnt)
### BUGGY CODE ###

from collections import deque
import math,itertools,fractions,heapq,bisect,sys,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  field=[list(S()) for _ in range(h)]

  ans=0
  visited=[[-1 for __ in range(w)] for _ in range(h)]
  q=deque()
  for i in range(h):
    for j in range(w):
      if field[i][j]=='#':
        continue
      q.append((i,j,0))
      for _i in range(h):
        for _j in range(w):
          visited[_i][_j]=-1
      while len(q)>0:
        y,x,c=q.popleft()
        visited[y][x]=c

        for dy,dx in ((-1,0),(0,1),(1,0),(0,-1)):
          ny=y+dy
          nx=x+dx
          if 0<=ny<h and 0<=nx<w:
            if field[ny][nx]=='#':
              continue
            if visited[ny][nx]!=-1:
              continue
            q.append((ny,nx,c+1))
      ans=max(ans,c)
  return ans

# main()
print(main())


### FIXED CODE ###

import sys,collections

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  field=[S() for _ in range(h)]

  ans=0
  visited=[[-1 for __ in range(w)] for _ in range(h)]
  q=collections.deque()
  for i in range(h):
    for j in range(w):
      if field[i][j]=='#':
        continue
      q.append((i,j))
      for _i in range(h):
        for _j in range(w):
          visited[_i][_j]=-1
      visited[i][j]=0
      _ans=0
      while q:
        y,x=q.popleft()

        for dy,dx in ((-1,0),(0,1),(1,0),(0,-1)):
          ny=y+dy
          nx=x+dx
          if not 0<=ny<h:
            continue
          if not 0<=nx<w:
            continue
          if field[ny][nx]=='#':
            continue
          if visited[ny][nx]!=-1:
            continue
          _ans=visited[y][x]+1
          visited[ny][nx]=_ans
          q.append((ny,nx))
      ans=max(ans,_ans)
  return ans

# main()
print(main())
### BUGGY CODE ###

s = list(input())
i,cnt = 1,0
while i!=len(s) and len(s)!=0:
    if s[i-1] != s[i]:
        del s[i]
        del s[i-1]
        i = 1
        cnt += 1
    else:
        i += 1
print(cnt*2)

### FIXED CODE ###

s = list(input())
n_1 = s.count('1')
n_0 = s.count('0')
print(min(n_0,n_1)*2)