Buggy python submission (verdict: Runtime Error):
import math
from itertools import combinations

def main():
    k = int(input())
    res = 0
    memo = [[0 for _ in range(k+1)] for _ in range(k+1)]
    for i ian range(1, k+1):
        for j in range(i, k+1):
            tmp = math.gcd(i,j)
            memo[i][j] = tmp
            memo[j][i] = tmp

    for i in range(1, k+1):
        res += i 

    for c in combinations(range(1,k+1),2):
        ab = memo[c[0], c[1]]
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = memo[c[0], c[1]]
        abc = memo[ab, c[2]]
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()


Fixed submission:
from itertools import permutations

def main():
    n = int(input())
    s = input()
    res = 0
    r = 0
    g = 0
    b = 0
    for i, c in enumerate(s):
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1
    
    rgb = []
    for t in permutations('RGB'):
        rgb.append(''.join(t))

    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            k = 2*j - i 
            if k >= n:
                break
            if (s[i] + s[j] + s[k]) in rgb:
                cnt += 1
    print(r*g*b - cnt)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = input()
    res = 0
    r = []
    g = []
    b = []
    for i, c in enumerate(s):
        if c == 'R':
            r.append(i)
        elif c == 'G':
            g.append(i)
        else:
            b.append(i)
    
    cnt = 0
    for i in r:
        for j in g:
            diff = abs(j-i)
            for k in b:
                if abs(i-k) != diff and abs(j-k) != diff and abs(j-k) != abs(i-k):
                    cnt += 1
    print(cnt)
                    


if __name__ == '__main__':
    main()



Fixed submission:
from itertools import permutations

def main():
    n = int(input())
    s = input()
    res = 0
    r = 0
    g = 0
    b = 0
    for i, c in enumerate(s):
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1
    
    rgb = []
    for t in permutations('RGB'):
        rgb.append(''.join(t))

    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            k = 2*j - i 
            if k >= n:
                break
            if (s[i] + s[j] + s[k]) in rgb:
                cnt += 1
    print(r*g*b - cnt)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = input()
    res = 0
    r = []
    g = []
    b = []
    for i, c in enumerate(s):
        if c == 'R':
            r.append(i)
        elif c == 'G':
            g.append(i)
        else:
            b.append(i)
    
    cnt = 0
    for i in r:
        for j in g:
            for k in b:
                if abs(j-i) != abs(k-j) and abs(j-i) != abs(k-i) and abs(k-j) != abs(k-i):
                    cnt += 1
    print(cnt)
                    


if __name__ == '__main__':
    main()



Fixed submission:
from itertools import permutations

def main():
    n = int(input())
    s = input()
    res = 0
    r = 0
    g = 0
    b = 0
    for i, c in enumerate(s):
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1
    
    rgb = []
    for t in permutations('RGB'):
        rgb.append(''.join(t))

    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            k = 2*j - i 
            if k >= n:
                break
            if (s[i] + s[j] + s[k]) in rgb:
                cnt += 1
    print(r*g*b - cnt)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = input()
    res = 0
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue
            for k in range(j+1, n):
                if (j-i) == (k-j):
                    continue
                if s[i] == s[k] or s[j] == s[k]:
                    continue
                res += 1
    print(res)

if __name__ == '__main__':
    main()



Fixed submission:
from itertools import permutations

def main():
    n = int(input())
    s = input()
    res = 0
    r = 0
    g = 0
    b = 0
    for i, c in enumerate(s):
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1
    
    rgb = []
    for t in permutations('RGB'):
        rgb.append(''.join(t))

    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            k = 2*j - i 
            if k >= n:
                break
            if (s[i] + s[j] + s[k]) in rgb:
                cnt += 1
    print(r*g*b - cnt)


if __name__ == '__main__':
    main()