#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];
int after(vector<int> idx, vector<vector<int>>& pos, vector<vector<int>>& cnt, int nd)
{
int res = 0;
for (int i = 0; i < 3; i++)
{
if (i == nd)
{
continue;
}
if (idx[i] > 0 && pos[nd][idx[nd] - 1] < pos[i][idx[i] - 1])
{
res += cnt[i][pos[i][idx[i] - 1]] - cnt[i][pos[nd][idx[nd] - 1]];
}
}
return res;
}
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 = after(idx, pos, cnt, 0), 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 = after(idx, pos, cnt, 1), 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 = after(idx, pos, cnt, 2), 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:97:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 0; i < pos[0].size() + 1; i++)
| ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:99:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int j = 0; j < pos[1].size() + 1; j++)
| ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:101:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for (int k = 0; k < pos[2].size() + 1; k++)
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
325 ms |
780252 KB |
Output is correct |
2 |
Correct |
326 ms |
780252 KB |
Output is correct |
3 |
Correct |
327 ms |
780268 KB |
Output is correct |
4 |
Correct |
327 ms |
780284 KB |
Output is correct |
5 |
Correct |
344 ms |
780256 KB |
Output is correct |
6 |
Correct |
342 ms |
780288 KB |
Output is correct |
7 |
Correct |
328 ms |
780268 KB |
Output is correct |
8 |
Correct |
335 ms |
780256 KB |
Output is correct |
9 |
Correct |
333 ms |
780236 KB |
Output is correct |
10 |
Correct |
333 ms |
780232 KB |
Output is correct |
11 |
Correct |
324 ms |
780412 KB |
Output is correct |
12 |
Correct |
330 ms |
780200 KB |
Output is correct |
13 |
Correct |
360 ms |
780272 KB |
Output is correct |
14 |
Correct |
329 ms |
780352 KB |
Output is correct |
15 |
Correct |
333 ms |
780288 KB |
Output is correct |
16 |
Correct |
328 ms |
780400 KB |
Output is correct |
17 |
Correct |
336 ms |
780236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
325 ms |
780252 KB |
Output is correct |
2 |
Correct |
326 ms |
780252 KB |
Output is correct |
3 |
Correct |
327 ms |
780268 KB |
Output is correct |
4 |
Correct |
327 ms |
780284 KB |
Output is correct |
5 |
Correct |
344 ms |
780256 KB |
Output is correct |
6 |
Correct |
342 ms |
780288 KB |
Output is correct |
7 |
Correct |
328 ms |
780268 KB |
Output is correct |
8 |
Correct |
335 ms |
780256 KB |
Output is correct |
9 |
Correct |
333 ms |
780236 KB |
Output is correct |
10 |
Correct |
333 ms |
780232 KB |
Output is correct |
11 |
Correct |
324 ms |
780412 KB |
Output is correct |
12 |
Correct |
330 ms |
780200 KB |
Output is correct |
13 |
Correct |
360 ms |
780272 KB |
Output is correct |
14 |
Correct |
329 ms |
780352 KB |
Output is correct |
15 |
Correct |
333 ms |
780288 KB |
Output is correct |
16 |
Correct |
328 ms |
780400 KB |
Output is correct |
17 |
Correct |
336 ms |
780236 KB |
Output is correct |
18 |
Correct |
332 ms |
780296 KB |
Output is correct |
19 |
Correct |
334 ms |
780260 KB |
Output is correct |
20 |
Correct |
339 ms |
780216 KB |
Output is correct |
21 |
Correct |
336 ms |
780264 KB |
Output is correct |
22 |
Correct |
331 ms |
780264 KB |
Output is correct |
23 |
Correct |
327 ms |
780468 KB |
Output is correct |
24 |
Correct |
331 ms |
780232 KB |
Output is correct |
25 |
Correct |
342 ms |
780296 KB |
Output is correct |
26 |
Correct |
331 ms |
780288 KB |
Output is correct |
27 |
Correct |
328 ms |
780180 KB |
Output is correct |
28 |
Correct |
332 ms |
780268 KB |
Output is correct |
29 |
Correct |
333 ms |
780236 KB |
Output is correct |
30 |
Correct |
336 ms |
780224 KB |
Output is correct |
31 |
Correct |
331 ms |
780492 KB |
Output is correct |
32 |
Correct |
329 ms |
780280 KB |
Output is correct |
33 |
Correct |
328 ms |
780364 KB |
Output is correct |
34 |
Correct |
327 ms |
780292 KB |
Output is correct |
35 |
Correct |
334 ms |
780296 KB |
Output is correct |
36 |
Correct |
328 ms |
780212 KB |
Output is correct |
37 |
Correct |
334 ms |
780236 KB |
Output is correct |
38 |
Correct |
326 ms |
780284 KB |
Output is correct |
39 |
Correct |
333 ms |
780296 KB |
Output is correct |
40 |
Correct |
330 ms |
780276 KB |
Output is correct |
41 |
Correct |
332 ms |
780232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
324 ms |
780232 KB |
Output is correct |
2 |
Correct |
329 ms |
780264 KB |
Output is correct |
3 |
Correct |
330 ms |
780264 KB |
Output is correct |
4 |
Correct |
331 ms |
780328 KB |
Output is correct |
5 |
Correct |
325 ms |
780292 KB |
Output is correct |
6 |
Correct |
330 ms |
780364 KB |
Output is correct |
7 |
Correct |
321 ms |
780236 KB |
Output is correct |
8 |
Correct |
334 ms |
780296 KB |
Output is correct |
9 |
Correct |
319 ms |
780300 KB |
Output is correct |
10 |
Correct |
324 ms |
780236 KB |
Output is correct |
11 |
Correct |
323 ms |
780308 KB |
Output is correct |
12 |
Correct |
322 ms |
780272 KB |
Output is correct |
13 |
Correct |
355 ms |
780332 KB |
Output is correct |
14 |
Correct |
328 ms |
780276 KB |
Output is correct |
15 |
Correct |
321 ms |
780232 KB |
Output is correct |
16 |
Correct |
327 ms |
780368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
325 ms |
780252 KB |
Output is correct |
2 |
Correct |
326 ms |
780252 KB |
Output is correct |
3 |
Correct |
327 ms |
780268 KB |
Output is correct |
4 |
Correct |
327 ms |
780284 KB |
Output is correct |
5 |
Correct |
344 ms |
780256 KB |
Output is correct |
6 |
Correct |
342 ms |
780288 KB |
Output is correct |
7 |
Correct |
328 ms |
780268 KB |
Output is correct |
8 |
Correct |
335 ms |
780256 KB |
Output is correct |
9 |
Correct |
333 ms |
780236 KB |
Output is correct |
10 |
Correct |
333 ms |
780232 KB |
Output is correct |
11 |
Correct |
324 ms |
780412 KB |
Output is correct |
12 |
Correct |
330 ms |
780200 KB |
Output is correct |
13 |
Correct |
360 ms |
780272 KB |
Output is correct |
14 |
Correct |
329 ms |
780352 KB |
Output is correct |
15 |
Correct |
333 ms |
780288 KB |
Output is correct |
16 |
Correct |
328 ms |
780400 KB |
Output is correct |
17 |
Correct |
336 ms |
780236 KB |
Output is correct |
18 |
Correct |
332 ms |
780296 KB |
Output is correct |
19 |
Correct |
334 ms |
780260 KB |
Output is correct |
20 |
Correct |
339 ms |
780216 KB |
Output is correct |
21 |
Correct |
336 ms |
780264 KB |
Output is correct |
22 |
Correct |
331 ms |
780264 KB |
Output is correct |
23 |
Correct |
327 ms |
780468 KB |
Output is correct |
24 |
Correct |
331 ms |
780232 KB |
Output is correct |
25 |
Correct |
342 ms |
780296 KB |
Output is correct |
26 |
Correct |
331 ms |
780288 KB |
Output is correct |
27 |
Correct |
328 ms |
780180 KB |
Output is correct |
28 |
Correct |
332 ms |
780268 KB |
Output is correct |
29 |
Correct |
333 ms |
780236 KB |
Output is correct |
30 |
Correct |
336 ms |
780224 KB |
Output is correct |
31 |
Correct |
331 ms |
780492 KB |
Output is correct |
32 |
Correct |
329 ms |
780280 KB |
Output is correct |
33 |
Correct |
328 ms |
780364 KB |
Output is correct |
34 |
Correct |
327 ms |
780292 KB |
Output is correct |
35 |
Correct |
334 ms |
780296 KB |
Output is correct |
36 |
Correct |
328 ms |
780212 KB |
Output is correct |
37 |
Correct |
334 ms |
780236 KB |
Output is correct |
38 |
Correct |
326 ms |
780284 KB |
Output is correct |
39 |
Correct |
333 ms |
780296 KB |
Output is correct |
40 |
Correct |
330 ms |
780276 KB |
Output is correct |
41 |
Correct |
332 ms |
780232 KB |
Output is correct |
42 |
Correct |
324 ms |
780232 KB |
Output is correct |
43 |
Correct |
329 ms |
780264 KB |
Output is correct |
44 |
Correct |
330 ms |
780264 KB |
Output is correct |
45 |
Correct |
331 ms |
780328 KB |
Output is correct |
46 |
Correct |
325 ms |
780292 KB |
Output is correct |
47 |
Correct |
330 ms |
780364 KB |
Output is correct |
48 |
Correct |
321 ms |
780236 KB |
Output is correct |
49 |
Correct |
334 ms |
780296 KB |
Output is correct |
50 |
Correct |
319 ms |
780300 KB |
Output is correct |
51 |
Correct |
324 ms |
780236 KB |
Output is correct |
52 |
Correct |
323 ms |
780308 KB |
Output is correct |
53 |
Correct |
322 ms |
780272 KB |
Output is correct |
54 |
Correct |
355 ms |
780332 KB |
Output is correct |
55 |
Correct |
328 ms |
780276 KB |
Output is correct |
56 |
Correct |
321 ms |
780232 KB |
Output is correct |
57 |
Correct |
327 ms |
780368 KB |
Output is correct |
58 |
Correct |
489 ms |
780364 KB |
Output is correct |
59 |
Correct |
497 ms |
780364 KB |
Output is correct |
60 |
Correct |
496 ms |
780320 KB |
Output is correct |
61 |
Correct |
494 ms |
780316 KB |
Output is correct |
62 |
Correct |
328 ms |
780236 KB |
Output is correct |
63 |
Correct |
345 ms |
780292 KB |
Output is correct |
64 |
Correct |
360 ms |
780204 KB |
Output is correct |
65 |
Correct |
399 ms |
780260 KB |
Output is correct |
66 |
Execution timed out |
521 ms |
780240 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |