Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < b + 1; i++)
#define all(v) v.begin(), v.end()
#define SENTINEL 2000000000
#define NIL -1
using namespace std;
typedef long long ll;

const ll MAX = 510000;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> white;
  vector<int> red;
  int tmp = 0;
  REP(i, n) {
    if (s[i] == 'W') {
      tmp++;
    } else {
      if (tmp != 0) {
        white.push_back(tmp);
        tmp = 0;
      }
    }
  }
  tmp = 0;
  REP(i, n) {
    if (i == 0 && s[i] == 'R') {
      tmp = -1;
    }
    if (s[i] == 'R') {
      if (tmp == -1) {
        continue;
      }
      tmp++;
    } else {
      if (tmp > 0) {
        red.push_back(tmp);
      }
      tmp = 0;
    }
  }
  if (tmp != 0) {
    red.push_back(tmp);
  }
  int ans = 0;
  int nowr = red.size() - 1;
  if (nowr == -1) {
    cout << ans << endl;
    return 0;
  }
  int wsize = white.size();
  for (int i = 0; i < wsize; i++) {
    if (i == nowr) {
      ans += min(white[i], red[nowr]);
      break;
    }
    if (red[nowr] < white[i]) {
      white[i] -= red[nowr];
      ans += red[nowr];
      red[nowr] = 0;
      nowr--;
      i--;
    } else if (red[nowr] == white[i]) {
      ans += red[nowr];
      red[nowr] = white[i] = 0;
      nowr--;
    } else {
      ans += white[i];
      red[nowr] -= white[i];
      white[i] = 0;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < b + 1; i++)
#define all(v) v.begin(), v.end()
#define SENTINEL 2000000000
#define NIL -1
using namespace std;
typedef long long ll;

const ll MAX = 510000;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> white;
  vector<int> red;
  int tmp = 0;
  REP(i, n) {
    if (s[i] == 'W') {
      tmp++;
    } else {
      if (tmp != 0) {
        white.push_back(tmp);
        tmp = 0;
      }
    }
  }
  tmp = 0;
  REP(i, n) {
    if (i == 0 && s[i] == 'R') {
      tmp = -1;
    }
    if (s[i] == 'R') {
      if (tmp == -1) {
        continue;
      }
      tmp++;
    } else {
      if (tmp > 0) {
        red.push_back(tmp);
      }
      tmp = 0;
    }
  }
  if (tmp != 0) {
    red.push_back(tmp);
  }
  int ans = 0;
  int nowr = red.size() - 1;
  if (nowr == -1) {
    cout << ans << endl;
    return 0;
  }
  int wsize = white.size();
  for (int i = 0; i < wsize; i++) {
    if (i == nowr) {
      ans += min(white[i], red[nowr]);
      break;
    }
    if (i > nowr) {
      break;
    }
    if (red[nowr] < white[i]) {
      white[i] -= red[nowr];
      ans += red[nowr];
      red[nowr] = 0;
      nowr--;
      i--;
    } else if (red[nowr] == white[i]) {
      ans += red[nowr];
      red[nowr] = white[i] = 0;
      nowr--;
    } else {
      ans += white[i];
      red[nowr] -= white[i];
      white[i] = 0;
    }
  }
  cout << ans << endl;
}