Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

/**************************************************
  Solution to: ABC 174 D - Alter Altar
  Writer: InvisibleCyclist
  By/Based on:
**************************************************/
#include <bits/stdc++.h>
using namespace std;
int n;
int res = 0;
int a[200005];
int main() {
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      cnt++;
    }
  }
  for (int i = 0; i < cnt; i++) {
    if (s[i] == 'W') {
      res++;
    }
  }
  cout << res;
  /*	for(int i=0;i<n;i++){
                  if(s[i]=='W'){
                          for(int j=n-1;j>=i;j--){
                                  if(s[j]=='R'){
                                          res++;
                                          s[j]='W';
                                          s[i]='R';
                                          break;
                                  }
                          }
                  }
          }
          for(int i=0;i<n;i++){
                  if(s[i]=='W'){
                          for(int j=i;j<n;j++){
                                  if(s[j]=='R'){
                                          res++;
                                          s[i]==1;
                                          break;
                                  }
                          }
                  }
          }
          cout<<res;
          for(int i=0;i<n;i++){
                  char c;
                  cin>>c;
                  if(c=='R'){
                          a[i]=1;
                  }
                  if(c=='W'){
                          a[i]=2;
                  }
          }
          for(int i=0;i<n;i++){
                  if(a[i]==2){
                          for(int j=n-1;j>=i;j--){
                                  if(a[j]==1){
                                          res++;
                                          a[j]=2;
                                          a[i]=1;
                                          break;
                                  }
                          }
                  }
          }
          for(int i=0;i<n;i++){
                  if(a[i]==2){
                          for(int j=i;j<n;j++){
                                  if(a[j]==1){
                                          res++;
                                          a[i]==1;
                                          break;
                                  }
                          }
                  }
          }
          cout<<res;*/
  return 0;
}


Error: TLE

Fixed submission:

/**************************************************
  Solution to: ABC 174 D - Alter Altar
  Writer: InvisibleCyclist
  By/Based on:
**************************************************/
#include <bits/stdc++.h>
using namespace std;
int n;
int res = 0;
int a[200005];
int main() {
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      for (int j = n - 1; j >= i; j--) {
        if (s[j] == 'R') {
          res++;
          s[j] = 'W';
          s[i] = 'R';
          break;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      for (int j = i; j < n; j++) {
        if (s[j] == 'R') {
          res++;
          s[i] == 1;
          break;
        }
      }
    }
  }
  cout << res;
  /*	for(int i=0;i<n;i++){
                  char c;
                  cin>>c;
                  if(c=='R'){
                          a[i]=1;
                  }
                  if(c=='W'){
                          a[i]=2;
                  }
          }
          for(int i=0;i<n;i++){
                  if(a[i]==2){
                          for(int j=n-1;j>=i;j--){
                                  if(a[j]==1){
                                          res++;
                                          a[j]=2;
                                          a[i]=1;
                                          break;
                                  }
                          }
                  }
          }
          for(int i=0;i<n;i++){
                  if(a[i]==2){
                          for(int j=i;j<n;j++){
                                  if(a[j]==1){
                                          res++;
                                          a[i]==1;
                                          break;
                                  }
                          }
                  }
          }
          cout<<res;*/
  return 0;
}