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"
#define F first
#define S second
#define ll long long
#define pii pair<ll,ll>
const ll mxN = 3e5 + 5;
const ll mod = 1e9 + 7;
using namespace std;
int dp[mxN][4][4][4][4];
signed main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int n;
string s = "#",s1;
cin >>n>>s1;
s += s1;
int ans = 0;
for(int i = 1;i <= n;i++){
int num = (s[i] == 'M' ? 1 : (s[i] == 'B' ? 2 : 3));
for(int a = 1;a < 4;a++){
for(int b = 1; b < 4;b++){
for(int c = 1;c < 4;c++){
for(int d = 1;d < 4;d++){
if(dp[i - 1][a][b][c][d] == -1) continue;
int val;
if(num != a && a != b && a && b) val = 3;
else if(num != a && (a == b || !b)&& a) val = 2;
else if((num == a || !a) && a != b && b) val = 2;
else val = 1;
// cout<<i<<' '<<num<<' '<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<val<<'\n';
dp[i][num][a][c][d] =max(dp[i][num][a][c][d], dp[i - 1][a][b][c][d] + val);
if(num != c && c != d && c && d) val = 3;
else if(num != c && (c == d || !d) && c) val = 2;
else if((num == c || !c) && c != d && d) val = 2;
else val = 1;
// cout<<i<<' '<<num<<' '<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<val<<'\n';
dp[i][a][b][num][c] = max(dp[i][a][b][num][c],dp[i - 1][a][b][c][d] + val);
ans = max({ans,dp[i][num][a][c][d],dp[i][a][b][num][c]});
}
}
}
}
// cout<<ans<<' ';
}
cout<<ans - 6;
}
# | 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... |