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

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define mp make_pair
#define eb emplace_back
#define f first
#define s second
#define sz(x) (ll) x.size()
#define lnt(s) (ll) s.length()
#define mt(cnt) cnt.empty()
using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    osSet;

using pll = pair<ll, ll>;
using pl = pair<ll, pll>;
using tll = tuple<ll, ll, ll>;

const ll maxN = 1e5;
const ll INF = 1e17;
const ll MOD = 1e9 + 7;

ll xs[] = {-1, 1, 0, 0, -1, 1, 1, -1};
ll ys[] = {0, 0, -1, 1, -1, 1, -1, 1};

ll segtree[maxN << 2];
ll lazy[maxN << 2];
ll A[maxN];

inline ll left(ll x) { return x << 1; }

inline ll right(ll x) { return (x << 1) + 1; }

ll func(ll x) { return (x * (x + 1)) / 2; }

ll _pow(ll b, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return b;
  ll exB = b * b;
  if (p & 1)
    return b * _pow(exB, p / 2);
  return _pow(exB, p / 2);
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  ll n;
  string s;
  cin >> n >> s;
  ll l = 0, r = lnt(s) - 1;
  ll ans = 0;
  while (l < r) {
    if (s[l] == 'W' and s[r] == 'R') {
      ans++;
      l++;
      r--;
    }
    while (s[r] != 'R' and l < r)
      r--;
    while (s[l] != 'W' and l < r)
      l++;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define mp make_pair
#define eb emplace_back
#define f first
#define s second
#define sz(x) (ll) x.size()
#define lnt(s) (ll) s.length()
#define mt(cnt) cnt.empty()
using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    osSet;

using pll = pair<ll, ll>;
using pl = pair<ll, pll>;
using tll = tuple<ll, ll, ll>;

const ll maxN = 1e5;
const ll INF = 1e17;
const ll MOD = 1e9 + 7;

ll xs[] = {-1, 1, 0, 0, -1, 1, 1, -1};
ll ys[] = {0, 0, -1, 1, -1, 1, -1, 1};

ll segtree[maxN << 2];
ll lazy[maxN << 2];
ll A[maxN];

inline ll left(ll x) { return x << 1; }

inline ll right(ll x) { return (x << 1) + 1; }

ll func(ll x) { return (x * (x + 1)) / 2; }

ll _pow(ll b, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return b;
  ll exB = b * b;
  if (p & 1)
    return b * _pow(exB, p / 2);
  return _pow(exB, p / 2);
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  ll n;
  string s;
  cin >> n >> s;
  ll l = 0, r = lnt(s) - 1;
  ll ans = 0;
  while (l < r) {
    if (s[l] == 'W' and s[r] == 'R') {
      ans++;
      l++;
      r--;
    }
    while (s[r] != 'R')
      r--;
    while (s[l] != 'W')
      l++;
  }
  cout << ans;
  return 0;
}