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

#include <bits/stdc++.h>

using namespace std;

typedef long long int li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pi;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define B begin()
#define E end()
#define f(i, l, r) for (li i = l; i <= r; ++i)
#define fr(i, l, r) for (li i = l; i >= r; --i)
#define u_map unordered_map
#define endl "\\n"

li fastpow(li base, li exp, li M) {
  li res = 1;
  while (exp > 0) {
    if (exp & 1)
      res = (res * base) % M;

    base = (base * base) % M;
    exp >>= 1;
  }
  return res;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  li n;

  cin >> n;

  li a[n + 1];

  f(i, 1, n) { cin >> a[i]; }

  vi sum(n + 1);

  vi dp(n + 1);

  f(i, 1, n) { sum[i] = (i > 2 ? sum[i - 2] : 0) + a[i]; }

  f(i, 2, n) {
    if (i & 1)
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    else
      dp[i] = max(sum[i - 1], dp[i - 2] + a[i]);
  }

  cout << dp[n];

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pi;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define B begin()
#define E end()
#define f(i, l, r) for (li i = l; i <= r; ++i)
#define fr(i, l, r) for (li i = l; i >= r; --i)
#define u_map unordered_map
#define endl "\\n"

li fastpow(li base, li exp, li M) {
  li res = 1;
  while (exp > 0) {
    if (exp & 1)
      res = (res * base) % M;

    base = (base * base) % M;
    exp >>= 1;
  }
  return res;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  li n;

  cin >> n;

  li a[n];

  f(i, 0, n - 1) { cin >> a[i]; }

  li ans = 0;

  if (n % 2 == 0) {
    ans = -1e18;

    li sum1 = 0, sum2 = 0;

    for (li i = 0; i < n - 1; i += 2) {
      sum1 += a[i];
      sum2 += a[i + 1];
    }

    ans = max(sum1, sum2);
  } else {
    vector<vi> dp(n + 1, vi(n / 2 + 1));

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

    f(i, 2, n) { dp[i][1] = max(dp[i - 1][1], a[i - 1]); }

    f(i, 2, n) {
      f(j, 2, i / 2) {
        li tp1 = -1e18, tp2 = -1e18;

        if (i >= 2) {
          tp1 = dp[i - 2][j - 1] + a[i - 1];
        }

        if (i >= 1) {
          tp2 = dp[i - 1][j];
        }

        dp[i][j] = max(tp1, tp2);
      }
    }

    // f(i,1,n)
    // {
    //     f(j,1,n/2)
    //     {
    //         cout<<dp[i][j]<<" ";
    //     }
    //     cout<<endl;
    // }

    ans = dp[n][n / 2];
  }

  cout << ans;

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int INF = 1e18 + 239;

int n;
vector<int> a;
map<pair<int, int>, int> mem;

int solve(int len, int cnt) {
  if (cnt > (len + 1) / 2)
    return -INF;
  if (len <= 0)
    return 0;
  if (!mem.count({len, cnt})) {
    // cerr << len << ' ' << cnt << endl;
    int f = a[len - 1] + solve(len - 2, cnt - 1);
    int s = solve(len - 1, cnt);
    mem[{len, cnt}] = max(f, s);
  }
  return mem[{len, cnt}];
}

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

  cin >> n;
  a.resize(n);
  for (auto &t : a) {
    cin >> t;
    // t = rand();
  }
  cout << solve(n, n / 2) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int INF = 1e18 + 239;

int n;
vector<int> a;

int solve(int len, int cnt) {
  if (cnt > (len + 1) / 2)
    return -INF;
  if (len <= 0)
    return 0;
  int f = a[len - 1] + solve(len - 2, cnt - 1);
  int s = solve(len - 1, cnt);
  return max(f, s);
}

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

  cin >> n;
  a.resize(n);
  for (auto &t : a) {
    cin >> t;
    // t = rand();
  }
  cout << solve(n, n / 2) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

const ll maxs = 2e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'

void pre() {}

ll n;
ll a[maxs];
ll dp[maxs][4];
bool vis[maxs][4];

ll go(ll pos, ll have) {
  if (pos >= n) {
    if (have <= 1)
      return 0;
    return -oo;
  }
  ll &ans = dp[pos][have];
  if (vis[pos][have])
    return ans;
  vis[pos][have] = true;

  if (!have)
    ans = a[pos] + go(pos + 2, have);
  else
    ans = max(a[pos] + go(pos + 2, have), go(pos + 1, have - 1));

  return ans;
}

void solve() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  ll ans;
  if (n & 1) {
    ans = max(go(0, 2), go(1, 1));
  } else {
    ans = max(go(0, 1), go(1, 0));
  }
  cout << ans << endl;
}

int main() {
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

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

const ll maxs = 2e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'

void pre() {}

ll n;
ll a[maxs];
ll dp[maxs][4];
bool vis[maxs][4];

ll go(ll pos, ll have) {
  if (pos >= n) {
    if (have <= 1)
      return 0;
    return -oo;
  }
  ll &ans = dp[pos][have];
  if (vis[pos][have])
    return ans;
  vis[pos][have] = true;

  if (!have)
    ans = a[pos] + go(pos + 2, have);
  else
    ans = max(a[pos] + go(pos + 2, have), go(pos + 1, have - 1));
}

void solve() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  ll ans;
  if (n & 1) {
    ans = max(go(0, 2), go(1, 1));
  } else {
    ans = max(go(0, 1), go(1, 0));
  }
  cout << ans << endl;
}

int main() {
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

const ll maxs = 2e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'

void pre() {}

ll dp[maxs];
ll pr[maxs];

void solve() {
  ll n;
  cin >> n;
  ll a[n + 1];
  for (ll i = 1; i < n + 1; i++)
    cin >> a[i];

  pr[1] = a[1];
  for (ll i = 2; i < n + 1; i++) {
    pr[i] = pr[i - 2] + a[i];
  }

  dp[0] = dp[1] = 0;
  for (ll i = 2; i < n + 1; i++) {
    if (i & 1) { // we have option to skip
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    } else {
      dp[i] = max(dp[i - 2] + a[i], pr[i - 1]);
    }
  }
  cout << dp[n] << endl;
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input00.txt","r",stdin);
  // #endif
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

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

const ll maxs = 1e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'

void pre() {}

ll dp[maxs];

void solve() {
  ll n;
  cin >> n;
  ll a[n + 1];
  for (ll i = 1; i < n + 1; i++)
    cin >> a[i];

  if (n & 1) {

    dp[0] = dp[1] = 0;
    for (ll i = 2; i <= n; i++) {
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    }
    cout << dp[n] << endl;
  } else {
    dp[1] = a[1];
    dp[2] = a[2];
    for (ll i = 3; i < n + 1; i++) {
      dp[i] = dp[i - 2] + a[i];
    }
    ll ans = max(dp[n], dp[n - 1]);
    cout << ans << endl;
  }
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input00.txt","r",stdin);
  // #endif
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
const int MaxN = 2e5 + 10;
int N;
int A[MaxN];

void solve() {
  vector<ll> prefix(N + 1);
  vector<ll> f(N + 1);
  prefix[1] = A[1];
  for (int i = 3; i <= N; i += 2)
    prefix[i] = prefix[i - 2] + A[i];
  for (int n = 2; n <= N; n++) {
    if (n & 1)
      f[n] = max(f[n - 1], A[n] + f[n - 2]);
    else
      f[n] = max(prefix[n - 1], A[n] + f[n - 2]);
  }
  cout << f[N] << endl;
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  solve();
  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
const int MaxN = 1e5 + 10;
int N;
int A[MaxN];

void solve() {
  vector<ll> prefix(N + 1);
  vector<ll> f(N + 1);
  prefix[1] = A[1];
  for (int i = 3; i <= N; i += 2)
    prefix[i] = prefix[i - 2] + A[i];
  for (int n = 2; n <= N; n++) {
    if (n & 1)
      f[n] = max(f[n - 1], A[n] + f[n - 2]);
    else
      f[n] = max(prefix[n - 1], A[n] + f[n - 2]);
  }
  cout << f[N] << endl;
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  solve();
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define LL long long
#define MAXN 200010
#define P 1000000007
using namespace std;
int N;
int A[MAXN];
map<int, LL> dp[MAXN];

int main() {

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  dp[1][0] = 0;
  dp[1][1] = A[1];
  dp[2][0] = 0;
  dp[2][1] = max(A[1], A[2]);
  for (int i = 3; i <= N; i++) {
    for (int j = i / 2 - 1; j <= (i + 1) / 2; j++) {
      // if(dp[i-1].count(j) && !dp[i-2].count(j-1)) continue;	//
      // 确定每一轮都要加一个
      if (dp[i - 1].count(j)) {
        dp[i][j] = dp[i - 1][j];
      }
      if (dp[i - 2].count(j - 1)) {
        if (!dp[i].count(j))
          dp[i][j] = dp[i - 2][j - 1] + A[i];
        else
          dp[i][j] = max(dp[i][j], dp[i - 2][j - 1] + A[i]);
      }
    }
  }

  cout << dp[N][N / 2] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

#define LL long long
// #define MAXN    200010
#define MAXN 20010
#define P 1000000007
using namespace std;
int N;
int A[MAXN];
int dp[MAXN][MAXN / 2];

int main() {

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  memset(dp, 0, sizeof(dp));
  dp[1][1] = A[1];
  dp[2][1] = max(A[1], A[2]);
  for (int i = 2; i <= N; i++) {
    for (int j = 1; j <= i / 2; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + A[i]);
    }
  }

  /*
          int i, j;
          i = N;j=N/2;
          cout<<"select:"<<endl;
          while(i&&j) {
                  if(sel[i][j] != 0) {
                          cout<<A[sel[i][j]]<<endl;
                          i-=2;
                          j-=1;
                  }else {
                          i-=1;
                  }
          }
          */
  cout << dp[N][N / 2] << endl;

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

#define long long long
#define ft(i, a, b) for (int i = (long)a; i <= (long)b; i++)
#define fd(i, a, b) for (int i = (long)a; i >= (long)b; i--)

const int mxN = 2e5 + 5;
const long oo = 1e15;

long n, ans;
long a[mxN], dp[mxN], pre[mxN];

void Solve() {
  pre[1] = a[1];
  for (int i = 3; i <= n; i += 2) {
    pre[i] = pre[i - 2] + a[i];
  }
  /*ft(i , 1 , n) if (i % 2 == 1) cout << pre[i] << " ";
  else cout << "  ";
  cout << "\\n";*/
  dp[1] = 0;
  dp[2] = max(a[1], a[2]);
  ft(i, 3, n) {
    if (i % 2 == 1) {
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    } else
      dp[i] = max(pre[i - 1], dp[i - 2] + a[i]);
  }
  // ft(i , 1 , n) cout << dp[i] << " ";
  cout << dp[n];
}

void FastInput() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen(".inp", "r", stdin);
  // freopen(".out", "w", stdout);
}

void Input() {
  cin >> n;
  ft(i, 1, n) cin >> a[i];
}

int main() {
  FastInput();
  Input();
  Solve();
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

#define long long long
#define ft(i, a, b) for (int i = (long)a; i <= (long)b; i++)
#define fd(i, a, b) for (int i = (long)a; i >= (long)b; i--)

const int mxN = 1e4 + 5;
const long oo = 1e15;

long n;
long a[mxN], dp[mxN][mxN];
bool mark[mxN][mxN];

long cal(int idx, int cnt) {
  if (idx > n) {
    if (cnt == n / 2)
      return 0;
    return -oo;
  }
  if (mark[idx][cnt])
    return dp[idx][cnt];
  mark[idx][cnt] = true;

  dp[idx][cnt] = max(cal(idx + 1, cnt), cal(idx + 2, cnt + 1) + a[idx]);
  return dp[idx][cnt];
}

void Solve() { cout << cal(1, 0); }

void FastInput() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen(".inp", "r", stdin);
  // freopen(".out", "w", stdout);
}

void Input() {
  cin >> n;
  ft(i, 1, n) cin >> a[i];
}

int main() {
  FastInput();
  Input();
  Solve();
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long A, B, C, D, H, K, L, M, N, T, W, X;
  string S;
  cin >> N;
  vector<long long> vec(N);
  long long dp[N][5];
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }

  A = 0;

  if (N % 2 == 0) {
    dp[1][0] = vec[0];
    dp[1][1] = vec[1];
    if (N > 2) {
      for (int j = 2; j < N / 2 + 1; j++) {
        dp[j][0] = dp[j - 1][0] + vec[j * 2 - 2];
        dp[j][1] =
            max(dp[j - 1][0] + vec[j * 2 - 1], dp[j - 1][1] + vec[j * 2 - 1]);
      }
      cout << max(dp[N / 2][0], dp[N / 2][1]) << endl;
    } else {
      cout << max(vec[0], vec[1]) << endl;
    }
  } else {
    dp[2][0] = max(vec[0], vec[1]);
    dp[2][1] = vec[2];
    dp[2][2] = vec[0] + vec[2];
    if (N > 3) {
      for (int i = 3; i < (N + 1) / 2 + 1; i++) {
        dp[i][0] = max(dp[i - 1][0] + vec[i * 2 - 3], dp[i - 1][2]);
        dp[i][1] =
            max(dp[i - 1][0] + vec[i * 2 - 2], dp[i - 1][1] + vec[i * 2 - 2]);
        dp[i][2] = dp[i - 1][2] + vec[i * 2 - 2];
        // cout << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << endl;
      }
      cout << max(dp[(N + 1) / 2][0], dp[(N + 1) / 2][1]) << endl;
    } else {
      cout << max(max(vec[0], vec[1]), vec[2]) << endl;
    }
  }
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 6) >= this->size() (which is 6)


Fixed submission:

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

int main() {
  long long A, B, C, D, H, K, L, M, N, T, W, X;
  string S;
  cin >> N;
  vector<long long> vec(N);
  long long dp[N][5];
  for (int i = 0; i < N; i++) {
    cin >> vec.at(N);
  }

  A = 0;

  if (N % 2 == 0) {
    dp[1][0] = vec[0];
    dp[1][1] = vec[1];
    for (int j = 2; j < N / 2 + 1; j++) {
      dp[j][0] = dp[j - 1][0] + vec[j * 2 - 1];
      dp[j][1] =
          min(dp[j - 1][0] + vec[j * 2 - 2], dp[j - 1][1] + vec[j * 2 - 2]);
    }
    cout << min(dp[N / 2][0], dp[N / 2][1]) << endl;
  }
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cout << ' ';
    cout << elem;
  }
  cout << endl;
}

// 解説参考.
int main() {
  int n;
  cin >> n;

  vector<llong> a(n);
  REP(i, n) cin >> a[i];

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

  /*REP(i,n) cout<<dp[i][0]<<' ';
  cout<<endl;
  REP(i,n) cout<<dp[i][1]<<' ';
  cout<<endl;*/

  cout << max(dp[n - 1][0], dp[n - 1][1]) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cout << ' ';
    cout << elem;
  }
  cout << endl;
}

// 解説参考.
int main() {
  int n;
  cin >> n;

  vector<llong> a(n);
  REP(i, n) cin >> a[i];

  vector<vector<llong>> dp(n, vector<llong>(n + 1, -INF));
  dp[0][0] = 0;
  dp[0][1] = a[0];
  dp[1][0] = 0;
  dp[1][1] = max(a[0], a[1]);
  for (int i = 2; i < n; ++i) {
    for (int j = (i + 1 - 1) / 2; j <= (i + 1 + 1) / 2; ++j) {
      debug(j);
      dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + a[i]);
    }
  }

  // REP(i,n) cout<<dp[i][(i+1)/2]<<endl;

  cout << dp[n - 1][n / 2] << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pbds                                                                   \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define _ ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define mod 1000000007
#define deb(x) cerr << "\\n" << #x << "=" << x << "\\n";
#define deb2(x, y)                                                             \\
  cerr << "\\n" << #x << "=" << x << "\\n" << #y << "=" << y << "\\n";
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

int n;
vector<int> a;
map<pair<int, int>, int> dp;

int go(int ind, int left) {
  if (left == 0)
    return 0;

  if (ind >= n || n - ind < left * 2 - 1)
    return -1e16;

  if (dp.find({ind, left}) != dp.end()) {
    return dp[{ind, left}];
  }
  return dp[{ind, left}] =
             max(a[ind] + go(ind + 2, left - 1), go(ind + 1, left));
}

int32_t main() {
  _ cin >> n;
  a.resize(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  cout << go(0, n / 2);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pbds                                                                   \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define _ ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define mod 1000000007
#define deb(x) cerr << "\\n" << #x << "=" << x << "\\n";
#define deb2(x, y)                                                             \\
  cerr << "\\n" << #x << "=" << x << "\\n" << #y << "=" << y << "\\n";
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

int n;
vector<int> a;
map<pair<int, int>, int> dp;

int go(int ind, int left) {
  if (left == 0)
    return 0;

  if (ind >= n)
    return -1e16;

  if (dp.find({ind, left}) != dp.end())
    return dp[{ind, left}];

  return dp[{ind, left}] =
             max(a[ind] + go(ind + 2, left - 1), go(ind + 1, left));
}

int32_t main() {
  _ cin >> n;
  a.resize(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  cout << go(0, n / 2);
  return 0;
}