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

#include <iostream>
using namespace std;
const int MAX = 200001;
int main() {
  int N, A[MAX];
  long long int sum[MAX][3], ans = 0;
  bool visit[MAX][3] = {0};
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N - N % 2; i += 2)
    ans += A[i];

  if (N <= 3) {
    ans = A[0];
    for (int i = 1; i < N; i++) {
      if (ans < A[i])
        ans = A[i];
    }
    cout << ans;
    return 0;
  }

  if (N % 2) {
    sum[0][2] = A[0];
    visit[0][2] = true;
    sum[1][1] = A[1];
    visit[1][1] = true;
    sum[2][0] = A[2];
    visit[2][0] = true;
  } else {
    sum[0][1] = A[0];
    visit[0][1] = true;
    sum[1][0] = A[1];
    visit[1][0] = true;
  }

  for (int i = 0; i + 2 < N; i++)
    for (int j = 0; j < 3; j++) {
      if (!visit[i][j])
        continue;
      for (int k = 0; k <= min(j, N - i - 3); k++) {
        if (visit[i + 2 + k][j - k])
          sum[i + 2 + k][j - k] =
              max(sum[i + 2 + k][j - k], sum[i][j] + A[i + 2 + k]);
        else
          sum[i + 2 + k][j - k] = sum[i][j] + A[i + 2 + k];
        visit[i + 2 + k][j - k] = true;
        if (i + 2 + k >= N - (j - k + 1))
          ans = max(ans, sum[i + 2 + k][j - k]);
      }
    }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
const int MAX = 20001;
int main() {
  int N, A[MAX];
  long long sum[MAX][3], sum1 = 0, sum2 = 0, ans = 0;
  bool visit[MAX][3] = {0};
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  if (N % 2 == 0) {
    for (int i = 0; i < N; i += 2)
      sum1 += A[i];
    for (int i = 1; i < N; i += 2)
      sum2 += A[i];
    cout << max(sum1, sum2);
    return 0;
  }
  if (N == 3) {
    ans = A[0];
    for (int i = 1; i < N; i++) {
      if (ans < A[i])
        ans = A[i];
    }
    cout << ans;
    return 0;
  } else if (N == 5) {
    ans = A[0] + A[2];
    for (int i = 0; i < N - 2; i++)
      for (int j = i + 2; j < N; j++) {
        if (ans < A[i] + A[j])
          ans = A[i] + A[j];
      }
    cout << ans;
    return 0;
  }
  for (int i = 0; i < N - 1; i += 2)
    ans += A[i];
  sum[0][2] = A[0];
  sum[1][1] = A[1];
  sum[2][0] = A[2];
  for (int i = 0; i + 2 < N; i++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k <= min(j, N - i - 3); k++) {
        if (visit[i + 2 + k][j - k])
          sum[i + 2 + k][j - k] =
              max(sum[i + 2 + k][j - k], sum[i][j] + A[i + 2 + k]);
        else
          sum[i + 2 + k][j - k] = sum[i][j] + A[i + 2 + k];
        visit[i + 2 + k][j - k] = true;
        if (i + 2 + k >= N - (j - k + 1))
          ans = max(ans, sum[i + 2 + k][j - k]);
      }
  cout << ans;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

const int N = 2 * 100 * 1000 + 20;
ll n, sum[N], dp[N], a[N];

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


Error: 0

Fixed submission:

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

typedef long long ll;

const int N = 100 * 1000 + 20;
ll n, sum[N], dp[N], a[N];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    sum[i] = a[i] + (i > 1 ? sum[i - 2] : 0);
  for (int i = 1; i < n; i++) {
    if (i % 2 == 0)
      dp[i] = max(a[i] + dp[i - 2], dp[i - 1]);
    else
      dp[i] = max(a[i] + dp[i - 2], sum[i - 1]);
  }
  return cout << dp[n - 1] << endl, 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; // i<4:4way i<8:8way
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

ll dp[200005][4]; // dp[i][j] := i番目まで見てj個余分なXを入れたときのmax

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int k = 1 + n % 2;
  rep(i, n + 1) rep(j, k + 1) dp[i][j] = -1e15;
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, k + 1) {
      // x を一個挿入する
      chmax(dp[i + 1][j + 1], dp[i][j]);

      // 余計なxを入れずにox列の通りに次の数を当てはめる
      ll now = dp[i][j]; // 現在までのmax
      if ((i + j) % 2 == 0)
        now += a[i]; // 現在地がoだったら加算する
      chmax(dp[i + 1][j], now); // i+1番目まで見てxをj個入れた時の値を更新する
    }
  }
  ll ans = dp[n][k];
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; // i<4:4way i<8:8way
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

