#include <bits/stdc++.h>
using namespace std;
inline int val(char ch){ return ch=='M'?0: ch=='F'?1:2; }
inline int gain3(int x,int y,int z){
    bool s0 = (x==0)||(y==0)||(z==0);
    bool s1 = (x==1)||(y==1)||(z==1);
    bool s2 = (x==2)||(y==2)||(z==2);
    return (x==3 && y==3 && z==3) ? 0 : (s0+s1+s2);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; string s; cin >> n >> s;
    const int INF = -1e9;
    static int dp[16][16], ndp[16][16];
    for(int i=0;i<16;i++) for(int j=0;j<16;j++) dp[i][j]=INF;
    dp[15][15]=0;
    for(char ch: s){
        int c = val(ch);
        for(int i=0;i<16;i++) for(int j=0;j<16;j++) ndp[i][j]=INF;
        for(int sa=0; sa<16; sa++){
            int a0 = sa/4, a1 = sa%4;
            for(int sb=0; sb<16; sb++){
                int cur = dp[sa][sb];
                if(cur==INF) continue;
                int b0 = sb/4, b1 = sb%4;
                int ga = gain3(a0,a1,c);
                int na = 4*a1 + c;
                ndp[na][sb] = max(ndp[na][sb], cur + ga);
                int gb = gain3(b0,b1,c);
                int nb = 4*b1 + c;
                ndp[sa][nb] = max(ndp[sa][nb], cur + gb);
            }
        }
        for(int i=0;i<16;i++) for(int j=0;j<16;j++) dp[i][j]=ndp[i][j];
    }
    int ans = 0;
    for(int i=0;i<16;i++) for(int j=0;j<16;j++) ans = max(ans, dp[i][j]);
    cout << ans << endl;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |