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

#include <bits/stdc++.h>
using namespace std;
// #pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef pair<int, int> P;
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define lowbit(x) (x & -x)
const ll INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int N = 2e5 + 10;
const ll mod = 1e9 + 7;
inline ll read() {
  ll f = 1, x = 0;
  char ch = getchar();
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch <= '9' && ch >= '0'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
ll n, x, a[N];
map<int, ll> dp[N];
ll solve(int now, int cnt) {
  if ((now + 1) / 2 < cnt)
    return -INF * 1000000;
  if (cnt == 0 || now < 1)
    return 0;
  if (dp[now].count(cnt))
    return dp[now][cnt];
  return dp[now][cnt] =
             max(solve(now - 2, cnt - 1) + a[now], solve(now - 1, cnt));
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << solve(n, n / 2);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef pair<int, int> P;
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define lowbit(x) (x & -x)
const ll INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int N = 2e5 + 10;
const ll mod = 1e9 + 7;
inline ll read() {
  ll f = 1, x = 0;
  char ch = getchar();
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch <= '9' && ch >= '0'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
ll n, x, a[N];
ll ans = -1e15;
void dp(int now, int cnt, ll sum) {
  // printf("now=%d cnt=%d sum=%d\\n",now,cnt,sum);
  if (cnt == n / 2) {
    ans = max(ans, sum);
    return;
  }
  if (now > n)
    return;
  dp(now + 2, cnt + 1, sum + a[now]);
  dp(now + 1, cnt, sum);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp(1, 0, 0);
  cout << ans;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

typedef long long ll;
const int maxn = 4e5 + 3;
const ll ninf = std::numeric_limits<ll>::min();
int n, a[maxn];
std::map<int, ll> s[maxn];

void filter(int i) {
  static std::map<int, ll> t;
  t.clear();
  int r = (i + 1) / 2 + 5, l = r - 20;
  for (auto &&[u, v] : s[i]) {
    if (u < l || u > r)
      continue;
    t.emplace(u, v);
  }
  t.swap(s[i]);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  s[0][0] = 0;
  s[1][0] = 0;
  s[1][1] = a[0];
  s[2][0] = 0;
  s[2][1] = a[1];
  for (int i = 3; i <= n; ++i) {
    auto &cs = s[i];
    for (int j = std::max(i - 4, 0); j <= i - 2; ++j) {
      for (auto &&[x, y] : s[j]) {
        if (cs.find(x + 1) == cs.end())
          cs[x + 1] = y + a[i - 1];
        else
          cs[x + 1] = std::max(cs[x + 1], y + a[i - 1]);
      }
    }
    filter(i);
  }
  ll ans = ninf;
  for (int i = 0; i <= n; ++i) {
    if (s[i].find(n / 2) != s[i].end()) {
      ans = std::max(ans, s[i][n / 2]);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;
const int maxn = 4e5 + 3;
const ll ninf = std::numeric_limits<ll>::min();
int n, a[maxn];
std::map<int, ll> s[maxn];

void filter(int i) {
  static std::map<int, ll> t;
  t.clear();
  int r = (i + 1) / 2 + 5, l = r - 100;
  for (auto &&[u, v] : s[i]) {
    if (u < l || u > r)
      continue;
    t.emplace(u, v);
  }
  t.swap(s[i]);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  s[0][0] = 0;
  s[1][0] = 0;
  s[1][1] = a[0];
  s[2][0] = 0;
  s[2][1] = a[1];
  for (int i = 3; i <= n; ++i) {
    auto &cs = s[i];
    for (int j = std::max(i - 4, 0); j <= i - 2; ++j) {
      for (auto &&[x, y] : s[j]) {
        if (cs.find(x + 1) == cs.end())
          cs[x + 1] = y + a[i - 1];
        else
          cs[x + 1] = std::max(cs[x + 1], y + a[i - 1]);
      }
    }
    filter(i);
  }
  ll ans = ninf;
  for (int i = 0; i <= n; ++i) {
    if (s[i].find(n / 2) != s[i].end()) {
      ans = std::max(ans, s[i][n / 2]);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#ifdef ConanYu
#include "E:\\yuzining\\code\\local.hpp"
#else
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (false)
void Main();
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie();
  Main();
  return 0;
}
#endif

int a[200005];

ll b[200005], c[200005];
map<tuple<int, int, int>, ll> mp;
ll dfs(int i, int n, int d) {
  if (d <= 0)
    return 0;
  if ((n - i + 1) % 2 != 0 && d == (n - i + 1) / 2 + 1) {
    if (n & 1)
      return b[n] - b[i - 1];
    return c[n] - c[i - 1];
  }
  tuple<int, int, int> cur = {i, n, d};
  auto it = mp.find(cur);
  if (it != mp.end())
    return it->second;
  ll c1 = dfs(i + 1, n, d);
  ll c2 = a[i] + dfs(i + 2, n, d - 1);
  mp[cur] = max(c1, c2);
  return max(c1, c2);
}

void Main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    b[i] = b[i - 1];
    c[i] = c[i - 1];
    if (i & 1)
      b[i] += a[i];
    else
      c[i] += a[i];
  }
  cout << dfs(1, n, n / 2) << '\\n';
}

Error: TLE

Fixed submission:

#ifdef ConanYu
#include "E:\\yuzining\\code\\local.hpp"
#else
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (false)
void Main();
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie();
  Main();
  return 0;
}
#endif

int a[200005];

ll b[200005], c[200005];

ll dfs(int i, int n, int d) {
  if (d <= 0)
    return 0;
  if ((n - i + 1) % 2 != 0 && d == (n - i + 1) / 2 + 1) {
    if (n & 1)
      return b[n] - b[i - 1];
    return c[n] - c[i - 1];
  }
  ll c1 = dfs(i + 1, n, d);
  ll c2 = a[i] + dfs(i + 2, n, d - 1);
  return max(c1, c2);
}

void Main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    b[i] = b[i - 1];
    c[i] = c[i - 1];
    if (i & 1)
      b[i] += a[i];
    else
      c[i] += a[i];
  }
  cout << dfs(1, n, n / 2) << '\\n';
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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


Error: 0

Fixed submission:

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

// sunuke さんのコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(x, y) x = max(x, y)
const ll INF = 1e18;
ll dp[200001][4]; // [N][skip]

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

  rep(i, N + 1) dp[i][0] = dp[i][1] = dp[i][2] = -INF;
  dp[0][0] = 0;

  rep(i, N) {

    chmax(dp[i + 1][1], dp[i][0]);
    chmax(dp[i + 1][0], dp[i][0] + ((i + 0) % 2 ? 0 : A[i]));

    chmax(dp[i + 1][2], dp[i][1]);
    chmax(dp[i + 1][1], dp[i][1] + ((i + 1) % 2 ? 0 : A[i]));

    chmax(dp[i + 1][3], dp[i][2]);
    chmax(dp[i + 1][2], dp[i][2] + ((i + 2) % 2 ? 0 : A[i]));
  }

  cout << dp[N][N % 2 + 1] << endl;

  // // print =====================
  // puts("---");//
  // rep(i,N)cout<<A[i]<<" ";//
  // puts("");//
  // puts("===");//
  // cout<<"i: ";
  // rep(i,N+1) printf("%5d ",i);//
  // puts("");//
  // rep(j,skip+1){
  // 	cout<<j<<": ";
  // 	rep(i,N+1){
  // 		if(dp[i][j] == -INF) cout<<"----- ";
  // 		else printf("%5lld ",dp[i][j]);
  // 	}
  // 	puts("");
  // }
  // puts("===");//
  // rep(i,N+1){
  // 	cout<<i<<": ";
  // 	rep(j,skip+1){
  // 		if(dp[i][j] == -INF) cout<<"----- ";
  // 		else printf("%5lld ",dp[i][j]);
  // 	}
  // 	puts("");
  // }
  // // ===========================
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
#define irp(i, s, e) for (int i = (s); i > (e); --i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// #define NMAX 1000000000

using VL = vector<ll>;
using VVL = vector<VL>;
using TPL = tuple<ll, set<int>>; // <value,{index}>
using VT = vector<TPL>;

ll INF = LLONG_MAX >> 2;

int N;
VL A;
ll ans;

ll solve() {

  VVL dp(N / 2 + 2, VL(N + 2, -INF)); // dp[j][i]

  rp(i, 1, N + 1) dp[1][i] = A[i - 1];

  rp(i, 2, N + 1) {
    ll a = A[i - 1];
    int j = i / 2;
    dp[j][i] = max(dp[j][i], dp[j - 1][i - 2] + a);
    dp[j][i] = max(dp[j][i], dp[j][i - 1]);
  }

  // cout<<"   ";//
  // rp(i,0,dp[0].size())printf("%2d ",i);puts("");//
  // rp(i,0,dp.size()){
  // 	cout<<i<<": ";
  // 	rp(j,0,dp[i].size()){
  // 		if(dp[i][j]==-INF) cout<<"-- ";
  // 		else printf("%2lld ",dp[i][j]);
  // 	}puts("");
  // }puts("---");

  return dp[N / 2][N];
}

int main() {
  cin >> N;
  A.resize(N);
  rp(i, 0, N) cin >> A[i];
  cout << solve() << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

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

const int SIZE = 200005;
int N;
map<pair<int, int>, long long> m;

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

  for (int i = 1; i <= N; i += 2)
    m[make_pair(i, i / 2 + 1)] = m[make_pair(i - 2, i / 2)] + A[N - i];

  for (int i = 2; i <= N; i += 2) {
    long long larger = max(A[N - i], A[N - i + 1]);
    m[make_pair(i, i / 2)] = max(m[make_pair(i - 2, (i - 2) / 2)] + A[N - i],
                                 m[make_pair(i - 3, (i - 2) / 2)] + larger);
  }

  for (int i = 3; i <= N; i += 2) {
    m[make_pair(i, i / 2)] = m[make_pair(i - 2, i / 2 - 1)] + A[N - i];
    m[make_pair(i, i / 2)] = max(m[make_pair(i, i / 2)],
                                 m[make_pair(i - 3, i / 2 - 1)] + A[N - i + 1]);
    m[make_pair(i, i / 2)] = max(m[make_pair(i, i / 2)],
                                 m[make_pair(i - 4, i / 2 - 1)] + A[N - i + 2]);
  }

  cout << m[make_pair(N, N / 2)] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int SIZE = 1e5 + 1;
struct query {
  int N, N2;
  long long sum;
  query(int N, int N2, long long sum) : N(N), N2(N2), sum(sum) {}
};

long long B[SIZE];
int main() {
  int N;
  cin >> N;
  long long ans = 0, A[SIZE];
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  long long B_sum = 0;
  for (int i = 1; i <= N; i += 2) {
    B_sum += A[i];
    B[i] = B_sum;
  }

  queue<query> s;
  s.push(query(N, N / 2, 0));

  while (!s.empty()) {
    query q = s.front();
    s.pop();
    if (q.N == 0)
      continue;
    if (q.N2 == 1) {
      long long Max = -1e9 - 1;
      for (int i = 1; i <= q.N; i++) {
        Max = max(Max, A[i]);
      }
      ans = max(ans, q.sum + Max);
    } else if (q.N % 2 == 0) {
      ans = max(ans, q.sum + B[q.N - 1]);
      q.N2--;
      q.sum += A[q.N];
      q.N -= 2;
      s.push(q);
    } else {
      ans = max(ans, q.sum + B[q.N - 2]);
      query q0 = query(q.N - 2, q.N2 - 1, q.sum + A[q.N]);
      query q1 = query(q.N - 3, q.N2 - 1, q.sum + A[q.N - 1]);
      s.push(q0);
      s.push(q1);
    }
  }
  cout << ans << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[200010], n;
map<int, ll> mem[200010];
ll dp(int x, int nm) {
  if (nm == 1) {
    if (x >= n)
      return -1e17;
    return a[x];
  }
  if (n - x < (nm - 1) * 2 + 1) {
    return -1e17;
  }
  if (mem[x].count(nm)) {
    return mem[x][nm];
  }
  ll res = a[x];
  res = a[x] + dp(x + 2, nm - 1);
  // if((n-x-2)>2*(nm-1)){
  res = max(res, a[x] + dp(x + 3, nm - 1));
  // }
  mem[x][nm] = res;
  return res;
}
int main() {
  while (cin >> n) {
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      mem[i].clear();
      scanf("%d", a + i);
    }
    ans = max(dp(0, n / 2), dp(1, n / 2));
    ans = max(ans, dp(2, n / 2));
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[100010], n;
map<int, ll> mem[100010];
ll dp(int x, int nm) {
  if (nm == 1) {
    if (x >= n)
      return -1e17;
    return a[x];
  }
  if (n - x < (nm - 1) * 2 + 1) {
    return -1e17;
  }
  if (mem[x].count(nm)) {
    return mem[x][nm];
  }
  ll res = a[x];
  res = a[x] + dp(x + 2, nm - 1);
  // if((n-x-2)>2*(nm-1)){
  res = max(res, a[x] + dp(x + 3, nm - 1));
  // }
  mem[x][nm] = res;
  return res;
}
int main() {
  while (cin >> n) {
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      mem[i].clear();
      scanf("%d", a + i);
    }
    ans = max(dp(0, n / 2), dp(1, n / 2));
    ans = max(ans, dp(2, n / 2));
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define enld '\\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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;
}
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int k = 1 + N % 2;
  vector<vector<ll>> dp(N + 1, vector<ll>(4, -INF));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < k + 1; j++) {
      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);
    }
  }
  cout << dp[N][k] << enld;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define enld '\\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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;
}
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int k = 1 + N % 2;
  vector<vector<ll>> dp(N + 1, vector<ll>(3, -INF));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < k + 1; j++) {
      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);
    }
  }
  cout << dp[N][k] << enld;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
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;
}
#define print(x) cerr << #x << " is " << x << endl;
// #define print(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << "
// " << __FILE__ << endl;

