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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int n;
ll dp[200005], a[200005], b, ans = -1e15;
int main() {
  scanf("%d", &n);
  rep(i, n) {
    scanf("%lld", a + i);
    dp[i] = (i > 1 ? dp[i - 2] : 0) + a[i];
    if (i & 1)
      dp[i] = max(dp[i], dp[i - 1]);
  }
  if (n & 1) {
    for (int i = n - 1; i >= 0; i -= 2) {
      ans = max(ans, (i ? dp[i - 1] : 0) + b);
      b += a[i];
    }
  } else
    ans = dp[n - 1];
  printf("%lld\\n", ans);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define ll long long
#define fastIO                                                                 \\
  cout << fixed << setprecision(12), ios::sync_with_stdio(false),              \\
      cin.tie(nullptr), cout.tie(nullptr)
double const EPS = 1e-12, PI = acos(-1);
const int N = 5e5 + 9, M = 1e6 + 9, OO = 1e9 + 7, MOD = 1e9 + 7,
          MOD2 = 998244353;
const ll INF = 2e18;
using namespace std;

ll arr[N];
bool vis[N];
int n;

int main() {
  fastIO;
#ifdef LOCAL
//	freopen("input.in", "rt", stdin);
#endif
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  ll cur = 0;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; ++i)
    v.push_back({arr[i], i});
  sort(all(v), greater<pair<int, int>>());
  int cnt = n / 2;
  for (int i = 0; i < (int)v.size() && cnt; ++i) {
    int idx = v[i].second;
    if (idx - 1 >= 0 && vis[idx - 1])
      continue;
    if (idx + 1 < n && vis[idx + 1])
      continue;
    vis[idx] = 1;
    cur += v[i].first;
    --cnt;
  }
  assert(cnt == 0);
  cout << cur;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\\n", x)
#define plld(x) printf("%lld\\n", x)
#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 mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define fast_io ios::sync_with_stdio(false)

const int INF = 1e9;
const LL mod = 1e9 + 7;
const int maxn = 2e5 + 7;

LL dp[maxn];
LL a[maxn];
LL sum[maxn];

int main() {
  int n;
  while (~sd(n)) {
    rep(i, 1, n) slld(a[i]);
    sum[1] = a[1];
    rep(i, 1, n) {
      if (i & 1)
        sum[i] = sum[i - 1] + a[i];
      else
        sum[i] = sum[i - 1];
    }
    mem(dp);
    rep(i, 2, n) {
      if (i & 1)
        dp[i] = max(dp[i - 2] + a[i], dp[i - 1]);
      else
        dp[i] = max(dp[i - 2] + a[i], sum[i - 1]);
    }
    plld(dp[n]);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\\n", x)
#define plld(x) printf("%lld\\n", x)
#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 mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define fast_io ios::sync_with_stdio(false)

const int INF = 1e9;
const LL mod = 1e9 + 7;
const int maxn = 1e5 + 7;

LL dp[maxn];
LL a[maxn];
LL sum[maxn];

int main() {
  int n;
  while (~sd(n)) {
    rep(i, 1, n) slld(a[i]);
    sum[1] = a[1];
    rep(i, 1, n) {
      if (i & 1)
        sum[i] = sum[i - 1] + a[i];
      else
        sum[i] = sum[i - 1];
    }
    mem(dp);
    rep(i, 2, n) {
      if (i & 1)
        dp[i] = max(dp[i - 2] + a[i], dp[i - 1]);
      else
        dp[i] = max(dp[i - 2] + a[i], sum[i - 1]);
    }
    plld(dp[n]);
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<ll>> d(n + 100, vector<ll>(3, -INF));
  d[0][0] = a[0];
  d[1][1] = a[1];
  d[2][2] = a[2];
  rep(i, n) {
    if (i % 2 == 1) {
      if (i < n) {
        d[i + 2][1] = max(d[i + 2][1], d[i][1] + a[i + 2]);
        d[i + 3][2] = max(d[i + 3][2], d[i][1] + a[i + 3]);
      }
    } else {
      if (i < n) {
        d[i + 2][0] = max(d[i + 2][0], d[i][0] + a[i + 2]);
        d[i + 3][1] = max(d[i + 3][1], d[i][0] + a[i + 3]);
        d[i + 4][2] = max(d[i + 4][2], d[i][0] + a[i + 4]);
        d[i + 2][2] = max(d[i + 2][2], d[i][2] + a[i + 2]);
      }
    }
  }
  ll ans;
  if (n % 2 == 1) {
    ans = max(d[n - 1][2], d[n - 2][1]);
    ans = max(ans, d[n - 3][0]);
  }
  if (n % 2 == 0)
    cout << max(d[n - 1][1], d[n - 2][0]) << endl;
  else
    cout << ans << endl;
}

Error: 0

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll MOD = 1000000007;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j;
  cin >> n;
  vector<int64_t> a(n + 1);
  vector<vector<int64_t>> dp(n + 1, vector<int64_t>(2));
  for (i = 1; i <= n; i++)
    cin >> a.at(i);
  dp.at(1).at(0) = 0;
  dp.at(1).at(1) = a.at(1);
  dp.at(2).at(0) = 0;
  dp.at(2).at(1) = max(a.at(1), a.at(2));
  for (i = 3; i <= n; i++) {
    if (i % 2 == 1) {
      dp.at(i).at(0) = max(dp.at(i - 1).at(1), dp.at(i - 2).at(0) + a.at(i));
      dp.at(i).at(1) = dp.at(i - 2).at(1) + a.at(i);
    } else if (i % 2 == 0) {
      dp.at(i).at(0) = max(dp.at(i - 1).at(0), dp.at(i - 2).at(0) + a.at(i));
      dp.at(i).at(1) = max(dp.at(i - 1).at(1), dp.at(i - 2).at(1) + a.at(i));
    }
  }
  if (n % 2 == 0)
    cout << dp.at(n).at(1) << endl;
  else if (n % 2 == 1)
    cout << dp.at(n).at(0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j;
  cin >> n;
  vector<int64_t> a(n + 1);
  vector<vector<int64_t>> dp(n + 1, vector<int64_t>(n + 1));
  for (i = 1; i <= n; i++)
    cin >> a.at(i);
  dp.at(1).at(1) = a.at(1);
  dp.at(2).at(1) = max(a.at(1), a.at(2));
  for (i = 3; i <= n; i++) {
    for (j = (i - 1) / 2; j <= (i + 1) / 2; j++) {
      if (i == 2 * j - 1)
        dp.at(i).at(j) = dp.at(i - 2).at(j - 1) + a.at(i);
      else
        dp.at(i).at(j) =
            max(dp.at(i - 1).at(j), dp.at(i - 2).at(j - 1) + a.at(i));
    }
  }
  cout << dp.at(n).at(n / 2) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int MAXN = 5e5 + 25;

int dp[MAXN][3];
int a[MAXN];

const int INF = 1e18 + 1188;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i <= n; ++i) {
    dp[i][0] = -INF;
    dp[i][1] = -INF;
    dp[i][2] = -INF;
  }
  dp[1][0] = a[0];
  dp[2][1] = a[1];
  dp[3][2] = a[2];
  dp[3][0] = a[2] + a[0];
  for (int i = 3; i < n; ++i) {
    dp[i + 1][0] = dp[i - 1][0] + a[i];
    dp[i + 1][1] = max(dp[i - 2][0], dp[i - 1][1]) + a[i];
    int c = max(dp[i - 2][1], dp[i - 1][2]);
    c = max(c, dp[i - 3][0]);
    dp[i + 1][2] = c + a[i];
  }
  if (n % 2) {
    int mx = dp[n][2];
    mx = max(mx, dp[n - 1][1]);
    mx = max(mx, dp[n - 2][0]);
    cout << mx << endl;
  } else {
    int mx = dp[n][1];
    mx = max(mx, dp[n - 1][0]);
    cout << mx << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int MAXN = 1e5 + 25;

int dp[MAXN][3];
int a[MAXN];

const int INF = 1e18 + 1188;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  if (n == 2) {
    cout << max(a[1], a[0]) << endl;
  }
  for (int i = 0; i <= n; ++i) {
    dp[i][0] = -INF;
    dp[i][1] = -INF;
    dp[i][2] = -INF;
  }
  dp[1][0] = a[0];
  dp[2][1] = a[1];
  if (n > 2) {
    dp[3][2] = a[2];
  }
  for (int i = 2; i < n; ++i) {
    dp[i + 1][0] = dp[i - 1][0] + a[i];
    dp[i + 1][1] = max(dp[i - 2][0], dp[i - 1][1]) + a[i];
    if (i > 2) {
      int c = max(dp[i - 2][1], dp[i - 1][2]);
      c = max(c, dp[i - 3][0]);
      dp[i + 1][2] = c + a[i];
    }
  }
  if (n % 2) {
    int mx = dp[n][2];
    mx = max(mx, dp[n - 1][1]);
    mx = max(mx, dp[n - 2][0]);
    cout << mx << endl;
  } else {
    int mx = dp[n][1];
    mx = max(mx, dp[n - 1][0]);
    cout << mx << endl;
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mp make_pair
#define sqr(x) (x) * (x)
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int maxn = 200007;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
ll a[maxn];
ll dp[maxn][11], Sum;
int main() {

  int n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read(), Sum += a[i];
  memset(dp, 0x3f, sizeof(dp));
  dp[0][5] = 0;
  dp[1][6] = a[1];
  for (int i = 2; i <= n + 1; i++) {
    for (int k = 0; k <= 10; k++) {
      int now = i / 2 + k - 5;
      if (now >= 1) {
        int p1 = now - 1 - (i - 1) / 2 + 5;
        int p2 = now - 1 - (i - 2) / 2 + 5;
        if (p1 > 10 || p2 > 10 || p1 < 0 || p2 < 0)
          continue;
        dp[i][k] = min(dp[i - 1][p1], dp[i - 2][p2]) + a[i];
      }
    }
  }
  int you = n - (n / 2) + 1;
  int p = you - (n + 1) / 2 + 5;
  cout << Sum - dp[n + 1][p];
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mp make_pair
#define sqr(x) (x) * (x)
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int maxn = 200007;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
ll a[maxn];
ll dp[maxn][11], Sum;
int main() {
  freopen("test.in", "r", stdin);
  int n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read(), Sum += a[i];
  memset(dp, 0x3f, sizeof(dp));
  dp[0][5] = 0;
  dp[1][6] = a[1];
  for (int i = 2; i <= n + 1; i++) {
    for (int k = 0; k <= 10; k++) {
      int now = i / 2 + k - 5;
      if (now >= 1) {
        int p1 = now - 1 - (i - 1) / 2 + 5;
        int p2 = now - 1 - (i - 2) / 2 + 5;
        if (p1 > 10 || p2 > 10 || p1 < 0 || p2 < 0)
          continue;
        dp[i][k] = min(dp[i - 1][p1], dp[i - 2][p2]) + a[i];
      }
    }
  }
  int you = n - (n / 2) + 1;
  int p = you - (n + 1) / 2 + 5;
  cout << Sum - dp[n + 1][p];
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

const ll INF = 1LL << 60;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<ll> A(N);
  for (auto &a : A) {
    cin >> a;
  }
  int rest = N - N / 2 - N / 2 + 1;
  vector<vector<ll>> dp(N, vector<ll>(rest + 1, -INF));
  rep(i, 0, rest + 1) { dp[i][rest - i] = A[i]; }
  rep(i, 0, N) rep(j, 0, rest + 1) {
    rep(k, 0, j + 1) {
      if (i + k + 2 >= N) {
        break;
      }
      chmax(dp[i + k + 2][j - k], dp[i][j] + A[i + k + 2]);
    }
  }
  ll ans = -INF;
  rep(i, 0, rest + 1) { chmax(ans, dp[N - 1 - i][i]); }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

const ll INF = 1LL << 60;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<ll> A(N);
  for (auto &a : A) {
    cin >> a;
  }
  int rest = N - N / 2 - N / 2 + 1 + N % 2;
  vector<vector<ll>> dp(N, vector<ll>(rest + 1, -INF));
  rep(i, 0, rest + 1) { dp[i][rest - i] = A[i]; }
  rep(i, 0, N) rep(j, 0, rest + 1) {
    rep(k, 0, j + 1) {
      if (i + k + 2 >= N) {
        break;
      }
      chmax(dp[i + k + 2][j - k], dp[i][j] + A[i + k + 2]);
    }
  }
  ll ans = 0;
  rep(i, 0, rest + 1) { chmax(ans, dp[N - 1][i]); }
  cout << ans << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define repl(i, n) REPL(i, 0, n)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  vector<vector<vector<ll>>> dp(
      N + 1,
      vector<vector<ll>>(
          2, vector<ll>(
                 3, -longinf))); // index, select(1) or not(0), not select count
  dp[0][0][0] = 0;
  rep(i, N) {
    rep(k, 3) { // select
      dp[i + 1][1][k] = max(dp[i][0][k] + A[i], dp[i + 1][1][k]);
    }
    if (i) {      // not select
      rep(k, 3) { // selected previous index
        dp[i + 1][0][k] = max(dp[i][1][k], dp[i + 1][0][k]);
      }
      rep(k, 2) { // not selected previous index
        dp[i + 1][0][k + 1] = max(dp[i][0][k], dp[i + 1][0][k + 1]);
      }
    } else {
      dp[i + 1][0][1] = 0;
    }
  }

  ll ans = -longinf;
  if (N % 2)
    ans = max(dp[N][1][2], dp[N][0][1]);
  else
    ans = max(dp[N][1][1], dp[N][0][0]);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define repl(i, n) REPL(i, 0, n)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N + 1, vector<ll>((N + 1) / 2 + 1, -longinf));
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[1][1] = A[0];
  rep(i, N) {
    int l = N / 2 - (N - i) / 2;
    int r = i / 2 + 1;
    for (int k = l; k <= r; k++) {
      for (int j = i - 5; j <= i - 2; j++) {
        if (k - 1 >= 0 && j + 1 >= 0) {
          dp[i + 1][k] = max(dp[j + 1][k], dp[i + 1][k]);
          dp[i + 1][k] = max(dp[j + 1][k - 1] + A[i], dp[i + 1][k]);
        }
      }
    }
  }
  ll mx = -longinf;
  rep(i, N + 1) mx = max(dp[i][N / 2], mx);
  cout << mx << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200005;

int n;

ll a[N_MAX];

ll dp[N_MAX][3];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  if (n == 2) {
    cout << max(a[1], a[2]) << "\\n";
    return 0;
  }
  ll ans = LLONG_MIN;
  int up;
  if (n & 1)
    up = 2;
  else
    up = 1;
  dp[1][0] = a[1];
  dp[1][1] = dp[1][2] = LLONG_MIN;
  for (int i = 2; i <= n; i++)
    for (int j = 0; j <= up; j++) {
      dp[i][j] = LLONG_MIN;
      for (int k = 0; k <= j; k++)
        if (i - 2 - (j - k) >= 0)
          if (dp[i - 2 - (j - k)][k] != LLONG_MIN)
            dp[i][j] = max(dp[i][j], dp[i - 2 - (j - k)][k] + a[i]);
      if (j + (n - i) == up)
        ans = max(ans, dp[i][j]);
    }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 100002;

int n;

int a[N_MAX];

ll dp[N_MAX][3];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  int up;
  if (n & 1)
    up = 2;
  else
    up = 1;
  dp[1][0] = a[1];
  dp[1][1] = dp[1][2] = LLONG_MIN;
  for (int i = 2; i <= n; i++)
    for (int j = 0; j <= up; j++) {
      dp[i][j] = LLONG_MIN;
      for (int k = 0; k <= j; k++)
        if (i - 2 - (j - k) >= 0)
          if (dp[i - 2 - (j - k)][k] != LLONG_MIN)
            dp[i][j] = max(dp[i][j], dp[i - 2 - (j - k)][k] + a[i]);
      if (j + (n - i) == up)
        ans = max(ans, dp[i][j]);
    }
  cout << ans << "\\n";
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  int N;
  cin >> N;
  VL A(N);
  rep(i, N) cin >> A[i];
  VVL dp(N + 2, VL(N % 2 + 2));
  rep(i, N + 2) rep(j, N % 2 + 2) dp[i][j] = -1001001001001001;
  dp[0][0] = 0;
  rep(i, N + 1) {
    rep(j, N % 2 + 2) {
      if (j < N % 2 + 1)
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
      if (i < N)
        dp[i + 2][j] = max(dp[i + 2][j], dp[i][j] + A[i]);
    }
  }
  cout << dp[N + 1][N % 2 + 1] << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  int N;
  cin >> N;
  VL A(N);
  rep(i, N) cin >> A[i];
  VVL dp(200200, VL(3));
  rep(i, N + 2) rep(j, N % 2 + 2) dp[i][j] = -1001001001001001;
  dp[0][0] = 0;
  rep(i, N + 1) {
    rep(j, N % 2 + 2) {
      if (j < N % 2 + 2)
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
      if (i < N)
        dp[i + 2][j] = max(dp[i + 2][j], dp[i][j] + A[i]);
    }
  }
  cout << dp[N + 1][N % 2 + 1] << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

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

INT A[200100], n, C[200100][3];

INT f(INT i, INT c) {
  if (C[i][c])
    return C[i][c];
  INT k;
  if (i + c == n)
    return A[i];
  if (i > n)
    return 0;
  k = f(i + 2, c) + A[i];
  if (c > 0)
    k = max(k, f(i + 3, c - 1) + A[i]);
  if (c > 1)
    k = max(k, f(i + 4, c - 2) + A[i]);

  return C[i][c] = k;
}
int main() {
  INT i;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> A[i];
  if (n % 2 == 0)
    cout << max(f(1, 1), f(2, 0));
  else
    cout << max(f(1, 2), max(f(2, 1), f(3, 0)));
}


Error: TLE

Fixed submission:

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

INT A[200100], n, C[200100][3];

INT f(INT i, INT c) {
  INT k;
  if (i + c == n)
    return A[i];
  if (i > n)
    return 0;
  k = f(i + 2, c) + A[i];
  if (c > 0)
    k = max(k, f(i + 3, c - 1) + A[i]);
  if (c > 1)
    k = max(k, f(i + 4, c - 2) + A[i]);

  return k;
}
int main() {
  INT i;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> A[i];
  if (n % 2 == 0)
    cout << max(f(1, 1), f(2, 0));
  else
    cout << max(f(1, 2), max(f(2, 1), f(3, 0)));
}