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

// In the name of God

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n;
long long a[N], dp[N][2];

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

  cin >> n;

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

  for (int i = n - 1; i >= 0; i--) {
    dp[i][0] = dp[i][1] = -1e18;

    int t = (n - i - 1) / 2;
    if (t == 0)
      dp[i][0] = a[i];
    else {
      for (int j = i + 2; j < min(i + 25, n); j++) {
        int k1 = (n - j - 1) / 2;
        int k2 = (n - j - 2) / 2;

        if (k1 == t - 1)
          dp[i][0] = max(dp[i][0], a[i] + dp[j][0]);
        if (k2 == t - 1)
          dp[i][0] = max(dp[i][0], a[i] + dp[j][1]);
      }
    }

    t = (n - i - 2) / 2;

    if (t == 0)
      dp[i][1] = a[i];
    else {
      for (int j = i + 2; j < min(i + 25, n); j++) {
        int k1 = (n - j - 1) / 2;
        int k2 = (n - j - 2) / 2;
        if (k1 == t - 1)
          dp[i][1] = max(dp[i][1], a[i] + dp[j][0]);
        if (k2 == t - 1)
          dp[i][1] = max(dp[i][1], a[i] + dp[j][1]);
      }
    }
  }

  long long ans = -1e18;
  for (int i = 0; i < n; i++) {
    if ((n - 1 - i) / 2 + 1 == n / 2)
      ans = max(ans, dp[i][0]);
    if ((n - 2 - i) / 2 + 1 == n / 2)
      ans = max(ans, dp[i][1]);
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

// In the name of God

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n;
long long a[N], dp[N][2];

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

  cin >> n;

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

  for (int i = n - 1; i >= 0; i--) {
    dp[i][0] = dp[i][1] = -1e18;

    int t = (n - i - 1) / 2;
    if (t == 0)
      dp[i][0] = a[i];
    else {
      for (int j = i + 2; j < n; j++) {
        int k1 = (n - j - 1) / 2;
        int k2 = (n - j - 2) / 2;

        if (k1 == t - 1)
          dp[i][0] = max(dp[i][0], a[i] + dp[j][0]);
        if (k2 == t - 1)
          dp[i][0] = max(dp[i][0], a[i] + dp[j][1]);
      }
    }

    t = (n - i - 2) / 2;

    if (t == 0)
      dp[i][1] = a[i];
    else {
      for (int j = i + 2; j < n; j++) {
        int k1 = (n - j - 1) / 2;
        int k2 = (n - j - 2) / 2;
        if (k1 == t - 1)
          dp[i][1] = max(dp[i][1], a[i] + dp[j][0]);
        if (k2 == t - 1)
          dp[i][1] = max(dp[i][1], a[i] + dp[j][1]);
      }
    }
  }

  long long ans = -1e18;
  for (int i = 0; i < n; i++) {
    if ((n - 1 - i) / 2 + 1 == n / 2)
      ans = max(ans, dp[i][0]);
    if ((n - 2 - i) / 2 + 1 == n / 2)
      ans = max(ans, dp[i][1]);
  }
  cout << ans;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
  }
  vector<vector<long long>> DP(N, vector<long long>(2, -1e18));
  // i 番目までからj個を選んだ
  // jの範囲は i/2 -1 から(i+1)/2)
  for (int i = 1; i <= N; ++i) {
    for (int j = (i + 1) / 2; j >= i / 2 - 1; --j) {
      if (j < 0) {
        continue;
      }
      DP[j][0] = max(DP[j][0], DP[j][1]);
      if (j > 0) {
        if (-1e18 != DP[j - 1][0]) {
          DP[j][1] = DP[j - 1][0] + A[i - 1];
        } else {
          DP[j][1] = A[i - 1];
        }
      }
    }
  }
  long long ans = 0;
  ans = max(DP[N / 2][0], DP[N / 2][1]);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
  }
  vector<vector<long long>> DP(N, vector<long long>(2, -1e18));
  vector<vector<long long>> subDP(N, vector<long long>(2));
  // i 番目までからj個を選んだ
  // jの範囲は i/2 -1 から(i+1)/2)
  for (int i = 1; i <= N; ++i) {
    subDP = DP;
    for (int j = i / 2 - 1; j <= (i + 1) / 2; ++j) {
      if (j < 0) {
        continue;
      }
      DP[j][0] = max(subDP[j][0], subDP[j][1]);
      if (j > 0) {
        if (-1e18 != DP[j - 1][0]) {
          DP[j][1] = subDP[j - 1][0] + A[i - 1];
        } else {
          DP[j][1] = A[i - 1];
        }
      }
    }
  }
  long long ans = 0;
  ans = max(DP[N / 2][0], DP[N / 2][1]);
  cout << ans << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\\n'
