alexjercan/bugnet huggingface dataset:
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

ll dp[200010][3][2];

int main() {
  ll N;
  cin >> N;
  vec d(N);
  rep(i, N) cin >> d[i];
  rep(i, 200010) rep(j, 3) rep(k, 2) dp[i][j][k] = -INF;
  dp[0][0][0] = 0;
  rep(i, N) rep(j, 3) {
    dp[i + 1][j][1] = dp[i][j][0] + d[i];
    dp[i + 1][j][0] = dp[i][j][1];
    if (j > 0)
      dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - 1][0]);
  }
  ll ans = -INF;
  if (N % 2)
    ans = max(dp[N][1][0], dp[N][2][1]);
  else
    ans = max(dp[N][0][0], dp[N][1][1]);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> P;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

ll Solve_g(vec x) {
  ll N = x.size();
  ll M = N / 2;
  vec a(M + 1, 0), b(M + 1, 0);
  rep(i, M) a.at(i + 1) = a.at(i) + x.at(i * 2);
  rep(i, M) b.at(i + 1) = b.at(i) + x.at(N - 1 - (i * 2));
  ll ret = a.at(M);
  rep(i, M) ret = max(ret, a.at(i) + b.at(M - i));
  return ret;
}

ll Solve_k(vec x) {
  ll N = x.size();
  ll M = N / 2;
  ll MIN = INF;
  vec a(N + 2, 0), b(N + 2, 0);
  for (int i = 0; i < N; i += 2) {
    a.at(i + 2) = a.at(i) + x.at(i);
    MIN = min(x.at(i), MIN);
  }
  for (int i = 1; i < N; i += 2) {
    b.at(i + 2) = b.at(i) + x.at(i);
  }
  ll SUM = a.at(N + 1);
  ll ret = SUM - MIN;
  /*rep(i,N+2) cout<<a.at(i)<<' ';
  cout<<endl;
  rep(i,N+2) cout<<b.at(i)<<' ';
  cout<<endl;
  cout<<ret<<endl;*/
  for (int i = 1; i < N; i += 2)
    for (int j = i; j < N; j += 2) {
      ll UP = b.at(j + 2) - b.at(i);
      ll DOWN = a.at(j + 3) - a.at(i - 1);
      ret = max(SUM + UP - DOWN, ret);
    }
  return ret;
}

