#include <bits/stdc++.h>
//qwerty47924692
using namespace std;
using ll = long long;
#define f first
#define s second
const ll N=3e5+29;
ll cur1,cur2,cnt[2],n;
string s;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>s;
for(char i:s){
if(i=='G')cnt[0]++;
else cnt[1]++;
}
ll ans=1e18;
if(abs(cnt[1]-cnt[0])>1){
cout<<-1;
return 0;
}
if(cnt[1]>=cnt[0]){
cur1=0;
cur2=1;
ll sum=0;
for(ll i=0;i<n;i++){
if(s[i]=='R'){
sum+=abs(i-cur1);
cur1+=2;
}
else{
sum+=abs(i-cur2);
cur2+=2;
}
}
ans=min(ans,sum);
}
if(cnt[1]<=cnt[0]){
cur1=1;
cur2=0;
ll sum=0;
for(ll i=0;i<n;i++){
if(s[i]=='R'){
sum+=abs(i-cur1);
//cout<<cur1<<' ';
cur1+=2;
}
else{
sum+=abs(i-cur2);
// cout<<cur2<<' ';
cur2+=2;
}
}
ans=min(ans,sum);
}
cout<<ans/2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
412 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |