OI XXII - trz

// https://szkopul.edu.pl/problemset/problem/Grfouq9u3g_TYktFXO2sNjCU/site/?key=statement

#include <bits/stdc++.h>

// #define GARY_DBG
#define GARY_LIB

constexpr int sizik = 1000 * 1001;

#define ar std::array

typedef std::vector<std::vector<int>> _kra;

int w[sizik][3];
int n;

int a, b, c;

bool p(int x, int y) {
    return (x == 0 && y == 0) || (x != y);
}

bool check(int i, int j) {
    a = w[j][0] - w[i - 1][0];
    b = w[j][1] - w[i - 1][1];
    c = w[j][2] - w[i - 1][2];

    return p(a, b) && p(a, c) && p(b, c);
}

void brut() {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            if (check(i, j)) {
                ans = std::max(ans, j - i + 1);
            }
        }
    }
    std::cout << ans << '\n';
}

void wzor() {
    int ans = 0;
    for (int i = 1; i <= 3; i++) {
        for (int j = i; j <= n; j++) {
            if (check(i, j)) {
                ans = std::max(ans, j - i + 1);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = n - 3; j <= n; j++) {
            if (i > j) continue;
            if (check(i, j)) {
                ans = std::max(ans, j - i + 1);
            }
        }
    }
    std::cout << ans << '\n';
}

void solve() {
    std::cin >> n;

    for (int i = 1; i <= n; i++) {
        char c;
        std::cin >> c;

        if (c == 'S') {
            w[i][0] = 1;
        } else if (c == 'B') {
            w[i][1] = 1;
        } else {
            w[i][2] = 1;
        }
        w[i][0] += w[i - 1][0];
        w[i][1] += w[i - 1][1];
        w[i][2] += w[i - 1][2];
    }

    if (n <= 9) {
        brut();
    } else {
        wzor();
    }
}

int32_t main() {
#ifndef GARY_DBG
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
#endif

    int t = 1;
    // std::cin >> t;

    for (; t > 0; t--) {
        solve();
    }

    return 0;
}