#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=405,mod=1e16;
int t,dp[N][N][N][4],cnt[4],kth[4][N],aft[4][4][N][N],n;
string s;
main(){
// t=1;
cin >> n;
cin>>s;
s='#'+s;
for(int i=1;i<=n;i++){
if(s[i]=='R') s[i]='0';
else if(s[i]=='Y') s[i]='1';
else s[i]='2';
cnt[s[i]-'0']++;
kth[s[i]-'0'][cnt[s[i]-'0']] = i;
}
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
if(i==j) continue;
for(int k=0;k<=cnt[i];k++){
for(int k1=1;k1<=cnt[j];k1++){
aft[i][j][k][k1] = aft[i][j][k][k1-1] + (kth[j][k1] > kth[i][k]);
}
}
}
}
for(int i=0;i<=n;i++){
for(int j=0;j<=cnt[0];j++){
for(int k=0;k<=cnt[1];k++){
dp[i][j][k][0]=dp[i][j][k][1]=dp[i][j][k][2]=mod;
}} }
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(int i=1;i<=n;i++){
for(int j=0;j<=cnt[0];j++){
for(int k=0;k<=cnt[1];k++){
if(i-k-j>cnt[2]) continue;
if(j) dp[i][j][k][0] = min(dp[i-1][j-1][k][1],dp[i-1][j-1][k][2]) + kth[0][j] + aft[0][1][j][k] + aft[0][2][j][i-k-j] - i;
if(k) dp[i][j][k][1] = min(dp[i-1][j][k-1][2],dp[i-1][j][k-1][0]) + kth[1][k] + aft[1][0][k][j] + aft[1][2][k][i-k-j] - i;
if(i-j-k) dp[i][j][k][2] = min(dp[i-1][j][k][1],dp[i-1][j][k][0]) + kth[2][i-j-k] + aft[2][0][i-j-k][j] + aft[2][1][i-j-k][k] - i;
}
}
}
int c = min(dp[n][cnt[0]][cnt[1]][0],min(dp[n][cnt[0]][cnt[1]][1],dp[n][cnt[0]][cnt[1]][2]));
if(c==mod)cout<<-1;
else cout<<c;
}
Compilation message
joi2019_ho_t3.cpp:9:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
9 | main(){
| ^
/tmp/ccPVd8rA.o: In function `main':
joi2019_ho_t3.cpp:(.text.startup+0xc): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a(globals_io.o)
joi2019_ho_t3.cpp:(.text.startup+0x48): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a(globals_io.o)
joi2019_ho_t3.cpp:(.text.startup+0x5c6): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a(globals_io.o)
joi2019_ho_t3.cpp:(.text.startup+0x605): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a(globals_io.o)
/tmp/ccPVd8rA.o: In function `_GLOBAL__sub_I_t':
joi2019_ho_t3.cpp:(.text.startup+0x627): relocation truncated to fit: R_X86_64_PC32 against `.bss'
joi2019_ho_t3.cpp:(.text.startup+0x641): relocation truncated to fit: R_X86_64_PC32 against `.bss'
/usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a(vterminate.o): In function `__gnu_cxx::__verbose_terminate_handler()':
(.text._ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x1a): relocation truncated to fit: R_X86_64_PC32 against `.bss._ZZN9__gnu_cxx27__verbose_terminate_handlerEvE11terminating'
(.text._ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x27): relocation truncated to fit: R_X86_64_PC32 against `.bss._ZZN9__gnu_cxx27__verbose_terminate_handlerEvE11terminating'
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status