int main() {
  ll N;
  cin >> N;
  vec x(N);
  rep(i, N) cin >> x.at(i);
  if (N % 2 == 1)
    cout << Solve_k(x) << endl;
  else
    cout << Solve_g(x) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  vector<vector<ll>> dp(N, vector<ll>(3));
  dp.at(0).at(0) = 0;
  dp.at(0).at(1) = 0;
  dp.at(0).at(2) = A.at(0);
  dp.at(1).at(0) = 0;
  dp.at(1).at(1) = max(A.at(0), A.at(1));
  if (N == 2) {
    cout << dp.at(1).at(1) << endl;
    return 0;
  }
  for (ll i = 2; i < N; i++) {
    for (ll j = 0; j <= 2 - (i % 2); j++) {
      if (i == 2 && j == 0) {
        dp.at(i).at(j) = 0;
        continue;
      }
      if (i % 2 == 0) {
        if (j == 0 || j == 1) {
          dp.at(i).at(j) =
              max(dp.at(i - 2).at(j) + A.at(i), dp.at(i - 1).at(j));
        } else {
          dp.at(i).at(j) = dp.at(i - 2).at(j) + A.at(i);
        }
      } else {
        dp.at(i).at(j) =
            max(dp.at(i - 2).at(j) + A.at(i), dp.at(i - 1).at(j + 1));
      }
    }
  }
  cout << dp.at(N - 1).at(1) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;

ll modPow(ll a, ll n) {
  if (n == 0)
    return 1; // 0乗にも対応する場合
  if (n == 1)
    return a % MOD;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1)) % MOD;
  ll t = modPow(a, n / 2);
  return (t * t) % MOD;
}
vector<ll> Prime(10000, 0);
void sosu(ll n) {
  Prime.at(0) = 2;
  ll t = 1;
  for (ll i = 3; i < n; i++) {
    rep(j, t) {
      if (i % Prime.at(j) == 0) {
        break;
      }
      if (j == t - 1) {
        Prime.at(t) = i;
        t++;
      }
    }
  }
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  vector<vector<ll>> dp(N, vector<ll>(N));
  dp.at(0).at(0) = 0;
  dp.at(0).at(1) = A.at(0);
  dp.at(1).at(0) = 0;
  dp.at(1).at(1) = max(A.at(0), A.at(1));
  if (N == 2) {
    cout << dp.at(1).at(1) << endl;
    return 0;
  }
  dp.at(2).at(0) = 0;
  dp.at(2).at(1) = max(A.at(0), max(A.at(1), A.at(2)));
  dp.at(2).at(2) = A.at(0) + A.at(2);
  for (ll i = 3; i < N; i++) {
    for (ll j = (i + 1) / 2 - 1; j <= (i + 2) / 2; j++) {
      if (j == (i + 2) / 2 && i % 2 == 0) {
        dp.at(i).at(j) = dp.at(i - 2).at(j - 1);
      } else if (j == (i + 1) / 2 - 1 && i % 2 == 1) {
        dp.at(i).at(j) = dp.at(i - 1).at(j);
      } else {
        dp.at(i).at(j) =
            max(dp.at(i - 2).at(j - 1) + A.at(i), dp.at(i - 1).at(j));
      }
    }
  }
  cout << dp.at(N - 1).at(N / 2) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

/* ****TCIITB**** */

#include <bits/stdc++.h>
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;

#define N 200019
ll n;
std::vector<ll> a(N);
std::map<pii, ll> zz;
ll func(ll pos, ll rem) {

  if (rem == 0)
    return zz[{pos, rem}] = 0;
  if (zz.find({pos, rem}) != zz.end())
    return zz[{pos, rem}];
  if ((n - pos + 1) < 2 * rem || pos >= n) {
    return zz[{pos, rem}] = -1e18;
  }
  ll vl = func(pos + 1, rem);
  vl = max(vl, func(pos + 2, rem - 1) + a[pos]);
  return zz[{pos, rem}] = vl;
}
int main() {

  io

          cin >>
      n;
  rep(i, 0, n) cin >> a[i];
  cout << func(0, n / 2);
}

Error: TLE

Fixed submission:

/* ****TCIITB**** */

#include <bits/stdc++.h>
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;

#define N 200008
ll n;
std::vector<ll> a(N);

ll func(ll pos, ll rem, ll s) {
  if (pos >= n) {
    if (rem == 0)
      return s;
    else
      return -1;
  }
  if (rem == 0)
    return s;
  ll vl = 0;
  ll ss = (n - pos - 1);
  ll cnt = ss / 2;
  if (ss % 2)
    cnt++;
  if (cnt >= rem)
    vl = max(vl, func(pos + 1, rem, s));
  vl = max(vl, func(pos + 2, rem - 1, s + a[pos]));
  return vl;
}
int main() {

  io

          cin >>
      n;
  rep(i, 0, n) cin >> a[i];
  cout << func(0, n / 2, 0);
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18 + 5;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];

  int m;
  if (n % 2 == 0)
    m = 1;
  else
    m = 2;

  vvl dp(n + 10, vl(m + 1, -INF));
  for (int i = 0; i <= m; i++) {
    dp[i][i] = 0;
  }

  rep(i, n) {
    rep(j, m + 1) {
      if (dp[i][j] == -INF)
        continue;
      chmax(dp[i + 2][j], dp[i][j] + a[i]);
      if (j + 1 <= m) {
        chmax(dp[i + 3][j + 1], dp[i][j] + a[i]);
      }
      if (j + 2 <= m) {
        chmax(dp[i + 4][j + 2], dp[i][j] + a[i]);
      }
    }
  }

  ll ans = -INF;
  int mi;
  if (n % 2 == 0)
    mi = 0;
  else
    mi = 1;
  for (int i = n; i <= n + 5; i++) {
    for (int j = mi; j <= m; j++) {
      chmax(ans, dp[i][j]);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[4] = {1, 0, -1, 0};
const ll dx[4] = {0, -1, 0, 1};
ll solve(int l, int r, ll k, vl &a) {
  int n = (r - l + 1) / 2;
  ll p = 0;
  if (n == k) {
    if (r - l == 0)
      p = 0;
    else if (r - l == 1)
      p = max(a[l], a[r]);
    else if (r - l == 2)
      p = max(a[l], max(a[l + 1], a[l + 2]));
    else if ((r - l + 1) % 2) {
      p = max(a[l] + solve(l + 2, r, k - 1, a),
              a[l + 1] + solve(l + 3, r, k - 1, a));
      p = max(p, solve(l + 2, r, k, a));
    } else {
      p = max(a[l] + solve(l + 2, r, k - 1, a),
              a[l + 1] + solve(l + 3, r, k - 1, a));
    }
  } else {
    for (int i = l; i <= r; i += 2) {
      p += a[i];
    }
  }
  return p;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  ll ans = solve(0, n - 1, n / 2, a);
  cout << ans << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
ll dp[N], sum[N], a[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (i == 1)
      sum[i] = a[i];
    else
      sum[i] = sum[i - 2] + a[i];
  }
  for (int i = 2; i <= n; i++) {
    if (i & 1)
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    else
      dp[i] = max(dp[i - 2] + a[i], sum[i - 1]);
  }
  printf("%lld\\n", dp[n]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << "\\n";
}
template <typename Arg1, typename... Args>
void __f(const char *na, Arg1 &&arg1, Args &&...args) {
  const char *c = strchr(na + 1, ',');
  cerr.write(na, c - na) << " : " << arg1 << " , ";
  __f(c + 1, args...);
}
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define UNIQUE(v) sort(all(v)), v.erase(unique(all(v)), v.end())
typedef long long LL;
typedef pair<int, int> pii;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int inf = 1e9;
const LL INF = 1e16;
const int N = 1e5 + 5;

map<LL, LL> dp[N];
int n;
LL a[N];

LL solve(int idx, int taken) {
  int need = n / 2 - (n - idx + 1) / 2;
  if (taken < need)
    return -INF;
  if (idx >= n)
    return taken == n / 2 ? 0 : -INF;
  if (dp[idx].count(taken))
    return dp[idx][taken];
  LL ret = -INF;
  ret = max(ret, solve(idx + 2, taken + 1) + a[idx]);
  ret = max(ret, solve(idx + 1, taken));
  return dp[idx][taken] = ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif // LOCAL
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << solve(0, 0) << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mx = 2e5 + 10;
ll dp[mx], arr[mx];
int main() {
  ll n;
  cin >> n;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    if (i % 2 != 0)
      sum += arr[i];
    if (i != 1) {
      if (i % 2 != 0)
        dp[i] = max(dp[i - 2] + arr[i], dp[i - 1]);
      else
        dp[i] = max(dp[i - 2] + arr[i], sum);
    }
  }
  cout << dp[n] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define ZERO_IQ                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(x, y)                                                            \\
  cerr << (#x) << " " << (#y) << " is " << (x) << " " << (y) << endl
#define debug2(x, y, z)                                                        \\
  cerr << (#x) << " " << (#y) << " " << (#z) << " is " << (x) << " " << (y)    \\
       << " " << (z) << endl
#define watch(x) cerr << (#x) << " is " << (x) << endl

const int N = 2e5 + 10;
typedef long long ll;

ll dp[N];

int main() {
  ll n;
  cin >> n;
  vector<ll> v;
  vector<ll> summ(n + 1);
  for (int i = 1; i <= n; i++) {
    ll q;
    cin >> q;
    v.push_back(q);
    if (i % 2 == 1) {
      if (i == 1)
        summ[i] = q;
      else
        summ[i] = summ[i - 2] + q;
    }
  }
  dp[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (i % 2 == 1) {
      dp[i] = max(dp[i - 2] + v[i - 1], dp[i - 1]);
    } else
      dp[i] = max(dp[i - 2] + v[i - 1], summ[i - 1]);
    debug(i, dp[i]);
  }
  cout << dp[n];
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error



#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\\n";
  else
    cout << a << "\\n";
}
// end of template

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N, vector<ll>(3, -(1LL << 61))); // N/2, N/2+1
  dp[0][2] = 0;
  if (N % 2 == 1) {
    dp[0][0] = A[0];
    dp[1][1] = A[1];
  } else {
    dp[0][1] = A[0];
    dp[1][2] = A[1];
  }
  rep(i, 2, N) {
    rep(j, 3) dp[i][j] = dp[i - 2][j] + A[i];
    if (i >= 3) {
      rep(j, 2) dp[i][j + 1] = max(dp[i][j + 1], dp[i - 3][j] + A[i]);
    }
  }
  ll ans = max(dp[N - 1][2], dp[N - 2][1]);
  if (N >= 3)
    ans = max(ans, dp[N - 3][0]);
  output(ans);
  return 0;
}


Error: 0

Fixed submission:



#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\\n";
  else
    cout << a << "\\n";
}
// end of template

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N, vector<ll>(3, -(1LL << 61))); // N/2, N/2+1
  dp[0][2] = 0;
  if (N % 2 == 1) {
    dp[0][0] = A[0];
    dp[1][1] = A[1];
  } else {
    dp[0][1] = A[0];
    dp[1][2] = A[1];
  }
  rep(i, 2, N) {
    rep(j, 3) dp[i][j] = dp[i - 2][j] + A[i];
    if (i >= 3) {
      rep(j, 2) dp[i][j + 1] = max(dp[i][j + 1], dp[i - 3][j] + A[i]);
    }
  }
  output(max({dp[N - 1][2], dp[N - 2][1], dp[N - 3][0]}));

  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using i32 = int_fast32_t;
using i64 = int_fast64_t;
using p32 = pair<i32, i32>;

i32 n;
vector<i32> a;
map<p32, i64> dp;

i64 dfs(i32 index, i32 use) {
  if (use == n / 2)
    return 0;
  if (index / 2 < use)
    return -INT64_MAX / 2;
  if ((n - index + 1) / 2 < n / 2 - use)
    return -INT64_MAX / 2;
  if (dp.find({index, use}) != dp.end())
    return dp[{index, use}];
  return dp[{index, use}] =
             max(a[index] + dfs(index + 2, use + 1), dfs(index + 1, use));
}

int main() {
  cin >> n;
  a.assign(n, 0);
  for (auto &e : a)
    cin >> e;
  cout << dfs(0, 0) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using i32 = int_fast32_t;
using i64 = int_fast64_t;
using p32 = pair<i32, i32>;

i32 n;
vector<i32> a;
map<p32, i64> dp;

i64 dfs(i32 index, i32 remain) {
  if (dp[{index, remain}] != 0)
    dp[{index, remain}];
  if (remain == 0)
    return 0;
  if (index >= n)
    return -INT32_MAX;
  if (remain * 2 - 1 > (n - index))
    return 0;
  return dp[{index, remain}] =
             max(a[index] + dfs(index + 2, remain - 1), dfs(index + 1, remain));
}

int main() {
  cin >> n;
  a.assign(n, 0);
  for (auto &e : a)
    cin >> e;
  cout << dfs(0, n / 2) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

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

  ll N;
  cin >> N;

  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }
  ll s_min, s_max;
  ll g_min, g_max;
  ll num;
  ll min_sum = -1e15;
  typedef map<ll, ll> mp;
  vector<mp> dp_0(N), dp_1(N);
  dp_0[0][0] = 0;
  dp_0[0][1] = min_sum;
  dp_1[0][1] = A[0];
  dp_1[0][0] = min_sum;

  for (ll i = 1; i < N; i++) {
    s_min = max(0ll, N / 2 - (N - i + 1) / 2);
    s_max = min(N / 2, (i + 1) / 2);
    g_min = max(0ll, N / 2 - (N - (i + 1) + 1) / 2);
    g_max = min(N / 2, (i + 2) / 2);
    // cout<<i+1<<"回目"<<"\\n";
    // cout<<s_min<<" "<<s_max<<"\\n";
    // cout<<g_min<<" "<<g_max<<"\\n";
    num = g_min;
    while (num <= g_max) {
      // dp_0の更新
      if (s_max >= num) {
        dp_0[i][num] = max(dp_0[i - 1][num], dp_1[i - 1][num]);
        // cout<<0<<i<<" "<<num<<"\\n";
        // cout<<dp_0[i][num]<<"\\n";
      } else {
        dp_0[i][num] = min_sum;
      }

      // dp_1の更新
      if (s_min <= num - 1) {
        dp_1[i][num] = dp_0[i - 1][num - 1] + A[i];
        // cout<<1<<i<<" "<<num<<"\\n";
        // cout<<dp_1[i][num]<<"\\n";
      } else {
        dp_1[i][num] = min_sum;
      }
      ++num;
    }
  }

  ll ans = max(dp_0[N - 1][N / 2], dp_1[N - 1][N / 2]);
  cout << ans << "\\n";

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

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

  ll N;
  cin >> N;

  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }
  ll s_min, s_max;
  ll g_min, g_max;
  ll num;
  ll min_sum = -1e15;
  ll dp_0[N][N / 2 + 1], dp_1[N][N / 2 + 1];
  dp_0[0][0] = 0;
  dp_0[0][1] = min_sum;
  dp_1[0][1] = A[0];
  dp_1[0][0] = min_sum;

  // for(ll i=1;i<N;i++){
  //     s_min = max(0ll,N/2-(N-i+1)/2);
  //     s_max = min(N/2,(i+1)/2);
  //     g_min = max(0ll,N/2-(N-(i+1)+1)/2);
  //     g_max = min(N/2,(i+2)/2);
  //     // cout<<i+1<<"回目"<<"\\n";
  //     // cout<<s_min<<" "<<s_max<<"\\n";
  //     // cout<<g_min<<" "<<g_max<<"\\n";
  //     num = g_min;
  //     while(num<=g_max){
  //         // dp_0の更新
  //         if(s_max>=num){
  //             dp_0[i][num] = max(dp_0[i-1][num],dp_1[i-1][num]);
  //             // cout<<0<<i<<" "<<num<<"\\n";
  //             // cout<<dp_0[i][num]<<"\\n";
  //         }else{
  //             dp_0[i][num] = min_sum;
  //         }

  //         // dp_1の更新
  //         if(s_min<=num-1){
  //             dp_1[i][num] = dp_0[i-1][num-1]+A[i];
  //             // cout<<1<<i<<" "<<num<<"\\n";
  //             // cout<<dp_1[i][num]<<"\\n";
  //         }else{
  //             dp_1[i][num] = min_sum;
  //         }
  //         ++num;
  //     }
  // }

  ll ans = max(dp_0[N - 1][N / 2], dp_1[N - 1][N / 2]);
  cout << ans << "\\n";

  return 0;
}