Problem:


Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5

Sample Output 3

22



Sample input:
6
2 3 3 1 3 1


Sample output:
3

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

def abc166_e():
    N = int(readline())
    A = np.array(readline().split(), dtype=np.int64)

    # indexの差の絶対値
    idx = np.arange(1, N+1, dtype=np.int64) - np.arange(1, N+1, dtype=np.int64).reshape(-1, 1)
    np.absolute(idx, out=idx)
    # 値の和
    valsum = A + A.reshape(-1, 1)

    # 差をとって0なら等しい
    diff = idx - valsum
    ans = N*N - np.count_nonzero(diff)
    ans //= 2  # 同じ組合せが2回数えられているので半分にする
    print(ans)

abc166_e()

Submission status: Runtime Error

Solution code:
import numpy as np

def abc166_e():
    N = int(readline())
    A = np.array(readline().split(), dtype=np.int64)

    # indexの差の絶対値
    idx = np.arange(1, N+1, dtype=np.int64) - np.arange(1, N+1, dtype=np.int64).reshape(-1, 1)
    np.absolute(idx, out=idx)
    # 値の和
    valsum = A + A.reshape(-1, 1)

    # 差をとって0なら等しい
    # 対角成分はidxが0でvalsumが2以上なのでdiffは0にならないため無視できる
    diff = idx - valsum
    ans = N*N - np.count_nonzero(diff)
    ans //= 2  # 同じ組合せが2回数えられているので半分にする
    print(ans)

abc166_e()

Submission status: Runtime Error

Solution code:
import numpy as np

def abc166_e():
    N = int(input())
    A = np.array(input().split(), dtype=np.int64)

    # indexの差の絶対値
    idx = np.arange(1, N+1, dtype=np.int64) - np.arange(1, N+1, dtype=np.int64).reshape(-1, 1)
    np.absolute(idx, out=idx)
    # 値の和
    valsum = A + A.reshape(-1, 1)

    # 差をとって0なら等しい
    # 対角成分はidxが0でvalsumが2以上なのでdiffは0にならないため無視できる
    diff = idx - valsum
    ans = N*N - np.count_nonzero(diff)
    ans //= 2  # 同じ組合せが2回数えられているので半分にする
    print(ans)

abc166_e()

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
L,R={},{}
for i,a in enumerate(A):
  l,r = i-a,i+a
  if l in L: L[l]=0
  if r in R: R[r]=0
  L[l]+=1
  R[r]+=1

count = 0
for x in L:
  if x not in R: continue
  count += L[x]*R[x]
print(count)

Submission status: Runtime Error

Solution code:
import numpy as np

def abc166_e():
    N = int(input())
    A = np.array(input().split(), dtype=np.int64)

    # indexの差の絶対値
    idx = np.arange(1, N+1) - np.arange(1, N+1).reshape(-1, 1)
    np.absolute(idx, out=idx)
    # 値の和
    valsum = A + A.reshape(-1, 1)

    # 差をとって0なら等しい
    # 対角成分はidxが0でvalsumが2以上なのでdiffは0にならないため無視できる
    diff = idx - valsum
    ans = N*N - np.count_nonzero(diff)
    ans //= 2  # 同じ組合せが2回数えられているので半分にする
    print(ans)

abc166_e()

Submission status: Runtime Error

Solution code:
N = int(input())
A = (int,input().split())
C = N*(N - 1) / 2
x = 0
for i in range(round(C) + 1):
  for j, k in range(N):
    if  A.index(j) - A.index(k) == j + k:
      x += 1
      
print(x)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split(" ")))
c=0
for i in range(n):
  for j in range(i+1,n):
    if l[i]+l[j]=j-i:
      c+=1
      
print(c)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    n = II()
    A = LI()

    memo = {}
    ans = 0
    for j in range(n):
        sa = j - A[j]
        ans += memo.get(sa, 0)
        wa = j + A[j]
        memo[wa] = memo.get(wa, 0) + 1
        print(memo)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
