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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<lint> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  const lint INF = INT64_MIN / 2;
  vector2d<lint> dp(n, vector<lint>(3, INF));
  dp[0][0] = a[0];
  dp[0][1] = 0;
  dp[1][0] = INF;
  dp[1][1] = a[1];
  for (int i = 2; i < n; i++) {
    dp[i][0] = dp[i - 2][0] == INF ? INF : dp[i - 2][0] + a[i];

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

    if (i == 2) {
      dp[i][2] = a[2];
    } else if (i == 3) {
      dp[i][2] = max({dp[i - 2][2] + a[i], dp[i - 3][1] + a[i]});
    } else {
      dp[i][2] =
          max({dp[i - 2][2] + a[i], dp[i - 3][1] + a[i], dp[i - 4][0] + a[i]});
    }
  }

  if (n % 2 == 0) {
    cout << max({dp[n - 2][0], dp[n - 1][1]}) << endl;
  } else {
    cout << max({dp[n - 3][0], dp[n - 2][1], dp[n - 1][2]}) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

lint Solve(vector<lint> a, lint n) {
  if (n == 1) {
    return 0;
  }
  lint result = 0;
  for (int i = 1; i < n; i += 2) {
    result += a[i];
  }

  lint dp[200000][2][2];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[1][0][0] = a[0];
  dp[1][0][1] = 0;
  dp[1][1][0] = a[1];
  dp[1][1][1] = -INT64_MAX;
  for (int i = 1; i + 1 < n; i++) {
    dp[i + 1][1][0] =
        (dp[i][0][0] == -INT64_MAX ? -INT64_MAX : dp[i][0][0] + a[i + 1]);
    dp[i + 1][1][1] =
        (dp[i][0][1] == -INT64_MAX ? -INT64_MAX : dp[i][0][1] + a[i + 1]);
    dp[i + 1][0][0] = (dp[i][1][0] == -INT64_MAX ? -INT64_MAX : dp[i][1][0]);
    dp[i + 1][0][1] = max(dp[i][0][0], dp[i][1][1]);
  }
  return max({result, dp[n - 1][0][1], dp[n - 1][1][1]});
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<lint> a(n), a_center;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (2 <= i && i < n - 2) {
      a_center.push_back(a[i]);
    }
  }
  if (n % 2 == 0) {
    lint result_odd = 0, result_even = 0;
    for (int i = 0; i < n; i += 2) {
      result_even += a[i];
      result_odd += a[i + 1];
    }
    cout << max(result_even, result_odd) << endl;
    return 0;
  }

  cout << max(Solve(a, n), Solve(a_center, n - 4) + a.front() + a.back())
       << endl;

  return 0;
}