Problem p03063 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define Rep(i, n) for (int i = 0; i < n; i++)
#define For(i, n1, n2) for (int i = n1; i < n2; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define RREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, n1, n2) for (ll i = n1; i < n2; i++)
#define put(a) cout << a << "\n"
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort((a).begin(), (a).end())
#define oorret 0
#define oor(x)                                                                 \
  [&]() {                                                                      \
    try {                                                                      \
      x;                                                                       \
    } catch (const out_of_range &oor) {                                        \
      return oorret;                                                           \
    }                                                                          \
    return x;                                                                  \
  }()
typedef long long ll;
typedef pair<int, int> P;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;

int main() {
  cin >> n;
  string s;
  cin >> s;
  int l = n;
  vector<int> b(n, 0);
  vector<int> w(n, 0);
  vector<int> sb(n + 1, 0);
  vector<int> sw(n + 1, 0);
  vector<int> bp;
  REP(i, n) {
    if (s[i] == '.') {
      w[i] = 1;
    } else {
      bp.push_back(i);
      b[i] = 1;
    }
  }
  REP(i, n + 1) {
    sw[i + 1] = sw[i] + w[i];
    sb[i + 1] = sb[i] + b[i];
  }
  /*REP(i,n){
      if(b[i]==1){
          int temp = sb[i]-sb[0] + sw[n]- sw[i];
          chmin(res,temp);
      }
  }*/
  int res = bp.size();
  REP(i, bp.size()) {
    int temp = i + sw[n] - sw[bp[i]];
    chmin(res, temp);
  }
  put(res);
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define Rep(i, n) for (int i = 0; i < n; i++)
#define For(i, n1, n2) for (int i = n1; i < n2; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define RREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, n1, n2) for (ll i = n1; i < n2; i++)
#define put(a) cout << a << "\n"
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort((a).begin(), (a).end())
#define oorret 0
#define oor(x)                                                                 \
  [&]() {                                                                      \
    try {                                                                      \
      x;                                                                       \
    } catch (const out_of_range &oor) {                                        \
      return oorret;                                                           \
    }                                                                          \
    return x;                                                                  \
  }()
typedef long long ll;
typedef pair<int, int> P;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;

int main() {
  cin >> n;
  string s;
  cin >> s;
  int l = n;
  vector<int> b(n, 0);
  vector<int> w(n, 0);
  vector<int> sb(n + 1, 0);
  vector<int> sw(n + 1, 0);
  vector<int> bp;
  REP(i, n) {
    if (s[i] == '.') {
      w[i] = 1;
    } else {
      bp.push_back(i);
      b[i] = 1;
    }
  }
  REP(i, n) {
    sw[i + 1] = sw[i] + w[i];
    sb[i + 1] = sb[i] + b[i];
  }
  /*REP(i,n){
      if(b[i]==1){
          int temp = sb[i]-sb[0] + sw[n]- sw[i];
          chmin(res,temp);
      }
  }*/
  int res = bp.size();
  REP(i, bp.size()) {
    int temp = i + sw[n] - sw[bp[i]];
    chmin(res, temp);
  }
  put(res);
  return 0;
}