alexjercan/bugnet huggingface dataset:
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

int main() {

  long long int n;
  cin >> n;

  vector<long long int> v;
  for (long long int i = 0; i < n; i++) {
    long long int in;
    cin >> in;
    v.push_back(in);
  }

  long long int d[3][214514] = {};
  d[0][0] = v[0];
  d[1][1] = v[1];

  for (long long int i = 1; i < n; i++) {
    if (i >= 2) {
      d[0][i] = d[0][i - 2] + v[i];
      d[1][i] = d[1][i - 2] + v[i];
      d[2][i] = d[2][i - 2] + v[i];
    }
    if (i >= 3) {
      d[1][i] = max(d[0][i - 3] + v[i], d[1][i]);
      d[2][i] = max(d[1][i - 3] + v[i], d[2][i]);
    }
    if (i >= 4) {
      d[2][i] = max(d[0][i - 4] + v[i], d[2][i]);
    }
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

int main() {

  long long int n;
  cin >> n;

  vector<long long int> v;
  for (long long int i = 0; i < n; i++) {
    long long int in;
    cin >> in;
    v.push_back(in);
  }

  long long int d[3][114514] = {};
  d[0][0] = v[0];
  d[1][1] = v[1];

  for (long long int i = 1; i < n; i++) {
    if (i >= 2) {
      d[0][i] = d[0][i - 2] + v[i];
      d[1][i] = d[1][i - 2] + v[i];
      d[2][i] = d[2][i - 2] + v[i];
    }
    if (i >= 3) {
      d[1][i] = max(d[0][i - 3] + v[i], d[1][i]);
      d[2][i] = max(d[1][i - 3] + v[i], d[2][i]);
    }
    if (i >= 4) {
      d[2][i] = max(d[0][i - 4] + v[i], d[2][i]);
    }
  }

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

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

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

#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < (int)n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define fill(a, v) memset(a, v, sizeof(a))
#define d1(a) cout << a << "\\n"
#define d2(a, b) cout << a << " " << b << "\\n"
#define d3(a, b, c) cout << a << " " << b << " " << c << "\\n"
#define d4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\\n"
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;
const int N = 2e5 + 5;
int a[N];
int n;
map<pii, int> dp;
int solve(int id, int need) {
  if (need == 0)
    return 0;
  if (need > (n - id + 2) / 2 || id > n)
    return -inf64;
  if (dp.find({id, need}) != dp.end())
    return dp[{id, need}];
  int res = -inf64;
  res = max(res, solve(id + 1, need));
  res = max(res, solve(id + 2, need - 1) + a[id]);
  dp[{id, need}] = res;
  return res;
}
int32_t main() {
  fast;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = solve(1, n / 2);
  cout << ans;
}

Error: TLE

Fixed submission:

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

#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < (int)n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define fill(a, v) memset(a, v, sizeof(a))
#define d1(a) cout << a << "\\n"
#define d2(a, b) cout << a << " " << b << "\\n"
#define d3(a, b, c) cout << a << " " << b << " " << c << "\\n"
#define d4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\\n"
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;
const int N = 2e5 + 5;
int a[N];
int n;
int solve(int id, int need) {
  if (need == 0)
    return 0;
  if (need > (n - id + 2) / 2 || id > n)
    return -inf64;
  int res = -inf64;
  res = max(res, solve(id + 1, need));
  res = max(res, solve(id + 2, need - 1) + a[id]);
  return res;
}
int32_t main() {
  fast;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = solve(1, n / 2);
  cout << ans;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n = 0;
  cin >> n;
  ll k = 0;
  k = n % 2 + 1;
  vector<ll> num(n);
  for (int i = 0; i < n; i++)
    cin >> num[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(k + 2));
  for (int i = 0; i < n + 1; i++)
    for (int j = 0; j < k + 2; j++)
      dp[i][j] = -1000000000000000000;
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k + 1; j++) {
      if (dp[i + 1][j + 1] < dp[i][j])
        dp[i + 1][j + 1] = dp[i][j];
      ll add = 0;
      add = dp[i][j];
      if ((i + j) % 2 == 0)
        add += num[i];
      if (dp[i + 1][j] < add)
        dp[i + 1][j] = add;
    }
  }
  ll res = 0;
  // cout << n << " " << k << endl;
  res = dp[n][k];
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n = 0;
  cin >> n;
  ll k = 0;
  k = n % 2 + 1;
  vector<ll> num(n);
  for (int i = 0; i < n; i++)
    cin >> num[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(k + 1));
  for (int i = 0; i < n + 1; i++)
    for (int j = 0; j < k + 1; j++)
      dp[i][j] = -100000000000000000;
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k + 1; j++) {
      if (dp[i + 1][j + 1] < dp[i][j])
        dp[i + 1][j + 1] = dp[i][j];
      ll add = 0;
      if ((i + j) % 2 == 0)
        add = num[i];
      if (dp[i + 1][j] < dp[i][j] + add)
        dp[i + 1][j] = dp[i][j] + add;
    }
  }
  ll res = 0;
  res = dp[n][k];
  cout << res << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  long N;
  cin >> N;

  long long A[200000];

  long i;

  for (i = 0; i < N; i++)
    cin >> A[i];

  long long DP[200000];
  long long SUM_A[200000];
  SUM_A[0] = A[0]; // on somme les impairs
  for (i = 2; i < N; i += 2)
    SUM_A[i] = SUM_A[i - 2] + A[i];

  DP[0] = 0;
  DP[1] = max(A[0], A[1]);

  for (i = 3; i <= N; i++) {
    if (i % 2) { // si c'est 7, alors A_7 + DP(5) ou DP(6)
      DP[i - 1] = max(DP[i - 2], A[i - 1] + DP[i - 3]);
    } else { // si c'est 8
      DP[i - 1] = max(SUM_A[i - 2], DP[i - 3] + A[i - 1]);
    }
  }

  cout << DP[N - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  long N;
  cin >> N;

  long A[200000];

  long i, j;
  long result, s;

  for (i = 0; i < N; i++)
    cin >> A[i];

  long DP[200000];

  DP[0] = 0;
  DP[1] = max(A[0], A[1]);

  for (i = 3; i <= N; i++) {
    if (i % 2) {
      DP[i - 1] = max(DP[i - 2], A[i - 1] + DP[i - 3]);
    } else {
      s = 0;
      for (j = 0; j < i - 1; j += 2)
        s += A[j];
      DP[i - 1] = max(s, DP[i - 3] + A[i - 1]);
    }
  }

  cout << DP[N - 1] << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef tuple<ll, ll, ll> ti;

#define REP(a, b, c) for (ll a = (b); a < (c); a++)
#define PER(a, b, c) for (ll a = (b); a >= (c); a--)

ll ii() {
  ll x;
  cin >> x;
  return x;
}
string is() {
  string x;
  cin >> x;
  return x;
}
void oi(ll x) { cout << x; }
void od(ld x) { cout << fixed << setprecision(10) << x; }
void os(string x) { cout << x; }
void oie(ll x) { cout << x << endl; }
void ode(ld x) { cout << fixed << setprecision(10) << x << endl; }
void ose(string x) { cout << x << endl; }
void oe() { cout << endl; }

ll f(map<ti, ll> &m, ti k, ll o) {
  if (m.find(k) == m.end()) {
    return LLONG_MIN;
  }
  if (m[k] == LLONG_MIN)
    return LLONG_MIN;
  return m[k] + o;
}

int main() {
  ll N = ii();
  vector<ll> A;
  REP(i, 0, N) A.push_back(ii());

  map<ti, ll> dp;
  dp[make_tuple(0, 0, 0)] = 0;

  REP(i, 1, N + 1) {
    ll c = int(i / 2);
    REP(j, c - 1, c + 2) {
      dp[make_tuple(i, j, 0)] = max(f(dp, make_tuple(i - 1, j, 0), 0),
                                    f(dp, make_tuple(i - 1, j, 1), 0));
      dp[make_tuple(i, j, 1)] = f(dp, make_tuple(i - 1, j - 1, 0), A[i - 1]);
    }
  }
  oie(max(dp[make_tuple(N, int(N / 2), 0)], dp[make_tuple(N, int(N / 2), 1)]));

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;

#define REP(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define PER(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)

ll ii() {
  ll x;
  cin >> x;
  return x;
}
string is() {
  string x;
  cin >> x;
  return x;
}
void oi(ll x) { cout << x; }
void od(ld x) { cout << fixed << setprecision(10) << x; }
void os(string x) { cout << x; }
void oie(ll x) { cout << x << endl; }
void ode(ld x) { cout << fixed << setprecision(10) << x << endl; }
void ose(string x) { cout << x << endl; }
void oe() { cout << endl; }

int main() {
  vector<ll> A;
  ll N = ii();
  REP(i, 0, N) A.push_back(ii());
  ll K = int(N / 2);

  vector<map<ll, ll>> dp(N + 2);
  dp[0][0] = 0;
  REP(i, 0, N) {
    for (auto x : dp[i]) {
      if (x.first < K) {
        auto t = dp[i + 2].find(x.first + 1);
        if (t != dp[i + 2].end()) {
          dp[i + 2][x.first + 1] = max(t->second, x.second + A[i]);
        } else {
          dp[i + 2][x.first + 1] = x.second + A[i];
        }
      }

      auto t = dp[i + 1].find(x.first);
      if (t != dp[i + 1].end()) {
        dp[i + 1][x.first] = max(t->second, x.second);
      } else {
        dp[i + 1][x.first] = x.second;
      }
    }
  }
  ll ret = 0;
  REP(i, 0, N + 2) ret = max(ret, dp[i][K]);
  oie(ret);

  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll dp[200005][2];

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  dp[0][1] = A[0];
  dp[1][1] = max(A[0], A[1]);
  for (int i = 2; i < N; i++) {
    if (i % 2 == 0) {
      dp[i][0] = max(dp[i - 1][1], dp[i - 2][0] + A[i]);
      dp[i][1] = dp[i - 2][1] + A[i];
    } else {
      dp[i][0] = max(dp[i - 1][0], dp[i - 2][0] + A[i]);
      dp[i][1] = max(dp[i - 1][1], dp[i - 2][1] + A[i]);
    }
  }
  if (N % 2 == 0) {
    cout << dp[N - 1][1] << '\\n';
  } else {
    cout << dp[N - 1][0] << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll dp[100005][2];

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  dp[0][1] = A[0];
  dp[1][1] = max(A[0], A[1]);
  for (int i = 2; i < N; i++) {
    if (i % 2 == 0) {
      dp[i][0] = max(dp[i - 1][1], dp[i - 2][0] + A[i]);
      dp[i][1] = dp[i - 2][1] + A[i];
    } else {
      dp[i][0] = max(dp[i - 1][0], dp[i - 2][0] + A[i]);
      dp[i][1] = max(dp[i - 1][1], dp[i - 2][1] + A[i]);
    }
  }
  if (N % 2 == 0) {
    cout << dp[N - 1][1] << '\\n';
  } else {
    cout << dp[N - 1][0] << '\\n';
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define REP(i, init, n) for (int i = (int)(init); i < (int)(n); i++)

/*
#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>
*/

int main() {
  int N;
  cin >> N;
  vector<int> A(N);

  REP(i, 0, N) {
    int p;
    cin >> p;
    A[i] = p;
  }

  int select = N / 2;

  vector<vector<long>> DP(select, vector<long>(3, 0));
  REP(i, 0, 3) DP[0][i] = A[i];
  if (N % 2) {
    REP(i, 1, select) {
      DP[i][0] = DP[i - 1][0] + A[i * 2];
      DP[i][1] = max(DP[i - 1][0] + A[i * 2 + 1], DP[i - 1][1] + A[i * 2 + 1]);
      long temp = max(DP[i - 1][0] + A[i * 2 + 2], DP[i - 1][1] + A[i * 2 + 2]);
      DP[i][2] = max(temp, DP[i - 1][2] + A[i * 2 + 2]);
    }
  } else {
    REP(i, 1, select) {
      DP[i][0] = DP[i - 1][0] + A[i * 2];
      DP[i][1] = max(DP[i - 1][0] + A[i * 2 + 1], DP[i - 1][1] + A[i * 2 + 1]);
    }
  }
  long ans = 0 - __LONG_MAX__;
  REP(i, 0, 2 + N % 2) ans = max(ans, DP[select - 1][i]);
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, init, n) for (int i = (int)(init); i < (int)(n); i++)

#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>

int main() {
  int N;
  cin >> N;
  vector<long> A(N, 0);

  REP(i, 0, N) {
    int p = 0;
    cin >> p;
    A[i] = p;
  }

  int select = N / 2;

  vvl DP(select, vl(3, 0));
  REP(i, 0, 3) DP[0][i] = A[i];
  if (N % 2) {
    REP(i, 1, select) {
      DP[i][0] = DP[i - 1][0] + A[i * 2];
      DP[i][1] = max(DP[i - 1][0] + A[i * 2 + 1], DP[i - 1][1] + A[i * 2 + 1]);
      long temp = max(DP[i - 1][0] + A[i * 2 + 2], DP[i - 1][1] + A[i * 2 + 2]);
      DP[i][2] = max(temp, DP[i - 1][2] + A[i * 2 + 2]);
    }
  } else {
    REP(i, 1, select) {
      DP[i][0] = DP[i - 1][0] + A[i * 2];
      DP[i][1] = max(DP[i - 1][0] + A[i * 2 + 1], DP[i - 1][1] + A[i * 2 + 1]);
    }
  }
  long ans;
  REP(i, 0, 3) ans = max(ans, DP[select][i]);
  cout << ans << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

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

  long long sum = a[0];
  vector<long long> dp(n + 1, 0);
  for (int i = 1; i < n; i++) {
    if (i & 1) {
      dp[i + 1] = max(dp[i - 1] + a[i], sum);
    } else {
      dp[i + 1] = max(dp[i - 1] + a[i], dp[i]);
      sum += a[i];
    }
  }

  cout << dp[n] << '\\n';
  return 0;
}

Error: 0

Fixed submission:

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

constexpr long long INF = (1LL << 60);

template <class T, typename F> struct DisjointSparseTable {
private:
  const F f;
  vector<vector<T>> node;
  vector<int> idx;

public:
  DisjointSparseTable(const vector<T> &v, const F &f) : f(f) {
    int b = 0, n = (int)v.size();
    while ((1 << b) <= n)
      b++;
    idx.resize((1 << b), 0);
    node.resize(b, vector<T>(n));
    for (int i = 2; i < (1 << b); i++)
      idx[i] = idx[i >> 1] + 1;
    for (int i = 0; i < v.size(); i++)
      node[0][i] = v[i];
    for (int i = 1; i < b; i++) {
      int s = (1 << i);
      for (int j = 0; j < n; j += (s << 1)) {
        int t = min(j + s, n);
        node[i][t - 1] = v[t - 1];
        for (int k = t - 2; k >= j; k--) {
          node[i][k] = f(v[k], node[i][k + 1]);
        }
        if (n <= t)
          break;
        node[i][t] = v[t];
        for (int k = t + 1; k < min(t + s, n); k++) {
          node[i][k] = f(node[i][k - 1], v[k]);
        }
      }
    }
  }

  const T query(int l, int r) const {
    assert(l <= --r);
    if (l == r)
      return node[0][l];
    return f(node[idx[l ^ r]][l], node[idx[l ^ r]][r]);
  }
};

int main() {
  int n;
  cin >> n;
  vector<long long> a(n), nya(n, 0), nyaa(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i & 1)
      nya[i] = a[i];
    else
      nyaa[i] = a[i];
  }

  auto f = [](long long a, long long b) { return (a + b); };
  DisjointSparseTable<long long, decltype(f)> t(nya, f);
  DisjointSparseTable<long long, decltype(f)> s(nyaa, f);

  long long ans = -INF;

  if (n % 2 == 0) {
    ans = max(t.query(0, n), s.query(0, n));
    for (int i = 1; i < n - 1; i++) {
      if (i % 2 == 0) {
        if (i - 1 > 0) {
          long long res = s.query(0, i - 1) + t.query(i + 1, n);
          ans = max(res, ans);
        }
      } else {
        if (i - 1 > 0 and i + 1 < n) {
          long long r1 = s.query(0, i - 1) + t.query(i + 1, n);
          ans = max({ans, r1});
        }
      }
    }
  } else {
    ans = t.query(0, n);
    for (int i = 0; i < n; i++) {
      if (i % 2 == 0) {
        long long r1 = s.query(0, n) - a[i];
        ans = max(ans, r1);
        if (0 < i - 1 and i < n) {
          long long r2 = s.query(0, i - 1) + t.query(i + 1, n);
          long long r3 = t.query(0, i - 1) + s.query(i + 1, n);
          ans = max({ans, r2, r3});
        }
      } else {
        if (2 < i and i < n - 2) {
          long long r = s.query(0, i - 1) + a[i] + s.query(i + 2, n);
          ans = max(ans, r);
        }
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;

const int N = 2e5 + 10;
ll a[N], sum[N], dp[N][2];
const ll INF = 1e18;

map<pair<int, int>, ll> memo;

ll get(int n, int k) {
  if (n == 0 && k == 0)
    return 0;
  if (n < 2 * k - 1 || n < 1)
    return -INF;
  if (k == 0)
    return 0;
  if (n == 2 * k - 1)
    return sum[n];
  if (memo.find(PII(n, k)) == memo.end()) {
    memo[PII(n, k)] = max(a[n] + get(n - 2, k - 1), get(n - 1, k));
  }
  return memo[PII(n, k)];
}

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = a[i];
    if (i >= 2)
      sum[i] += sum[i - 2];
  }
  cout << get(n, n / 2) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;

const int N = 2e5 + 10;
ll a[N], sum[N], dp[N][2];
const ll INF = 1e18;

ll get(int n, int k) {
  if (n == 0 && k == 0)
    return 0;
  if (n < 2 * k - 1 || n < 1)
    return -INF;
  if (k == 0)
    return 0;
  if (n == 2 * k - 1)
    return sum[n];

  ll res = max(a[n] + get(n - 2, k - 1), get(n - 1, k));
  return res;
}

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = a[i];
    if (i >= 2)
      sum[i] += sum[i - 2];
  }
  cout << get(n, n / 2) << 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;

int n;
ll a[N], f[N], sum;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i & 1)
      sum += a[i];
    if (i == 1)
      continue;
    if (i & 1)
      f[i] = max(f[i - 2] + a[i], f[i - 1]);
    else
      f[i] = max(f[i - 2] + a[i], sum);
  }
  cout << f[n] << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n % 2) {
    vector<long long> a(n / 2 + 1);
    vector<long long> b(n / 2);
    long long suma = 0;
    long long sumb = 0;
    for (int i = 0; i < n / 2; i++) {
      cin >> a[i];
      suma += a[i];
      cin >> b[i];
      sumb += b[i];
    }
    cin >> a[n / 2 + 1];
    sort(a.begin(), a.end());
    if (suma - a[0] > sumb)
      cout << suma - a[0];
    else
      cout << sumb;
  }

  else {
    vector<long long> a1(n / 2);
    vector<long long> b1(n / 2);
    long long suma1 = 0;
    long long sumb1 = 0;
    for (int i = 0; i < n / 2; i++) {
      cin >> a1[i];
      suma1 += a1[i];
      cin >> b1[i];
      sumb1 += b1[i];
    }
    if (suma1 > sumb1)
      cout << suma1;
    else
      cout << sumb1;
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

int main(int, char **) {
  int n;
  cin >> n;

  // 偶数はi/2個かi/2-1個の選び方しかありえない
  // 奇数はi/2+1個かi/2個の選び方しかありえない
  vector<ll> max_choice(
      n + 1, 0); // 偶数からi/2個 or 奇数からi/2+1個 選んだ場合の総和の最大値
  vector<ll> min_choice(
      n + 1, 0); // 偶数からi/2-1個 or 奇数からi/2個 選んだ場合の総和の最大値

  ll tmp;
  for (int i = 1; i <= n; i++) {
    cin >> tmp;
    if (i == 1) {
      max_choice[i] = tmp;
      min_choice[i] = 0;
    } else if (i == 2) {
      max_choice[i] = max(max_choice[i - 1], tmp);
      min_choice[i] = 0;
    } else if (i % 2 == 0) {
      max_choice[i] = max(max_choice[i - 1], max_choice[i - 2] + tmp);
      min_choice[i] = max(min_choice[i - 1], min_choice[i - 2] + tmp);
    } else {
      max_choice[i] = max_choice[i - 2] + tmp;
      min_choice[i] = max(max_choice[i - 1], min_choice[i - 2] + tmp);
    }
  }

  if (n % 2 == 0)
    cout << max_choice[n] << endl;
  else
    cout << min_choice[n] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;

int main(int, char **) {
  int n;
  cin >> n;

  // acum[i][j] : 先頭からi個のデータのうちj個選んで総和を最大にしたときの最大値
  vector<vector<ll>> acum(n + 1, vector<ll>(n + 1, 0));

  ll tmp;
  for (int i = 1; i <= n; i++) {
    cin >> tmp;
    if (i == 1)
      acum[i][1] = tmp;
    else if (i == 2)
      acum[i][1] = max(acum[i - 1][1], tmp);
    else if (i % 2 == 0) {
      // 偶数はi/2個かi/2-1個の選び方しかありえない
      acum[i][i / 2] = max(acum[i - 1][i / 2], acum[i - 2][i / 2 - 1] + tmp);
      acum[i][i / 2 - 1] =
          max(acum[i - 1][i / 2 - 1], acum[i - 2][i / 2 - 2] + tmp);
    } else {
      // 奇数はi/2+1個かi/2個の選び方しかありえない
      acum[i][i / 2 + 1] = acum[i - 2][i / 2] + tmp;
      acum[i][i / 2] = max(acum[i - 1][i / 2], acum[i - 2][i / 2 - 1] + tmp);
    }
  }

  cout << acum[n][n / 2] << endl;
  return 0;
}