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

#include <algorithm>
#include <climits>
#include <iostream>
using namespace std;

int n, n2;
long *a;
long ans = LONG_MIN;
long *tmp[3];

void solve(int ii, int nn2, long now, int tobasi) {
  if (ii >= n) {
    return;
  }
  if (tmp[tobasi][ii] >= now) {
    return;
  }
  tmp[tobasi][ii] = now;
  if (nn2 == n2) {
    ans = max(ans, now);
    return;
  }
  if (tobasi == 2) {
    solve(ii + 2, nn2 + 1, now + a[ii + 2], tobasi);
    if (a[ii + 3] > a[ii + 2]) {
      solve(ii + 3, nn2 + 1, now + a[ii + 3], tobasi - 1);
    }
    if (a[ii + 4] > a[ii + 3] && a[ii + 4] > a[ii + 2]) {
      solve(ii + 4, nn2 + 1, now + a[ii + 4], tobasi - 2);
    }
  } else if (tobasi == 1) {
    solve(ii + 2, nn2 + 1, now + a[ii + 2], tobasi);
    if (a[ii + 3] > a[ii + 2]) {
      solve(ii + 3, nn2 + 1, now + a[ii + 3], tobasi - 1);
    }
  } else {
    solve(ii + 2, nn2 + 1, now + a[ii + 2], tobasi);
  }

  return;
}

int main() {
  int i;
  cin >> n;
  a = new long[n];
  tmp[0] = new long[n];
  tmp[1] = new long[n];
  tmp[2] = new long[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    tmp[0][i] = LONG_MIN;
    tmp[1][i] = LONG_MIN;
    tmp[2][i] = LONG_MIN;
  }
  n2 = n / 2;

  int imax;
  if (i % 2 == 1) {
    imax = 2;
  } else {
    imax = 1;
  }
  for (i = 0; i <= imax; i++) {
    solve(i, 1, a[i], imax - i);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
using namespace std;

int n, n2;
long *a;
long ans = LONG_MIN;

void solve(int ii, int nn2, long now, int tobasi) {
  if (ii >= n) {
    return;
  }
  if (nn2 == n2) {
    ans = max(ans, now);
    return;
  }
  if (tobasi == 2) {
    solve(ii + 2, nn2 + 1, now + a[ii + 2], tobasi);
    if (a[ii + 3] > a[ii + 2]) {
      solve(ii + 3, nn2 + 1, now + a[ii + 3], tobasi - 1);
    }
    if (a[ii + 4] > a[ii + 3] && a[ii + 4] > a[ii + 2]) {
      solve(ii + 4, nn2 + 1, now + a[ii + 4], tobasi - 2);
    }
  } else if (tobasi == 1) {
    solve(ii + 2, nn2 + 1, now + a[ii + 2], tobasi);
    if (a[ii + 3] > a[ii + 2]) {
      solve(ii + 3, nn2 + 1, now + a[ii + 3], tobasi - 1);
    }
  } else {
    solve(ii + 2, nn2 + 1, now + a[ii + 2], tobasi);
  }

  return;
}

int main() {
  int i;
  long tmp;
  cin >> n;
  a = new long[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  n2 = n / 2;

  int imax;
  if (i % 2 == 1) {
    imax = 2;
  } else {
    imax = 1;
  }
  for (i = 0; i <= imax; i++) {
    solve(i, 1, a[i], imax - i);
  }
  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const long long INF = 1e18;

long long dp[200005][4];
// long long dp[100][4];

// dp[i][j] : i個目まで見てj個余分なｘ（バツ）を入れたときのMAX

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

  // K: 何個余分のバツを入れれる？奇数は2で偶数は1
  int K = 1 + N % 2;

  for (int i = 0; i < N + 1; ++i) {
    for (int j = 0; j < K + 1; ++j) {
      dp[i][j] = -INF;
    }
  }

  dp[0][0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < K + 1; ++j) {
      // バツを一個挿入（今見てる数をスキップ）
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
      // 次の数を当てはめる,0からなので割り切れたらそれはカウント
      long long now = dp[i][j];
      if ((i + j) % 2 == 0)
        now += a[i];
      dp[i + 1][j] = max(dp[i + 1][j], now);
    }
  }

  long long ans = dp[N][K];
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const long long INF = 1e18;

// long long dp[200005][4];
long long dp[100][4];

// dp[i][j] : i個目まで見てj個余分なｘ（バツ）を入れたときのMAX

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

  // K: 何個余分のバツを入れれる？奇数は2で偶数は1
  int K = 1 + N % 2;

  for (int i = 0; i < N + 1; ++i) {
    for (int j = 0; j < K + 1; ++j) {
      dp[i][j] = -INF;
    }
  }

  dp[0][0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < K + 1; ++j) {
      // バツを一個挿入（今見てる数をスキップ）
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
      // 次の数を当てはめる,0からなので割り切れたらそれはカウント
      long long now = dp[i][j];
      if ((i + j) % 2 == 0)
        now += a[i];
      dp[i + 1][j] = max(dp[i + 1][j], now);
    }
  }

  long long ans = dp[N][K];
  cout << ans << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
using namespace std;

int main() {
  ll n;
  cin >> n;
  V a(n);
  rep(i, n) cin >> a[i];
  if (n % 2 == 1) {
    VVV dp(n + 1, VV(3, V(2, -1e10)));
    rep(i, n) {
      rep(j, 3) {
        if (i + 1 == 2 && j > 0)
          continue;
        if (i + 1 == 3 && j > 1)
          continue;
        dp[i + 1][j][0] = dp[i][j][1];
        if (j > 0)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - 1][0]);
        if (dp[i][j][0] == -1e10) {
          dp[i + 1][j][1] = a[i];
        } else {
          dp[i + 1][j][1] = dp[i][j][0] + a[i];
        }
      }
    }
    cout << max({dp[n][0][0], dp[n][1][0], dp[n][1][1], dp[n][2][1]}) << endl;
  } else {
    VVV dp(n + 1, VV(2, V(2, -1e10)));
    rep(i, n) {
      rep(j, 2) {
        if (i + 1 == 2 && j > 0)
          continue;
        dp[i + 1][j][0] = dp[i][j][1];
        if (j > 0)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - 1][0]);
        if (dp[i][j][0] == -1e10) {
          dp[i + 1][j][1] = a[i];
        } else {
          dp[i + 1][j][1] = dp[i][j][0] + a[i];
        }
      }
    }
    cout << max({dp[n][0][0], dp[n][0][1], dp[n][1][1]}) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
