Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1

    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N):
            Sj = S[j]
            if Si != Sj:
                if len(set([Si, Sj])^set(["R", "G"])) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(set([Si, Sj])^set(["G", "B"])) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(set([Si, Sj])^set(["R", "B"])) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    S = list(input())

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1
    set_rg = set(["R", "G"])
    set_gb = set(["G", "B"])
    set_rb = set(["R", "B"])
    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N-1):
            Sj = S[j]
            if Si != Sj:
                Sij = set([Si, Sj])
                if len(Sij^set_rg) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(Sij^set_gb) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(Sij^set_rb) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1

    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N):
            Sj = S[j]
            if Si != Sj:
                if len(set([Si, Sj])^set(["R", "G"])) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(set([Si, Sj])^set(["G", "B"])) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(set([Si, Sj])^set(["R", "B"])) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    S = list(input())

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1
    set_rg = set(["R", "G"])
    set_gb = set(["G", "B"])
    set_rb = set(["R", "B"])
    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N-1):
            Sj = S[j]
            if Si != Sj:
                Sij = set([Si, Sj])
                if len(Sij^set_rg) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(Sij^set_gb) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(Sij^set_rb) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1

    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N):
            Sj = S[j]
            if Si != Sj:
                if len(set([Si, Sj])^set(["R", "G"])) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(set([Si, Sj])^set(["G", "B"])) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(set([Si, Sj])^set(["R", "B"])) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    S = list(input())

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1
    set_rg = set(["R", "G"])
    set_gb = set(["G", "B"])
    set_rb = set(["R", "B"])
    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N-1):
            Sj = S[j]
            if Si != Sj:
                Sij = set([Si, Sj])
                if len(Sij^set_rg) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(Sij^set_gb) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(Sij^set_rb) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1

    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N):
            Sj = S[j]
            if Si != Sj:
                if len(set([Si, Sj])^set(["R", "G"])) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(set([Si, Sj])^set(["G", "B"])) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(set([Si, Sj])^set(["R", "B"])) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    S = list(input())

    ans = 0
    ruiseki_r = [0]*N
    ruiseki_g = [0]*N
    ruiseki_b = [0]*N

    for i in range(N):
        if S[i] == "R":
            ruiseki_r[i] = ruiseki_r[i-1] + 1
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "G":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1] + 1
            ruiseki_b[i] = ruiseki_b[i-1]
        if S[i] == "B":
            ruiseki_r[i] = ruiseki_r[i-1]
            ruiseki_g[i] = ruiseki_g[i-1]
            ruiseki_b[i] = ruiseki_b[i-1] + 1
    set_rg = set(["R", "G"])
    set_gb = set(["G", "B"])
    set_rb = set(["R", "B"])
    for i in range(N-2):
        Si = S[i]
        for j in range(i+1, N-1):
            Sj = S[j]
            if Si != Sj:
                Sij = set([Si, Sj])
                if len(Sij^set_rg) == 0:
                    remain_b = ruiseki_b[-1] - ruiseki_b[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "B":
                            remain_b -= 1
                    ans = ans + remain_b
                if len(Sij^set_gb) == 0:
                    remain_r = ruiseki_r[-1] - ruiseki_r[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "R":
                            remain_r -= 1
                    ans = ans + remain_r
                if len(Sij^set_rb) == 0:
                    remain_g = ruiseki_g[-1] - ruiseki_g[j]
                    if 2*j-i < N:
                        if S[2*j-i] == "G":
                            remain_g -= 1
                    ans = ans + remain_g
    print(ans)


if __name__ == "__main__":
    main()