fun main(arg: Array<String>) {
    val (R, G, B) = listOf(0, 1, 2)
    val N = nextInt()
    val S = next()
    val A = S.map {
        when(it) {
            'R' -> 0
            'G' -> 1
            'B' -> 2
            else -> 999
        } }
    val RGB = Array(3) { IntArray(N) }
    A.forEachIndexed { index, ch -> RGB[ch][index] = 1 }
    for (m in 0 until 3)
        for (n in (1 until N).reversed()) RGB[m][n - 1] += RGB[m][n]
    fun get(c: Int, n: Int, m: Int): Int {
        var p = RGB[c][m + 1]
        if (p > 0 && m + (m - n) < N && c == A[m + (m - n)]) p -= 1
        return p
    }
    var ans = 0L
    for (n in 0 until N - 2) {
        when (A[n]) {
            R -> {
                for (m in n + 1 until N - 1) {
                    when (A[m]) {
                        G -> ans += get(B, n, m)
                        B -> ans += get(G, n, m)
                        else -> { }
                    }
                }
            }
            G -> {
                for (m in n + 1 until N - 1) {
                    when (A[m]) {
                        R -> ans += get(B, n, m)
                        B -> ans += get(R, n, m)
                        else -> { }
                    }
                }
            }
            B -> {
                for (m in n + 1 until N - 1) {
                    when (A[m]) {
                        R -> ans += get(G, n, m)
                        G -> ans += get(R, n, m)
                        else -> { }
                    }
                }
            }
        }
    }
    return println(ans)
}

fun next() = readLine()!!
fun nextInt(offset: Int = 0) = Integer.parseInt(next()) + offset
fun nextLong() = next().toLong()
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map(String::toInt)
fun listOfLong() = listOfString().map(String::toLong)
fun listOfDouble() = listOfString().map(String::toDouble)
fun listOfInt(offset: Int = 0) = listOfString().map { Integer.parseInt(it) + offset }
fun listOfInt(vararg offset: Int) = listOfString().map { Integer.parseInt(it) }.mapIndexed { index, int -> int + offset[index % offset.size] }
fun listOfLong(offset: Long = 0L) = listOfString().map { java.lang.Long.parseLong(it) + offset }
fun listOfLong(vararg offset: Long) = listOfString().map { java.lang.Long.parseLong(it) }.mapIndexed { index, long -> long + offset[index % offset.size] }
fun listOfDouble(offset: Double = 0.0) = listOfString().map { java.lang.Double.parseDouble(it) + offset }
fun pairIntLong() = listOfLong().let { Pair(it[0].toInt(), it[1]) }
fun tripleIntIntLong() = listOfLong().let { Triple(it[0].toInt(), it[1].toInt(), it[2]) }
fun tripleIntLongLong() = listOfLong().let { Triple(it[0].toInt(), it[1], it[2]) }
fun pairIntLong(offset: Pair<Int, Long> = Pair(0, 0L)) = listOfLong().let { Pair(it[0].toInt() + offset.first, it[1] + offset.second) }
fun tripleIntIntLong(offset: Triple<Int, Int, Long> = Triple(0, 0, 0L)) = listOfLong().let { Triple(it[0].toInt() + offset.first, it[1].toInt() + offset.second, it[2] + offset.third) }
fun tripleIntLongLong(offset: Triple<Int, Long, Long> = Triple(0, 0L, 0L)) = listOfLong().let { Triple(it[0].toInt() + offset.first, it[1] + offset.second, it[2] + offset.third) }


Measured memory_bytes: 42324