Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define isNum(a) (a >= '0' && a <= '9')
#define SP putchar(' ')
#define EL putchar('\n')
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)

template <class T> inline void read(T &);
template <class T> inline void write(const T &);

typedef long long ll;
typedef const long long &cll;
typedef const int &ci;
typedef std::pair<int, int> pii;
const int iinf = 2147483647;
const ll llinf = 9223372036854775807ll;
using std::abs;
using std::max;
using std::min;
using std::sort;
const int N = 50005;

ll dp[N][2];
int a[N];

int main() {
  int n;
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
  }
  dp[1][0] = a[1];
  dp[1][1] = -iinf;
  for (int i = 2; i <= n; ++i) {
    dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i];
    dp[i][1] = max(dp[i - 1][0] - 2 * a[i - 1] - a[i],
                   dp[i - 1][1] + 2 * a[i - 1] - a[i]);
  }
  write(max(dp[n][0], dp[n][1])), EL;
  return 0;
}

template <class T> void read(T &Re) {
  T k = 0;
  char ch = getchar();
  int flag = 1;
  while (!isNum(ch)) {
    if (ch == '-') {
      flag = -1;
    }
    ch = getchar();
  }
  while (isNum(ch)) {
    k = (k << 1) + (k << 3) + ch - '0';
    ch = getchar();
  }
  Re = flag * k;
}
template <class T> void write(const T &Wr) {
  if (Wr < 0) {
    putchar('-');
    write(-Wr);
  } else {
    if (Wr < 10) {
      putchar(Wr + '0');
    } else {
      write(Wr / 10);
      putchar((Wr % 10) + '0');
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define isNum(a) (a >= '0' && a <= '9')
#define SP putchar(' ')
#define EL putchar('\n')
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)

template <class T> inline void read(T &);
template <class T> inline void write(const T &);

typedef long long ll;
typedef const long long &cll;
typedef const int &ci;
typedef std::pair<int, int> pii;
const int iinf = 2147483647;
const ll llinf = 9223372036854775807ll;
using std::abs;
using std::max;
using std::min;
using std::sort;
const int N = 100005;

ll dp[N][2];
int a[N];

int main() {
  int n;
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
  }
  dp[1][0] = a[1];
  dp[1][1] = -iinf;
  for (int i = 2; i <= n; ++i) {
    dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i];
    dp[i][1] = max(dp[i - 1][0] - 2 * a[i - 1] - a[i],
                   dp[i - 1][1] + 2 * a[i - 1] - a[i]);
  }
  write(max(dp[n][0], dp[n][1])), EL;
  return 0;
}

template <class T> void read(T &Re) {
  T k = 0;
  char ch = getchar();
  int flag = 1;
  while (!isNum(ch)) {
    if (ch == '-') {
      flag = -1;
    }
    ch = getchar();
  }
  while (isNum(ch)) {
    k = (k << 1) + (k << 3) + ch - '0';
    ch = getchar();
  }
  Re = flag * k;
}
template <class T> void write(const T &Wr) {
  if (Wr < 0) {
    putchar('-');
    write(-Wr);
  } else {
    if (Wr < 10) {
      putchar(Wr + '0');
    } else {
      write(Wr / 10);
      putchar((Wr % 10) + '0');
    }
  }
}