This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
const int MAXN = 1e5+5;
const int MAXA = 250100;
const int MAXK = 1e5+10;
const int INF = 1e18+10;
void chmx(int &a, int b){ a = max(a, b); }
int n;
int x[MAXN], dp[MAXN][4][4][4][4]; // idx 000 000
int arr[5];
signed main(){
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
string s; cin >> s; s = '.'+s;
for(int i=1; i<=n; i++){
if(s[i]=='M') x[i] = 1;
else if(s[i]=='B') x[i] = 2;
else x[i] = 3;
}
for(int i=0; i<=n; i++)
for(int a=0; a<4; a++)
for(int b=0; b<4; b++)
for(int c=0; c<4; c++)
for(int d=0; d<4; d++) dp[i][a][b][c][d] = -INF;
dp[0][0][0][0][0] = 0;
for(int i=1; i<=n; i++){
for(int a=0; a<4; a++){
for(int b=0; b<4; b++){
for(int c=0; c<4; c++){
for(int d=0; d<4; d++){
int f = 0, s = 0;
for(int pp=1; pp<=3; pp++) arr[pp] = 0;
arr[a]++; arr[b]++; arr[x[i]]++;
for(int pp=1;pp<=3;pp++) if(arr[pp]) f++;
chmx(dp[i][b][x[i]][c][d], dp[i-1][a][b][c][d] + f);
for(int pp=1; pp<=3; pp++) arr[pp] = 0;
arr[c]++; arr[d]++; arr[x[i]]++;
for(int pp=1;pp<=3;pp++) if(arr[pp]) s++;
chmx(dp[i][a][b][d][x[i]], dp[i-1][a][b][c][d] + s);
}
}
}
}
}
int ans = 0;
for(int a=0; a<4; a++)
for(int b=0; b<4; b++)
for(int c=0; c<4; c++)
for(int d=0; d<4; d++) chmx(ans, dp[n][a][b][c][d]);
cout << ans << '\n';
}
# | 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... |