Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// priority_queue<int,vector<int>, greater<int> > q2;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  VI A(N);
  REP(i, N) cin >> A[i];

  vector<VI> dp(N + 1, VI(3, 0));

  eFOR(i, 1, N) {

    if (i % 2) {
      if (i != 1)
        dp[i][2] = max(dp[i - 1][1], dp[i - 2][2]) + A[i - 1];
      else
        dp[i][2] = max(dp[i - 1][1], 0LL) + A[i - 1];

      if (i != 1)
        dp[i][1] = max(dp[i - 1][0] + A[i - 1], dp[i - 2][0] + A[i - 1]);
      else
        dp[i][1] = max(dp[i - 1][0] + A[i - 1], A[i - 1]);

      dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]);
    } else {
      if (i != 1)
        dp[i][2] = max(dp[i - 1][0], dp[i - 2][1]) + A[i - 1];
      else
        dp[i][2] = max(dp[i - 1][0], A[i - 1]);

      dp[i][1] = dp[i - 1][2];
      dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
    }
  }

  if (N % 2)
    cout << max(dp[N][0], dp[N][1]) << endl;
  else
    cout << max(dp[N][2], dp[N][1]) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// priority_queue<int,vector<int>, greater<int> > q2;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  VI A(N);
  REP(i, N) cin >> A[i];
  if (N % 2 == 0) {
    int ans1 = 0, ans2 = 0;
    REP(i, N) {
      if (i % 2)
        ans1 += A[i];
      else
        ans2 += A[i];
    }
    cout << max(ans1, ans2) << endl;

  } else {

    int sum[2][20010] = {0};
    // int sum[2][200010] = { 0 };

    REP(i, N) {
      sum[i % 2][i + 1] += A[i];
      sum[0][i + 1] += sum[0][i];
      sum[1][i + 1] += sum[1][i];
    }
    int ans = -LLINF;
    eFOR(i, 1, N - 2) {
      int tmp = max(sum[1][N] - sum[1][i], sum[0][N] - sum[0][i]);
      tmp += sum[0][i - 1];
      // 1-indexで偶数の時

      int ttmp = sum[0][N] - sum[0][i + 1];
      ttmp += max(sum[0][i - 1], sum[1][i - 1]);
      // 奇数

      // cout << i << " " << tmp << " " << ttmp << endl;
      if (i % 2)
        chmax(ans, ttmp);
      else {
        chmax(ans, tmp);

        int tttmp = sum[0][N] - A[i];

        // cout << tttmp << endl;
        chmax(ans, tttmp);
      }
    }
    // cout << sum[1][N] << " " << sum[0][N] << endl;
    // cout << sum[1][N - 2] << " " << sum[0][N - 2] << endl;
    cout << max(ans, sum[1][N]) << endl;
  }

  return 0;
}