const int mxn = 2e5 + 1;
ll arr[mxn], dp[mxn], pref[mxn];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  dp[0] = 0;
  pref[0] = 0;
  pref[1] = arr[1];
  for (int i = 2; i <= n; ++i)
    pref[i] = pref[i - 2] + arr[i];
  dp[1] = 0;
  for (int i = 2; i <= n; ++i) {
    if (i & 1) {
      dp[i] = max(dp[i - 1], dp[i - 2] + arr[i]);
    } else {
      dp[i] = max(dp[i - 2] + arr[i], pref[i - 1]);
    }
  }
  cout << dp[n] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\\n'
const int mxn = 2e3 + 1;
ll arr[mxn], dp[mxn][mxn];

ll solve(int n, int x) {
  if (n <= 0 && x)
    return 1e-10;
  if (x == 0)
    return 0;
  if (x > (n + 1) / 2)
    return -1e10;
  if (n == 1)
    return arr[n];
  return max(solve(n - 2, x - 1) + arr[n],
             max(solve(n - 3, x - 1) + arr[n - 1], solve(n - 1, x)));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  memset(dp, (ll)-1e18, sizeof(dp));
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  cout << solve(n, n / 2);
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
int main() {
  ll N;
  cin >> N;
  ll A;
  ll i;
  ll dp[N + 1][3][2];
  dp[0][0][0] = 0;
  dp[0][0][1] = 0;
  dp[0][1][0] = 0;
  dp[0][1][1] = 0;
  dp[0][2][0] = 0;
  dp[0][2][1] = 0;
  for (i = 1; i <= N; i++) {
    cin >> A;
    dp[i][0][0] = dp[i - 1][0][1];
    dp[i][0][1] = dp[i - 1][0][0] + A;
    dp[i][1][0] = max(dp[i - 1][1][1], dp[i - 1][0][0]);
    dp[i][1][1] = dp[i - 1][1][0] + A;
    dp[i][2][0] = max(dp[i - 1][2][1], dp[i - 1][1][0]);
    dp[i][2][1] = dp[i - 1][2][0] + A;
  }
  if (N % 2 == 0) {
    cout << max(dp[N - 1][0][1], dp[N][1][1]) << endl;
  } else {
    cout << max(dp[N - 2][0][1], max(dp[N - 1][1][1], dp[N][2][1])) << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
int main() {
  ll N;
  cin >> N;
  ll A[N];
  ll i;
  ll dp[N + 1][3][2];
  for (i = 1; i <= N; i++) {
    cin >> A[i];
  }
  dp[0][0][0] = 0;
  dp[0][0][1] = 0;
  dp[0][1][0] = 0;
  dp[0][1][1] = 0;
  dp[0][2][0] = 0;
  dp[0][2][1] = 0;
  for (i = 1; i <= N; i++) {
    dp[i][0][0] = dp[i - 1][0][1];
    dp[i][0][1] = dp[i - 1][0][0] + A[i];
    dp[i][1][0] = max(dp[i - 1][1][1], dp[i - 1][0][0]);
    dp[i][1][1] = dp[i - 1][1][0] + A[i];
    dp[i][2][0] = max(dp[i - 1][2][1], dp[i - 1][1][0]);
    dp[i][2][1] = dp[i - 1][2][0] + A[i];
  }
  if (N % 2 == 0) {
    cout << max(dp[N - 1][0][1], dp[N][1][1]);
  } else {
    cout << max(dp[N - 2][0][1], max(dp[N - 1][1][1], dp[N][2][1]));
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  ll v;
  scanf("%d", &n);
  vector<ll> a(n + 1);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i + 1]);
  }

  vector<ll> dp(n + 1, 0);
  dp[1] = 0;
  // odd prefix
  vector<ll> prefix(n + 1);
  prefix[1] = a[1];
  for (int i = 3; i <= n; i += 2) {
    prefix[i] = prefix[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 {
      // two case when even
      dp[i] = a[i] + dp[i - 2];          // select
      dp[i] = max(dp[i], prefix[i - 1]); // or don't select, get all odd prefix
    }
  }
  cout << dp[n];
}


Error: 0

Fixed submission:

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

ll solve(int pos, int left) {
  if (left == 0)
    return 0;
  else if (pos >= vals.size())
    return -1e9;

  if (dp[pos][left] != -1e9)
    return dp[pos][left];
  ll best = solve(pos + 1, left);
  best = max(best, solve(pos + 2, left - 1) + vals[pos]);
  dp[pos][left] = best;
  return best;
}

int main() {
  int n;
  ll v;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &v);
    vals.push_back(v);
  }
  int c = n / 2;
  dp.assign(n + 1, vector<ll>(c + 1, -1e9));
  printf("%lld\\n", solve(0, c));
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ALL(v) std::begin(v), std::end(v)
using lint = long long;
using ld = long double;
template <class T> using numr = std::numeric_limits<T>;
void cmx(lint &x, lint y) {
  if (x < y)
    x = y;
}
int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout.setf(std::ios_base::fixed);
  std::cout.precision(15);
  lint inf = numr<lint>::max();
  lint n;
  std::cin >> n;
  std::vector<lint> a(n + 4);
  for (lint i = 2; i < n + 2; i++) {
    std::cin >> a.at(i);
  }
  n += 4;
  std::vector<std::array<lint, 3>> dp(n, std::array<lint, 3>{-inf, -inf, -inf});
  dp.at(0).at(0) = 0;
  for (lint i = 0; i < n; i++) {
    for (lint j = 0; j < 3; j++) {
      if (dp.at(i).at(j) == -inf)
        continue;
      for (lint k = 0; j + k < 3 && i + 2 + k < n; k++) {
        cmx(dp.at(i + 2 + k).at(j + k), dp.at(i).at(j) + a.at(i + 2 + k));
      }
    }
  }
  std::cout << dp.at(n - 1).at(1 + n % 2) << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ALL(v) std::begin(v), std::end(v)
using lint = long long;
using ld = long double;
template <class T> using numr = std::numeric_limits<T>;
void cmx(lint &x, lint y) {
  if (x < y)
    x = y;
}
int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout.setf(std::ios_base::fixed);
  std::cout.precision(15);
  lint inf = numr<lint>::max();
  lint n;
  std::cin >> n;
  std::vector<lint> a(n);
  for (lint &x : a)
    std::cin >> x;
  std::vector<std::array<lint, 4>> dp(
      n, std::array<lint, 4>{-inf, -inf, -inf, -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);
  for (lint i = 0; i < n; i++) {
    for (lint j = 0; j < 4; j++) {
      if (dp.at(i).at(j) == -inf)
        continue;
      for (lint k = 0; j + k < 4 && i + 2 + k < n; k++) {
        cmx(dp.at(i + 2 + k).at(j + k), dp.at(i).at(j) + a.at(i + 2 + k));
      }
    }
  }
  std::cout << dp.at(n - 1).at(1 + n % 2) << '\\n';
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <limits>
#include <numeric>

constexpr long small_enough = -2000000001;

int main() {
  int N;
  std::cin >> N;

  long skip0 = 0;
  auto skip1 = small_enough;
  auto skip2 = small_enough;

  for (auto i = 1; i <= N; i++) {
    long input;
    std::cin >> input;

    if (i % 2 == 0) {
      skip2 = std::max(skip1, skip2);
      skip1 += input;

    } else {
      skip2 += input;
      skip1 = std::max(skip0, skip1);
      skip0 += input;
    }
  }

  std::cout << std::max(skip1, (N % 2 == 0 ? skip0 : skip2)) << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <limits>
#include <numeric>
#include <vector>

int main() {
  int N;
  std::cin >> N;

  const int counts = N / 2;

  std::vector<long> odds{0}, evens{0};

  odds.reserve((N + 1) / 2 + 1);
  evens.reserve(N / 2 + 1);
  {
    auto *ptr1 = &odds, *ptr2 = &evens;

    for (auto i = 0; i < N; i++) {
      long tmp;
      std::cin >> tmp;

      ptr1->push_back(tmp);

      std::swap(ptr1, ptr2);
    }
  }

  std::vector<long> odd_sums, even_sums;
  odd_sums.reserve(odds.size());
  even_sums.reserve(evens.size());
  std::partial_sum(odds.cbegin(), odds.cend(), std::back_inserter(odd_sums),
                   [](auto lhs, auto rhs) -> long { return lhs + rhs; });
  std::partial_sum(evens.cbegin(), evens.cend(), std::back_inserter(even_sums),
                   [](auto lhs, auto rhs) -> long { return lhs + rhs; });

  auto max = std::numeric_limits<long>::min();

  if (N % 2 == 0) {
    for (auto i = 0; i <= counts; i++) {
      max = std::max(max, odd_sums.at(i) + even_sums.back() - even_sums.at(i));
    }

  } else {
    auto prev_max_j = -1;

    for (auto i = 0; i <= counts; i++) {
      if (prev_max_j < i) {
        for (auto j = i; j <= counts; j++) {
          auto tmp = odd_sums.at(i) + (even_sums.at(j) - even_sums.at(i)) +
                     (odd_sums.back() - odd_sums.at(j + 1));

          if (max < tmp) {
            max = tmp;
            prev_max_j = j;
          }
        }
      } else {
        auto j = prev_max_j;
        max =
            std::max(max, odd_sums.at(i) + (even_sums.at(j) - even_sums.at(i)) +
                              (odd_sums.back() - odd_sums.at(j + 1)));
      }
    }
  }

  std::cout << max << std::endl;

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 2e5 + 10;
long long dp[2][maxn];
// dp[0] at least, dp[1] at most
long long save[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &save[i]);
  }
  dp[0][1] = 0;
  dp[1][1] = save[1];
  dp[0][2] = max(save[1], save[2]);
  dp[1][2] = dp[0][2];
  for (int i = 3; i <= n; i++) {
    if (i % 2) {
      dp[0][i] = max(dp[0][i - 2] + save[i], dp[0][i - 1]);
      dp[1][i] = dp[1][i - 2] + save[i];
    } else { // least == most
      // we may pich the even item
      dp[0][i] = max(dp[1][i - 1], dp[0][i - 2] + save[i]);
      dp[1][i] = dp[0][i];
    }
  }
  printf("%lld\\n", dp[0][n]);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
long long dp[2][maxn];
// dp[0] at least, dp[1] at most
long long save[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &save[i]);
  }
  dp[0][1] = 0;
  dp[1][1] = save[1];
  dp[0][2] = max(save[1], save[2]);
  dp[1][2] = dp[0][2];
  for (int i = 3; i <= n; i++) {
    if (i % 2) {
      dp[0][i] = max(dp[0][i - 2] + save[i], dp[0][i - 1]);
      dp[1][i] = dp[1][i - 2] + save[i];
    } else { // least == most
      // we may pich the even item
      dp[0][i] = max(dp[1][i - 1], dp[0][i - 2] + save[i]);
      dp[1][i] = dp[0][i];
    }
  }
  printf("%lld\\n", max(dp[0][n], dp[1][n]));
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

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

  ll n;
  cin >> n;
  vector<ll> a(n + 2, -INF), pre(n + 2, -1), nxt(n + 2, -1);
  repl(i, 1, n + 1) cin >> a[i];
  rep(i, n + 2) {
    pre[i] = i - 1;
    nxt[i] = i + 1;
  }

  set<P> st;
  rep(i, n + 2) { st.insert(P(a[i], i)); }

  ll sum = 0;
  rep(_, n / 2) {
    P p = *st.rbegin();
    sum += p.fi;
    ll idx = p.se;

    st.erase(st.find(P(p.fi, p.se)));
    st.erase(st.find(P(a[pre[idx]], pre[idx])));
    st.erase(st.find(P(a[nxt[idx]], nxt[idx])));

    ll val = a[pre[idx]] + a[nxt[idx]] - a[idx];
    a[idx] = -INF;
    a[nxt[idx]] = -INF;
    ll nnxt = nxt[nxt[idx]];
    nxt[pre[idx]] = nnxt;
    if (nnxt < n + 2)
      pre[nnxt] = pre[idx];
    a[pre[idx]] = val;

    st.insert(P(a[pre[idx]], pre[idx]));
  }
  cout << sum << endl;

  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e10
#define mod 1000000007

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

  ll n;
  cin >> n;
  vector<ll> a(n + 2, -INF), pre(n + 2, -1), nxt(n + 2, -1);
  repl(i, 1, n + 1) cin >> a[i];
  rep(i, n + 2) {
    pre[i] = i - 1;
    nxt[i] = i + 1;
  }

  set<P> st;
  rep(i, n + 2) { st.insert(P(a[i], i)); }

  ll sum = 0;
  rep(_, n / 2) {
    P p = *st.rbegin();
    sum += p.fi;
    ll idx = p.se;

    st.erase(st.find(P(p.fi, p.se)));
    st.erase(st.find(P(a[pre[idx]], pre[idx])));
    st.erase(st.find(P(a[nxt[idx]], nxt[idx])));

    ll val = a[pre[idx]] + a[nxt[idx]] - a[idx];
    a[idx] = -INF;
    a[nxt[idx]] = -INF;
    ll nnxt = nxt[nxt[idx]];
    nxt[pre[idx]] = nnxt;
    if (nnxt < n + 2)
      pre[nnxt] = pre[idx];
    a[pre[idx]] = val;

    st.insert(P(a[pre[idx]], pre[idx]));
  }
  cout << sum << endl;

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

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e18
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline ll read() {
  ll x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

ll a[maxn], dp[maxn][5], n, pre[maxn];

int main() {
  n = read();
  rep(i, 1, n) a[i] = read();
  rep(i, 1, n) rep(j, 0, 3) dp[i][j] = -inf;
  dp[1][0] = a[1];
  dp[2][1] = a[2];
  dp[3][2] = a[3];
  for (int i = 3; i <= n; i++) {
    dp[i][0] = dp[i - 2][0] + a[i];
    dp[i][1] = max(dp[i - 2][1], dp[i - 3][0]) + a[i];
    if (i >= 4)
      dp[i][2] = max(max(dp[i - 2][2], dp[i - 3][1]), dp[i - 4][0]) + a[i];
  }
  if (n % 2 == 1)
    cout << max(max(dp[n][2], dp[n - 1][1]), dp[n - 2][0]) << endl;
  else
    cout << max(dp[n][1], dp[n - 1][0]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline ll read() {
  ll x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

ll n, a[maxn], tot, ans;
map<int, ll> p;

inline void dfs(int u, int cnt, ll nw) {
  int r1 = n - u + 1, r2 = tot - cnt;
  if (cnt == tot) {
    // cout<<u<<" "<<cnt<<" "<<nw<<endl;
    ans = max(ans, nw);
  }
  if (r2 > (r1 + 1) / 2 || u > n)
    return;
  dfs(u + 1, cnt, nw);
  dfs(u + 2, cnt + 1, nw + a[u]);
}

int main() {
  n = read();
  rep(i, 1, n) a[i] = read();
  tot = n / 2;
  dfs(1, 0, 0);
  cout << ans << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int *a = new int[n];
  long long **maxsum = new long long *[n];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxsum[i] = new long long[3];
  }
  maxsum[0][0] = maxsum[0][1] = 0;
  maxsum[0][2] = a[0];
  maxsum[1][0] = 0;
  maxsum[1][1] = maxsum[1][2] = (long long)max(a[0], a[1]);
  for (int i = 2; i < n; i++) {
    if (i % 2 == 0) {
      maxsum[i][0] = maxsum[i][1] =
          max(maxsum[i - 1][1], maxsum[i - 2][0] + a[i]);
      maxsum[i][2] = maxsum[i - 2][2] + a[i];
    } else {
      maxsum[i][0] = max(maxsum[i - 1][0], maxsum[i - 2][0] + a[i]);
      maxsum[i][1] = maxsum[i][2] =
          max(maxsum[i - 1][2], maxsum[i - 2][2] + a[i]);
    }
  }
  cout << maxsum[n - 1][1] << "\\n";
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  long long n;
  cin >> n;
  long long *a = new long long[n];
  long long m;
  long long sum;

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

  m = 0;

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

  if (n % 2 == 0) {
    for (int i = 0; i <= n / 2; i++) {
      sum = 0;
      for (int j = 0; j < n / 2; j++) {
        if (j >= i)
          sum += a[2 * j + 1];
        else
          sum += a[2 * j];
      }
      if (sum > m)
        m = sum;
    }
  } else {
    for (int i = 0; i <= n / 2; i++) {
      for (int j = i; j <= n / 2; j++) {
        sum = 0;
        for (int k = 0; k < n / 2; k++) {
          if (k >= j)
            sum += a[2 * k + 2];
          else if (k >= i)
            sum += a[2 * k + 1];
          else
            sum += a[2 * k];
        }
        if (sum > m)
          m = sum;
      }
    }
  }
  cout << m << "\\n";
  return 0;
}