for i in range(N):
    if i>A[i]:
        d[i]=A[i]

count=0
for i in range(N):
    for j in [j for j in d if j > i]:
        if j-i==A[i]+aj:
            count += 1
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = []
C = []
count = 0
for i in range(0,N):
    B.append(A[i]+i)
    C.append(i-A[i])

for i in B:
    if i in C and i < j:
        count += 1

print(count)

Submission status: Runtime Error

Solution code:
# Problem E - This Message Will Self-Destruct in 5s

# input
N = int(input())
a_nums = list(map(int, input().split()))

# initialization
tmp_1 = [0]*(10**6)
ans = 0

# count
for i in range(1, N+1):
    tmp_1[i + a_nums[i-1]] += 1

# count 2
for j in range(1, N+1):
    if j-a_nums[j-1]>=0:
        ans +=  tmp_1[j-a_nums[j-1]]

# output
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
  plus = i+1 + A[i]
  AP.append(plus)
  minus = i+1 - A[i]
  AM.append(minus)
  
print(AP)
print(AM)
ans = 0 

for j in range(N):
  ans += AP.count(j) * AM.count(j)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    c = [0] * n
    s = 0
    for i, a in enumerate(a_list):
        if i+a < n:
            d[i+a] += 1
        if i-a >= 0:
            s += d[i-a]
    print(s)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int().input()
l = map(int, input(),split())
count = 0
for i, v in enumerate(l):
  for j in range(n):
    if i-j == v:
      count += 1
print(count)

Submission status: Runtime Error

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

nn = n + max(a)
ml = [0] * nn
pl = [0] * nn

out = 0

for i in range(0, n):
    pl[i+a[i]] += 1
    if(i-a[i]>=0):
        ml[i-a[i]] += 1

for i in range(0, nn):
    out += pl[i] * ml[i]

print(out)

Submission status: Runtime Error

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

nn = n + max(a)
ml = [0] * nn
pl = [0] * nn

out = 0

for i in range(0, n):
    pl[i+a[i]] += 1
    if(i-a[i]>=0):
        ml[i-a[i]] += 1

for i in range(0, n):
    out += pl[i] * ml[i]

print(out)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase
from collections import defaultdict

import __pypy__


def main():
    n = int(input())
    a = [int(i) for i in input().split(" ")]
    s = defaultdict(int)
    for i in xrange(n):
    	ans += s[a[i]-i]
        s[-i-a[i]] += 1
    print(ans)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase
from collections import defaultdict

import __pypy__


def main():
    n = int(input())
    a = [int(i) for i in input().split(" ")]
    s = defaultdict(int)
    for i in xrange(n):
    	ans += s[a[i]-i]
        s[-i-a[i]] += 1
    print(ans)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
dic = {}
for i in range(n-1):
  idx = i + a[i] + 1
  if idx in dic:
    dic[idx] += 1
  else:
    dic[idx] = 1
ans = 0
j = n-1
while j > 0:
  target = j + 1 - a[j]
  idx = j + 1 + a[j]
  if target in dic:
    ans += dic[target]
  dic[idx] -= 1
  j -= 1
print(ans)

Submission status: Runtime Error

Solution code:
def E():
    n = int(input())
    a = list(map(int, input()))
    cnt = 0
    for i in range(1,n):
        for j in range(i+1,n+1):
            if j-i == a[i-1] + a[j-1]:
                cnt += 1
    print(cnt)
E()

Submission status: Runtime Error

Solution code:
def E():
    #n = int(input())
    #a = list(map(int, input()))
    cnt = 0
    for i in range(1,n):
        for j in range(i+1,n+1):
            if j-i == a[i-1] + a[j-1]:
                cnt += 1
    print(cnt)
E()

Submission status: Runtime Error

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

for i in a:
    if i > n:
        a.pop(i)

ans = 0
for j in range(len(a)):
    for k in range(j+1, len(a)):
        #print(j,k)
        if  a[j] + a[k] == abs(j - k):
           #print(j,k, a[j],a[k])
           ans = ans + 1

