Problem p03063 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, init, a) for (int i = init; i < a; i++)
#define rep(i, a) FOR(i, 0, a)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()
#define chmin(a, b) a = min(a, b);
#define chmax(a, b) a = max(a, b);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int N;
int cntw[100009], cntb[100009];
int rw[100009], rb[100009];
string stn;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> stn;
  rep(i, N) {
    if (stn[i] == '.') {
      cntw[i]++;
    } else {
      cntb[i]++;
    }
    if (i) {
      cntw[i] += cntw[i - 1];
      cntb[i] += cntb[i - 1];
    }
  }
  rrep(i, N - 1) {
    if (stn[i] == '.') {
      rw[i]++;
    } else {
      rb[i]++;
    }
    if (i < N - 1) {
      rw[i] += rw[i + 1];
      rb[i] += rb[i + 1];
    }
  }
  int ans = min(cntb[N - 1], cntw[N - 1]);
  rep(i, N) {
    int n = cntb[i] + rw[i + 1];
    chmin(ans, n)
  }
  cout1(ans)
}

Fixed submission:
#include "bits/stdc++.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, init, a) for (int i = init; i < a; i++)
#define rep(i, a) FOR(i, 0, a)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()
#define chmin(a, b) a = min(a, b);
#define chmax(a, b) a = max(a, b);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int N;
int cntw[200009], cntb[200009];
int rw[200009], rb[200009];
string stn;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> stn;
  rep(i, N) {
    if (stn[i] == '.') {
      cntw[i]++;
    } else {
      cntb[i]++;
    }
    if (i) {
      cntw[i] += cntw[i - 1];
      cntb[i] += cntb[i - 1];
    }
  }
  rrep(i, N - 1) {
    if (stn[i] == '.') {
      rw[i]++;
    } else {
      rb[i]++;
    }
    if (i < N - 1) {
      rw[i] += rw[i + 1];
      rb[i] += rb[i + 1];
    }
  }
  int ans = min(cntb[N - 1], cntw[N - 1]);
  rep(i, N) {
    int n = cntb[i] + rw[i + 1];
    chmin(ans, n)
  }
  cout1(ans)
}