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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const int inf = 10000000000000007;
const int MOD = 1000000007;
int dp[5][2][300000];
signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 5, -inf);
  rep1(i, n) cin >> a[i];
  rep1(i, n) { a[i] += MOD; }
  int res = 0;
  rep(i, 5) {
    rep(j, 300000) {
      dp[i][0][j] = 0;
      dp[i][1][j] = 0;
    }
  }

  rep1(i, n) {
    dp[0][0][i] = dp[0][1][i - 1];
    dp[0][1][i] = dp[0][0][i - 1] + a[i];
    dp[1][0][i] = max(dp[0][0][i - 1], dp[1][1][i - 1]);
    dp[1][1][i] = dp[1][0][i - 1] + a[i];
    dp[2][0][i] = max(dp[2][1][i - 1], max(-inf, dp[1][0][i - 1]));
    dp[2][1][i] = dp[2][0][i - 1] + a[i];
    if (i <= 1) {
      dp[1][1][i] = 0;
      // dp[1][0][i]=0;
    }
    if (i <= 1) {
      dp[2][1][i] = 0;
      // dp[2][0][i]=0;
    }
  }
  if (n % 2 == 0) {
    cout << max(dp[0][1][n], max(dp[0][0][n], dp[1][1][n])) - MOD * n / 2
         << endl;
  } else {
    // exit(1);
    res = max(max(-dp[2][0][n], dp[2][1][n]), max(dp[1][1][n], dp[1][0][n]));
    if (res == -dp[2][0][n])
      exit(1);
    cout << res - n / (int)2 * MOD << endl;
    // cout<<dp[2][0][n]<<"\\n";
    // cout<<dp[2][1][n]<<"\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const int inf = 10000000000000007;
const int MOD = 1000000007;
int dp[5][2][300000];
signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 5, -inf);
  rep1(i, n) cin >> a[i];
  rep1(i, n) { a[i] += MOD; }
  int res = 0;
  rep(i, 5) {
    rep(j, 300000) {
      dp[i][0][j] = 0;
      dp[i][1][j] = 0;
    }
  }

  rep1(i, n) {
    dp[0][0][i] = dp[0][1][i - 1];
    dp[0][1][i] = dp[0][0][i - 1] + a[i];
    dp[1][0][i] = max(dp[0][0][i - 1], dp[1][1][i - 1]);
    dp[1][1][i] = dp[1][0][i - 1] + a[i];
    dp[2][0][i] = max(dp[2][1][i - 1], max(-inf, dp[1][0][i - 1]));
    dp[2][1][i] = dp[2][0][i - 1] + a[i];
    if (i <= 1) {
      dp[1][1][i] = 0;
      // dp[1][0][i]=0;
    }
    if (i <= 1) {
      dp[2][1][i] = 0;
      // dp[2][0][i]=0;
    }
  }
  if (n % 2 == 0) {
    cout << max(dp[0][1][n], max(dp[0][0][n], dp[1][1][n])) - MOD * n / 2
         << endl;
  } else {
    // exit(1);
    res = max(-max(dp[2][0][n], dp[2][1][n]), max(dp[1][1][n], dp[1][0][n]));
    if (res == dp[2][0][n])
      exit(1);
    cout << res - n / (int)2 * MOD << endl;
    // cout<<dp[2][0][n]<<"\\n";
    // cout<<dp[2][1][n]<<"\\n";
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int Maxn = 2e5 + 5;
int n, vis[2][Maxn];
long long a[Maxn], f[Maxn], dp[2][Maxn];
long long findou(int nowy) {
  if (nowy == n + 1)
    return 0;
  if (vis[0][nowy])
    return dp[0][nowy];
  else
    vis[0][nowy] = 1;
  return dp[0][nowy] = max(a[nowy] + findou(nowy + 2), f[nowy + 1]);
}
long long findji(int nowy) {
  if (nowy == n)
    return 0;
  if (vis[1][nowy])
    return dp[1][nowy];
  else
    vis[1][nowy] = 1;
  return dp[1][nowy] = max(a[nowy] + findji(nowy + 2), findou(nowy + 1));
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i >= 1; i -= 2)
    f[i] = f[i + 2] + a[i];
  if (n % 2)
    cout << findji(1);
  else
    cout << findou(1);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int Maxn = 2e5 + 5;
int n, vis[2][Maxn];
long long a[Maxn], dp[2][Maxn];
long long findou(int nowy) {
  if (nowy == n + 1)
    return 0;
  if (vis[0][nowy])
    return dp[0][nowy];
  else
    vis[0][nowy] = 1;
  long long sum = 0;
  for (int i = nowy + 1; i <= n; i += 2)
    sum += a[i];
  return dp[0][nowy] = max(a[nowy] + findou(nowy + 2), sum);
}
long long findji(int nowy) {
  if (nowy == n)
    return 0;
  if (vis[1][nowy])
    return dp[1][nowy];
  else
    vis[1][nowy] = 1;
  return dp[1][nowy] = max(a[nowy] + findji(nowy + 2), findou(nowy + 1));
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  if (n % 2)
    cout << findji(1);
  else
    cout << findou(1);
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int main() {
#ifdef _DEBUG
  freopen("input1.txt", "r", stdin);
  freopen("output2.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<vector<ll>> dp(n + 20, vector<ll>(4, -1e18));
  dp[0][0] = 0;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int sz = 1;
  sz += n % 2;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < sz + 1; j++) {
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
      ll add = dp[i][j];
      if ((i + j) % 2 == 0)
        add += a[i];
      dp[i + 1][j] = max(add, dp[i + 1][j]);
    }
  }
  cout << dp[n][sz];
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int main() {
#ifdef _DEBUG
  freopen("input1.txt", "r", stdin);
  freopen("output2.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -1e10));
  vector<ll> a(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dp[1][1] = a[1];
  dp[2][1] = max(a[1], a[2]);
  if (n & 1)
    dp[3][1] = a[3];
  for (ll i = 3; i <= n; i++) {
    for (ll j = 1; j <= i; j++) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (i >= 2) {
        dp[i][j] = max(dp[i][j], dp[i - 2][j - 1] + a[i]);
      }
      if (i >= 3) {
        dp[i][j] = max(dp[i][j], dp[i - 3][j - 1] + a[i]);
      }
    }
  }
  cout << dp[n][n / 2];
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
const ll INF = 9999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vl A;
  ll dp[n][4]; // どこまで見たか・i番目まで見たときにi-(使った数)*2+2
  rep(i, n) {
    cin >> a;
    A.push_back(a);
  }

  rep(i, n) {
    if (i == 0) {
      dp[0][0] = A[0];
      dp[0][1] = -INF;
      dp[0][2] = -INF;
    } else {
      if (i == 1)
        dp[i][0] = -INF;
      else
        dp[i][0] = dp[i - 2][0] + A[i];
      if (i == 1)
        dp[i][1] = A[i];
      else
        dp[i][1] = dp[i - 2][1] + A[i];
      if (i > 2)
        dp[i][1] = max(dp[i - 3][0] + A[i], dp[i][1]);
      if (i == 1)
        dp[i][2] = -INF;
      else if (i == 2)
        dp[i][2] = A[i];
      else
        dp[i][2] = dp[i - 2][2] + A[i];
      if (i > 2)
        dp[i][2] = max(dp[i - 3][1] + A[i], dp[i][2]);
      if (i > 3)
        dp[i][2] = max(dp[i - 4][0] + A[i], dp[i][2]);
    }
  }

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

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
const ll INF = 9999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vl A;
  ll dp[n][4]; // どこまで見たか・i番目まで見たときにi-(使った数)*2+2
  rep(i, n) {
    cin >> a;
    A.push_back(a);
  }

  rep(i, n + 1) {
    if (i == 0) {
      dp[0][0] = A[0];
      dp[0][1] = -INF;
      dp[0][2] = -INF;
    } else {
      if (i == 1)
        dp[i][0] = -INF;
      else
        dp[i][0] = dp[i - 2][0] + A[i];
      if (i == 1)
        dp[i][1] = A[i];
      else
        dp[i][1] = dp[i - 2][1] + A[i];
      if (i > 2)
        dp[i][1] = max(dp[i - 3][0] + A[i], dp[i][1]);
      if (i == 1)
        dp[i][2] = -INF;
      else if (i == 2)
        dp[i][2] = A[i];
      else
        dp[i][2] = dp[i - 2][2] + A[i];
      if (i > 2)
        dp[i][2] = max(dp[i - 3][1] + A[i], dp[i][2]);
      if (i > 3)
        dp[i][2] = max(dp[i - 4][0] + A[i], dp[i][2]);
    }
  }

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

  cout << ans << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int64_t> A(n);
  copy_n(istream_iterator<int64_t>(cin), n, A.begin());

  vector<map<int, int64_t>> DP(n + 2);
  DP[0][0] = 0;
  const constexpr int64_t INF = 1e17;
  vector<int> B(n);
  for (int i = 0; i < n; i++) {
    B[i] = n / 2 - (n - i + 1) / 2;
  }

  for (int i = 0; i < n; i++) {
    for (const auto &[k, v] : DP[i]) {
      if (k < B[i])
        continue;
      if (DP[i + 1].count(k) == 0)
        DP[i + 1][k] = -INF;
      DP[i + 1][k] = max(DP[i + 1][k], DP[i][k]);
      if (DP[i + 2].count(k + 1) == 0)
        DP[i + 2][k + 1] = -INF;
      DP[i + 2][k + 1] = max(DP[i + 2][k + 1], DP[i][k] + A[i]);
    }
  }

  int64_t ans = -INF;
  for (int i = 0; i <= n + 1; i++) {
    if (DP[i].count(n / 2) != 0)
      ans = max(ans, DP[i][n / 2]);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int64_t> A(n);
  copy_n(istream_iterator<int64_t>(cin), n, A.begin());

  vector<map<int, int64_t>> DP(n + 2);
  DP[0][0] = 0;
  const constexpr int64_t INF = 1e17;

  for (int i = 0; i < n; i++) {
    for (const auto &[k, v] : DP[i]) {
      if (DP[i + 1].count(k) == 0)
        DP[i + 1][k] = -INF;
      DP[i + 1][k] = max(DP[i + 1][k], DP[i][k]);
      if (DP[i + 2].count(k + 1) == 0)
        DP[i + 2][k + 1] = -INF;
      DP[i + 2][k + 1] = max(DP[i + 2][k + 1], DP[i][k] + A[i]);
    }
  }

  int64_t ans = 0;
  for (int i = 0; i <= n + 1; i++)
    ans = max(ans, DP[i][n / 2]);
  cout << ans << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
const int maxn = 2e5 + 9;
using namespace std;
int main() {
  ll n, a[maxn] = {0}, ans;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  ll dp[maxn][3] = {0}, k = n / 2;
  if (n & 1) {
    for (int i = 1; i <= k; i++) {
      dp[i][0] = dp[i - 1][0] + a[i * 2 - 1];
      dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + a[i * 2];
      dp[i][2] =
          max(dp[i - 1][0], max(dp[i - 1][2], dp[i - 1][1])) + a[i * 2 + 1];
    }
    ans = max(max(dp[k][0], dp[k][1]), dp[k][2]);
  } else {
    for (int i = 1; i <= k; i++) {
      dp[i][0] = dp[i - 1][0] + a[i * 2 - 1];
      dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + a[i * 2];
    }
    ans = max(dp[k][0], dp[k][1]);
  }
  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
const int maxn = 2e5 + 9;
using namespace std;
ll ans = -1e18;
ll n, a[maxn];
void dfs(int pos, int num, ll sum) {
  if (num == n / 2) {
    ans = max(ans, sum);
    return;
  }
  if (pos > n || num > n / 2)
    return;
  if (num + (n - pos + 2) / 2 < n / 2)
    return;
  dfs(pos + 1, num, sum);
  dfs(pos + 2, num + 1, sum + a[pos]);
}
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  dfs(1, 0, 0);
  printf("%lld\\n", ans);
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

const Int INF = 1e15;

int main() {
  int N;
  cin >> N;
  vector<Int> A(N + 1);
  rep1(i, N) cin >> A[i];

  // dp[i][0]: A[1]~A[i]からi/2個とるときの最大値
  // dp[i][1]: A[1]~A[i]からi/2+1個とるときの最大値(i:奇数)
  vector<vector<Int>> dp(200010, vector<Int>(2, -INF));

  dp[1][0] = 0;
  dp[1][1] = A[1];
  dp[2][0] = max(A[1], A[2]);

  for (int i = 3; i <= N; i++) {
    Int last = A[i];
    if (i % 2 == 0) {
      dp[i][0] = max(dp[i - 1][1], last + dp[i - 2][0]);
    } else {
      dp[i][0] = max(dp[i - 1][0], last + dp[i - 2][0]);
      dp[i][1] = last + dp[i - 2][1];
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

// Aから連続するところがないようにK個とってくる
Int solve(int K, vector<Int> A) {
  int N = A.size();

  if (K == 1)
    return *max_element(A.begin(), A.end());

  if (N % 2 != 0 && (K == N / 2 + 1)) {
    Int sum = 0;
    rep(i, N) {
      if (i % 2 == 0)
        sum += A[i];
    }
    return sum;
  }

  Int last = A[N - 1];

  A.pop_back();
  Int leave_last = solve(K, A);

  A.pop_back();
  Int take_last = last + solve(K - 1, A);

  // cout << N << " " << take_last << " " << leave_last << endl;

  return max(take_last, leave_last);
}

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

  cout << solve(N / 2, A) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(v) for (auto itr = v.begin(), itr != v.end(); ++itr)
#define LB(v, x) lower_bound(v.begin(), v.end(), x) - v.begin()
#define UB(v, x) upper_bound(v.begin(), v.end(), x) - v.begin()
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<map<int, ll>> dp(N + 1);
  dp[0][0] = 0;
  REP(i, N) {
    if (i == 0) {
      dp[i + 1][0] = 0;
      dp[i + 1][1] = A[i];
      continue;
    }
    FOR(j, i / 2, i / 2 + 2) {
      dp[i + 1][j] = -(ll)1e18;
      if (dp[i].count(j))
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (dp[i - 1].count(j - 1))
        dp[i + 1][j] = max(dp[i + 1][j], dp[i - 1][j - 1] + A[i]);
    }
  }
  cout << dp[N][N / 2] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(v) for (auto itr = v.begin(), itr != v.end(); ++itr)
#define LB(v, x) lower_bound(v.begin(), v.end(), x) - v.begin()
#define UB(v, x) upper_bound(v.begin(), v.end(), x) - v.begin()
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<vector<ll>> dp(N + 1, vector<ll>(N / 2 + 1, -ll(1e18)));
  dp[0][0] = 0;
  REP(i, N) {
    if (i == 0) {
      dp[i + 1][0] = 0;
      dp[i + 1][1] = A[i];
      continue;
    }
    FOR(j, i / 2, i / 2 + 2) {
      dp[i + 1][j] = max(dp[i][j], dp[i - 1][j - 1] + A[i]);
    }
  }
  cout << dp[N][N / 2] << endl;

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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <utility>
#define fr first
#define sc second
#define pii pair<int, bool>
#define mp make_pair
using namespace std;
const int maxn = 1e6 + 10;
int n, a[maxn];
map<pii, long long> dp[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  dp[0][mp(0, 0)] = 0;
  for (int i = 1; i <= n; i++)
    for (auto j : dp[i - 1]) {
      if (!j.fr.sc) {
        if (dp[i].count(mp(j.fr.fr + 1, 1)))
          dp[i][mp(j.fr.fr + 1, 1)] =
              max(dp[i][mp(j.fr.fr + 1, 1)], j.sc + a[i]);
        else
          dp[i][mp(j.fr.fr + 1, 1)] = j.sc + a[i];
      }
      if ((n - i + 1) / 2 + j.fr.fr >= (n / 2)) {
        if (dp[i].count(mp(j.fr.fr, 0)))
          dp[i][mp(j.fr.fr, 0)] = max(dp[i][mp(j.fr.fr, 0)], j.sc);
        else
          dp[i][mp(j.fr.fr, 0)] = j.sc;
      }
    }
  printf("%lld\\n", max(dp[n][mp(n / 2, 0)], dp[n][mp(n / 2, 1)]));
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#define pb push_back
using namespace std;
const int maxn = 1e6 + 10;
int n, a[maxn];
long long ans = -1e18;
struct da {
  int num;
  long long sum;
  bool f;
};
vector<da> dp[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  dp[0].pb({0, 0, 0});
  for (int i = 1; i <= n; i++)
    for (auto j : dp[i - 1]) {
      if (!j.f)
        dp[i].pb({j.num + 1, j.sum + a[i], 1});
      if ((n - i + 1) / 2 + j.num >= (n / 2))
        dp[i].pb({j.num, j.sum, 0});
    }
  for (auto i : dp[n])
    if (i.num == (n / 2))
      ans = max(ans, i.sum);
  printf("%lld\\n", ans);
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using ull = long long unsigned;
using P = pair<ll, ll>;
// const int 1001001001;
const int NMAX = 8;
const ll MOD = 1000000007;
const ll INF = 1e18;
const int mod = 998244353;
ll dp[200005][4];

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll k = n % 2 + 1; // 何個余分な数が入るか

  rep(i, n + 1) rep(j, k + 1) dp[i][j] = -INF;
  dp[0][0] = 0;
  rep(i, n) rep(j, k + 1) {
    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
    // 選ばれない時
    if ((i + j) % 2 == 0) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a[i]);
    } else {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
    //	cout<<dp[i+1][j]<<'i'<<i<<'j'<<j<<endl;
  }
  ll ans = dp[n][k];
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using ull = long long unsigned;
using P = pair<ll, ll>;
// const int 1001001001;
const int NMAX = 8;
const ll MOD = 1000000007;
const ll INF = 1e18;
const int mod = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<ll>> dp(n + 5, vector<ll>(n + 5, -INF));
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[1][1] = a[0];
  for (ll i = 2; i <= n; i++) {
    // nが偶数
    if (n % 2 == 0) {
      if (i % 2 == 0) {
        dp[i][i / 2] = max(dp[i - 1][i / 2], dp[i - 2][i / 2 - 1] + a[i - 1]);
        //	cout<<'g'<<endl;
      }
      if (i % 2 == 1) {
        if (i >= 3) {
          dp[i][(i - 1) / 2] = max(dp[i - 1][(i - 1) / 2],
                                   dp[i - 2][(i - 1) / 2 - 1] + a[i - 1]);
        }
        dp[i][(i + 1) / 2] =
            max(dp[i - 1][(i + 1) / 2], dp[i - 2][(i + 1) / 2 - 1] + a[i - 1]);
      }
    }
    // nが奇数
    if (n % 2 == 1) {
      if (i % 2 == 0) {
        dp[i][i / 2] = max(dp[i - 1][i / 2], dp[i - 2][i / 2 - 1] + a[i - 1]);
        dp[i][i / 2 - 1] =
            max(dp[i - 1][i / 2 - 1], dp[i - 2][i / 2 - 2] + a[i - 1]);
      }
      if (i % 2 == 1) {
        if (i >= 3) {
          dp[i][(i - 1) / 2] = max(dp[i - 1][(i - 1) / 2],
                                   dp[i - 2][(i - 1) / 2 - 1] + a[i - 1]);
        }
        dp[i][(i + 1) / 2] =
            max(dp[i - 1][(i + 1) / 2], dp[i - 2][(i + 1) / 2 - 1] + a[i - 1]);
      }
    }
    // cout<<dp[i][2]<<'i'<<i<<endl;
  }
  ll ans = -INF;
  ans = max(ans, dp[n][n / 2]);
  cout << ans;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int j, n, m, po, no, flag, cnt = 0, cnt2, ar[200001], x, y, i,
                                       co[200001], sum = 0, k, ans = INT_MIN,
                                       sum1 = 0;
  string str[1000], ptr, sr, tr, rtr, rt;
  vector<long long int> v1, v2;
  set<string> s;
  queue<long long int> qu;
  set<string>::iterator it;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  co[0] = 0;
  co[1] = 0;
  x = ar[1];
  for (i = 2; i <= n; i++) {
    if (i % 2 == 0)
      co[i] = max((co[i - 2] + ar[i]), x);
    else {
      co[i] = max((co[i - 2] + ar[i]), co[i - 1]);
      x += ar[i];
    }
  }

  cout << co[n] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int j, n, m, po, no, flag, cnt = 0, cnt2, ar[100000], x, y, i,
                                       co[100000], sum = 0, k, ans = INT_MIN,
                                       sum1 = 0;
  string str[1000], ptr, sr, tr, rtr, rt;
  vector<long long int> v1, v2;
  set<string> s;
  queue<long long int> qu;
  set<string>::iterator it;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  co[0] = 0;
  co[1] = 0;
  x = ar[1];
  for (i = 2; i <= n; i++) {
    if (i % 2 == 0)
      co[i] = max((co[i - 2] + ar[i]), x);
    else {
      co[i] = max((co[i - 2] + ar[i]), co[i - 1]);
      x += ar[i];
    }
  }

  cout << co[n] << endl;
}