Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()
dp = [[0] * 7 for i in range(4000)]

for i in range(n):
    if s[i] == 'R':
        dp[i + 1][0] = dp[i][0] + 1
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][1] + dp[i][3]
        dp[i + 1][4] = dp[i][4]
        dp[i + 1][5] = dp[i][2] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][4]
    elif s[i] == 'G':
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1] + 1
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][0] + dp[i][3]
        dp[i + 1][4] = dp[i][2] + dp[i][4]
        dp[i + 1][5] = dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][5]
    else:
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2] + 1
        dp[i + 1][3] = dp[i][3]
        dp[i + 1][4] = dp[i][1] + dp[i][4]
        dp[i + 1][5] = dp[i][0] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][3]
    for j in range(1, i // 2 + 1):
        if s[i] != s[i - j] and s[i] != s[i - j * 2] and s[i - j] != s[i - 2 * j]:
            dp[i + 1][6] -= 1
print(dp[n][6])

Fixed submission:
n = int(input())
s = input()
dp = [[0] * 7 for i in range(4001)]

for i in range(n):
    if s[i] == 'R':
        dp[i + 1][0] = dp[i][0] + 1
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][1] + dp[i][3]
        dp[i + 1][4] = dp[i][4]
        dp[i + 1][5] = dp[i][2] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][4]
    elif s[i] == 'G':
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1] + 1
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][0] + dp[i][3]
        dp[i + 1][4] = dp[i][2] + dp[i][4]
        dp[i + 1][5] = dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][5]
    else:
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2] + 1
        dp[i + 1][3] = dp[i][3]
        dp[i + 1][4] = dp[i][1] + dp[i][4]
        dp[i + 1][5] = dp[i][0] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][3]
    for j in range(1, i // 2 + 1):
        if s[i] != s[i - j] and s[i] != s[i - j * 2] and s[i - j] != s[i - 2 * j]:
            dp[i + 1][6] -= 1
print(dp[n][6])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
dp = [0] * 4000
dp[0] = 0

for i in range(n):
    dp[i + 1] = dp[i]
    for j in range(i):
        for k in range(j + 1, i):
            if k - j != i - k and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                dp[i + 1] += 1
print(dp[n])

Fixed submission:
n = int(input())
s = input()
dp = [[0] * 7 for i in range(4001)]

for i in range(n):
    if s[i] == 'R':
        dp[i + 1][0] = dp[i][0] + 1
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][1] + dp[i][3]
        dp[i + 1][4] = dp[i][4]
        dp[i + 1][5] = dp[i][2] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][4]
    elif s[i] == 'G':
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1] + 1
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][0] + dp[i][3]
        dp[i + 1][4] = dp[i][2] + dp[i][4]
        dp[i + 1][5] = dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][5]
    else:
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2] + 1
        dp[i + 1][3] = dp[i][3]
        dp[i + 1][4] = dp[i][1] + dp[i][4]
        dp[i + 1][5] = dp[i][0] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][3]
    for j in range(1, i // 2 + 1):
        if s[i] != s[i - j] and s[i] != s[i - j * 2] and s[i - j] != s[i - 2 * j]:
            dp[i + 1][6] -= 1
print(dp[n][6])