#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 };
for (int lst = 0; lst < 3; lst++)
{
for (int pr = 0; pr < 3; pr++)
{
if (lst == pr || idx[lst] == 0)
{
continue;
}
int ii = i, jj = j, kk = k;
if (lst == 0)
{
ii--;
}
else if (lst == 1)
{
jj--;
}
else
{
kk--;
}
if (dp[ii][jj][kk][pr] != INF)
{
int npos = pos[lst][idx[lst] - 1] + after(idx, pos, cnt, lst);
int dist = abs(npos - (i + j + k - 1));
dp[i][j][k][lst] = min(dp[i][j][k][lst], dp[ii][jj][kk][pr] + 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++)
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
780212 KB |
Output is correct |
2 |
Correct |
357 ms |
780216 KB |
Output is correct |
3 |
Correct |
351 ms |
780292 KB |
Output is correct |
4 |
Correct |
399 ms |
780292 KB |
Output is correct |
5 |
Correct |
342 ms |
780180 KB |
Output is correct |
6 |
Correct |
390 ms |
780292 KB |
Output is correct |
7 |
Correct |
324 ms |
780268 KB |
Output is correct |
8 |
Correct |
324 ms |
780200 KB |
Output is correct |
9 |
Correct |
335 ms |
780236 KB |
Output is correct |
10 |
Correct |
334 ms |
780288 KB |
Output is correct |
11 |
Correct |
328 ms |
780208 KB |
Output is correct |
12 |
Correct |
350 ms |
780236 KB |
Output is correct |
13 |
Correct |
330 ms |
780188 KB |
Output is correct |
14 |
Correct |
331 ms |
780272 KB |
Output is correct |
15 |
Correct |
335 ms |
780216 KB |
Output is correct |
16 |
Correct |
325 ms |
780260 KB |
Output is correct |
17 |
Correct |
324 ms |
780396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
780212 KB |
Output is correct |
2 |
Correct |
357 ms |
780216 KB |
Output is correct |
3 |
Correct |
351 ms |
780292 KB |
Output is correct |
4 |
Correct |
399 ms |
780292 KB |
Output is correct |
5 |
Correct |
342 ms |
780180 KB |
Output is correct |
6 |
Correct |
390 ms |
780292 KB |
Output is correct |
7 |
Correct |
324 ms |
780268 KB |
Output is correct |
8 |
Correct |
324 ms |
780200 KB |
Output is correct |
9 |
Correct |
335 ms |
780236 KB |
Output is correct |
10 |
Correct |
334 ms |
780288 KB |
Output is correct |
11 |
Correct |
328 ms |
780208 KB |
Output is correct |
12 |
Correct |
350 ms |
780236 KB |
Output is correct |
13 |
Correct |
330 ms |
780188 KB |
Output is correct |
14 |
Correct |
331 ms |
780272 KB |
Output is correct |
15 |
Correct |
335 ms |
780216 KB |
Output is correct |
16 |
Correct |
325 ms |
780260 KB |
Output is correct |
17 |
Correct |
324 ms |
780396 KB |
Output is correct |
18 |
Correct |
336 ms |
780280 KB |
Output is correct |
19 |
Correct |
336 ms |
780300 KB |
Output is correct |
20 |
Correct |
330 ms |
780212 KB |
Output is correct |
21 |
Correct |
326 ms |
780372 KB |
Output is correct |
22 |
Correct |
335 ms |
780216 KB |
Output is correct |
23 |
Correct |
336 ms |
780340 KB |
Output is correct |
24 |
Correct |
332 ms |
780384 KB |
Output is correct |
25 |
Correct |
326 ms |
780344 KB |
Output is correct |
26 |
Correct |
344 ms |
780220 KB |
Output is correct |
27 |
Correct |
328 ms |
780260 KB |
Output is correct |
28 |
Correct |
338 ms |
780260 KB |
Output is correct |
29 |
Correct |
330 ms |
780240 KB |
Output is correct |
30 |
Correct |
338 ms |
780192 KB |
Output is correct |
31 |
Correct |
360 ms |
780248 KB |
Output is correct |
32 |
Correct |
339 ms |
780296 KB |
Output is correct |
33 |
Correct |
364 ms |
780188 KB |
Output is correct |
34 |
Correct |
348 ms |
780196 KB |
Output is correct |
35 |
Correct |
330 ms |
780304 KB |
Output is correct |
36 |
Correct |
339 ms |
780236 KB |
Output is correct |
37 |
Correct |
364 ms |
780236 KB |
Output is correct |
38 |
Correct |
330 ms |
780340 KB |
Output is correct |
39 |
Correct |
337 ms |
780240 KB |
Output is correct |
40 |
Correct |
333 ms |
780308 KB |
Output is correct |
41 |
Correct |
343 ms |
780356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
780312 KB |
Output is correct |
2 |
Correct |
338 ms |
780324 KB |
Output is correct |
3 |
Correct |
343 ms |
780292 KB |
Output is correct |
4 |
Correct |
356 ms |
780416 KB |
Output is correct |
5 |
Correct |
330 ms |
780288 KB |
Output is correct |
6 |
Correct |
330 ms |
780296 KB |
Output is correct |
7 |
Correct |
335 ms |
780236 KB |
Output is correct |
8 |
Correct |
329 ms |
780248 KB |
Output is correct |
9 |
Correct |
345 ms |
780360 KB |
Output is correct |
10 |
Correct |
328 ms |
780188 KB |
Output is correct |
11 |
Correct |
339 ms |
780280 KB |
Output is correct |
12 |
Correct |
326 ms |
780244 KB |
Output is correct |
13 |
Correct |
342 ms |
780312 KB |
Output is correct |
14 |
Correct |
325 ms |
780396 KB |
Output is correct |
15 |
Correct |
330 ms |
780212 KB |
Output is correct |
16 |
Correct |
336 ms |
780284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
780212 KB |
Output is correct |
2 |
Correct |
357 ms |
780216 KB |
Output is correct |
3 |
Correct |
351 ms |
780292 KB |
Output is correct |
4 |
Correct |
399 ms |
780292 KB |
Output is correct |
5 |
Correct |
342 ms |
780180 KB |
Output is correct |
6 |
Correct |
390 ms |
780292 KB |
Output is correct |
7 |
Correct |
324 ms |
780268 KB |
Output is correct |
8 |
Correct |
324 ms |
780200 KB |
Output is correct |
9 |
Correct |
335 ms |
780236 KB |
Output is correct |
10 |
Correct |
334 ms |
780288 KB |
Output is correct |
11 |
Correct |
328 ms |
780208 KB |
Output is correct |
12 |
Correct |
350 ms |
780236 KB |
Output is correct |
13 |
Correct |
330 ms |
780188 KB |
Output is correct |
14 |
Correct |
331 ms |
780272 KB |
Output is correct |
15 |
Correct |
335 ms |
780216 KB |
Output is correct |
16 |
Correct |
325 ms |
780260 KB |
Output is correct |
17 |
Correct |
324 ms |
780396 KB |
Output is correct |
18 |
Correct |
336 ms |
780280 KB |
Output is correct |
19 |
Correct |
336 ms |
780300 KB |
Output is correct |
20 |
Correct |
330 ms |
780212 KB |
Output is correct |
21 |
Correct |
326 ms |
780372 KB |
Output is correct |
22 |
Correct |
335 ms |
780216 KB |
Output is correct |
23 |
Correct |
336 ms |
780340 KB |
Output is correct |
24 |
Correct |
332 ms |
780384 KB |
Output is correct |
25 |
Correct |
326 ms |
780344 KB |
Output is correct |
26 |
Correct |
344 ms |
780220 KB |
Output is correct |
27 |
Correct |
328 ms |
780260 KB |
Output is correct |
28 |
Correct |
338 ms |
780260 KB |
Output is correct |
29 |
Correct |
330 ms |
780240 KB |
Output is correct |
30 |
Correct |
338 ms |
780192 KB |
Output is correct |
31 |
Correct |
360 ms |
780248 KB |
Output is correct |
32 |
Correct |
339 ms |
780296 KB |
Output is correct |
33 |
Correct |
364 ms |
780188 KB |
Output is correct |
34 |
Correct |
348 ms |
780196 KB |
Output is correct |
35 |
Correct |
330 ms |
780304 KB |
Output is correct |
36 |
Correct |
339 ms |
780236 KB |
Output is correct |
37 |
Correct |
364 ms |
780236 KB |
Output is correct |
38 |
Correct |
330 ms |
780340 KB |
Output is correct |
39 |
Correct |
337 ms |
780240 KB |
Output is correct |
40 |
Correct |
333 ms |
780308 KB |
Output is correct |
41 |
Correct |
343 ms |
780356 KB |
Output is correct |
42 |
Correct |
344 ms |
780312 KB |
Output is correct |
43 |
Correct |
338 ms |
780324 KB |
Output is correct |
44 |
Correct |
343 ms |
780292 KB |
Output is correct |
45 |
Correct |
356 ms |
780416 KB |
Output is correct |
46 |
Correct |
330 ms |
780288 KB |
Output is correct |
47 |
Correct |
330 ms |
780296 KB |
Output is correct |
48 |
Correct |
335 ms |
780236 KB |
Output is correct |
49 |
Correct |
329 ms |
780248 KB |
Output is correct |
50 |
Correct |
345 ms |
780360 KB |
Output is correct |
51 |
Correct |
328 ms |
780188 KB |
Output is correct |
52 |
Correct |
339 ms |
780280 KB |
Output is correct |
53 |
Correct |
326 ms |
780244 KB |
Output is correct |
54 |
Correct |
342 ms |
780312 KB |
Output is correct |
55 |
Correct |
325 ms |
780396 KB |
Output is correct |
56 |
Correct |
330 ms |
780212 KB |
Output is correct |
57 |
Correct |
336 ms |
780284 KB |
Output is correct |
58 |
Execution timed out |
680 ms |
780524 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |