Buggy python submission (verdict: Time Limit Exceeded):
import math


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

    _rs = []
    _gs = []
    _bs = []
    for i in range(n):
        if s[i] == "R":
            _rs.append(i)
        elif s[i] == "G":
            _gs.append(i)
        else:
            _bs.append(i)

    cnt = 0
    lrs = len(_rs)
    lgs = len(_gs)
    lbs = len(_bs)
    rs = _rs
    gs = _gs
    bs = _bs

    if lrs < lgs < lbs:
        rs = _rs
        gs = _gs
        bs = _bs
    elif lrs < lbs < lgs:
        rs = _rs
        gs = _bs
        bs = _gs
    elif lgs < lrs < lbs:
        rs = _gs
        gs = _rs
        bs = _bs
    elif lgs < lbs < lrs:
        rs = _gs
        gs = _bs
        bs = _rs
    elif lbs < lrs < lgs:
        rs = _bs
        gs = _rs
        bs = _gs
    elif lbs < lgs < lrs:
        rs = _bs
        gs = _gs
        bs = _rs

    for ri in rs:
        for gi in gs:
            if ri < gi:
                df = gi - ri
                rr = gi + df
                ll = ri - df
                mm = -1
                if df % 2 == 0:
                    mm = ri + int(df / 2)

                lim = rr if rr <= (n - 1) else max(mm, ll)

                if lim < 0:
                    continue

                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt += 1
                    elif bi == mm:
                        cnt += 1
                    elif bi == rr:
                        cnt += 1

            else:
                df = ri - gi
                rr = ri + df
                ll = gi - df
                mm = -1
                if df % 2 == 0:
                    mm = gi + int(df / 2)

                lim = rr if rr <= (n - 1) else max(mm, ll)

                if lim < 0:
                    continue
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt += 1
                    elif bi == mm:
                        cnt += 1
                    elif bi == rr:
                        cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()


Fixed submission:
import math


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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi,ri) + df
            ll = min(gi,ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri)/2)

            if rr in bs:
                cnt +=1
            if mm in bs:
                cnt+=1
            if ll in bs:
                cnt+=1

            # if ri < gi:
            #     df = gi - ri
            #     rr = gi + df
            #     ll = ri - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = ri + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
            #
            # else:
            #     df = ri - gi
            #     rr = ri + df
            #     ll = gi - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = gi + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Buggy python submission (verdict: Time Limit Exceeded):
import math


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

    _rs = []
    _gs = []
    _bs = []
    for i in range(n):
        if s[i] == "R":
            _rs.append(i)
        elif s[i] == "G":
            _gs.append(i)
        else:
            _bs.append(i)

    cnt = 0
    lrs = len(_rs)
    lgs = len(_gs)
    lbs = len(_bs)
    rs = _rs
    gs = _gs
    bs = _bs

    if lrs < lgs < lbs:
        rs = _rs
        gs = _gs
        bs = _bs
    elif lrs < lbs < lgs:
        rs = _rs
        gs = _bs
        bs = _gs
    elif lgs < lrs < lbs:
        rs = _gs
        gs = _rs
        bs = _bs
    elif lgs < lbs < lrs:
        rs = _gs
        gs = _bs
        bs = _rs
    elif lbs < lrs < lgs:
        rs = _bs
        gs = _rs
        bs = _gs
    elif lbs < lgs < lrs:
        rs = _bs
        gs = _gs
        bs = _rs

    for ri in rs:
        for gi in gs:
            if ri < gi:
                df = gi - ri
                rr = gi + df
                ll = ri - df
                mm = -1
                if df %2 ==0:
                    mm = ri + int(df/2)

                lim = rr if rr <= (n-1) else max(mm, ll)
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt+=1
                    elif bi == mm:
                        cnt+=1
                    elif bi == rr:
                        cnt+=1

            else:
                df = ri - gi
                rr = ri + df
                ll = gi - df
                mm = -1
                if df %2 ==0:
                    mm = gi + int(df/2)

                lim = rr if rr <= (n-1) else max(mm, ll)
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt+=1
                    elif bi == mm:
                        cnt+=1
                    elif bi == rr:
                        cnt+=1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)



main()


Fixed submission:
import math


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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi,ri) + df
            ll = min(gi,ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri)/2)

            if rr in bs:
                cnt +=1
            if mm in bs:
                cnt+=1
            if ll in bs:
                cnt+=1

            # if ri < gi:
            #     df = gi - ri
            #     rr = gi + df
            #     ll = ri - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = ri + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
            #
            # else:
            #     df = ri - gi
            #     rr = ri + df
            #     ll = gi - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = gi + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Buggy python submission (verdict: Time Limit Exceeded):
import math


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

    rs = []
    gs = []
    bs = []
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.append(i)

    cnt = 0
    for ri in rs:
        for gi in gs:
            if ri < gi:
                df = gi - ri
                rr = gi + df
                ll = ri - df
                mm = -1
                if df %2 ==0:
                    mm = ri + int(df/2)

                lim = rr if rr <= (n-1) else max(mm, ll)
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt+=1
                    elif bi == mm:
                        cnt+=1
                    elif bi == rr:
                        cnt+=1

            else:
                df = ri - gi
                rr = ri + df
                ll = gi - df
                mm = -1
                if df %2 ==0:
                    mm = gi + int(df/2)

                lim = rr if rr <= (n-1) else max(mm, ll)
                for bi in bs:
                    if bi < ll:
                        continue
                    if lim < bi:
                        break

                    if bi == ll:
                        cnt+=1
                    elif bi == mm:
                        cnt+=1
                    elif bi == rr:
                        cnt+=1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)