ll a[200005];
ll n;
map<pair<int, int>, ll> mp;
ll func(int x, int y) { // 区間[x,n-1]からy個取る
  if (mp.count({x, y}))
    return mp[{x, y}];
  if (y == 0)
    return 0;
  if (y && x >= n)
    return -1e15;
  ll ret = -1e15;
  for (int i = x; i < x + 3; i++) {
    // i地点からとった場合を考える
    if (i < n)
      chmax(ret, a[i] + func(i + 2, y - 1));
  }
  return mp[{x, y}] = ret;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  cin >> n;
  rep(i, n) cin >> a[i];
  cout << func(0, (n / 2)) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define l(i) (n / 2 - (n - i + 1) / 2)
#define r(i) (i + 1) / 2
#define st(i) i / 2 - 1
using namespace std;
typedef long long ll;
int n, a[200005];
ll ans = -1e16;
ll dp[200005][3], pre[200005][3]; // j的范围在i/2-1到(i+1)/2，最多3个
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 3; j++)
      dp[i][j] = -1e16;
  }
  dp[1][1 - st(1)] = a[1], dp[2][1 - st(2)] = a[2];
  dp[1][0 - st(1)] = 0;
  if (0 - st(2) >= 0)
    dp[2][0 - st(2)] = 0;
  for (int i = 3; i <= n; i++) {
    for (int j = l(i); j <= r(i); j++) {
      if (j == 0)
        dp[i][j - st(i)] = 0;
      else {
        for (int k = i - 2; l(k) <= j - 1 && r(k) >= j - 1 && k >= 1; k--)
          dp[i][j - st(i)] = max(dp[i][j - st(i)], dp[k][j - 1 - st(k)] + a[i]);
      }
    }
  }
  for (int i = n / 2; i <= n; i++)
    for (int j = l(i); j <= r(i); j++)
      j == n / 2 ? ans = max(ans, dp[i][j - st(i)]) : 0;
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define l(i) (n / 2 - (n - i + 1) / 2)
#define r(i) (i + 1) / 2
#define st(i) i / 2 - 1
using namespace std;
typedef long long ll;
int n, a[100005];
ll ans = -1e16;
ll dp[100005][3], pre[100005][3]; // j的范围在i/2-1到(i+1)/2，最多3个
int main() {
  cin >> n;
  memset(dp, -0x3f3f3f3f, sizeof(dp));
  memset(pre, -0x3f3f3f3f, sizeof(pre));
  for (int i = 1; i <= n; i++)
    cin >> a[i], dp[i][0] = 0;
  dp[1][1 - st(1)] = a[1], dp[2][1 - st(2)] = a[2];
  for (int i = 3; i <= n; i++) {
    for (int j = l(i); j <= r(i); j++) {
      if (j == 0)
        continue;
      for (int k = i - 2; l(k) <= j - 1 && r(k) >= j - 1 && k >= 1; k--)
        dp[i][j - st(i)] = max(dp[i][j - st(i)], dp[k][j - 1 - st(k)] + a[i]);
    }
  }
  for (int i = n / 2; i <= n; i++)
    for (int j = l(i); j <= r(i); j++)
      j == n / 2 ? ans = max(ans, dp[i][j - st(i)]) : 0;
  cout << ans;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<int, int> pii;

ll solve(vector<int> a, int k) {
  int n = (int)a.size();
  vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, -1'000'000'000'000'000LL));

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

  for (int i = 2; i < n; i += 2) {
    dp[i][0] = dp[i - 2][0] + a[i];
    dp[i][1] = dp[i - 2][1];

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

    if (i >= 4) {
      dp[i][1] = max(dp[i][1], max(dp[i - 4][0], dp[i - 3][0]));
      dp[i + 1][1] = max(dp[i + 1][1], max(dp[i - 4][0], dp[i - 3][0]));
    }

    dp[i][1] += a[i];
    dp[i + 1][1] += a[i];
  }

  if (n % 2 == 1) {
    return max(dp[n - 3][0], max(dp[n - 2][0], dp[n - 1][1]));
  }

  return max(dp[n - 2][k], dp[n - 1][k]);
}

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

  int n;
  cin >> n;

  vector<int> a(n);

  for (int &x : a) {
    cin >> x;
  }

  if (n == 3) {
    cout << *max_element(a.begin(), a.end());
    return 0;
  }

  if (n % 2 == 0) {
    cout << solve(a, 0);
  } else {
    cout << solve(a, 1);
  }

  return 0;
}

