Buggy python submission (verdict: Time Limit Exceeded):
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

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

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(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

def main():
  n = int(input())
  if n < 4:
    print(0)
    exit()
  ans = 0
  s = list(input())
  for i, a in enumerate(s):
    j = i+1
    k = i+2
    for bi, b in enumerate(s[j:]):
      if a == b:
        continue
      bb = bi+j
      for ci, c in enumerate(s[k:]):
        if a == c or b == c:
          continue
        cc = ci+k
        if i < bb < cc:
          if bb - i == cc - bb:
            continue
          ans += 1
  print(ans)          
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Fixed submission:
import sys
input = sys.stdin.readline
import bisect


# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    n = int(input())
    s = input()
    R, G, B = [], [], []
    for i in range(n):
        index = i+1
        if s[i] == "R":
            R.append(index)
        elif s[i] == "G":
            G.append(index)
        elif s[i] == "B":
            B.append(index)
    all_count = len(R)*len(G)*len(B)
    count = 0
    # j - i != k - j
    # k = 2j - i
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue
            k = 2*j - i
            if k >= n:
                continue
            if s[k] == s[i] or s[k] == s[j]:
                continue
            count += 1
    print(all_count - count)

def bi(num, a, b, x):
    print((a * num) + (b * len(str(b))))
    if (a * num) + (b * len(str(b))) <= x:
        return False
    return True

def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

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

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(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

def main():
  n = int(input())
  if n < 4:
    print(0)
    exit()
  ans = 0
  s = list(input())
  for i, a in enumerate(s):
    j = i+1
    k = i+2
    for bi, b in enumerate(s[j:]):
      bb = bi+j
      for ci, c in enumerate(s[k:]):
        cc = ci+k
        if i < bb < cc:
          if a == b or b == c or a == c:
            continue
          if bb - i == cc - bb:
            continue
          ans += 1
  print(ans)          
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Fixed submission:
import sys
input = sys.stdin.readline
import bisect


# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    n = int(input())
    s = input()
    R, G, B = [], [], []
    for i in range(n):
        index = i+1
        if s[i] == "R":
            R.append(index)
        elif s[i] == "G":
            G.append(index)
        elif s[i] == "B":
            B.append(index)
    all_count = len(R)*len(G)*len(B)
    count = 0
    # j - i != k - j
    # k = 2j - i
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue
            k = 2*j - i
            if k >= n:
                continue
            if s[k] == s[i] or s[k] == s[j]:
                continue
            count += 1
    print(all_count - count)

def bi(num, a, b, x):
    print((a * num) + (b * len(str(b))))
    if (a * num) + (b * len(str(b))) <= x:
        return False
    return True

def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

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

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(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

def main():
  n = int(input())
  if n < 4:
    print(0)
    exit()
  ans = 0
  s = list(input())
  for i, a in enumerate(s):
    j = i+1
    k = i+2
    for bi, b in enumerate(s):
      if bi < j:
        continue
      for ci, c in enumerate(s):
        if ci < k:
          continue
        if i > j:
          continue
        if j > k:
          continue
        if i < bi < ci:
          if a == b or b == c or a == c:
            continue
          if bi - i == ci - bi:
            continue
          ans += 1
  print(ans)          
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Fixed submission:
import sys
input = sys.stdin.readline
import bisect


# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    n = int(input())
    s = input()
    R, G, B = [], [], []
    for i in range(n):
        index = i+1
        if s[i] == "R":
            R.append(index)
        elif s[i] == "G":
            G.append(index)
        elif s[i] == "B":
            B.append(index)
    all_count = len(R)*len(G)*len(B)
    count = 0
    # j - i != k - j
    # k = 2j - i
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue
            k = 2*j - i
            if k >= n:
                continue
            if s[k] == s[i] or s[k] == s[j]:
                continue
            count += 1
    print(all_count - count)

def bi(num, a, b, x):
    print((a * num) + (b * len(str(b))))
    if (a * num) + (b * len(str(b))) <= x:
        return False
    return True

def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()