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

// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

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

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

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

  int N;
  cin >> N;

  vector<int> A(N);
  vector<map<pii, int>> memo(N);

  rep(i, 0, N) { cin >> A[i]; }

  int ans = -INF;
  memo[0][mp(2, 1)] = A[0];
  memo[1][mp(1, 1)] = A[1];
  if (N <= 3) {
    chmax(ans, max(A[0], A[1]));
  }
  if (N > 2) {
    memo[2][mp(0, 1)] = A[2];
    if (N <= 3) {
      chmax(ans, max(A[0], A[1]));
    }
  }

  rep(i, 0, N - 1) {
    for (auto p : memo[i]) {
      rep(j, 0, p.first.first + 1) {
        if (i + 2 + j >= N)
          break;
        if (memo[i + 2 + j].find(mp(p.first.first - j, p.first.second + 1)) ==
            memo[i + 2 + j].end()) {
          memo[i + 2 + j][mp(p.first.first - j, p.first.second + 1)] =
              p.second + A[i + 2 + j];
        } else {
          chmax(memo[i + 2 + j][mp(p.first.first - j, p.first.second + 1)],
                p.second + A[i + 2 + j]);
        }
        if (p.first.second + 1 == N / 2) {
          chmax(ans, p.second + A[i + 2 + j]);
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

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

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int N;
vector<int> A;
map<pii, int> memo1[100010], memo2[100010], memo3[100010];

int DP1(int idx, int cnt, int use) {
  if (cnt == N / 2)
    return 0;
  if (memo1[idx].find(mp(cnt, use)) != memo1[idx].end())
    return memo1[idx][mp(cnt, use)];
  int res = -INF;
  if (cnt < N / 2) {
    chmax(res, DP1(idx - 2, cnt + 1, use));
    if (use == 0) {
      chmax(res, DP1(idx - 3, cnt + 1, 1));
      chmax(res, DP1(idx - 4, cnt + 1, 2));
    } else if (use == 1) {
      chmax(res, DP1(idx - 3, cnt + 1, 2));
    }
  }
  res += A[idx];
  memo1[idx][mp(cnt, use)] = res;
  return res;
}

int DP2(int idx, int cnt, int use) {
  if (cnt == N / 2)
    return 0;
  if (memo2[idx].find(mp(cnt, use)) != memo2[idx].end())
    return memo2[idx][mp(cnt, use)];
  int res = -INF;
  if (cnt < N / 2) {
    chmax(res, DP2(idx - 2, cnt + 1, use));
    if (use == 0) {
      chmax(res, DP2(idx - 3, cnt + 1, 1));
    }
  }
  res += A[idx];
  memo2[idx][mp(cnt, use)] = res;
  return res;
}

int DP3(int idx, int cnt, int use) {
  if (cnt == N / 2)
    return 0;

  int res = -INF;
  if (cnt < N / 2) {
    chmax(res, DP3(idx - 2, cnt + 1, 0));
  }
  res += A[idx];
  return res;
}

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

  cin >> N;
  A.resize(N);
  rep(i, 0, N) { cin >> A[i]; }

  int ans = -INF;

  if (N % 2 == 0) {
    chmax(ans, DP3(N - 1, 0, 0));
    chmax(ans, DP3(N - 2, 0, 0));
  } else {
    chmax(ans, DP1(N - 1, 0, 0));
    chmax(ans, DP2(N - 2, 0, 0));
    chmax(ans, DP3(N - 3, 0, 0));
  }

  cout << ans << endl;

  return 0;
}