Error: WA

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;

ll solve(vector<ll> a) {
  assert(a.size() % 2 == 0 && a.size() >= 2);
  int n = (int)a.size();
  vector<ll> dp(a.size());

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

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

  return max(dp[n - 2], dp[n - 1]);
}

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

  int n = 200'000 - 1;
  cin >> n;

  vector<ll> a(n);

  for (ll &x : a) {
    cin >> x;
    //		x = 1'000'000'000;
  }

  //	if (n <= 3) {
  //		if (n == 2) {
  //			cout << max(a[0], a[1]);
  //		} else if (n == 3) {
  //			cout << max(a[0], max(a[1], a[2]));
  //		} else {
  //			assert(0);
  //		}
  //		return 0;
  //	}

  ll res = (ll)-1e17;

  if (n % 2 == 1) {
    for (int i = 0; i < 2; i++) {
      ll cur = a[i];
      vector<ll> b(a.begin() + 3, a.end());
      res = max(res, cur + solve(b));
    }

    ll cur = 0;

    for (int i = 2; i < n; i += 2) {
      cur += a[i];
    }

    res = max(res, cur);
  } else {
    res = solve(a);
  }

  cout << res;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int arr[200000];
long long arr2[200000];
long long arr3[200000];
long long cul(int c, int b) {

  if (c == 0)
    return arr[0];
  if (c == 1)
    return max(arr[0], arr[1]);
  if (c == 2)
    return max(max(arr[0], arr[1]), arr[2]);
  if (c % 2 != 0)
    return arr3[c];
  return max(max(arr3[c - 1], arr2[c - 2]), arr[c] + cul(c - 2, b));
}

