Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
    n = int(input())
    s = np.array(list(read().rstrip()))

    r = np.sum(s == 'R')
    g = np.sum(s == 'G')
    b = np.sum(s == 'B')

    def f(s):
        ret = 0
        for i1 in range(n):
            for i2 in range(i1 + 1, n):
                i3 = i2 * 2 - i1
                if i3 <= n - 1:
                    if s[i1] != s[i2] and s[i1] != s[i3] and s[i2] != s[i3]:
                        ret += 1
        return ret
    
    res = r * g * b - f(s)
    print(res)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    rr = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']

    r = len(rr) * len(g) * len(b)
    for rre in rr:
        for ge in g:
            for be in b:
                r -= rre * 2 == ge + be
                r -= ge * 2 == rre + be
                r -= be * 2 == ge + rre
    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    rr = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']

    r = len(rr) * len(g) * len(b)
    for rre in rr:
        for ge in g:
            for be in b:
                r -= rre * 2 == ge + be
                r -= ge * 2 == rre + be
                r -= be * 2 == ge + rre
    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import product
def main():
    n = int(input())
    s = input()

    r = [0] * n
    g = [0] * n
    b = [0] * n
    for i, se in enumerate(s):
        if se == 'R':
            r[i] = 1
        elif se == 'G':
            g[i] = 1
        else:
            b[i] = 1

    res = sum(r) * sum(g) * sum(b)
    minus = 0
    for i1 in range(n):
        for i2 in range(n):
            for i3 in range(n):
                if r[i1] and g[i2] and b[i3]:
                    minus += i1 * 2 == (i2 + i3)
                    minus += i2 * 2 == (i1 + i3)
                    minus += i3 * 2 == (i1 + i2)
    print(res-minus)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import product
def main():
    n = int(input())
    s = input()

    r = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']
    com = product(r, g, b)
    r = 0
    for c in com:
        r += sum(c) * 2 != (min(c) + max(c)) * 3
    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import product
def main():
    n = int(input())
    s = input()

    r = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']
    com = product(r, g, b)
    r = 0
    for c in com:
        r += c[0] < c[1] < c[2] and c[1] - c[0] != c[2] - c[1]
        r += c[0] < c[2] < c[1] and c[2] - c[0] != c[1] - c[2]
        r += c[2] < c[1] < c[0] and c[0] - c[1] != c[1] - c[2]
        r += c[2] < c[0] < c[1] and c[0] - c[2] != c[1] - c[0]
        r += c[1] < c[0] < c[2] and c[0] - c[1] != c[2] - c[0]
        r += c[1] < c[2] < c[0] and c[2] - c[1] != c[0] - c[2]
    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import product
def main():
    n = int(input())
    s = input()

    r = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']
    com = product(r, g, b)
    r = 0
    for c in com:
        r += c[0] < c[1] < c[2] and c[1] - c[0] != c[2] - c[1]
        r += c[0] < c[2] < c[1] and c[2] - c[0] != c[1] - c[2]
        r += c[2] < c[1] < c[0] and c[0] - c[1] != c[1] - c[2]
        r += c[2] < c[0] < c[1] and c[0] - c[2] != c[1] - c[0]
        r += c[1] < c[0] < c[2] and c[0] - c[1] != c[2] - c[0]
        r += c[1] < c[2] < c[0] and c[2] - c[1] != c[0] - c[2]
    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            for ib in range(ia+1,n-1):
                if sg[ib]:
                    r += sum(sb[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sb[ib * 2 - ia]
                elif sb[ib]:
                    r += sum(sg[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sg[ib * 2 - ia]
        elif ta == 'G':
            for ib in range(ia+1,n-1):
                if sb[ib]:
                    r += sum(sr[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sr[ib * 2 - ia]
                elif sr[ib]:
                    r += sum(sb[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sb[ib * 2 - ia]
        else:
            for ib in range(ia+1,n-1):
                if sr[ib]:
                    r += sum(sg[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sg[ib * 2 - ia]
                elif sg[ib]:
                    r += sum(sr[ib+1:])
                    if ib * 2 - ia < n:
                        r -= sr[ib * 2 - ia]

    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
from collections import deque
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            tal = sr
        elif ta == 'G':
            tal = sg
        else:
            tal = sb
        for ib in range(ia+1, n-1):
            if tal[ib]:
                pass
            else:
                tb = s[ib]
                t = (ta, tb)
                sa = ib - ia

                if 'R' in t and 'G' in t:
                    r += sum(sb[ib+1:])
                    if ib + sa < n:
                        r -= sb[ib + sa]
                elif 'G' in t and 'B' in t:
                    r += sum(sr[ib+1:])
                    if ib + sa < n:
                        r -= sr[ib + sa]
                else:
                    r += sum(sg[ib + 1:])
                    if ib + sa < n:
                        r -= sg[ib + sa]

    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        for ib in range(ia+1, n-1):
            tb = s[ib]
            t = (ta, tb)
            sa = ib - ia
            if ta == tb:
                pass
            else:
                if 'R' in t and 'G' in t:
                    r += sum(sb[ib+1:])
                    if ib + sa < n:
                        r -= sb[ib + sa]
                elif 'G' in t and 'B' in t:
                    r += sum(sr[ib+1:])
                    if ib + sa < n:
                        r -= sr[ib + sa]
                else:
                    r += sum(sg[ib + 1:])
                    if ib + sa < n:
                        r -= sg[ib + sa]

    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        for ib in range(ia+1, n-1):
            tb = s[ib]
            t = (ta, tb)
            sa = ib - ia
            if ta == tb:
                pass
            else:
                if 'R' in t and 'G' in t:
                    r += sum(sb[ib+1:])
                    if ib + sa < n:
                        r -= sb[ib + sa]
                elif 'G' in t and 'B' in t:
                    r += sum(sr[ib+1:])
                    if ib + sa < n:
                        r -= sr[ib + sa]
                else:
                    r += sum(sg[ib + 1:])
                    if ib + sa < n:
                        r -= sg[ib + sa]

    print(r)

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = list(input())

    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        for ib in range(ia+1, n-1):
            tb = s[ib]
            t = (ta, tb)
            sa = ib - ia
            if ta == tb:
                pass
            else:
                cnt = 1
                ts = s[ib+1:]
                for ets in ts:
                    if cnt == sa:
                        pass
                    elif not ets in t:
                        r += 1
                    cnt += 1
    print(r)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = list(input())

    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        for ib in range(ia+1, n-1):
            ta = s[ia]
            if s[ia] == s[ib]:
                pass
            else:
                sa = ib - ia
                cnt = 1
                ts = s[ib+1:]
                for ets in ts:
                    if cnt == sa:
                        pass
                    elif s[ib] != ets != ta:
                        r += 1
                    cnt += 1

    print(r)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = input()

    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        for ib in range(ia+1, n-1):
            ta = s[ia]
            if s[ia] == s[ib]:
                pass
            else:
                sa = ib - ia
                cnt = 1
                ts = s[ib+1:]
                for ets in ts:
                    if cnt == sa:
                        pass
                    elif s[ib] != ets != ta:
                        r += 1
                    cnt += 1

    print(r)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):

def main():
    n = int(input())
    s = input()

    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        for ib in range(ia+1, n-1):
            for ic in range(ib+1,n):
                if (ib - ia) != (ic - ib) and s[ia] != s[ib] and s[ia] != s[ic] and s[ib] != s[ic]:
                    r += 1

    print(r)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()