Problem p03063 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define x first
#define y second
#define all(aaa) (aaa).begin(), (aaa).end()
#define REP(i, sss, eee) for (int i = sss; i <= eee; ++i)
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define N 1000000007
#define inf 1000000000000000000
using namespace __gnu_pbds;
using namespace std;
template <class T> T MAX(T a, T b) { return ((a > b) ? a : b); }
template <class T> T MIN(T a, T b) { return ((a < b) ? a : b); }
template <class T> T MOD(T a) { return ((a >= 0) ? a : (-1 * a)); }
template <class T> void SWAP(T &a, T &b) {
  T temp = a;
  a = b;
  b = temp;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; // for multiset use less_equal above

inline int ex(int a, int b) {
  if (b == 0 || a == 1) {
    return 1;
  }
  if (a == 0 || b == 1) {
    return a;
  }
  int tmp = ex(a, (b / 2));
  if (b % 2) {
    return ((((tmp * tmp) % N) * a) % N);
  } else {
    return ((tmp * tmp) % N);
  }
}
inline int inv(int k) {
  if (k == 1) {
    return 1;
  } else {
    return ex(k, N - 2);
  }
}
/*****************default********************/
int solve() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  std::vector<int> pref(n + 1, 0);
  REP(i, 0, n - 1) {
    pref[i + 1] = pref[i];
    if (str[i] == '#') {
      pref[i + 1]++;
    }
  }
  std::vector<int> suff(n + 1, 0);
  for (int i = n - 1; i >= 0; --i) {
    suff[i] = suff[i + 1];
    if (str[i] == '.') {
      ++suff[i];
    }
  }
  int ans = 10000000000000000;
  REP(partition, 0, n) { ans = MIN(ans, (pref[partition] + suff[partition])); }
  cout << ans << "\n";
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  speed int tt = 1;
  // cin>>tt;
  while (tt--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define x first
#define y second
#define all(aaa) (aaa).begin(), (aaa).end()
#define REP(i, sss, eee) for (int i = sss; i <= eee; ++i)
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define N 1000000007
#define inf 1000000000000000000
using namespace __gnu_pbds;
using namespace std;
template <class T> T MAX(T a, T b) { return ((a > b) ? a : b); }
template <class T> T MIN(T a, T b) { return ((a < b) ? a : b); }
template <class T> T MOD(T a) { return ((a >= 0) ? a : (-1 * a)); }
template <class T> void SWAP(T &a, T &b) {
  T temp = a;
  a = b;
  b = temp;
}

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; // for multiset use less_equal above

inline int ex(int a, int b) {
  if (b == 0 || a == 1) {
    return 1;
  }
  if (a == 0 || b == 1) {
    return a;
  }
  int tmp = ex(a, (b / 2));
  if (b % 2) {
    return ((((tmp * tmp) % N) * a) % N);
  } else {
    return ((tmp * tmp) % N);
  }
}
inline int inv(int k) {
  if (k == 1) {
    return 1;
  } else {
    return ex(k, N - 2);
  }
}
/*****************default********************/
int solve() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  std::vector<int> pref(n + 1, 0);
  REP(i, 0, n - 1) {
    pref[i + 1] = pref[i];
    if (str[i] == '#') {
      pref[i + 1]++;
    }
  }
  std::vector<int> suff(n + 1, 0);
  for (int i = n - 1; i >= 0; --i) {
    suff[i] = suff[i + 1];
    if (str[i] == '.') {
      ++suff[i];
    }
  }
  int ans = 10000000000000000;
  REP(partition, 0, n) { ans = MIN(ans, (pref[partition] + suff[partition])); }
  cout << ans << "\n";
}
signed main() {
  speed int tt = 1;
  // cin>>tt;
  while (tt--) {
    solve();
  }
  return 0;
}