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

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
#define sz(i) (int)(i.size())
#define F first
#define S second
#define L long double
#define P pair<int, int>
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\\n'
#endif
template <class T> void smin(T &a, T val) {
  if (a > val)
    a = val;
}
template <class T> void smax(T &a, T val) {
  if (a < val)
    a = val;
}
const int N = 2 * (int)1e5 + 10;

int n, a[N], dp[N][5];
int till = 0;

bool bad = false;

int solve(int idx, int removed) {
  if (removed < 0) {
    return -inf;
  }
  if (idx >= n) {
    if (bad and n % 2 and removed == 2) {
      return -inf;
    }
    return 0;
  }
  int &ans = dp[idx][removed];
  if (ans != -1) {
    return ans;
  }
  ans = -inf;
  smax(ans, solve(idx + 1, removed - 1));
  smax(ans, a[idx] + solve(idx + 2, removed));
  return ans;
}

int32_t main() {
  _

          cin >>
      n;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  int ans = -inf;
  memset(dp, -1, sizeof dp);

  if (n % 2) {
    bad = true;
    smax(ans, solve(0, 2));
    bad = false;
    memset(dp, -1, sizeof dp);
    smax(ans, solve(1, 1));
  } else {
    smax(ans, solve(0, 1));
    memset(dp, -1, sizeof dp);
    smax(ans, solve(1, 0));
  }

  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
#define sz(i) (int)(i.size())
#define F first
#define S second
#define L long double
#define P pair<int, int>
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\\n'
#endif
template <class T> void smin(T &a, T val) {
  if (a > val)
    a = val;
}
template <class T> void smax(T &a, T val) {
  if (a < val)
    a = val;
}
const int N = 5 * (int)1e5 + 10;

int n, dp[N];
vector<int> a, p;

int solve(int idx) {
  if (idx >= n) {
    return 0;
  }
  int &ans = dp[idx];
  if (ans != -1) {
    return ans;
  }
  ans = -inf;
  smax(ans, solve(idx + 1));
  smax(ans, a[idx] + solve(idx + 2));
  return ans;
}

void trace(int idx) {
  if (idx >= n) {
    return;
  }
  int ans = solve(idx);
  if (ans == solve(idx + 1)) {
    trace(idx + 1);
    return;
  } else {
    assert(ans == a[idx] + solve(idx + 2));
    p.push_back(idx);
    trace(idx + 2);
  }
  return;
}

int32_t main() {
  _

          cin >>
      n;
  a.resize(n);

  memset(dp, -1, sizeof dp);

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  solve(0);
  trace(0);

  tr(p);
  if ((int)(p.size()) >= n / 2) {

    sort(p.begin(), p.end(), [&](int x, int y) { return a[x] > a[y]; });

    int sum = 0;
    for (int i = 0; i < n / 2; ++i) {
      sum += a[p[i]];
    }

    cout << sum;
  }

  else {

    int sum = 0;
    for (int i = 0; i < (int)(p.size()); ++i) {
      sum += a[p[i]];
    }

    set<int> s;
    for (int i = 0; i < (int)(p.size()); ++i) {
      s.insert(p[i]);
    }

    vector<int> X;

    for (int i = 0; i < n; ++i) {
      if (s.count(i)) {
        continue;
      }
      X.push_back(i);
    }

    sort(X.begin(), X.end(), [&](int x, int y) { return a[x] > a[y]; });

    int tsum = 0;
    for (int i = 0; i < n / 2; ++i) {
      tsum += a[X[i]];
    }

    vector<int> nxt;

    for (int i = 0; i < n; ++i) {
      if (s.count(i + 1) or s.count(i - 1) or s.count(i)) {
        continue;
      }
      nxt.push_back(i);
    }

    sort(nxt.begin(), nxt.end(), [&](int x, int y) { return a[x] > a[y]; });

    tr(nxt);

    int rem = n / 2 - (int)(p.size());

    // assert((int)(nxt.size()) >= rem);

    for (int i = 0; i < rem; ++i) {
      sum += a[nxt[i]];
    }

    sum = max(sum, tsum);
    cout << sum;
  }

  return 0;
}