using namespace std;

int main() {
  ll n;
  cin >> n;
  V a(n);
  rep(i, n) cin >> a[i];
  VVV dp(n + 1, VV(n + 1, V(2, -1e10)));
  rep(i, n) {
    ll tmp = (i + 1) / 2;
    for (ll j = tmp - 1; j <= tmp; j++) {
      if (j < 0)
        continue;
      dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1]);
      if (i % 2 == 0 || j != tmp) {
        if (dp[i][j][0] == -1e10) {
          dp[i + 1][j + 1][1] = a[i];
        } else {
          dp[i + 1][j + 1][1] = dp[i][j][0] + a[i];
        }
      }
    }
  }
  cout << max(dp[n][n / 2][0], dp[n][n / 2][1]) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1;
map<pair<int, int>, long long> dp;
int vt[N];

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> vt[i];
  }
  dp[{-1, 0}] = dp[{0, 0}] = 0;
  long long mxdp = -1e18;
  for (int i = 1; i <= n; i++) {
    int mink = max(1, (n / 2) - (n - i + 1) / 2);
    int maxk = (i + 1) / 2;
    for (int j = mink; j <= maxk; j++) {
      for (int ni = i - 4; ni <= i - 2; ni++) {
        if (dp.count({ni, j - 1}) == 0)
          continue;
        if (dp.count({i, j}) == 0)
          dp[{i, j}] = dp[{ni, j - 1}] + vt[i];
        else
          dp[{i, j}] = max(dp[{i, j}], dp[{ni, j - 1}] + vt[i]);
      }
      if (j == n / 2)
        mxdp = max(mxdp, dp[{i, j}]);
    }
  }
  cout << mxdp;
}

