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

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define pf push_front
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define dec(x) fixed << setprecision(x)
#define lcm(a, b) (a * b / __gcd(a, b))

typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqmn;
typedef pair<ll, ll> pll;

const ll inf = LLONG_MAX;
const ll mod = 1e9 + 7;
const ld pi = acos(-1);

void solution() {

  ll n;
  cin >> n;
  string s;
  cin >> s;
  int j = n - 1;
  ll op = 0;
  string m = s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      while (s[j] != 'R') {
        j--;
        if (j <= i) {
          goto out;
        }
      }
      swap(s[i], s[j]);
      // cout<<s<<" \\n";
      op++;
    }
  }
out:
  bool flag = false;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (m[i] == 'R') {
      flag = true;
    } else if (m[i] == 'W' && flag) {
      ans++;
    }
  }
  bool flagg = false;
  ll ans2 = 0;
  for (int i = 0; i < n; i++) {
    if (m[i] == 'W') {
      flagg = true;
    } else if (m[i] == 'R' && flag) {
      ans2++;
    }
  }
  ll wh = 0;
  ll rr = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      wh++;
    else
      rr++;
  }
  ll tot = min(op, min(ans, min(wh, min(ans2, rr))));
  cout << tot;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t;
  t = 1;
  // cin>>t;
  while (t--) {
    solution();
    cout << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define pf push_front
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define dec(x) fixed << setprecision(x)
#define lcm(a, b) (a * b / __gcd(a, b))

typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqmn;
typedef pair<ll, ll> pll;

const ll inf = LLONG_MAX;
const ll mod = 1e9 + 7;
const ld pi = acos(-1);

void solution() {

  ll n;
  cin >> n;
  string s;
  cin >> s;
  int j = n - 1;
  ll op = 0;
  string m = s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      while (s[j] != 'R')
        j--;
      if (j < i) {
        break;
      }
      swap(s[i], s[j]);
      // cout<<s<<" \\n";
      op++;
    }
  }
  bool flag = false;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (m[i] == 'R') {
      flag = true;
    } else if (m[i] == 'W' && flag) {
      ans++;
    }
  }
  bool flagg = false;
  ll ans2 = 0;
  for (int i = 0; i < n; i++) {
    if (m[i] == 'W') {
      flagg = true;
    } else if (m[i] == 'R' && flag) {
      ans2++;
    }
  }
  ll wh = 0;
  ll rr = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      wh++;
    else
      rr++;
  }
  ll tot = min(op, min(ans, min(wh, min(ans2, rr))));
  cout << tot;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t;
  t = 1;
  // cin>>t;
  while (t--) {
    solution();
    cout << "\\n";
  }
  return 0;
}