ll N;
vector<ll> A(2e5 + 1), S(2e5 + 1);
map<i_i, ll> dp;
ll solve(int i, int k) {

  if (N - i == k * 2 - 1) {
    return S[i];
  } else if (i > N - 1 || N - i < k * 2 - 1) {
    return -1e15;
  }
  if (dp[make_pair(i, k)] != 0)
    return dp[make_pair(i, k)];
  ll ans = A[i] + solve(i + 2, k - 1);
  chmax(ans, A[i] + solve(i + 3, k - 1));
  chmax(ans, A[i] + solve(i + 4, k - 1));
  if (i + 1 < N) {
    chmax(ans, A[i + 1] + solve(i + 3, k - 1));
    chmax(ans, A[i + 1] + solve(i + 4, k - 1));
    if (i + 2 < N) {
      chmax(ans, A[i + 2] + solve(i + 4, k - 1));
    }
  }
  return dp[make_pair(i, k)] = ans;
}

int main() {
  cin >> N;

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

  S[N - 1] = A[N - 1];
  for (int i = N - 3; i >= 0; i -= 2) {
    S[i] = S[i + 2] + A[i];
  }

  ll ans = solve(0, N / 2);
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
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;
}
#define print(x) cerr << #x << " is " << x << endl;
// #define print(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << "
// " << __FILE__ << endl;