main()


Fixed submission:
import math


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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi,ri) + df
            ll = min(gi,ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri)/2)

            if rr in bs:
                cnt +=1
            if mm in bs:
                cnt+=1
            if ll in bs:
                cnt+=1

            # if ri < gi:
            #     df = gi - ri
            #     rr = gi + df
            #     ll = ri - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = ri + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
            #
            # else:
            #     df = ri - gi
            #     rr = ri + df
            #     ll = gi - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = gi + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Buggy python submission (verdict: Time Limit Exceeded):
import math


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

    rs = []
    gs = []
    bs = []
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.append(i)

    cnt = 0
    for ri in rs:
        for gi in gs:
            if ri < gi:
                df = gi - ri
                rr = gi + df
                ll = ri - df
                if rr in bs:
                    cnt+=1
                if ll in bs:
                    cnt+=1
                if df %2 ==0:
                    mm = ri + int(df/2)
                    if mm in bs:
                        cnt +=1
            else:
                df = ri - gi
                rr = ri + df
                ll = gi - df
                if rr in bs:
                    cnt+=1
                if ll in bs:
                    cnt+=1
                if df %2 ==0:
                    mm = gi + int(df/2)
                    if mm in bs:
                        cnt +=1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)



main()


Fixed submission:
import math


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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi,ri) + df
            ll = min(gi,ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri)/2)

            if rr in bs:
                cnt +=1
            if mm in bs:
                cnt+=1
            if ll in bs:
                cnt+=1

            # if ri < gi:
            #     df = gi - ri
            #     rr = gi + df
            #     ll = ri - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = ri + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
            #
            # else:
            #     df = ri - gi
            #     rr = ri + df
            #     ll = gi - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = gi + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Buggy python submission (verdict: Time Limit Exceeded):
import math


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

    rs = []
    gs = []
    bs = []
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.append(i)

    cnt = 0
    for ri in rs:
        for gi in gs:
            rg = abs(ri - gi)
            for bi in bs:
                rb = abs(ri - bi)
                if rg == rb:
                    cnt += 1
                elif (gi < ri and bi < ri) or (ri < gi and ri < bi):
                    if rg == 2 * rb or 2 * rg == rb:
                        cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)



main()


Fixed submission:
import math


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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi,ri) + df
            ll = min(gi,ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri)/2)

            if rr in bs:
                cnt +=1
            if mm in bs:
                cnt+=1
            if ll in bs:
                cnt+=1

            # if ri < gi:
            #     df = gi - ri
            #     rr = gi + df
            #     ll = ri - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = ri + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
            #
            # else:
            #     df = ri - gi
            #     rr = ri + df
            #     ll = gi - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = gi + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()

Buggy python submission (verdict: Time Limit Exceeded):
import math


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

    rs = []
    gs = []
    bs = []
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.append(i)

    cnt = 0
    for ri in rs:
        for gi in gs:
            for bi in bs:
                if ri < gi < bi:
                    if bi - gi != gi - ri:
                        cnt += 1
                elif ri < bi < gi:
                    if gi - bi != bi - ri:
                        cnt += 1
                elif bi < gi < ri:
                    if ri - gi != gi - bi:
                        cnt += 1
                elif bi < ri < gi:
                    if gi - ri != ri - bi:
                        cnt += 1
                elif gi < ri < bi:
                    if bi - ri != ri - gi:
                        cnt += 1
                else:
                    if ri - bi != bi - gi:
                        cnt += 1

    print(cnt)


main()


Fixed submission:
import math


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

    rs = []
    gs = []
    bs = {}
    for i in range(n):
        if s[i] == "R":
            rs.append(i)
        elif s[i] == "G":
            gs.append(i)
        else:
            bs.setdefault(i, 1)

    cnt = 0

    for ri in rs:
        for gi in gs:
            df = abs(gi - ri)
            rr = max(gi,ri) + df
            ll = min(gi,ri) - df
            mm = -1
            if df % 2 == 0:
                mm = int((gi + ri)/2)

            if rr in bs:
                cnt +=1
            if mm in bs:
                cnt+=1
            if ll in bs:
                cnt+=1

            # if ri < gi:
            #     df = gi - ri
            #     rr = gi + df
            #     ll = ri - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = ri + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
            #
            # else:
            #     df = ri - gi
            #     rr = ri + df
            #     ll = gi - df
            #     mm = -1
            #     if df % 2 == 0:
            #         mm = gi + int(df / 2)
            #
            #     lim = rr if rr <= (n - 1) else max(mm, ll)
            #
            #     if lim < 0:
            #         continue
            #     for bi in bs:
            #         if bi < ll:
            #             continue
            #         if lim < bi:
            #             break
            #
            #         if bi == ll:
            #             cnt += 1
            #         elif bi == mm:
            #             cnt += 1
            #         elif bi == rr:
            #             cnt += 1
    all_cnt = len(rs) * len(gs) * len(bs)

    print(all_cnt - cnt)


main()