Problem p03132 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define forup(i, l, r) for (register int i = l; i <= r; i += lowbit(i))
#define fordown(i, id) for (register int i = id; i; i -= lowbit(i))
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define lowbit(x) (x & (-x))

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pr;

const ll inf = 2e18;
const int N = 3e6 + 10;
const int maxn = 100020;
const ll mod = 1e9 + 7;

int a[maxn], n;
ll sum[maxn], cnt0[maxn], cnt1[maxn], cnt2[maxn], mn2[maxn], mn[maxn];

int main() {
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", &a[i]), sum[i] = a[i] + sum[i - 1];
    cnt0[i] = cnt0[i - 1];
    cnt2[i] = cnt2[i - 1];
    cnt1[i] = cnt1[i - 1];
    if (a[i] % 2 == 0) {
      if (a[i])
        cnt2[i]++;
      else
        cnt0[i]++;
    } else
      cnt1[i]++;
  }
  memset(mn, 0x3f, sizeof(mn));
  memset(mn2, 0x3f, sizeof(mn));

  ll premn = inf, postmn = inf;
  rep(i, 1, n + 1) {
    premn = min(premn, sum[i - 1] - cnt1[i - 1] - 2 * cnt0[i - 1]);
    mn[i] = min(mn[i - 1], premn + cnt0[i - 1] * 2 + cnt1[i - 1] - cnt0[i - 1] -
                               cnt2[i - 1]);
  }
  repd(i, n, 1) {
    postmn = min(postmn, sum[n] - sum[i] + cnt0[i] * 2 + cnt1[i]);
    mn2[i] =
        min(mn2[i + 1], postmn + cnt0[i] + cnt2[i] - cnt0[i] * 2 - cnt1[i]);
  }
  ll ans = inf;
  rep(i, 1, n) ans = min(ans, mn[i + 1] + mn2[i]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define forup(i, l, r) for (register int i = l; i <= r; i += lowbit(i))
#define fordown(i, id) for (register int i = id; i; i -= lowbit(i))
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define lowbit(x) (x & (-x))

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pr;

const ll inf = 2e18;
const int N = 3e6 + 10;
const int maxn = 200020;
const ll mod = 1e9 + 7;

int a[maxn], n;
ll sum[maxn], cnt0[maxn], cnt1[maxn], cnt2[maxn], mn2[maxn], mn[maxn];

int main() {
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", &a[i]), sum[i] = a[i] + sum[i - 1];
    cnt0[i] = cnt0[i - 1];
    cnt2[i] = cnt2[i - 1];
    cnt1[i] = cnt1[i - 1];
    if (a[i] % 2 == 0) {
      if (a[i])
        cnt2[i]++;
      else
        cnt0[i]++;
    } else
      cnt1[i]++;
  }
  memset(mn, 0x3f, sizeof(mn));
  memset(mn2, 0x3f, sizeof(mn));

  ll premn = inf, postmn = inf;
  rep(i, 1, n + 1) {
    premn = min(premn, sum[i - 1] - cnt1[i - 1] - 2 * cnt0[i - 1]);
    mn[i] = min(mn[i - 1], premn + cnt0[i - 1] * 2 + cnt1[i - 1] - cnt0[i - 1] -
                               cnt2[i - 1]);
  }
  repd(i, n, 1) {
    postmn = min(postmn, sum[n] - sum[i] + cnt0[i] * 2 + cnt1[i]);
    mn2[i] =
        min(mn2[i + 1], postmn + cnt0[i] + cnt2[i] - cnt0[i] * 2 - cnt1[i]);
  }
  ll ans = inf;
  rep(i, 1, n) ans = min(ans, mn[i + 1] + mn2[i]);
  cout << ans << endl;
}