print(ans)

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split())

S=['']*N
for i in range(N):
    S[i]=input()

result=[]
kekka=True


for i in range(N):
    if (A==0 and B==0 and S[i]=='AB') or (C==0 and B==0 and S[i]=='BC') or (A==0 and C==0 and S[i]=='AC'):
        kekka=False
        break
    else:
        if S[i]=='AB':
            if A==0:
                A += 1
                B -= 1
                result.append('A')
            elif B==0:
                A -=1
                B +=1
                result.append('B')
            elif A==1 and B==1 and C==0:
                if S[i+1]=='AC':
                    A += 1
                    B -= 1
                    result.append('A')
                else:
                    A -=1
                    B +=1
                    result.append('B')
            elif A>B:
                A -=1
                B +=1
                result.append('B')
            else:
                A +=1
                B -=1
                result.append('A')
        elif S[i]=='AC':
            if A==0:
                A += 1
                C -= 1
                result.append('A')
            elif C==0:
                A -=1
                C +=1
                result.append('C')
            elif A==1 and C==1 and B==0:
                if S[i+1]=='AB':
                    A += 1
                    C -= 1
                    result.append('A')
                else:
                    A -=1
                    C +=1
                    result.append('C')
            elif A>C:
                A -=1
                C +=1
                result.append('C')
            else:
                A +=1
                C -=1
                result.append('A') 
        elif S[i]=='BC':
            if B==0:
                B += 1
                C -= 1
                result.append('B')
            elif C==0:
                B -=1
                C +=1
                result.append('C')
            elif B==1 and C==1 and A==0:
                if S[i+1]=='AB':
                    B += 1
                    C -= 1
                    result.append('B')
                else:
                    B -=1
                    C +=1
                    result.append('C')
            elif B>C:
                B -=1
                C +=1
                result.append('C')
            else:
                B +=1
                C -=1
                result.append('B')

if kekka ==False:
    print('No')
else:
    print('Yes')
    for i in range(len(result)):
        print(result[i])

Submission status: Runtime Error

Solution code:
n = int(input().strip())
L = list(map(int, input().strip().split()))
seen=set()
ans=0
for j in range(n):
    if j-L[j] in seen:
        ans+=1
    seen.add(A[j]+j)
print(ans)

Submission status: Runtime Error

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

for i in a:
    if i > 2 * (10 ** 5):
        a.pop(i)

ans = 0
for j in range(len(a)):
    for k in range(j+1, len(a)):
        #print(j,k)
        if  a[j] + a[k] == abs(j - k):
           #print(j,k, a[j],a[k])
           ans = ans + 1

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

N = int(input())
A = list(map(int, input().split()))
count=0
L = [0]*N
R = [0]*N
hash_L=[0]*10000000000
hash_R=[0]*10000000000
for i in range(N):
    L[i]=A[i]+i
    hash_L[L[i]]=L[i]
    R[i]=-A[i]+i
    hash_R[R[i]]=R[i]

for i in range(2*N):
    if hash_L[i]==hash_R[i]:
        count=count+1

print("{}".format(count))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

N = int(input())
A = list(map(int, input().split()))
count=0

for i in range(N):
    m=max[i:]
    for j in range(N-i-A[i]):
        k=i+j+A[i]
        
        if A[i]+A[k]==k-i:
            count = count+1
            #print("{} {}".format(i+1,k+1))
        if A[i]+m+i<k:
            break


print("{}".format(count))

Submission status: Runtime Error

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

for i in range(N):
    if A[i]>2*100000:
        A.remove(A[i])
#print(A)
N=len(A)
#print(N)
for i in range(N):
        for k in range(i+1 , N):
            s=A[i]+A[k]
            g=k-i
            if s==g:
                l+=1
print(l)

Submission status: Runtime Error

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

for i in range(N):
    if A[i]>2*100000:
        A.remove(A[i])
N=len(A)
for i in range(N):
        for k in range(i+1 , N):
            if A[i]+A[k]==k-i or A[i]+A[k]==i-k:
                l+=1
