Problem p03132 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define N 200010
char getc() {
  char c = getchar();
  while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
    c = getchar();
  return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
int n, a[N], delta[N];
ll f[N][5];
// ll ans,f[N][2],s[N],g[N][2],h[N];
int main() {
  freopen("d.in", "r", stdin);
  freopen("d.out", "w", stdout);
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  /*for (int i=1;i<=n;i++)
  if (a[i]==0) delta[i]=2;
  else delta[i]=a[i]&1;
  for (int i=1;i<=n;i++) g[i][0]=g[i-1][0]+delta[i],s[i]=s[i-1]+a[i];
  ll u=0;
  for (int i=1;i<=n;i++)
  {
          u=min(u,s[i]-g[i][0]);
          f[i][0]=g[i][0]+u;
  }
  for (int i=n;i>=1;i--) g[i][1]=g[i+1][1]+delta[i],s[i]=s[i+1]+a[i];
  u=0;
  for (int i=n;i>=1;i--)
  {
          u=min(u,s[i]-g[i][1]);
          f[i][1]=g[i][1]+u;
  }
  for (int i=1;i<=n;i++) ans+=a[i];
  u=0;
  for (int i=1;i<=n;i++) ans=min(ans,f[i][1]+s[i-1]);
  for (int i=1;i<=n;i++) h[i]=h[i-1]+(a[i]&1^1);
  for (int i=1;i<=n+1;i++)
  {
          ans=min(ans,f[i][1]+u+h[i-1]);
          u=min(u,f[i][0]-h[i]);
  }*/
  for (int i = 1; i <= n; i++) {
    f[i][0] = f[i - 1][0];
    for (int j = 1; j <= 4; j++)
      f[i][j] = min(f[i][j - 1], f[i - 1][j]);
    f[i][0] += a[i], f[i][4] += a[i];
    f[i][1] += a[i] == 0 ? 2 : (a[i] & 1);
    f[i][3] += a[i] == 0 ? 2 : (a[i] & 1);
    f[i][2] += a[i] & 1 ^ 1;
  }
  ll ans = f[n][0];
  for (int i = 1; i <= 4; i++)
    ans = min(ans, f[n][i]);
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define N 200010
char getc() {
  char c = getchar();
  while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
    c = getchar();
  return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
int n, a[N], delta[N];
ll f[N][5];
// ll ans,f[N][2],s[N],g[N][2],h[N];
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  /*for (int i=1;i<=n;i++)
  if (a[i]==0) delta[i]=2;
  else delta[i]=a[i]&1;
  for (int i=1;i<=n;i++) g[i][0]=g[i-1][0]+delta[i],s[i]=s[i-1]+a[i];
  ll u=0;
  for (int i=1;i<=n;i++)
  {
          u=min(u,s[i]-g[i][0]);
          f[i][0]=g[i][0]+u;
  }
  for (int i=n;i>=1;i--) g[i][1]=g[i+1][1]+delta[i],s[i]=s[i+1]+a[i];
  u=0;
  for (int i=n;i>=1;i--)
  {
          u=min(u,s[i]-g[i][1]);
          f[i][1]=g[i][1]+u;
  }
  for (int i=1;i<=n;i++) ans+=a[i];
  u=0;
  for (int i=1;i<=n;i++) ans=min(ans,f[i][1]+s[i-1]);
  for (int i=1;i<=n;i++) h[i]=h[i-1]+(a[i]&1^1);
  for (int i=1;i<=n+1;i++)
  {
          ans=min(ans,f[i][1]+u+h[i-1]);
          u=min(u,f[i][0]-h[i]);
  }*/
  for (int i = 1; i <= n; i++) {
    f[i][0] = f[i - 1][0];
    for (int j = 1; j <= 4; j++)
      f[i][j] = min(f[i][j - 1], f[i - 1][j]);
    f[i][0] += a[i], f[i][4] += a[i];
    f[i][1] += a[i] == 0 ? 2 : (a[i] & 1);
    f[i][3] += a[i] == 0 ? 2 : (a[i] & 1);
    f[i][2] += a[i] & 1 ^ 1;
  }
  ll ans = f[n][0];
  for (int i = 1; i <= 4; i++)
    ans = min(ans, f[n][i]);
  cout << ans;
  return 0;
}