#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <ctime>
#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <fstream>
#include <functional>
#include <complex>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e9, MOD = 1e9 + 7, MOD2 = 998244353, LOG = 18;
const long double EPS = 1e-9, PI = acos(-1);
const int N = 405;
int dp[N][N][N][3];
void solve()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
for (int lst = 0; lst < 3; lst++)
{
dp[i][j][k][lst] = INF;
}
}
}
}
for (int lst = 0; lst < 3; lst++)
{
dp[0][0][0][lst] = 0;
}
int n;
string s;
cin >> n >> s;
vector<vector<int>> pos(3), cnt(3, vector<int>(n));
for (int i = 0; i < n; i++)
{
if (s[i] == 'R')
{
pos[0].push_back(i);
}
else if (s[i] == 'G')
{
pos[1].push_back(i);
}
else
{
pos[2].push_back(i);
}
for (int j = 0; j < 3; j++)
{
cnt[j][i] = pos[j].size();
}
}
for (int i = 0; i < pos[0].size() + 1; i++)
{
for (int j = 0; j < pos[1].size() + 1; j++)
{
for (int k = 0; k < pos[2].size() + 1; k++)
{
if (max({ i, j, k }) >= (i + j + k + 1) / 2 + 1)
{
continue;
}
vector<int> idx = { i, j, k };
if (idx[0] > 0)
{
int aft = max(idx[1] - cnt[1][pos[0][idx[0] - 1]], 0) + max(idx[2] - cnt[2][pos[0][idx[0] - 1]], 0);
int npos = pos[0][idx[0] - 1] + aft;
int dist = abs(npos - (i + j + k - 1));
dp[i][j][k][0] = min(dp[i][j][k][0], min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + dist);
}
if (idx[1] > 0)
{
int aft = max(idx[0] - cnt[0][pos[1][idx[1] - 1]], 0) + max(idx[2] - cnt[2][pos[1][idx[1] - 1]], 0);
int npos = pos[1][idx[1] - 1] + aft;
int dist = abs(npos - (i + j + k - 1));
dp[i][j][k][1] = min(dp[i][j][k][1], min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + dist);
}
if (idx[2] > 0)
{
int aft = max(idx[0] - cnt[0][pos[2][idx[2] - 1]], 0) + max(idx[1] - cnt[1][pos[2][idx[2] - 1]], 0);
int npos = pos[2][idx[2] - 1] + aft;
int dist = abs(npos - (i + j + k - 1));
dp[i][j][k][2] = min(dp[i][j][k][2], min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + dist);
}
}
}
}
int ans = INF;
for (int lst = 0; lst < 3; lst++)
{
ans = min(ans, dp[pos[0].size()][pos[1].size()][pos[2].size()][lst]);
}
cout << (ans == INF ? -1 : ans) << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(time(NULL));
int tst = 1;
//cin >> tst;
while (tst--)
{
solve();
}
return 0;
}
/*
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀
⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀
⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀
⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀
⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁
⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀
⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀
⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾⠀⠀
⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇⠀⠀
⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇⠀⠀
⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧⠀⠀
⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸⠀⠀
⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇⠀
⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀
⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧
⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘
⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀⠀⠀⠀⠀
⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
*/
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < pos[0].size() + 1; i++)
| ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:81:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int j = 0; j < pos[1].size() + 1; j++)
| ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:83:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int k = 0; k < pos[2].size() + 1; k++)
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
325 ms |
780220 KB |
Output is correct |
2 |
Correct |
334 ms |
780232 KB |
Output is correct |
3 |
Correct |
336 ms |
780188 KB |
Output is correct |
4 |
Correct |
338 ms |
780300 KB |
Output is correct |
5 |
Correct |
332 ms |
780196 KB |
Output is correct |
6 |
Correct |
325 ms |
780304 KB |
Output is correct |
7 |
Correct |
334 ms |
780272 KB |
Output is correct |
8 |
Correct |
329 ms |
780280 KB |
Output is correct |
9 |
Correct |
330 ms |
780228 KB |
Output is correct |
10 |
Correct |
330 ms |
780196 KB |
Output is correct |
11 |
Correct |
330 ms |
780264 KB |
Output is correct |
12 |
Correct |
332 ms |
780364 KB |
Output is correct |
13 |
Correct |
333 ms |
780200 KB |
Output is correct |
14 |
Correct |
334 ms |
780276 KB |
Output is correct |
15 |
Correct |
334 ms |
780268 KB |
Output is correct |
16 |
Correct |
334 ms |
780200 KB |
Output is correct |
17 |
Correct |
330 ms |
780248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
325 ms |
780220 KB |
Output is correct |
2 |
Correct |
334 ms |
780232 KB |
Output is correct |
3 |
Correct |
336 ms |
780188 KB |
Output is correct |
4 |
Correct |
338 ms |
780300 KB |
Output is correct |
5 |
Correct |
332 ms |
780196 KB |
Output is correct |
6 |
Correct |
325 ms |
780304 KB |
Output is correct |
7 |
Correct |
334 ms |
780272 KB |
Output is correct |
8 |
Correct |
329 ms |
780280 KB |
Output is correct |
9 |
Correct |
330 ms |
780228 KB |
Output is correct |
10 |
Correct |
330 ms |
780196 KB |
Output is correct |
11 |
Correct |
330 ms |
780264 KB |
Output is correct |
12 |
Correct |
332 ms |
780364 KB |
Output is correct |
13 |
Correct |
333 ms |
780200 KB |
Output is correct |
14 |
Correct |
334 ms |
780276 KB |
Output is correct |
15 |
Correct |
334 ms |
780268 KB |
Output is correct |
16 |
Correct |
334 ms |
780200 KB |
Output is correct |
17 |
Correct |
330 ms |
780248 KB |
Output is correct |
18 |
Correct |
332 ms |
780304 KB |
Output is correct |
19 |
Correct |
326 ms |
780256 KB |
Output is correct |
20 |
Correct |
344 ms |
780268 KB |
Output is correct |
21 |
Correct |
357 ms |
780272 KB |
Output is correct |
22 |
Correct |
341 ms |
780260 KB |
Output is correct |
23 |
Correct |
330 ms |
780236 KB |
Output is correct |
24 |
Correct |
331 ms |
780344 KB |
Output is correct |
25 |
Correct |
330 ms |
780380 KB |
Output is correct |
26 |
Correct |
329 ms |
780240 KB |
Output is correct |
27 |
Correct |
339 ms |
780220 KB |
Output is correct |
28 |
Correct |
331 ms |
780228 KB |
Output is correct |
29 |
Correct |
328 ms |
780296 KB |
Output is correct |
30 |
Correct |
351 ms |
780272 KB |
Output is correct |
31 |
Correct |
336 ms |
780392 KB |
Output is correct |
32 |
Correct |
332 ms |
780404 KB |
Output is correct |
33 |
Correct |
331 ms |
780492 KB |
Output is correct |
34 |
Correct |
327 ms |
780344 KB |
Output is correct |
35 |
Correct |
338 ms |
780224 KB |
Output is correct |
36 |
Correct |
333 ms |
780236 KB |
Output is correct |
37 |
Correct |
331 ms |
780252 KB |
Output is correct |
38 |
Correct |
328 ms |
780296 KB |
Output is correct |
39 |
Correct |
326 ms |
780308 KB |
Output is correct |
40 |
Correct |
328 ms |
780284 KB |
Output is correct |
41 |
Correct |
332 ms |
780264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
331 ms |
780204 KB |
Output is correct |
2 |
Correct |
331 ms |
780196 KB |
Output is correct |
3 |
Correct |
329 ms |
780396 KB |
Output is correct |
4 |
Correct |
337 ms |
780312 KB |
Output is correct |
5 |
Correct |
332 ms |
780308 KB |
Output is correct |
6 |
Correct |
356 ms |
780224 KB |
Output is correct |
7 |
Correct |
347 ms |
780244 KB |
Output is correct |
8 |
Correct |
346 ms |
780192 KB |
Output is correct |
9 |
Correct |
324 ms |
780232 KB |
Output is correct |
10 |
Correct |
336 ms |
780268 KB |
Output is correct |
11 |
Correct |
332 ms |
780368 KB |
Output is correct |
12 |
Correct |
333 ms |
780400 KB |
Output is correct |
13 |
Correct |
332 ms |
780236 KB |
Output is correct |
14 |
Correct |
333 ms |
780200 KB |
Output is correct |
15 |
Correct |
336 ms |
780276 KB |
Output is correct |
16 |
Correct |
328 ms |
780244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
325 ms |
780220 KB |
Output is correct |
2 |
Correct |
334 ms |
780232 KB |
Output is correct |
3 |
Correct |
336 ms |
780188 KB |
Output is correct |
4 |
Correct |
338 ms |
780300 KB |
Output is correct |
5 |
Correct |
332 ms |
780196 KB |
Output is correct |
6 |
Correct |
325 ms |
780304 KB |
Output is correct |
7 |
Correct |
334 ms |
780272 KB |
Output is correct |
8 |
Correct |
329 ms |
780280 KB |
Output is correct |
9 |
Correct |
330 ms |
780228 KB |
Output is correct |
10 |
Correct |
330 ms |
780196 KB |
Output is correct |
11 |
Correct |
330 ms |
780264 KB |
Output is correct |
12 |
Correct |
332 ms |
780364 KB |
Output is correct |
13 |
Correct |
333 ms |
780200 KB |
Output is correct |
14 |
Correct |
334 ms |
780276 KB |
Output is correct |
15 |
Correct |
334 ms |
780268 KB |
Output is correct |
16 |
Correct |
334 ms |
780200 KB |
Output is correct |
17 |
Correct |
330 ms |
780248 KB |
Output is correct |
18 |
Correct |
332 ms |
780304 KB |
Output is correct |
19 |
Correct |
326 ms |
780256 KB |
Output is correct |
20 |
Correct |
344 ms |
780268 KB |
Output is correct |
21 |
Correct |
357 ms |
780272 KB |
Output is correct |
22 |
Correct |
341 ms |
780260 KB |
Output is correct |
23 |
Correct |
330 ms |
780236 KB |
Output is correct |
24 |
Correct |
331 ms |
780344 KB |
Output is correct |
25 |
Correct |
330 ms |
780380 KB |
Output is correct |
26 |
Correct |
329 ms |
780240 KB |
Output is correct |
27 |
Correct |
339 ms |
780220 KB |
Output is correct |
28 |
Correct |
331 ms |
780228 KB |
Output is correct |
29 |
Correct |
328 ms |
780296 KB |
Output is correct |
30 |
Correct |
351 ms |
780272 KB |
Output is correct |
31 |
Correct |
336 ms |
780392 KB |
Output is correct |
32 |
Correct |
332 ms |
780404 KB |
Output is correct |
33 |
Correct |
331 ms |
780492 KB |
Output is correct |
34 |
Correct |
327 ms |
780344 KB |
Output is correct |
35 |
Correct |
338 ms |
780224 KB |
Output is correct |
36 |
Correct |
333 ms |
780236 KB |
Output is correct |
37 |
Correct |
331 ms |
780252 KB |
Output is correct |
38 |
Correct |
328 ms |
780296 KB |
Output is correct |
39 |
Correct |
326 ms |
780308 KB |
Output is correct |
40 |
Correct |
328 ms |
780284 KB |
Output is correct |
41 |
Correct |
332 ms |
780264 KB |
Output is correct |
42 |
Correct |
331 ms |
780204 KB |
Output is correct |
43 |
Correct |
331 ms |
780196 KB |
Output is correct |
44 |
Correct |
329 ms |
780396 KB |
Output is correct |
45 |
Correct |
337 ms |
780312 KB |
Output is correct |
46 |
Correct |
332 ms |
780308 KB |
Output is correct |
47 |
Correct |
356 ms |
780224 KB |
Output is correct |
48 |
Correct |
347 ms |
780244 KB |
Output is correct |
49 |
Correct |
346 ms |
780192 KB |
Output is correct |
50 |
Correct |
324 ms |
780232 KB |
Output is correct |
51 |
Correct |
336 ms |
780268 KB |
Output is correct |
52 |
Correct |
332 ms |
780368 KB |
Output is correct |
53 |
Correct |
333 ms |
780400 KB |
Output is correct |
54 |
Correct |
332 ms |
780236 KB |
Output is correct |
55 |
Correct |
333 ms |
780200 KB |
Output is correct |
56 |
Correct |
336 ms |
780276 KB |
Output is correct |
57 |
Correct |
328 ms |
780244 KB |
Output is correct |
58 |
Correct |
380 ms |
780244 KB |
Output is correct |
59 |
Correct |
392 ms |
780236 KB |
Output is correct |
60 |
Correct |
382 ms |
780400 KB |
Output is correct |
61 |
Correct |
395 ms |
780236 KB |
Output is correct |
62 |
Correct |
346 ms |
780240 KB |
Output is correct |
63 |
Correct |
377 ms |
780324 KB |
Output is correct |
64 |
Correct |
355 ms |
780388 KB |
Output is correct |
65 |
Correct |
357 ms |
780208 KB |
Output is correct |
66 |
Correct |
381 ms |
780252 KB |
Output is correct |
67 |
Correct |
371 ms |
780276 KB |
Output is correct |
68 |
Correct |
399 ms |
780320 KB |
Output is correct |
69 |
Correct |
389 ms |
780308 KB |
Output is correct |
70 |
Correct |
382 ms |
780328 KB |
Output is correct |
71 |
Correct |
397 ms |
780300 KB |
Output is correct |
72 |
Correct |
346 ms |
780260 KB |
Output is correct |
73 |
Correct |
394 ms |
780228 KB |
Output is correct |