Problem:
Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800

Solution in Haskell (GHC 8.8.3) (judge verdict: Time Limit Exceeded)

module Main where

import           Control.Monad
import           Debug.Trace
import qualified Data.Vector                   as V
import qualified Data.Vector.Mutable           as VM
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM
import           Data.List
import qualified Data.ByteString               as BS
import qualified Data.ByteString.Char8         as BSC8
import           Data.Char
import           Data.Maybe
import           Data.Ord
import           Data.Vector.Algorithms.Search
import qualified Data.Array                    as A
import qualified Data.Array.Unboxed            as AU
import qualified Data.Array.IO                 as AIO



getvuil :: IO (VU.Vector Int)
getvuil = VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
readi :: BSC8.ByteString -> Int
readi = fst . fromJust . BSC8.readInt
readil :: BSC8.ByteString -> [Int]
readil = map readi . BSC8.words
geti :: IO Int
geti = readi <$> BSC8.getLine
getil :: IO [Int]
getil = readil <$> BSC8.getLine

getNis :: Int -> IO [(Int, BSC8.ByteString)]
getNis 0 = return []
getisl n = do
    [aFst, aSnd] <- BSC8.words <$> BSC8.getLine
    next         <- (getisl (n - 1))
    return ((readi aFst, aSnd) : next)

il2ss :: [Int] -> String
il2ss = unwords . map show

il2sn :: [Int] -> String
il2sn = unlines . map show

main :: IO ()
main = do
    aN <- geti
    aS <- VU.fromList <$> getLine
    let (rL, gL, bL) = searchS aS
    let rLV          = VU.fromList rL
    let gLV          = VU.fromList gL
    let bLV          = VU.fromList bL
    let
        ret = if (length rL == 0) || (length gL == 0) || (length bL == 0)
            then 0
            else VU.foldl
                (\acc1 x ->
                    acc1
                        + (VU.foldl
                              (\acc2 y ->
                                  acc2
                                      + (VU.foldl
                                            (\acc3 z ->
                                                let rP     = rLV VU.! x
                                                    gP     = gLV VU.! y
                                                    bP     = bLV VU.! z
                                                    sorted = sort [rP, gP, bP]
                                                in  if (  (sorted !! 1 * 2)
                                                       == ( (sorted !! 0)
                                                          + (sorted !! 2
                                                            )
                                                          )
                                                       )
                                                    then
                                                        acc3
                                                    else
                                                        acc3 + 1
                                            )
                                            0
                                            (VU.fromList
                                                [0 .. VU.length bLV - 1]
                                            )
                                        )
                              )
                              0
                              (VU.fromList [0 .. VU.length gLV - 1])
                          )
                )
                0
                (VU.fromList [0 .. VU.length rLV - 1])

    print ret


searchS :: VU.Vector Char -> ([Int], [Int], [Int])
searchS vec = searchS' vec (VU.length vec - 1) ([], [], [])

searchS'
    :: VU.Vector Char -> Int -> ([Int], [Int], [Int]) -> ([Int], [Int], [Int])
searchS' vec poi list@(rL, gL, bL)
    | poi == -1
    = list
    | otherwise
    = let c = vec VU.! poi
      in  case c of
              'R'       -> searchS' vec (poi - 1) ((poi + 1) : rL, gL, bL)
              'G'       -> searchS' vec (poi - 1) (rL, (poi + 1) : gL, bL)
              'B'       -> searchS' vec (poi - 1) (rL, gL, (poi + 1) : bL)
              otherwise -> list