int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(20);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1;
map<pair<int, int>, long long> dp;
long long vt[N];

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> vt[i];
  }
  dp[{-1, 0}] = dp[{0, 0}] = 0;
  long long mxdp = -1e18;
  for (int i = 1; i <= n; i++) {
    int maxk = (i + 1) / 2;
    for (int j = maxk - 5; j <= maxk; j++) {
      for (int ni = i - 5; ni <= i - 2; ni++) {
        if (dp.count({ni, j - 1}) == 0)
          continue;
        if (dp.count({i, j}) == 0)
          dp[{i, j}] = dp[{ni, j - 1}] + vt[i];
        else
          dp[{i, j}] = max(dp[{i, j}], dp[{ni, j - 1}] + vt[i]);
      }
      if (j == n / 2)
        mxdp = max(mxdp, dp[{i, j}]);
    }
  }
  cout << mxdp;
}

int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(20);
  solve();
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>1
#include <map>
#include <math.h>
#include <queue>
#include <utility>
#include <vector>

// #include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using p = pair<int, int>;
using Graph = vector<vector<char>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define chmax(x, y) x = max(x, y);
int dx[4]{-1, 0, 1, 0};
int dy[4]{0, 1, 0, -1};
const ll INF = 1e18;
ll dp[200005][4]; // i番目までの個数でj個余分な×を選んだ際の和の最大値

//  ABC F 162
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  int k = 1 + n % 2;
  rep(i, n + 1) {
    rep(j, k + 1) { dp[i][j] = -INF; }
  }
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, k + 1) {
      chmax(dp[i + 1][j + 1], dp[i][j]);
      ll now = dp[i][j];
      if ((i + j) % 2 == 0) {
        now += a[i];
      }
      chmax(dp[i + 1][j], now);
    }
  }
  ll ans = dp[n][k];
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>1
#include <map>
#include <math.h>
#include <queue>
#include <utility>
#include <vector>

// #include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using p = pair<int, int>;
using Graph = vector<vector<char>>;
#define rep(i, n) for (int i = 0; i < n; i++)
int dx[4]{-1, 0, 1, 0};
int dy[4]{0, 1, 0, -1};

