Problem p02597 (C++)

Buggy submission (status: Runtime Error):
// #####   #####   #####   #####   #####   #       #####   #   #//
// #   #   #   #   #       #       #   #   #       #   #   ## ##//
// #####   #####   #####   #       #####   #       #####   # # #//
// ##      #   #       #   #       #   #   #       #   #   #   #//
// # #     #   #       #   #       #   #   #       #   #   #   #//
// #  #    #   #       #   #       #   #   #       #   #   #   #//
// #   #   #   #   #####   #####   #   #   #####   #   #   #   #//

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ppb pop_back
#define cs(n) cout << n << " "
#define cn(n) cout << n << "\n"
#define rep(i, j, k) for (int i = j; i < k; i++)
#define rrep(i, j, k) for (int i = j; i >= k; i--)
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
ll bin(ll a, ll b, ll mod) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return bin((a * a) % mod, b / 2, mod) % mod;
  return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod;
}
void solve() {
  int n;
  cin >> n;
  int ar[n];
  int c0 = 0, c1 = 0;
  rep(i, 0, n) {
    char ch;
    cin >> ch;
    ar[i] = (ch == 'W' ? 0 : 1);
    if (ar[i] == 0)
      c0++;
    else
      c1++;
  }
  int i = 0, j = n - 1, ans = 0;
  ;
  while (i < j) {
    while (ar[j] != 1) {
      j--;
    }
    if (ar[i] == 1) {
      i++;
      continue;
    }
    if (i >= j) {
      break;
    }
    ar[i] = 1;
    ar[j] = 0;
    i++;
    j--;
    ans++;
  }
  cout << ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
#ifndef ONLINE_JUDGE
  cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
  return 0;
}

Fixed submission:
// #####   #####   #####   #####   #####   #       #####   #   #//
// #   #   #   #   #       #       #   #   #       #   #   ## ##//
// #####   #####   #####   #       #####   #       #####   # # #//
// ##      #   #       #   #       #   #   #       #   #   #   #//
// # #     #   #       #   #       #   #   #       #   #   #   #//
// #  #    #   #       #   #       #   #   #       #   #   #   #//
// #   #   #   #   #####   #####   #   #   #####   #   #   #   #//

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ppb pop_back
#define cs(n) cout << n << " "
#define cn(n) cout << n << "\n"
#define rep(i, j, k) for (int i = j; i < k; i++)
#define rrep(i, j, k) for (int i = j; i >= k; i--)
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
ll bin(ll a, ll b, ll mod) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return bin((a * a) % mod, b / 2, mod) % mod;
  return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod;
}
void solve() {
  int n;
  cin >> n;
  int ar[n];
  int c0 = 0, c1 = 0;
  rep(i, 0, n) {
    char ch;
    cin >> ch;
    ar[i] = (ch == 'W' ? 0 : 1);
    if (ar[i] == 0)
      c0++;
    else
      c1++;
  }
  int i = 0, j = n - 1, ans = 0;
  ;
  while (i < j) {
    while (ar[j] != 1 && j > 0) {
      j--;
    }
    if (ar[i] == 1) {
      i++;
      continue;
    }
    if (i >= j) {
      break;
    }
    ar[i] = 1;
    ar[j] = 0;
    i++;
    j--;
    ans++;
  }
  cout << ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
#ifndef ONLINE_JUDGE
  cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
  return 0;
}