int main() {
  for (int i = 0; i < 200000; i++) {
    arr[i] = 0;
    arr2[i] = 0;
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  arr2[0] = arr[0];
  for (int i = 2; i < n; i = i + 2)
    arr2[i] = arr2[i - 2] + arr[i];
  arr3[1] = max(arr[0], arr[1]);
  for (int i = 3; i < n; i = i + 2)
    arr3[i] = max(arr3[i - 2] + arr[i], arr2[i - 1]);
  cout << cul(n - 1, n) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int arr[200000];

long long cul(int a, int b) {
  if (a == b - 1)
    return arr[b - 1];
  if (a == b - 3)
    return max(max(arr[b - 1], arr[b - 2]), arr[b - 3]);
  if (a == b - 2)
    return max(arr[b - 1], arr[b - 2]);
  long long cu1, cu2, cu3;
  cu1 = cu2 = cu3 = 0;
  if ((b - a) % 2 == 0) {
    cu1 = arr[a] + cul(a + 2, b);
    for (int i = a + 1; i < b; i = i + 2)
      cu2 += arr[i];
    return max(cu1, cu2);
  }
  cu1 = arr[a] + cul(a + 2, b);
  cu2 = arr[a + 1] + cul(a + 3, b);
  for (int i = a + 2; i < b; i = i + 2)
    cu3 += arr[i];
  return max(max(cu1, cu2), cu3);
}

int main() {
  for (int i = 0; i < 200000; i++)
    arr[i] = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << cul(0, n) << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {

  size_t N;
  cin >> N;
  vector<long long> a(N + 1);
  vector<long long> dp(N + 1, 0);
  vector<long long> prev(N + 1, 0);
  for (size_t i = 1; i <= N; ++i)
    cin >> a[i];
  prev[1] = a[1];
  for (size_t i = 3; i <= N; i += 2)
    prev[i] = a[i] + prev[i - 2];

  for (size_t i = 2; i <= N; ++i) {
    if ((i & 1) == 0) {
      dp[i] = max(prev[i - 1], dp[i - 2] + a[i]);
    } else {
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    }
  }

  cout << dp[N];
  return 0;
}

Error: WA

Fixed submission:

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

using namespace std;

int main() {

  size_t N;
  cin >> N;
  vector<long long> a(N + 1);
  vector<vector<long long>> dp(N + 1, vector<long long>(N / 2 + 1, -100000));
  for (size_t i = 1; i <= N; ++i)
    cin >> a[i];

  dp[1][1] = a[1];
  if (N + 1 >= 2) {
    dp[2][1] = a[2];
    for (size_t i = 2; i <= N; ++i) {
      for (size_t k = 1; k <= i / 2; ++k) {
        dp[i][k] = max(dp[i][k], max(dp[i - 1][k], dp[i - 2][k - 1] + a[i]));
      }
    }
  }

  cout << max(0LL, dp[N][N / 2]);
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll power(long a, long b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  int A[200100];
  rep(i, N) cin >> A[i];

  long dp[200100][2][3];
  rep(i, N + 1) rep(x, 2) rep(y, 3) dp[i][x][y] = -1LL << 60;
  dp[0][1][0] = 0;
  for (int i = 0; i < N; i++) {
    rep(j, 2) rep(k, 3) {
      // not take
      if (k + j < 3)
        dp[i + 1][1][k + j] = max(dp[i + 1][1][k + j], dp[i][j][k]);
      // take
      if (j == 1)
        dp[i + 1][0][k] = max(dp[i + 1][0][k], dp[i][j][k] + A[i]);
    }
  }
  if (N % 2 == 0)
    cout << max(dp[N][1][0], dp[N][0][1]) << "\\n";
  else
    cout << max(dp[N][0][2], dp[N][1][1]) << "\\n";
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll power(long a, long b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  int A[200100];
  rep(i, N) cin >> A[i];

  long dp[100100][2][3];
  rep(i, N + 1) rep(x, 2) rep(y, 3) dp[i][x][y] = -1LL << 60;
  dp[0][1][0] = 0;
  for (int i = 0; i < N; i++) {
    rep(j, 2) rep(k, 3) {
      // not take
      if (k + j < 3)
        dp[i + 1][1][k + j] = max(dp[i + 1][1][k + j], dp[i][j][k]);
      // take
      if (j == 1)
        dp[i + 1][0][k] = max(dp[i + 1][0][k], dp[i][j][k] + A[i]);
    }
  }
  if (N % 2 == 0)
    cout << max(dp[N][1][0], dp[N][0][1]) << "\\n";
  else
    cout << max(dp[N][0][2], dp[N][1][1]) << "\\n";
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<long long> dp(n, (long long)-2e18);
  dp[0] = 0;
  vector<queue<pair<int, long long>>> q(2);
  for (int i = 0; i < n; i++) {
    int rem = n / 2 - (n - i) / 2;
    for (int j = 3; j >= -2; j--) {
      if (rem + j < 0)
        continue;
      if (rem + j + 1 >= n)
        continue;
      if (dp[rem + j + 1] < dp[rem + j] + a[i]) {
        int y;
        long long z;
        y = rem + j + 1;
        z = dp[rem + j] + a[i];
        q[i % 2].push({y, z});
      }
    }
    while (q[!(i % 2)].size()) {
      int y;
      long long z;
      y = q[!(i % 2)].front().first;
      z = q[!(i % 2)].front().second;
      q[!(i % 2)].pop();
      dp[y] = max(dp[y], z);
    }
  }
  while (q[0].size()) {
    int y;
    long long z;
    y = q[0].front().first;
    z = q[0].front().second;
    q[0].pop();
    dp[y] = max(dp[y], z);
  }
  while (q[1].size()) {
    int y;
    long long z;
    y = q[1].front().first;
    z = q[1].front().second;
    q[1].pop();
    dp[y] = max(dp[y], z);
  }
  cout << dp[n / 2] << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<long long>> dp(2, vector<long long>(n, -2e18));
  vector<vector<long long>> pre_dp(2, vector<long long>(n, -2e18));
  dp[1][0] = 0;
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    pre_dp = dp;
    int rem = n / 2 - (n - i) / 2;
    for (int j = 3; j >= -2; j--) {
      if (rem + j < 0)
        continue;
      if (rem + j >= n)
        continue;
      dp[i % 2][rem + j + 1] =
          max(pre_dp[i % 2][rem + j + 1], pre_dp[i % 2][rem + j] + a[i]);
    }
  }
  cout << max(dp[0][n / 2], dp[1][n / 2]) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define memset(a, n) memset(a, n, sizeof(a))
#define mp make_pair
#define pb push_back
using namespace std;
const int maxn = 2e5 + 100;

int main() {
  int n;
  int a[maxn];
  ll p[maxn];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  p[1] = a[1];
  for (int i = 3; i <= n; i += 2) {
    p[i] = p[i - 2] + a[i];
  }
  ll dp[maxn] = {0};
  for (int i = 2; i <= n; i++) {
    if (i & 1) {
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    } else {
      dp[i] = max(dp[i - 2] + a[i], p[i - 1]);
    }
  }
  printf("%lld\\n", dp[n]);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define memset(a, n) memset(a, n, sizeof(a))
#define mp make_pair
#define pb push_back
using namespace std;
const int maxn = 1e5 + 100;

int main() {
  int n;
  int a[maxn];
  int p[maxn];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  p[1] = a[1];
  for (int i = 3; i <= n; i += 2) {
    p[i] = p[i - 2] + a[i];
  }
  int dp[maxn] = {0};
  for (int i = 1; i <= n; i++) {
    if (i & 1) {
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    } else {
      dp[i] = max(dp[i - 2] + a[i], p[i - 1]);
    }
  }
  printf("%d\\n", dp[n]);
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long LL;
inline LL read() {
  LL kk = 0, f = 1;
  char cc = getchar();
  while (cc < '0' || cc > '9') {
    if (cc == '-')
      f = -1;
    cc = getchar();
  }
  while (cc >= '0' && cc <= '9') {
    kk = (kk << 1) + (kk << 3) + cc - '0';
    cc = getchar();
  }
  return kk * f;
}
LL a[2000222], dp[4], n, sum[2000222];
#define P(x) (x % 3 + 3) % 3
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    sum[i] = sum[i - 1];
    if (i & 1)
      sum[i] += a[i];
  }
  dp[1] = 0;
  dp[2] = max(a[1], a[2]);
  for (int i = 3; i <= n; ++i) {
    LL now, pre1, pre2;
    now = P(i);
    pre1 = P(i - 1);
    pre2 = P(i - 2);
    dp[now] = 0;
    if (i & 1) {
      dp[now] = max(dp[pre1], dp[pre2] + a[i]);
    } else {
      dp[now] = max(sum[i], dp[pre2] + a[i]);
    }
  }
  printf("%lld\\n", dp[P(n)]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define oo 1e15
using namespace std;

ll dp[200005];
ll n, arr[200005];

ll solve(int pos, int cnt) {
  if (pos >= n) {
    if (!cnt)
      return 0;
    return -oo;
  }
  // if (dp[pos] != -1)return dp[pos];
  ll ans = max(solve(pos + 1, cnt), arr[pos] + solve(pos + 2, cnt - 1));
  return dp[pos] = ans;
}

/*void build(int pos, int cnt)
{
        if (pos >= n)return;
        if (arr[pos] + solve(pos + 2, cnt - 1) == dp[pos])
        {
                cout << arr[pos] << '\\n';
                build(pos + 2, cnt - 1);
        }
        else build(pos + 1, cnt);
        return;
}*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  memset(dp, -1, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << solve(0, n / 2) << '\\n';
  // build(0, n / 2);
  // for (int i = 0; i < n; i++)cout << dp[i] << ' ';
  return 0;
}