Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
#define F first
#define S second
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define print1(a)                                                              \\
  for (auto x : a)                                                             \\
  cout << x.F << " " << x.S << endl
#define print2(a, x, y)                                                        \\
  for (int i = x; i < y; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl
#define mk(x, y) make_pair(x, y)
typedef long long int ll;
const ll N = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, k, m, l, r, x, y, q, u, z, a[N];

ll pow_mod_m(ll a, ll n) {
  if (!n)
    return 1;
  ll pt = pow_mod_m(a, n / 2);
  pt *= pt;
  if (n & 1)
    pt *= a;
  return pt;
}

int main() {
  IOS;
  int ts = 1;
  ///  cin >> ts ;
  while (ts--) {
    cin >> n;
    string s;
    cin >> s;
    vector<ll> v;
    rep(i, 0, n) {
      if (s[i] == 'R') {
        v.pb(i);
      }
    }
    m = v.size();
    ll ans = 0;
    bool f = 1;
    rep(i, 0, n) {
      while (i < n and s[i] == 'W') {
        if (m - 1 >= 0 and v[m - 1] >= i) {
          /// cout << " current i and v[m-1] : " << i << " " << v[m-1] << endl;
          ans++, m--;
        } else {
          f = 0;
          break;
        }
        i++;
      }
      if (!f)
        break;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
#define F first
#define S second
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define print1(a)                                                              \\
  for (auto x : a)                                                             \\
  cout << x.F << " " << x.S << endl
#define print2(a, x, y)                                                        \\
  for (int i = x; i < y; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl
#define mk(x, y) make_pair(x, y)
typedef long long int ll;
const ll N = 1e3 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, k, m, l, r, x, y, q, u, z, a[N];

ll pow_mod_m(ll a, ll n) {
  if (!n)
    return 1;
  ll pt = pow_mod_m(a, n / 2);
  pt *= pt;
  if (n & 1)
    pt *= a;
  return pt;
}

int main() {
  IOS;
  int ts = 1;
  ///  cin >> ts ;
  while (ts--) {
    cin >> n;
    string s;
    cin >> s;
    vector<ll> v;
    rep(i, 0, n) {
      if (s[i] == 'R') {
        v.pb(i);
      }
    }
    m = v.size();
    ll ans = 0;
    bool f = 1;
    rep(i, 0, n) {
      while (i < n and s[i] == 'W') {
        if (v[m - 1] >= i) {
          /// cout << " current i and v[m-1] : " << i << " " << v[m-1] << endl;
          ans++, m--;
        } else {
          f = 0;
          break;
        }
        i++;
      }
      if (!f)
        break;
    }
    cout << ans << endl;
  }
}