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

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
const long long INF = 1 << 29;

const int MOD = 1000000007;
long long ruizyou(long long m, long long n) {
  if (m == 0)
    return 0;
  if (m == 1)
    return 1;
  long long ans = 1;
  long long tmp = m;
  for (int i = 0; i <= 30; i++) {
    if (n & (1 << i)) {
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(long long x) {
  if (x == 0)
    return 1;
  return x * kaizyou(x - 1) % MOD;
}
long long comb(long long x, long long y) {
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x - y) * kaizyou(y) % MOD;
  return bunsi * ruizyou((int)bunbo, MOD - 2) % MOD;
}
struct unionfind {
  vector<int> par;
  vector<int> hukasa;
  unionfind(int n) {
    par = vector<int>(n);
    hukasa = vector<int>(n, 0);
    for (int i = 0; i < n; i++) {
      par.at(i) = i;
    }
  }
  int root(int x) {
    if (par.at(x) == x)
      return x;
    return root(par.at(x));
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (hukasa.at(rx) >= hukasa.at(ry)) {
      par.at(ry) = rx;
      hukasa.at(rx) = max(hukasa.at(ry) + 1, hukasa.at(rx));
    } else {
      par.at(rx) = ry;
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
};
int ctoi(char a) { return a - '0'; }
long long gcd(long long a, long long b) {
  long long c = max(a, b);
  long long d = min(a, b);
  if (d % c == 0)
    return c;
  return gcd(c, d % c);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
  int n;
  cin >> n;
  vector<char> s(n);
  for (int i = 0; i < n; i++)
    cin >> s.at(i);
  int countw = 0;
  vector<int> witi;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'W') {
      countw++;
      witi.push_back(i);
    }
  }
  int ans = 0;
  int ww = 0;
  while (countw) {
    if (s.back() == 'W') {
      s.pop_back();
      countw--;
    } else {
      s.at(witi.at(ww)) = 'R';
      s.at(s.size() - 1) = 'W';
      ww++;
      ans++;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
const long long INF = 1 << 29;

const int MOD = 1000000007;
long long ruizyou(long long m, long long n) {
  if (m == 0)
    return 0;
  if (m == 1)
    return 1;
  long long ans = 1;
  long long tmp = m;
  for (int i = 0; i <= 30; i++) {
    if (n & (1 << i)) {
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(long long x) {
  if (x == 0)
    return 1;
  return x * kaizyou(x - 1) % MOD;
}
long long comb(long long x, long long y) {
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x - y) * kaizyou(y) % MOD;
  return bunsi * ruizyou((int)bunbo, MOD - 2) % MOD;
}
struct unionfind {
  vector<int> par;
  vector<int> hukasa;
  unionfind(int n) {
    par = vector<int>(n);
    hukasa = vector<int>(n, 0);
    for (int i = 0; i < n; i++) {
      par.at(i) = i;
    }
  }
  int root(int x) {
    if (par.at(x) == x)
      return x;
    return root(par.at(x));
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (hukasa.at(rx) >= hukasa.at(ry)) {
      par.at(ry) = rx;
      hukasa.at(rx) = max(hukasa.at(ry) + 1, hukasa.at(rx));
    } else {
      par.at(rx) = ry;
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
};
int ctoi(char a) { return a - '0'; }
long long gcd(long long a, long long b) {
  long long c = max(a, b);
  long long d = min(a, b);
  if (d % c == 0)
    return c;
  return gcd(c, d % c);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
  int n;
  cin >> n;
  vector<char> s(n);
  for (int i = 0; i < n; i++)
    cin >> s.at(i);
  int countw = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'W')
      countw++;
  }
  int ans = 0;
  while (countw) {
    if (s.back() == 'W') {
      s.pop_back();
      countw--;
    } else {
      int a = 0;
      for (int j = 0; j < n; j++) {
        if (s.at(j) == 'W') {
          a = j;
          break;
        }
      }
      s.at(a) = 'R';
      s.at(s.size() - 1) = 'W';
      ans++;
    }
  }
  cout << ans << endl;
}