int main() {
  int N;
  cin >> N;
  vector<pair<ll, int>> data;

  for (int i = 0; i < N; i++) {
    long a;
    cin >> a;
    data.push_back(make_pair(a, i));
  }
  sort(data.begin(), data.end());
  reverse(data.begin(), data.end());
  long n = N / 2;
  ll ans = 0;
  int count = 0;
  int c = 0;
  vector<ll> pre;
  while (count < n) {
    bool contain = false;
    for (int i = 0; i < pre.size(); i++) {
      if (pre[i] + 1 == data[c].second || pre[i] - 1 == data[c].second) {
        c++;
        contain = true;
        break;
      }
    }
    if (contain) {
      continue;
    }
    ans += data[c].first;
    pre.push_back(data[c].second);
    count++;
    c++;
  }
  cout << ans << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

//       Author: Mostafa Mounir Shehab

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

#define PI 3.14159265 // PI = acos(-1)
#define EPS (1e-10)
#define endl "\\n"
#define SZ(v) (int)(v.size())
typedef long long ll;
typedef long double ld;
// template <class Type>

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

inline int D() {
  int t;
  scanf("%d", &t);
  return t;
}
inline ll llD() {
  ll t;
  scanf("%lld", &t);
  return t;
}

ll MOD = 1e9 + 7;
const int N = 2e5 + 4;
ll arr[N];
unordered_map<int, ll> dp[N];
ll OO = 1e16;

ll n, m, k, cnt, SU, idx, len, MN, MX, T, prime;
ll l, r, mid;
ll a, b, c, d;
vector<int> vec, v;
string str, s;

ll Solve(int idx, int len) {

  if ((n - idx + 1) / 2 + len < n / 2)
    return -OO;

  if (idx >= n)
    return len == n / 2 ? 0 : -OO;

  if (dp[idx].count(len))
    return dp[idx][len];

  ll ret = -OO;

  ret = max(ret, arr[idx] + Solve(idx + 2, len + 1));
  ret = max(ret, Solve(idx + 1, len));

  return dp[idx][len] = ret;
}

int main() {

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

  printf("%lld\\n", Solve(0, 0));

  return 0;
}


Error: TLE

Fixed submission:

//       Author: Mostafa Mounir Shehab

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

#define PI 3.14159265 // PI = acos(-1)
#define EPS (1e-10)
#define endl "\\n"
#define SZ(v) (int)(v.size())
typedef long long ll;
typedef long double ld;
// template <class Type>

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

inline int D() {
  int t;
  scanf("%d", &t);
  return t;
}
inline ll llD() {
  ll t;
  scanf("%lld", &t);
  return t;
}

ll MOD = 1e9 + 7;
const int N = 2e5 + 4;
ll arr[N];
unordered_map<int, ll> dp[N];
ll OO = 1e16;

ll n, m, k, cnt, SU, idx, len, MN, MX, T, prime;
ll l, r, mid;
ll a, b, c, d;
vector<int> vec, v;
string str, s;

ll Solve(int idx, int len) {

  if (idx >= n)
    return len == n / 2 ? 0 : -OO;

  if (dp[idx].count(len))
    return dp[idx][len];

  ll ret = -OO;

  ret = max(ret, arr[idx] + Solve(idx + 2, len + 1));
  ret = max(ret, Solve(idx + 1, len));

  return dp[idx][len] = ret;
}

int main() {

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

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll mod = 1000000007;
const double eps = 1e-8;
const ll INF = 1e16;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

template <typename A, size_t N, typename T>
void st_fill(A (&arr)[N], const T &val) {
  fill((T *)arr, (T *)(arr + N), val);
}
void chmax(ll &a, ll b) { a = max(a, b); }

ll dp[200010][2][3];

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  st_fill(dp, -INF);

  int r = 1 + (N % 2);
  dp[0][0][r - 1] = 0;
  dp[0][1][r] = A[0];
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < 3; ++j) {
      chmax(dp[i][1][j], dp[i - 1][0][j] + A[i]);
      chmax(dp[i][0][j], dp[i - 1][1][j]);
      if (j > 0)
        chmax(dp[i][0][j - 1], dp[i - 1][0][j]);
    }
  }

  ll res = -INF;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      res = max(res, dp[N - 1][i][j]);
    }
  }
  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll mod = 1000000007;
const double eps = 1e-8;
const ll INF = 1e16;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

template <typename A, size_t N, typename T>
void st_fill(A (&arr)[N], const T &val) {
  fill((T *)arr, (T *)(arr + N), val);
}
void chmax(ll &a, ll b) { a = max(a, b); }