print(l)

Submission status: Runtime Error

Solution code:
if __name__ == "__main__":
  N = int(input())
  
  list_n = range(1, n, 1)
  list_A = list(map(int, input().split(" ")))
  
  num_pair = 0
  for i in range(N, 2, -1):
    for j in range(i, 1, -1):
      sum_A = list_A[i] + list_A[j]
      if sum_A == abs(i - j):
        num_pair += 1
        
  print(num_pair)

Submission status: Runtime Error

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

mp = [0] * n
#right = [0] * n

ans = 0

for i in range(n):
    ans += mp[i-a[i]]
    if n > a[i]+i:
        mp[a[i]+i] += 1

print(ans)

Submission status: Runtime Error

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

cnt = 0

seppenPlus = [0] * (n + max(heights))
seppenMinus = [0] * (n + max(heights))

for i in range(len(heights)):
    seppenPlusNum = i + heights[i]
    seppenMinusNum = i - heights[i]

    seppenPlus[seppenPlusNum] += 1

    if seppenMinusNum >= 0:
        seppenMinus[seppenMinusNum] += 1

for i in range(len(seppenPlus)):
    cnt += seppenPlus[i] * seppenMinus[i]

print(cnt)

Submission status: Runtime Error

Solution code:
# class Map(dict):
#     def __missing__(self, key):
#         # この割当てなしのほうが速い。
#         # self[key] = 0
#         return 0


def main():
    int(input())

    d = {}
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        got = d.get(i + height) or 0
        d[i + height] += got + 1
        ans += d.get(i - height) or 0

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N = int(input())
dict = defaultdict(int)
ans = 0
for i in range(N):
    s = ''.join(sorted(input()))
    ans += dict[s]
    dict[s] += 1
print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
h = list(map(int,input().split()))
l = []
for i in range(m):
  a,b = map(int,input().split())
  a-=1
  b-=1
  if h[a]<h[b]:
    l.append(a)
  elif h[a] == h[b]:
    l += [a,b]
  else:
    l.append(b)
s = set(l)
print(n-len(s))

Submission status: Runtime Error

Solution code:
def main():
    int(input())
    d = {}
    ans = 0
    for i, height in enumerate(input().split()):
        d[i + height] += 1
        ans += d[i - height]


if __name__ == '__main__':
    print(main())

Submission status: Runtime Error

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

L=[0]*2*n
c=0

for i,a in enumerate(A):
  L[i+a]+=1
  if 2*n>i-a>0:
    c+=L[i-a]

print(c)

Submission status: Runtime Error

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

L=[0]*2*n
c=0

for i,a in enumerate(A):
  L[i+a]+=1
  c+=L[i-a]

print(c)

Submission status: Runtime Error

Solution code:
import math

n=int(input())
A=list(map(int, input().split()))
L=[0]*2*n
R=[0]*2*n
c=0
x=0
for i,a in enumerate(A):
  L[i+a]+=1
  R[i-a]+=1
  c+=L[i-a]

print(c)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

n, a, b, c = map(int, input().split())
s = [None] * n
for i in range(n):
    s[i] = input().rstrip()

if a + b + c == 0:
    print("No")
    exit(0)

ans = [None] * n
if a + b + c == 1:
    for i in range(n):
        if s[i] == 'AB':
            if a == 0 and b == 0:
                print('No')
                exit(0)
            if b == 0:
                b += 1
                a -= 1
                ans[i] = 'B'
            else:
                a += 1
                b -= 1
                ans[i] = 'A'
        elif s[i] == 'BC':
            if b == 0 and c == 0:
                print('No')
                exit(0)
            if c == 0:
                b -= 1
                c += 1
                ans[i] = 'C'
            else:
                b += 1
                c -= 1
                ans[i] = 'B'
        else:
            if a == 0 and c == 0:
                print('No')
                exit(0)
            if c == 0:
                a -= 1
                c += 1
                ans[i] = 'C'
            else:
                a += 1
                c -= 1
                ans[i] = 'A'

    print("Yes")
    for i in range(n):
        print(ans[i])
    exit(0)