ll N;
vector<ll> A(2e5 + 1), S(2e5 + 1);

ll solve(int i, int k) {

  if (N - i == k * 2 - 1) {
    return S[i];
  } else if (i > N - 1 || N - i < k * 2 - 1) {
    return -1e15;
  }
  ll ans = A[i] + solve(i + 2, k - 1);
  chmax(ans, A[i] + solve(i + 3, k - 1));
  chmax(ans, A[i] + solve(i + 4, k - 1));
  if (i + 1 < N) {
    chmax(ans, A[i + 1] + solve(i + 3, k - 1));
    chmax(ans, A[i + 1] + solve(i + 4, k - 1));
    if (i + 2 < N) {
      chmax(ans, A[i + 2] + solve(i + 4, k - 1));
    }
  }
  return ans;
}

int main() {
  cin >> N;

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

  S[N - 1] = A[N - 1];
  for (int i = N - 3; i >= 0; i -= 2) {
    S[i] = S[i + 2] + A[i];
  }

  ll ans = solve(0, N / 2);
  cout << ans;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define for(i, a, b) for (ll i = a; i < b; i++)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll n;
  cin >> n;
  vl a(n);
  for (i, 0, n) {
    cin >> a[i];
  }
  vvl dp(n, vl(3));
  for (i, 0, n) {
    for (j, 0, 3) {
      dp[i][j] = -INF;
    }
  }
  dp[0][0] = a[0];
  dp[1][1] = a[1];
  if (n >= 3) {
    dp[2][2] = a[2];
    if (n >= 4) {
      dp[3][1] = max(dp[0][0], dp[1][1]) + a[3];
    }
    dp[2][0] = a[0] + a[2];
  }
  if (n > 4) {
    for (i, 4, n) {
      if (i % 2 == 0) {
        dp[i][0] = dp[i - 2][0] + a[i];
      }
      dp[i][1] = max(dp[i - 2][1] + a[i], a[i] + dp[i - 3][0]);
      dp[i][2] =
          max(dp[i - 2][2] + a[i], max(dp[i - 3][1], dp[i - 4][0]) + a[i]);
    }
  }
  if (n % 2 == 0)
    cout << max(dp[n - 2][0], dp[n - 1][1]) << endl;
  else
    cout << max(max(dp[n - 1][2], dp[n - 2][1]), dp[n - 3][0]) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define for(i, a, b) for (ll i = a; i < b; i++)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll n;
  cin >> n;
  vl a(n);
  for (i, 0, n) {
    cin >> a[i];
  }
  vvl dp(n, vl(3));
  for (i, 0, n) {
    for (j, 0, 2) {
      dp[i][j] = 0;
    }
  }
  dp[0][0] = a[0];
  dp[1][1] = a[1];
  dp[2][2] = a[2];
  dp[3][1] = max(dp[0][0], dp[1][1]) + a[3];
  dp[2][0] = a[0] + a[2];
  for (i, 4, n) {
    if (i % 2 == 0) {
      dp[i][0] = dp[i - 2][0] + a[i];
    }
    dp[i][1] = max(dp[i - 2][1] + a[i], a[i] + dp[i - 3][0]);
    dp[i][2] = max(dp[i - 2][2] + a[i], max(dp[i - 3][1], dp[i - 4][0]) + a[i]);
  }
  if (n % 2 == 0) {
    cout << max(dp[n - 2][0], dp[n - 1][1]) << endl;
  } else
    cout << max(max(dp[n - 1][2], dp[n - 2][1]), dp[n - 3][0]) << endl;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

int v[200005];
bool mark[200005][5][2];
ll dp[200005][5][2];
int n, T;

ll f(int i, int j, bool has) {
  if (i == n)
    return j > 1 ? -1e18 : 0;
  if (mark[i][j][has])
    return dp[i][j][has];
  mark[i][j][has] = true;

  ll ret = -1e18;

  if (has) {
    ret = max(ret, f(i + 1, j, false) + v[i]);
    if (j)
      ret = max(ret, f(i + 1, j - 1, true));
  } else
    ret = max(ret, f(i + 1, j, true));

  return dp[i][j][has] = ret;
}

int main() {
  scanf("%d", &n);

  ll ans = 0;
  for (int i = 0; i < n; i++)
    scanf("%d", &v[i]);

  int space = (n % 2 == 0) ? 1 : 2;
  printf("%lld\\n", f(0, space, true));

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

int v[100005];
bool mark[100005][5][2];
ll dp[100005][5][2];
int n, T;

ll f(int i, int j, bool has) {
  if (i == n)
    return 0;
  if (mark[i][j][has])
    return dp[i][j][has];
  mark[i][j][has] = true;

  ll ret = -1e18;

  if (has) {
    ret = max(ret, f(i + 1, j, false) + v[i]);
    if (j)
      ret = max(ret, f(i + 1, j - 1, true));
  } else
    ret = max(ret, f(i + 1, j, true));

  return dp[i][j][has] = ret;
}

int main() {
  scanf("%d", &n);

  ll ans = 0;
  for (int i = 0; i < n; i++)
    scanf("%d", &v[i]);

  int space = (n % 2 == 0) ? 1 : 2;
  printf("%lld\\n", f(0, space, true));

  return 0;
}