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

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

signed main() {
  int N;
  cin >> N;
  int vec[N + 1];
  vec[0] = 0;
  REP(i, N) { cin >> vec[i + 1]; }
  vector<vector<int>> dp(N + 1, vector<int>(2, 0));
  dp[1][1] = vec[1];
  dp[2][1] = max(vec[1], vec[2]);
  for (int i = 3; i <= N; i++) {
    if (i % 2 == 1) {
      dp[i][0] = max(dp[i - 2][0] + vec[i], dp[i - 1][1]);
      dp[i][1] = dp[i - 2][1] + vec[i];
    } else {
      dp[i][0] = max(dp[i - 2][0] + vec[i], dp[i - 1][0]);
      dp[i][1] = max(dp[i - 2][1] + vec[i], dp[i - 1][1]);
    }
  }
  cout << dp[N][(N + 1) % 2] << endl;
}


Error: TLE

Fixed submission:

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

signed main() {
  int N;
  cin >> N;
  int vec[N + 1];
  vec[0] = 0;
  REP(i, N) { cin >> vec[i + 1]; }
  int a = N / 2;
  vector<vector<int>> dp(N + 1, vector<int>(a + 2, 0));
  dp[1][1] = vec[1];
  dp[2][1] = max(vec[1], vec[2]);
  for (int i = 3; i <= N; i++) {
    if (i % 2 == 1) {
      dp[i][i / 2] = max(dp[i - 2][i / 2 - 1] + vec[i], dp[i - 1][i / 2]);
      dp[i][i / 2 + 1] = max(dp[i - 2][i / 2] + vec[i], dp[i - 1][i / 2 + 1]);
    } else {
      dp[i][i / 2 - 1] =
          max(dp[i - 2][i / 2 - 2] + vec[i], dp[i - 1][i / 2 - 1]);
      dp[i][i / 2] = max(dp[i - 2][i / 2 - 1] + vec[i], dp[i - 1][i / 2]);
    }
  }
  cout << dp[N][a] << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef unsigned long long _ulong;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define endk '\\n'
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const lint MOD = 1e9 + 7, INF = 1e18;
lint dx[8] = {0, -1, 1, 0, 1, -1, 1, -1}, dy[8] = {1, 0, 0, -1, -1, -1, 1, 1};

lint N, arr[200000], sum = 0, odd_sum = 0;
lint dp[200100][5];
int main() {
  cin >> N;
  REP(i, N) {
    cin >> arr[i];
    sum += arr[i];
    if (i % 2 == 0)
      odd_sum += arr[i];
  }
  REP(i, N + 10) {
    REP(j, 3) { dp[i][j] = -INF; }
  }
  dp[0][0] = 0;
  REP(i, N) {
    REP(j, 3) {
      REP(k, 3) {
        chmax(dp[i + 1][k + 1], dp[i][k]);
        chmax(dp[i + 2][k], dp[i][k] + arr[i]);
        chmax(dp[i + 3][k + 1], dp[i][k] + arr[i + 1]);
        chmax(dp[i + 4][k + 2], dp[i][k] + arr[i + 2]);
      }
    }
  }
  lint ans = -INF;
  if (N == 2) {
    cout << max(arr[0], arr[1]) << endk;
  } else if (N == 3 || N % 2 == 0) {
    REP(i, 3) { chmax(ans, dp[N + i][1]); }
    cout << ans << endk;
  } else {
    REP(i, 3) { chmax(ans, dp[N + i][2]); }
    cout << ans << endk;
  }
}

Error: WA

Fixed submission:

#pragma region template
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e9;
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W,
    x, y, z;
long long max_value = INF, max_index = -1;
long long min_value = MAX, min_index = -1;
typedef long long ll;
typedef pair<ll, ll> Pa;

ll arr[100000];
ll dfs(int index, int jumped) {
  if (index >= N)
    return 0;
  if (jumped == 2)
    return arr[index] + dfs(index + 2, jumped);
  else if (jumped == 1) {
    return max(arr[index] + dfs(index + 2, jumped),
               arr[index] + dfs(index + 3, jumped + 1));
  } else {
    return max(arr[index] + dfs(index + 2, jumped),
               max(arr[index] + dfs(index + 3, jumped + 1),
                   arr[index] + dfs(index + 4, jumped + 2)));
  }
}

int main() {
  cin >> N;
  rep(i, N) { cin >> arr[i]; }
  int init = N % 2 == 0 ? 1 : 0;
  ll max_val = dfs(0, init);
  max_val = max(max_val, dfs(1, init + 1));
  if (init == 0)
    max_val = max(max_val, dfs(2, 2));
  cout << max_val;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long dp[200005], sum[200005], s[200005];

int main() {
  int n;
  while (cin >> n) {
    for (int i = 1; i <= n; i++)
      cin >> s[i];
    for (int i = 1; i <= n; i++) {
      if (i == 1)
        sum[i] = s[i];
      else
        sum[i] = sum[i - 2] + s[i];
    }
    for (int i = 2; i <= n; i++) {
      if (i % 2 == 0) {
        dp[i] = max(dp[i - 2] + s[i], sum[i - 1]);
      } else {
        dp[i] = max(dp[i - 2] + s[i], dp[i - 1]);
      }
    }
    cout << dp[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define mset(s, _) memset(s, _, sizeof s)
#define fi first
#define se second
#define pb push_back

template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

map<int, ll> dp[200010];
ll a[200010], n;

ll dfs(int num, int k) {
  if (num > n && k > (n - num + 1) / 2)
    return -1e18;
  if (num > n || k < 0)
    return 0;
  if (dp[num].count(k))
    return dp[num][k];
  if ((n - num + 1) & 1) {
    return dp[num][k] = max(dfs(num + 2, k - 1) + a[num],
                            max(dfs(num + 3, k - 1) + a[num + 1],
                                dfs(num + 4, k - 1) + a[num + 2]));
  } else {
    return dp[num][k] = max(dfs(num + 2, k - 1) + a[num],
                            dfs(num + 3, k - 1) + a[num + 1]);
  }
}

int main() {
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  cout << dfs(1, n / 2) << '\\n';
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error



#include <bits/stdc++.h>

using namespace std;

#define ll long long int

#define endl '\\n'

#define rep(i, a, b) for (ll i = a; i < b; i++)

void solve()

{

  ll n;

  cin >> n;

  ll a[n + 1];

  rep(i, 1, n + 1) cin >> a[i];

  ll ps[n + 1], dp[n + 1];

  dp[0] = 0;
  dp[1] = 0;

  ps[1] = a[1];

  for (ll i = 3; i <= n; i += 2)
    ps[i] = ps[i - 2] + a[i];

  rep(i, 2, n + 1) {

    if (i & 1) {

      dp[i] = max(dp[i - 1], a[i] + dp[i - 2]);

    }

    else {

      dp[i] = max(dp[i - 2] + a[i], ps[i - 1]);
    }
  }

  cout << dp[n] << endl;
}

int32_t main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define pii pair<ll, ll>
#define debug(a) cout << a << '\\n'
#define maxn 3009 /// leave to tomorrow anything that can be done today
#define MOD 1e9 + 7
#define F first
#define S second
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define per(i, b, a) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e17 + 9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  ll vet[n + 2];
  ll dp[n + 2][n + 2];
  memset(dp, 0, sizeof dp);
  rep(i, 1, n + 1) cin >> vet[i];
  dp[1][1] = vet[1];
  rep(i, 2, n + 1) {
    rep(j, 1, (i + 1) / 2 + 1) {
      dp[i][j] = max(dp[i - 2][j - 1] + vet[i], dp[i - 1][j]);
    }
  }
  cout << dp[n][n / 2] << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long n, aa[202020], dp[202020][3], sum, sum1;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> aa[i];
    sum += aa[i];
  }
  if (n == 3) {
    cout << max(aa[1], max(aa[2], aa[3])) << endl;
    return 0;
  }
  if (n % 2 == 0) {
    for (int i = 1; i <= n; i++) {
      dp[i][0] = -1e16;
      dp[i][1] = -1e16;
    }
    dp[1][0] = aa[1];
    dp[2][1] = aa[2];
    for (int i = 3; i <= n; i++) {
      dp[i][0] = dp[i - 2][0] + aa[i];
      dp[i][1] = max(dp[i - 3][0], dp[i - 2][1]) + aa[i];
    }
    cout << max(dp[n - 1][0], dp[n][1]) << endl;
  } else {
    for (int i = 1; i <= n; i++) {
      dp[i][0] = -1e16;
      dp[i][1] = -1e16;
      dp[i][2] = -1e16;
    }
    dp[1][0] = aa[1];
    dp[2][1] = aa[2];
    dp[3][0] = dp[1][0] + aa[3];
    dp[3][2] = dp[0][0] + aa[3];
    for (int i = 4; i <= n; i++) {
      dp[i][0] = dp[i - 2][0] + aa[i];
      dp[i][1] = max(dp[i - 3][0], dp[i - 2][1]) + aa[i];
      dp[i][2] = max(max(dp[i - 4][0], dp[i - 3][1]), dp[i - 2][2]) + aa[i];
    }
    cout << max(dp[n - 2][0], max(dp[n - 1][1], dp[n][2])) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long n, aa[101010], dp[101010][3], sum, sum1;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> aa[i];
    sum += aa[i];
  }
  if (n == 3) {
    cout << max(aa[1], max(aa[2], aa[3])) << endl;
    ;
    return 0;
  }
  if (n % 2 == 0) {
    for (int i = 1; i <= n; i += 2) {
      sum1 += aa[i];
    }
    cout << max(sum1, sum - sum1) << endl;
  } else {
    for (int i = 1; i <= n; i++) {
      dp[i][0] = -1e16;
      dp[i][1] = -1e16;
    }
    dp[1][0] = aa[1];
    dp[2][0] = aa[2];
    dp[3][0] = dp[1][0] + aa[3];
    dp[3][1] = dp[0][0] + aa[3];
    for (int i = 4; i <= n; i++) {
      dp[i][0] = dp[i - 2][0] + aa[i];
      dp[i][1] = max(dp[i - 3][0], dp[i - 2][1]) + aa[i];
      dp[i][2] = max(max(dp[i - 4][0], dp[i - 3][1]), dp[i - 2][2]) + aa[i];
    }
    cout << max(dp[n - 2][0], max(max(dp[n - 1][0], dp[n - 1][1]), dp[n][2]))
         << endl;
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
const ll MOD = 1000000007, MOD2 = 998244353, INF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  vector<vector<ll>> dp(n, vector<ll>(5, -INF));
  dp.at(0).at(0) = a.at(0);
  dp.at(1).at(1) = a.at(1);
  if (n > 2)
    dp.at(2).at(2) = a.at(2);
  int yutori;
  if (n % 2 == 0) {
    yutori = 1;
  } else {
    yutori = 2;
  }
  rep(i, n) {
    rep(j, 3) {
      if (i + 2 <= n - 1)
        chmax(dp.at(i + 2).at(j), dp.at(i).at(j) + a.at(i + 2));
      if (i + 3 <= n - 1)
        chmax(dp.at(i + 3).at(j + 1), dp.at(i).at(j) + a.at(i + 3));
      if (i + 4 <= n - 1)
        chmax(dp.at(i + 4).at(j + 2), dp.at(i).at(j) + a.at(i + 4));
    }
  }
  ll ans = dp.at(n - 1).at(yutori);
  chmax(ans, dp.at(n - 2).at(yutori - 1));
  if (yutori == 2)
    chmax(ans, dp.at(n - 3).at(yutori - 2));
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
const ll MOD = 1000000007, MOD2 = 998244353, INF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  vector<vector<ll>> dp(n, vector<ll>(5, -INF));
  dp.at(0).at(0) = a.at(0);
  dp.at(1).at(1) = a.at(1);
  dp.at(2).at(2) = a.at(2);
  int yutori;
  if (n % 2 == 0) {
    yutori = 1;
  } else {
    yutori = 2;
  }
  rep(i, n) {
    rep(j, 3) {
      if (i + 2 <= n - 1)
        chmax(dp.at(i + 2).at(j), dp.at(i).at(j) + a.at(i + 2));
      if (i + 3 <= n - 1)
        chmax(dp.at(i + 3).at(j + 1), dp.at(i).at(j) + a.at(i + 3));
      if (i + 4 <= n - 1)
        chmax(dp.at(i + 4).at(j + 2), dp.at(i).at(j) + a.at(i + 4));
    }
  }
  ll ans = dp.at(n - 1).at(yutori);
  chmax(ans, dp.at(n - 2).at(yutori - 1));
  if (yutori == 2)
    chmax(ans, dp.at(n - 3).at(yutori - 2));
  cout << ans << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 2e5 + 10;
int N;
int arr[MAXN];

typedef long long int ll;

ll dp[MAXN][3];

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", arr + i);

  dp[1][0] = 0;
  dp[1][1] = arr[1];
  dp[0][0] = 0;
  dp[2][0] = 0;

  for (int i = 0; i <= N; i++) {
    for (int j = max(i / 2 - 1, 0); j <= (i + 1) / 2; j++) {
      // printf("i=%d j=%d\\n",i,j);
      if (i + 2 <= N)
        dp[i + 2][(j + 1) % 3] = dp[i][j % 3] + arr[i + 2];
      if (i + 1 <= N)
        dp[i + 1][j % 3] = max(dp[i + 1][j % 3], dp[i][j % 3]);
    }
  }

  printf("%lld\\n", dp[N][(N / 2) % 3]);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 2e5 + 10;
int N;
int arr[MAXN];

typedef long long int ll;

vector<ll> dp[MAXN];

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", arr + i);

  for (int i = 0; i <= N; i++)
    dp[i].resize(N);

  dp[1][0] = 0;
  dp[1][1] = arr[1];
  dp[0][0] = 0;
  dp[2][0] = 0;

  for (int i = 0; i <= N; i++) {
    for (int j = max(i / 2 - 1, 0); j <= (i + 1) / 2; j++) {
      // printf("i=%d j=%d\\n",i,j);
      if (i + 2 <= N)
        dp[i + 2][j + 1] = dp[i][j] + arr[i + 2];
      if (i + 1 <= N)
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }

  printf("%lld\\n", dp[N][N / 2]);

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

//  Copyright © 2020 Diego Garcia Rodriguez del Caviso. All rights reserved.
#include <bits/stdc++.h>
#define MAX 200005
#define pii pair<ll, ll>
#define fi first
#define se second
#define INF 1e18
#define optimiza_io                                                            \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0);
#define forn(i, a, b) for (ll i = a; i <= b; i++)
#define mod(n, k) (((n % k) + k) % k)
using namespace std;
typedef long long ll;

ll N, a[MAX];
map<pii, ll> vis;

ll dfs(ll p, ll l) {
  if (!l)
    return 0;
  if (p > N)
    return -INF;
  if (p + l * 2 - 2 > N)
    return -INF;
  auto it = vis.find({p, l});
  if (it != vis.end())
    return it->second;

  ll ans = -INF;
  ans = max(ans, dfs(p + 2, l - 1) + a[p]);
  ans = max(ans, dfs(p + 3, l - 1) + a[p]);
  ans = max(ans, dfs(p + 4, l - 1) + a[p]);
  return vis[{p, l}] = ans;
}

int main() {
  optimiza_io cin >> N;
  forn(i, 1, N) cin >> a[i];

  cout << max(dfs(1, N / 2), max(dfs(2, N / 2), dfs(3, N / 2))) << "\\n";
  return 0;
}

// CHECAR LIMITES

Error: TLE

Fixed submission:

//  Copyright © 2020 Diego Garcia Rodriguez del Campo. All rights reserved.
#include <bits/stdc++.h>
#define MAX 200005
#define INF 1e18
#define optimiza_io                                                            \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0);
#define forn(i, a, b) for (ll i = a; i <= b; i++)
#define mod(n, k) (((n % k) + k) % k)
using namespace std;
typedef long long ll;

ll N, a[MAX];

ll dfs(ll p, ll l) {
  if (!l)
    return 0;
  if (p > N)
    return -INF;
  if (p + l * 2 - 2 > N)
    return -INF;

  ll ans = -INF;
  ans = max(ans, dfs(p + 2, l - 1) + a[p]);
  ans = max(ans, dfs(p + 3, l - 1) + a[p]);
  ans = max(ans, dfs(p + 4, l - 1) + a[p]);
  return ans;
}

int main() {
  optimiza_io cin >> N;
  forn(i, 1, N) cin >> a[i];

  cout << max(dfs(1, N / 2), max(dfs(2, N / 2), dfs(3, N / 2))) << "\\n";
  return 0;
}

// CHECAR LIMITES
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

long long n;
long long a[200010];
long long dp[200010][10];

int main(void) {
  scanf("%lld", &n);

  for (long long i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  dp[0][0] = a[0];
  dp[1][1] = a[1];

  if (n > 2) {
    dp[2][2] = a[2];
    dp[2][0] = a[0] + a[2];
  }

  if (n > 3)
    dp[3][1] = max(a[0] + a[3], a[1] + a[3]);

  for (int i = 4; i < n; i++) {
    if (i % 2 == 0)
      dp[i][0] = dp[i - 2][0] + a[i];
    dp[i][1] = max(dp[i - 2][1] + a[i], dp[i - 3][0] + a[i]);
    dp[i][2] =
        max(dp[i - 2][2] + a[i], max(dp[i - 3][1] + a[i], dp[i - 4][0] + a[i]));
  }

  if (n % 2 == 0)
    printf("%lld", max(dp[n - 1][1], dp[n - 2][0]));
  else
    printf("%lld", max(max(dp[n - 1][2], dp[n - 2][1]), dp[n - 3][0]));

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long n;
long long a[200010];
long long maxx;
long long ssum;

void jjak(long long ch, long long here, long long hap, long long t) {
  if (ch == n / 2) {
    maxx = max(maxx, hap);
    return;
  }

  jjak(ch + 1, here + 2, hap + a[here + 2], t);

  if (t == 0)
    jjak(ch + 1, here + 3, hap + a[here + 3], 1);
}

void hhol(long long ch, long long here, long long hap, long long t) {
  if (ch == n / 2) {
    maxx = max(maxx, hap);
    return;
  }
  // printf("%lld\\n", hap);
  hhol(ch + 1, here + 2, hap + a[here + 2], t);
  //	printf("\\n");
  if (t <= 1) {
    //	printf("%lld\\n", hap);
    hhol(ch + 1, here + 3, hap + a[here + 3], t + 1);
    //	printf("\\n");
  }
}

int main(void) {
  scanf("%lld", &n);

  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);

  if (n % 2 == 0) {
    jjak(0, 1, a[1], 0);

    for (int i = 2; i <= n; i += 2)
      ssum += a[i];

    maxx = max(maxx, ssum);
  }

  else {
    hhol(0, 1, a[1], 0);
    jjak(0, 2, a[2], 0);
    hhol(0, 3, a[3], 0);
  }

  printf("%lld", maxx);
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 200200;
const ll Inf = 1e18;

int n;
int a[N];
ll Dp[N][3];

ll Rec(int i, int j) {
  if (j < 0)
    return -Inf;
  if (i >= n)
    return j <= 1 ? 0 : -Inf;

  if (Dp[i][j] + 1)
    return Dp[i][j];

  return Dp[i][j] = max(a[i] + Rec(i + 2, j), Rec(i + 1, j - 1));
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    scanf("%d", a + i);

  memset(Dp, -1, sizeof(Dp));

  cout << Rec(0, n & 1 ? 2 : 1) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 100100;
const ll Inf = 1e18;

int n;
int a[N];
ll Dp[N][3];

ll Rec(int i, int j) {
  if (j < 0)
    return -Inf;
  if (i >= n)
    return j == 0 ? 0 : -Inf;

  if (Dp[i][j] + 1)
    return Dp[i][j];

  return Dp[i][j] = max(a[i] + Rec(i + 2, j), Rec(i + 1, j - 1));
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    scanf("%d", a + i);

  memset(Dp, -1, sizeof(Dp));

  cout << Rec(0, n & 1 ? 2 : 1) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<long long> dpfl(200010, -INF), dpc(200010, -INF);
  dpfl[N - 1] = 0LL;
  dpc[N - 1] = A[N - 1];
  if (N >= 2) {
    dpfl[N - 2] = max(A[N - 2], A[N - 1]);
    dpc[N - 2] = max(A[N - 2], A[N - 1]);
  }
  if (N >= 3) {
    dpfl[N - 3] = max({A[N - 3], A[N - 2], A[N - 1]});
    dpc[N - 3] = A[N - 3] + A[N - 1];
  }

  for (int i = N - 4; i >= 0; i--) {
    if ((N - i) % 2 == 0) {
      dpfl[i] = dpc[i] = max(A[i] + dpfl[i + 2], A[i + 1] + dpc[i + 3]);
    } else {
      dpfl[i] = max({A[i] + dpfl[i + 2], A[i + 1] + dpfl[i + 3], dpc[i + 2]});
      dpc[i] = A[i] + dpc[i + 2];
    }
  }

  cout << dpfl[0] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<long long> dpfl(200010, -INF), dpc(200010, -INF);
  dpfl[N - 1] = 0LL;
  dpfl[N - 2] = max(A[N - 2], A[N - 1]);
  dpfl[N - 3] = max({A[N - 3], A[N - 2], A[N - 1]});
  dpc[N - 1] = A[N - 1];
  dpc[N - 2] = max(A[N - 2], A[N - 1]);
  dpc[N - 3] = A[N - 3] + A[N - 1];
  for (int i = N - 4; i >= 0; i--) {
    if ((N - i) % 2 == 0) {
      dpfl[i] = dpc[i] = max(A[i] + dpfl[i + 2], A[i + 1] + dpc[i + 3]);
    } else {
      dpfl[i] = max({A[i] + dpfl[i + 2], A[i + 1] + dpfl[i + 3], dpc[i + 2]});
      dpc[i] = A[i] + dpc[i + 2];
    }
  }

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

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ll long long

void file() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}

const int MAX = 2e5 + 5;

int n;

int a[MAX];

ll dp[MAX][3]; // dp[N][K] holds the maximum sum of choosing N / 2 element and
               // skipping K elements

ll solve(int idx, int k) {
  if (idx > n) {
    return k == 2 ? -1e17 : 0;
  }

  ll &ret = dp[idx][k];

  if (~ret) {
    return ret;
  }
  ret = -1e17;

  ret = max(ret, solve(idx + 2, k) + a[idx]);

  if (k) // you can skip k elements in the serius
  {
    ret = max(ret, solve(idx + 1, k - 1));
  }
  return ret;
}

int main(int argc, char const *argv[]) {
  file();

  cin >> n;

  memset(dp, -1, sizeof(dp));

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

  cout << solve(0, (n & 1) + 1);
}

Error: TLE

Fixed submission:

// using no_way as a way
// using no_limitation as a limitation

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = (1e-9);
typedef long long ll;
typedef unsigned long long ull;
#define flush fflush(stdout), cout.flush()
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define clr(v, line) memset(v, line, sizeof(v))
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define RT(x) return cout << x, 0;
// #define lcm(a,b) (a*(b/__gcd(a,b)))
#define P 1000000007
#define endl '\\n'
// const int dx[] = {1,-1,0,0,1,-1,1,-1};
// const int dy[] = {0,0,1,-1,1,-1,-1,1};
// const int dxhorse[] = {-2,-2,-1,-1,1,1,2,2};
// const int dyhorse[] = {1,-1,2,-2,2,-2,1,-1};

void file() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
const int MX = 2e5 + 5;
int n;

ll dp[MX][3], v[MX];

ll solve(int idx, int cnt, int k) {
  if (idx > n or cnt == n / 2) {
    return 0;
  }

  ll ret = dp[idx][k];

  if (~ret)
    return ret;

  ll d = LLONG_MIN;

  for (int i = 0; i <= k; ++i) // choose idx
  {
    d = max(d, solve(idx + 2 + i, cnt + 1, k - i) + v[idx]);
  }
  for (int i = 1; i <= k; ++i) // don't choose idx
  {
    d = max(d, solve(idx + i, cnt, k - i));
  }
  return ret = d;
}
int main(int argc, char const *argv[]) {
  // file();
  // fast();
  clr(dp, -1);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &v[i]);
  }
  printf("%lld", solve(1, 0, 1 + (n & 1)));
}