if s[0] == 'AB' and a == 0 and b == 0:
    print("No")
    exit(0)
if s[0] == 'BC' and b == 0 and c == 0:
    print("No")
    exit(0)
if s[0] == 'AC' and a == 0 and c == 0:
    print("No")
    exit(0)

print("Yes")
for i in range(n):
    if s[i] == 'AB':
        if a == 0:
            print('A')
            a += 1
            b -= 1
            continue
        elif b == 0:
            print('B')
            a -= 1
            b += 1
            continue
    elif s[i] == 'BC':
        if b == 0:
            b += 1
            c -= 1
            print('B')
            continue
        elif c == 0:
            b -= 1
            c += 1
            print('C')
            continue
    else:
        if a == 0:
            a += 1
            c -= 1
            print('A')
            continue
        elif c == 0:
            a -= 1
            c += 1
            print('C')
            continue
    
    if a + b + c == 2 and i < n - 1 and s[i] != s[i + 1]:
        if s[i] == 'AB' and a == 1 and b == 1:
            if s[i + 1] == 'AC':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
        elif s[i] == 'BC' and b == 1 and c == 1:
            if s[i + 1] == 'AB':
                a -= 1
                b += 1
                print('B')
            else:
                a += 1
                b -= 1
                print('A')
        elif s[i] == 'AC' and a == 1 and c == 1:
            if s[i + 1] == 'AB':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
    
    if s[i] == 'AB':
        if a < b:
            a += 1
            b -= 1
            print('A')
        else:
            a -= 1
            b += 1
            print('B')
    elif s[i] == 'BC':
        if b < c:
            b += 1
            c -= 1
            print('B')
        else:
            b -= 1
            c += 1
            print('C')
    else:
        if a < c:
            a += 1
            c -= 1
            print('A')
        else:
            a -= 1
            c += 1
            print('C')

Submission status: Runtime Error

Solution code:
from collections import defaultdict
n = int(input())
a = np.array(list(map(int, input().split())))
ci = defaultdict(int)
cj = defaultdict(int)
for i in range(n):
    ci[a[i]+i] += 1
    cj[i-a[i]] += 1
cnt = 0
for k in ci.keys():
    cnt += ci[k] * cj[k]
print(cnt)

Submission status: Runtime Error

Solution code:
# 愚直な方法: O(n^2)
n = int(input())
a = np.array(list(map(int, input().split())))
cnt = 0
for i in range(1, n+1):
    for j in range(i+1, n+1):
        if abs(i-j) == (a[i-1]+a[j-1]):
            cnt += 1
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import numpy as np
N = int(input())
A = list(map(int, input().split()))

D = defaultdict(int)

ans = 0

for i, a in enumerate(A):
    ans += D[i-a]
    D[i+a] += 1

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*((10**9)*2)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*((max(arr)+N)*2)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*(max(arr)*2+1000)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*(max(arr)*2)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*(max(arr)+N+1)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

q1=[]
q2=[]

for i in range(n):
  q1.append(i+a[i])
  q2.append(i-a[i])

c2=Counter(q2)

ans=0
for i in c1.keys():
  ans+=c2[i]

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
Hs = np.array(list(map(int, input().split())))
As = np.tile(Hs, (N, 1))
h = As.T.copy()
L = np.array([i for i in range(N)])
idxs = np.tile(L, (N, 1))
tidxs = idxs.T.copy()
try:
    co = As + h - np.abs(idxs-tidxs)
except:
    co = As
