#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e9;
const int N = 405;
char a;
int n, r, g, y, rp[N], gp[N], yp[N], rpref[N], gpref[N], ypref[N];
vector<vector<vector<vector<int>>>> dp;
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a;
rpref[i] += rpref[i - 1]
gpref[i] += gpref[i - 1];
ypref[i] += ypref[i - 1];
if(a == 'R') {rp[++r] = i; rpref[i]++;}
if(a == 'G') {gp[++g] = i; gpref[i]++;}
if(a == 'Y') {yp[++y] = i; ypref[i]++;}
}
if(max({r, g, y}) > (n + 1) / 2){
cout << -1;
return 0;
}
// cout << r << ' ' << g << ' ' << y << '\n';
// int dp[5][r + 5][g + 5][y + 5];
//
// cout << "OK\n";
// cout << sizeof(dp);
// for(int i = 0; i <= 2; ++i){
// for(int j = 0; j <= r; ++j){
// for(int k = 0; k <= g; ++k){
// for(int l = 0; l <= y; ++l){
// dp[i][j][k][l] = inf;
// }
// }
// }
// }
dp.resize(3);
for(auto& i : dp){
i.resize(r + 1);
for(auto& j : i){
j.resize(g + 1);
for(auto& k : j){
k.resize(y + 1);
for(int& x : k){
x = inf;
}
}
}
}
// cout << "OK\n";
// memset(dp, sizeof(dp), inf);
// cout << "OK\n";
dp[0][0][0][0] = 0;
dp[1][0][0][0] = 0;
dp[2][0][0][0] = 0;
int x;
for(int i = 0; i <= r; ++i)
for(int j = 0; j <= g; ++j)
for(int k = 0; k <= y; ++k){
if(i != 0){
x = max(gpref[rp[i]] - j, 0ll) + max(ypref[rp[i]] - k, 0ll);
dp[0][i][j][k] = min({dp[0][i][j][k], dp[1][i - 1][j][k] + x, dp[2][i - 1][j][k] + x});
}
if(j != 0){
x = max(rpref[gp[j]] - i, 0ll) + max(ypref[gp[j]] - k, 0ll);
dp[1][i][j][k] = min({dp[1][i][j][k], dp[0][i][j - 1][k] + x, dp[2][i][j - 1][k] + x});
}
if(k != 0){
x = max(rpref[yp[k]] - i, 0ll) + max(gpref[yp[k]] - j, 0ll);
dp[2][i][j][k] = min({dp[2][i][j][k], dp[0][i][j][k - 1] + x, dp[1][i][j][k - 1] + x});
}
}
cout << min(dp[0][r][g][y], min(dp[1][r][g][y], dp[2][r][g][y]));
return 0;
}
Compilation message
joi2019_ho_t3.cpp:13:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
13 | main(){
| ^~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:22:27: error: expected ';' before 'gpref'
22 | rpref[i] += rpref[i - 1]
| ^
| ;
23 | gpref[i] += gpref[i - 1];
| ~~~~~
joi2019_ho_t3.cpp:77:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
77 | for(int i = 0; i <= r; ++i)
| ^~~
joi2019_ho_t3.cpp:94:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
94 | cout << min(dp[0][r][g][y], min(dp[1][r][g][y], dp[2][r][g][y]));
| ^~~~