Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifdef APURBA
#define debug(a) cout << #a << " --> " << (a) << "\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9 + 7;
const int N = 1e6 + 5;
typedef pair<int, int> pii;

void TEST_CASES(int cas) {
  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  int p1 = 0, p2 = n - 1;
  while (1) {
    while (p1 < n && s[p1] != 'W') {
      p1++;
    }
    while (p2 < n && s[p2] != 'R') {
      p2--;
    }
    if (p1 >= p2) {
      break;
    }
    cnt++;
    swap(s[p1], s[p2]);
  }
  printf("%d\n", cnt);
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifdef APURBA
#define debug(a) cout << #a << " --> " << (a) << "\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9 + 7;
const int N = 1e6 + 5;
typedef pair<int, int> pii;

void TEST_CASES(int cas) {
  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  int p1 = 0, p2 = n - 1;
  while (1) {
    while (p1 < n && s[p1] != 'W') {
      p1++;
    }
    while (p2 >= 0 && s[p2] != 'R') {
      p2--;
    }
    if (p1 >= p2) {
      break;
    }
    cnt++;
    swap(s[p1], s[p2]);
  }
  printf("%d\n", cnt);
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}