ans = N**2-np.count_nonzero(co)
print(ans//2)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
Hs = np.array(list(map(int, input().split())), dtype='int64')
As = np.tile(Hs, (N, 1))
h = As.T.copy()
L = np.array([i for i in range(N)], dtype='int64')
idxs = np.tile(L, (N, 1))
tidxs = idxs.T.copy()
co = As + h - np.abs(idxs-tidxs)
ans = N**2-np.count_nonzero(co)
print(ans//2)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
Hs = np.array(list(map(int, input().split())), dtype='int64').reshape(-1, 1)
As = Hs.repeat(N, axis=1)
h = As.T.copy()
L = np.array([i for i in range(N)], dtype='int64').reshape(-1, 1)
idxs = L.repeat(N, axis=1)
tidxs = idxs.T.copy()
co = As + h - np.abs(idxs-tidxs)
ans = N**2-np.count_nonzero(co)
print(ans//2)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
Hs = np.array(list(map(int, input().split()))).reshape(-1, 1)
As = Hs.repeat(N, axis=1)
h = As.T.copy()
L = np.array([i for i in range(N)]).reshape(-1, 1)
idxs = L.repeat(N, axis=1)
tidxs = idxs.T.copy()
co = As + h - np.abs(idxs-tidxs)
ans = N**2-np.count_nonzero(co)
print(ans//2)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

d = dict(collections.Counter([i + A[i-1] for i in range(1, N+1)]))
ans = sum([d.get(i - A[i-1], 0) for i in range(1, N+1)])

print(ans)

Submission status: Runtime Error

Solution code:
def main():
  n=int(input())
  heights=list(map(int,input().split()))
  assert len(heights)==n
  i=0
  count=0
  while i<n:
    j=i+1
    while j<n:
      if heights[i]+heights+[j]==(j-i):
        count+=1
  print(count)
main()

Submission status: Runtime Error

Solution code:
def main():
  n=int(input())
  heights=list(map(int,input.split()))
  assert len(heights)==n
  i=0
  count=0
  while i<n:
    j=i+1
    while j<n:
      if heights[i]+heights+[j]==(j-i):
        count+=1
  print(count)
main()

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
k=0
if n%2==1:
  for i in range(m):
    print(i+1,end=" ")
    print(n-i)
else:
  for i in range((n+2)//4-1):
    if k>=m:
      break
    print(i+1,end=" ")
    print(n-i)
    k+=1
  for i in range((n+2)//4-1,n//2):
    if k>=m:
      break
    print(i+1,end=" ")
    print(n-i-1)
    k+=1

Submission status: Runtime Error

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

lst = [0] * N

ans = 0
for i in range(N):
    if i + 1 + A[i] < n: #iは0から始まるので +1 してあげる
        lst[i + A[i]] += 1
    if i + 1 - A[i] >= 1:
        ans += lst[i - A[i]]

print(ans)

Submission status: Runtime Error

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

lst = [0] * N

ans = 0
for i in range(N):
    if i + 1 + A[i] < n: #iは0から始まるので +1 してあげる
        lst[i + A[i]] += 1
    if i + 1 - A[i] >= 1:
        ans += lst[i + 1 - A[i]]

print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

r = []
for i, ai in enumerate(a):
   l.append(i+ai)
   r.append(i-ai)

ans = 0
b = Counter(r)
for i in l:
   ans += b.get(i, 0)

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
s=0
L=[i for i in range(N)]
A=list(map(int,input().split()))
dic1={[i,i+a]:0 for i,a in zip(L,A)}
dic2={[i,i-a]:0 for i,a in zip(L,A)}
for p in range(N):
    z=[k for k,v in dic1.items() if k[2]==p]
    w=[k for k,v in dic2.items() if k[2]==p]
    s+=len(z)*len(w)
print(s)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = [0] * n
    ans = 0
    for i in range(n):
        ai = a[i]
        l, r = i + ai, i - ai
        if 0 <= l < n:
            d[l] += 1
        if 0 <= r < n:
            ans += d[r]
        print(d)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
A = np.array([int(i) for i in input().split()])
A = A[A<=2*(10**5)]

H = [0]*(5*(10**5))
L = [0]*(5*(10**5))

for i in range(N):
    H[A[i]+i] += 1
    if i-A[i] >= 0:
        L[i-A[i]] += 1

ans = 0

for i in range(5*(10**5)):
    ans += H[i]*L[i]

print(ans)