Problem p02714 (python)

Fixed submission:
n = int(input())
s = list(input())
r = [0]*n
g = [0]*n
b = [0]*n
for i in range(n):
    if s[i]=='R':
        r[i] = 1
    elif s[i] == 'G':
        g[i] = 1
    elif s[i] == 'B':
        b[i] = 1

r_cum = [0]*n
g_cum = [0]*n
b_cum = [0]*n

x = y = z = 0
i = n-1
while i>=0:
    if r[i]==1:
        x+=1
    if g[i]==1:
        y+=1
    if b[i]==1:
        z+=1
    r_cum[i] = x
    g_cum[i] = y
    b_cum[i] = z
    i-=1
s = 0
for i in range(n):
    for j in range(i+1,n):
        #RGB
        if r[i]==1 and g[j]==1:
            if j+1<n and 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
            elif j+1<n:
                s = s + b_cum[j+1]
        #RBG
        if r[i]==1 and b[j]==1:
            if j+1<n and 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GBR
        if g[i]==1 and b[j]==1:
            if j+1<n and 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GRB
        if g[i]==1 and r[j]==1:
            if j+1<n and 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and r[j]==1:
            if j+1<n and 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and g[j]==1:
            if j+1<n and 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,r_cum[j+1])
print(s)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = list(input())
r = [0]*n
g = [0]*n
b = [0]*n
for i in range(n):
    if s[i]=='R':
        r[i] = 1
    elif s[i] == 'G':
        g[i] = 1
    elif s[i] == 'B':
        b[i] = 1

r_cum = [0]*n
g_cum = [0]*n
b_cum = [0]*n

x = y = z = 0
i = n-1
while i>=0:
    if r[i]==1:
        x+=1
    if g[i]==1:
        y+=1
    if b[i]==1:
        z+=1
    r_cum[i] = x
    g_cum[i] = y
    b_cum[i] = z
    i-=1
s = 0
for i in range(n):
    for j in range(i+1,n):
        #RGB
        if r[i]==1 and g[j]==1:
            if j+1<n and 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        #RBG
        if r[i]==1 and b[j]==1:
            if j+1<n and 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GBR
        if g[i]==1 and b[j]==1:
            if j+1<n and 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GRB
        if g[i]==1 and r[j]==1:
            if j+1<n and 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            else:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and r[j]==1:
            if j+1<n and 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and g[j]==1:
            if j+1<n and 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,r_cum[j+1])
print(s)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = list(input())
r = [0]*n
g = [0]*n
b = [0]*n
for i in range(n):
    if s[i]=='R':
        r[i] = 1
    elif s[i] == 'G':
        g[i] = 1
    elif s[i] == 'B':
        b[i] = 1

r_cum = [0]*n
g_cum = [0]*n
b_cum = [0]*n

x = y = z = 0
i = n-1
while i>=0:
    if r[i]==1:
        x+=1
    if g[i]==1:
        y+=1
    if b[i]==1:
        z+=1
    r_cum[i] = x
    g_cum[i] = y
    b_cum[i] = z
    i-=1
s = 0
for i in range(n):
    for j in range(i+1,n):
        #RGB
        if r[i]==1 and g[j]==1:
            if 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        #RBG
        if r[i]==1 and b[j]==1:
            if 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GBR
        if g[i]==1 and b[j]==1:
            if 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GRB
        if g[i]==1 and r[j]==1:
            if 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            else:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and r[j]==1:
            if 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and g[j]==1:
            if 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,r_cum[j+1])
print(s)