ll dp[100010][2][3];

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  st_fill(dp, -INF);

  int r = 1 + (N % 2);
  dp[0][0][r - 1] = 0;
  dp[0][1][r] = A[0];
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < 3; ++j) {
      chmax(dp[i][1][j], dp[i - 1][0][j] + A[i]);
      chmax(dp[i][0][j], dp[i - 1][1][j]);
      if (j > 0)
        chmax(dp[i][0][j - 1], dp[i - 1][0][j]);
    }
  }

  ll res = -INF;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      res = max(res, dp[N - 1][i][j]);
    }
  }
  cout << res << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define INF (ll)1e16

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  if (n == 2) {
    cout << max(a[0], a[1]) << endl;
    return 0;
  }
  vector<ll> dp[3];
  rep(i, 3) dp[i] = vector<ll>(n, -INF);
  dp[0][0] = a[0];
  dp[1][1] = a[1];
  dp[2][2] = a[2];
  rep(i, n) {
    rep(k, 3) {
      if (i < k)
        continue;
      rep(j, 3) {
        if (k + j <= 2 && i + j + 2 < n) {
          dp[k + j][i + j + 2] =
              max(dp[k + j][i + j + 2], dp[k][i] + a[i + j + 2]);
        }
      }
    }
  }
  if (n % 2 == 0) {
    cout << max({dp[1][n - 1], dp[0][n - 2]}) << endl;
  } else {
    cout << max({dp[2][n - 1], dp[1][n - 2], dp[0][n - 3]}) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  if (n % 2 == 0) {
    ll sum[2] = {};
    rep(i, n) sum[i % 2] += a[i];
    cout << max(sum[0], sum[1]) << endl;
  } else {
    return 1;
    vector<ll> dp[3];
    rep(i, 3) dp[i] = vector<ll>(n, -1e16);
    dp[0][0] = a[0];
    dp[1][1] = a[1];
    dp[2][2] = a[2];
    rep(i, n) {
      rep(k, 3) {
        if (i < k)
          continue;
        rep(j, 3) {
          if (k + j <= 2 && i + j + 2 < n) {
            dp[k + j][i + j + 2] =
                max(dp[k + j][i + j + 2], dp[k][i] + a[i + j + 2]);
          }
        }
      }
    }
    cout << max({dp[2][n - 1], dp[1][n - 2], dp[0][n - 3]}) << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define INF (ll)1e16

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int MAX_JUMP = (n % 2 == 0) ? 2 : 3;
  vector<ll> dp[MAX_JUMP];
  rep(i, MAX_JUMP) dp[i] = vector<ll>(n, -INF);
  rep(i, MAX_JUMP) dp[i][i] = a[i];
  rep(i, n) {
    rep(k, MAX_JUMP) {
      if (i < k)
        continue;
      rep(j, MAX_JUMP) {
        if (k + j <= MAX_JUMP - 1 && i + j + 2 < n) {
          dp[k + j][i + j + 2] =
              max(dp[k + j][i + j + 2], dp[k][i] + a[i + j + 2]);
        }
      }
    }
  }
  if (n % 2 == 0) {
    cout << max({dp[1][n - 1], dp[0][n - 2]}) << endl;
  } else {
    cout << max({dp[2][n - 1], dp[1][n - 2], dp[0][n - 3]}) << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define INF (ll)1e16

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int MAX_JUMP = (n % 2 == 0) ? 2 : 3;
  vector<ll> dp[MAX_JUMP];
  rep(i, MAX_JUMP) dp[i] = vector<ll>(n, -INF);
  rep(i, MAX_JUMP) dp[i][i] = a[i];
  rep(i, n) {
    rep(k, MAX_JUMP) {
      if (i < k)
        continue;
      rep(j, MAX_JUMP) {
        if (k + j <= 2 && i + j + 2 < n) {
          dp[k + j][i + j + 2] =
              max(dp[k + j][i + j + 2], dp[k][i] + a[i + j + 2]);
        }
      }
    }
  }
  if (n % 2 == 0) {
    cout << max({dp[1][n - 1], dp[0][n - 2]}) << endl;
  } else {
    cout << max({dp[2][n - 1], dp[1][n - 2], dp[0][n - 3]}) << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
#define ll long long
#define MOD 998244353

ll dp[4][2][200010];

void chmax(ll &a, ll b) { a = max(a, b); }

int main() {
  for (int i = 0; i < 200010; i++) {
    for (int j = 0; j < 4; j++) {
      dp[j][0][i] = dp[j][1][i] = LLONG_MIN / 3;
    }
  }
  int N;
  cin >> N;
  ll A;
  for (int i = 0; i < N; i++) {
    cin >> A;
    if (i == 0) {
      // 选
      dp[3][1][0] = A;
      // 不选
      dp[1][0][0] = 0;
      continue;
    }
    // 选
    chmax(dp[1][1][i], dp[0][0][i - 1] + A);
    chmax(dp[2][1][i], dp[1][0][i - 1] + A);
    chmax(dp[3][1][i], dp[2][0][i - 1] + A);
    // 不选
    chmax(dp[0][0][i], dp[1][0][i - 1]);
    chmax(dp[0][0][i], dp[1][1][i - 1]);
    chmax(dp[1][0][i], dp[2][0][i - 1]);
    chmax(dp[1][0][i], dp[2][1][i - 1]);
    chmax(dp[2][0][i], dp[3][0][i - 1]);
    chmax(dp[2][0][i], dp[3][1][i - 1]);
  }

  if (N & 1) {
    cout << max(dp[1][0][N - 1], dp[1][1][N - 1]) << '\\n';
  } else {
    cout << max(dp[2][0][N - 1], dp[2][1][N - 1]) << '\\n';
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
#define ll long long
#define MOD 998244353

ll dp[3][2][200010];

void chmax(ll &a, ll b) { a = max(a, b); }

int main() {
  for (int i = 0; i < 200010; i++) {
    for (int j = 0; j < 3; j++) {
      dp[j][0][i] = dp[j][1][i] = LLONG_MIN / 3;
    }
  }
  int N;
  cin >> N;
  ll A;
  for (int i = 0; i < N; i++) {
    cin >> A;
    if (i == 0) {
      // 选
      dp[3][1][0] = A;
      // 不选
      dp[1][0][0] = 0;
      continue;
    }
    // 选
    chmax(dp[1][1][i], dp[0][0][i - 1] + A);
    chmax(dp[2][1][i], dp[1][0][i - 1] + A);
    chmax(dp[3][1][i], dp[2][0][i - 1] + A);
    // 不选
    chmax(dp[0][0][i], dp[1][0][i - 1]);
    chmax(dp[0][0][i], dp[1][1][i - 1]);
    chmax(dp[1][0][i], dp[2][0][i - 1]);
    chmax(dp[1][0][i], dp[2][1][i - 1]);
    chmax(dp[2][0][i], dp[3][0][i - 1]);
    chmax(dp[2][0][i], dp[3][1][i - 1]);
  }

  if (N & 1) {
    cout << max(dp[1][0][N - 1], dp[1][1][N - 1]) << '\\n';
  } else {
    cout << max(dp[2][0][N - 1], dp[2][1][N - 1]) << '\\n';
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define INF 0x7f7f7f7f // 7f7f7f7f
#define M 2000010
#define N 100010
const ll mod = 1e9 + 7;
ll a[M], dp[M], sum[M], n;
int main(void) {
  scanf("%lld", &n);
  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (i % 2)
      sum[i] = sum[i - 1] + a[i];
    else
      sum[i] = sum[i - 1];
  }
  for (ll i = 2; i <= n; i++) {
    if (i % 2)
      dp[i] = max(dp[i - 2] + a[i], dp[i - 1]);
    else
      dp[i] = max(dp[i - 2] + a[i], sum[i - 1]);
  }
  printf("%lld\\n", dp[n]);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int n;
string s;
int dp[4005][3];
// 0r 1g 2b
int main() {
  cin >> n >> s;
  for (int i = s.size() - 1; i >= 0; i--) {
    for (int j = 0; j < 3; j++)
      dp[i][j] = dp[i + 1][j];
    if (s[i] == 'R')
      dp[i][0]++;
    if (s[i] == 'G')
      dp[i][1]++;
    if (s[i] == 'B')
      dp[i][2]++;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      if (s[i] == 'R' && s[j] == 'G') {
        ans += dp[j + 1][2];
        if (j + j - i >= n)
          continue;
        if (s[j + (j - i)] == 'B')
          ans--;
      }
      if (s[i] == 'R' && s[j] == 'B') {
        ans += dp[j + 1][1];
        if (j + j - i >= n)
          continue;
        if (s[j + (j - i)] == 'G')
          ans--;
      }
      if (s[i] == 'B' && s[j] == 'R') {
        ans += dp[j + 1][1];
        if (j + j - i >= n)
          continue;
        if (s[j + (j - i)] == 'G')
          ans--;
      }
      if (s[i] == 'B' && s[j] == 'G') {
        ans += dp[j + 1][0];
        if (j + j - i >= n)
          continue;
        if (s[j + (j - i)] == 'R')
          ans--;
      }
      if (s[i] == 'G' && s[j] == 'B') {
        ans += dp[j + 1][0];
        if (j + j - i >= n)
          continue;
        if (s[j + (j - i)] == 'R')
          ans--;
      }
      if (s[i] == 'G' && s[j] == 'R') {
        ans += dp[j + 1][2];
        if (j + j - i >= n)
          continue;
        if (s[j + (j - i)] == 'B')
          ans--;
      }
    }
  cout << ans;
  return 0;
}