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

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

long long a[200010];
long long dp[200010];
long long presum[200010];
int n;
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  presum[1] = a[1];
  for (int i = 3; i <= n; i += 2)
    presum[i] = presum[i - 2] + a[i];
  for (int i = 2; i <= n; i++) {
    if (i & 1)
      dp[i] = max(dp[i - 2] + a[i], dp[i - 1]);
    else
      dp[i] = max(dp[i - 2] + a[i], presum[i - 1]);
  }
  cout << dp[n] << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int n;
int a[200005];
long long ans = -1e18;
void dfs(int cur, int num, long long sum) {

  if (num == n >> 1) {
    ans = max(ans, sum);
    return;
  }
  for (int i = cur; i <= n; i++) {
    dfs(i + 2, num + 1, sum + a[i]);
  }
  return;
}
// 5
//-1000 -100 -10 0 10
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  long long temp = 0;
  if (n % 2 == 0) {
    for (int i = 1; i <= n; i++)
      temp += a[2 * i];
    ans = max(ans, temp);
    dfs(1, 0, 0);
  } else {
    for (int i = 1; i + 2 <= n; i++)
      temp += a[i + 2];
    ans = max(ans, temp);
    dfs(1, 0, 0);
    dfs(2, 0, 0);
  }
  cout << ans << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <limits>
#include <numeric>
#include <vector>

std::vector<std::int64_t> a;
std::vector<std::vector<std::int64_t>> memo;
const auto nil = std::numeric_limits<std::int64_t>::min();

std::int64_t solve(int n, int k) {
  if (memo[n][k - n / 2] != nil) {
    return memo[n][k - n / 2];
  }
  std::int64_t r;
  {
    if (n == 0 || k == 0) {
      r = 0;
    } else if (n == 1) {
      r = a[0];
    } else if (n % 2 == 1 && k == (n / 2 + 1)) {
      r = solve(n - 2, k - 1) + a[n - 1];
    } else {
      auto r1 = solve(n - 2, k - 1) + a[n - 1];
      auto r2 = solve(n - 1, k);
      r = std::max(r1, r2);
    }
  }

  memo[n][k - n / 2] = r;
  return r;
}

int main() {
  int n;

  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    int tmp;
    std::cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0; i < n + 2; ++i) {
    memo.push_back(std::vector<std::int64_t>(3, nil));
  }

  std::cout << solve(n, n / 2);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits>
#include <numeric>
#include <vector>

std::vector<std::int64_t> a;
std::vector<std::vector<std::int64_t>> memo;
const auto nil = std::numeric_limits<std::int64_t>::min();

std::int64_t solve(int n, int k) {
  if (memo[n][k] != nil) {
    return memo[n][k];
  }
  std::int64_t r;
  {
    if (n == 0 || k == 0) {
      r = 0;
    } else if (n == 1) {
      r = a[0];
    } else if (n % 2 == 1 && k == (n / 2 + 1)) {
      r = solve(n - 2, k - 1) + a[n - 1];
    } else {
      auto r1 = solve(n - 2, k - 1) + a[n - 1];
      auto r2 = solve(n - 1, k);
      r = std::max(r1, r2);
    }
  }

  memo[n][k] = r;
  return r;
}

int main() {
  int n;

  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    int tmp;
    std::cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0; i < n + 2; ++i) {
    memo.push_back(std::vector<std::int64_t>(n / 2 + 2, nil));
  }

  std::cout << solve(n, n / 2);
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 10;
#define minf -10000000000000000
ll a[N];
ll ans = 0;
ll n;
map<pair<ll, ll>, ll> mp, ex;

ll cal(ll taken, ll i) {
  if (i > n - 1) {
    if (taken == n / 2)
      return 0;
    else
      return minf;
  }
  if (taken == n / 2)
    return 0;

  if ((n - i + 1) / 2 + taken < n / 2) {
    return minf;
  }

  if (ex[{i, taken}] == 1)
    return mp[{i, taken}];
  ll ans;
  ans = max(cal(taken, i + 1), a[i] + cal(taken + 1, i + 2));
  mp[{i, taken}] = ans;
  ex[{i, taken}] = 1;
  return ans;
}

int main() {

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  cout << cal(0ll, 0ll) << endl;
  //    for(auto it:mp)
  //	{
  //	    cout<<"key "<<it.first.first<<" "<<it.first.second<<endl;
  //	    cout<<"value ";
  //		for(auto it1:it.second)
  //		  cout<<it1<<" ";
  //		  cout<<endl;
  //	}
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 10;
#define minf -10000000000000000
ll a[N];
ll ans = 0;
ll n;

ll cal(ll taken, ll i, ll sm) {
  if (i > n - 1) {
    if (taken == n / 2)
      return sm;
    else
      return minf;
  }
  if (taken == n / 2)
    return minf;

  if ((n - i + 1) / 2 + taken < n / 2) {
    return minf;
  }

  ll ans;

  ans = max(cal(taken, i + 1, sm), cal(taken + 1, i + 2, sm + a[i]));

  return ans;
}

int main() {

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << cal(0ll, 0ll, 0ll);
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define fi first
#define sd second
#define lson (nd << 1)
#define rson (nd + nd + 1)
#define PB push_back
#define mid (l + r >> 1)
#define MP make_pair
#define SZ(x) (int)x.size()

using namespace std;

typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;

inline LL read() {
  LL res = 0, f = 1;
  char ch = getchar();
  while (ch<'0' | ch> '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res * f;
}

const int MAXN = 200'005;

const int MOD = 1000000007;

const LL inf = (LL)1e18;

void addmod(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
int mulmod(int a, int b) { return 1ll * a * b % MOD; }

template <typename T> void chmin(T a, T b) {
  if (a > b)
    a = b;
}

template <typename T> void chmax(T a, T b) {
  if (b > a)
    a = b;
}

LL a[MAXN], n;
map<int, long long> H[MAXN];

LL dfs(int idx, int cnt) {
  if (idx >= n + 1) {
    if (cnt == n / 2)
      return 0;
    else
      return -inf;
  }

  if (H[idx].count(cnt))
    return H[idx][cnt];

  if ((cnt + (n - idx + 2) / 2) < n / 2)
    return -inf;

  return H[idx][cnt] = max(dfs(idx + 1, cnt), dfs(idx + 2, cnt + 1) + a[idx]);
}

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();

  cout << dfs(1, 0);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define fi first
#define sd second
#define lson (nd << 1)
#define rson (nd + nd + 1)
#define PB push_back
#define mid (l + r >> 1)
#define MP make_pair
#define SZ(x) (int)x.size()

using namespace std;

typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;

inline LL read() {
  LL res = 0, f = 1;
  char ch = getchar();
  while (ch<'0' | ch> '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res * f;
}

const int MAXN = 200'005;

const int MOD = 1000000007;

const LL inf = (LL)1e18;

void addmod(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
int mulmod(int a, int b) { return 1ll * a * b % MOD; }

template <typename T> void chmin(T a, T b) {
  if (a > b)
    a = b;
}

template <typename T> void chmax(T a, T b) {
  if (b > a)
    a = b;
}

LL a[MAXN], n;
map<int, long long> H[MAXN];

LL dfs(int idx, int cnt) {
  if (idx >= n + 1) {
    if (cnt == n / 2)
      return 0;
    else
      return -inf;
  }

  if (H[idx].count(cnt))
    return H[idx][cnt];

  if (idx - cnt - 1 > (n + 1) / 2)
    return -inf;

  return H[idx][cnt] = max(dfs(idx + 1, cnt), dfs(idx + 2, cnt + 1) + a[idx]);
}

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();

  cout << dfs(1, 0);

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX_N = 2e5 + 5;

int n;
int a[MAX_N];
map<int, ll> dp[MAX_N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  dp[0][0] = 0;
  dp[1][1] = a[1];
  for (int i = 1; i <= n; ++i) {
    for (int k = max(0, i / 2 - 5); k <= min(n / 2, i / 2 + 5); ++k) {
      if (dp[i - 1].count(k)) {
        dp[i][k] = dp[i - 1][k];
      }
      for (int p = max(0, i - 4); p < i - 1; ++p) {
        if (dp[p].count(k - 1)) {
          if (dp[i].count(k)) {
            dp[i][k] = max(dp[i][k], dp[p][k - 1] + a[i]);
          } else {
            dp[i][k] = dp[p][k - 1] + a[i];
          }
        }
      }
    }
  }
  cout << dp[n][n / 2] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int MAX_N = 2e5 + 5;
const ll INF = 1e18;

int n, a[MAX_N];
ll dp[MAX_N][4];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  if (n % 2 == 0) {
    ll sum0 = 0, sum1 = 0;
    for (int i = 1; i <= n; ++i) {
      if (i % 2 == 0)
        sum0 += a[i];
      else
        sum1 += a[i];
    }
    cout << max(sum0, sum1) << "\\n";
    return 0;
  }
  ll ans = -INF;
  for (int s = 1; s <= 5; ++s) {
    for (int i = 1; i <= 2 * n; ++i) {
      for (int j = 0; j < 3; ++j) {
        dp[i][j] = -INF;
      }
    }
    dp[s][0] = a[s];
    for (int i = s; i <= n; ++i) {
      if (i >= 2)
        dp[i][0] = dp[i - 2][0] + a[i];
      if (i >= 3)
        dp[i][1] = max(dp[i - 2][1], dp[i - 3][0]) + a[i];
      if (i >= 3)
        dp[i][2] = max(dp[i - 2][2], dp[i - 3][1]) + a[i];
      dp[i][1] = max(dp[i][1], dp[i][0]);
      dp[i][2] = max(dp[i][2], dp[i][1]);
    }
    ans = max(ans, dp[s + n - 3][0]);
    ans = max(ans, dp[s + n - 2][1]);
    ans = max(ans, dp[s + n - 1][2]);
  }
  reverse(a + 1, a + n + 1);
  for (int s = 1; s <= 5; ++s) {
    for (int i = 0; i <= 2 * n; ++i) {
      for (int j = 0; j < 3; ++j) {
        dp[i][j] = -INF;
      }
    }
    dp[s][0] = a[s];
    for (int i = s; i <= n; ++i) {
      if (i >= 2)
        dp[i][0] = dp[i - 2][0] + a[i];
      if (i >= 3)
        dp[i][1] = max(dp[i - 2][1], dp[i - 3][0]) + a[i];
      if (i >= 3)
        dp[i][2] = max(dp[i - 2][2], dp[i - 3][1]) + a[i];
      dp[i][1] = max(dp[i][1], dp[i][0]);
      dp[i][2] = max(dp[i][2], dp[i][1]);
    }
    ans = max(ans, dp[s + n - 3][0]);
    ans = max(ans, dp[s + n - 2][1]);
    ans = max(ans, dp[s + n - 1][2]);
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using large = long long;
constexpr large fail = LLONG_MIN / 2;
// divide by 2 to make space for adding negative values

//       pos   skip  used
large dp[200'010][3][2];

template <class T, size_t N, class V> void fill_(T (&a)[N], V v) {
  for (auto &elem : a)
    elem = v;
}

template <class T, size_t N, size_t M, class V>
void fill_(T (&md)[N][M], V val) {
  for (auto &row : md)
    fill_(row, val);
}

int main() {
  int n;
  cin >> n;
  auto a = vector<int>(n);
  for (auto &a_i : a)
    cin >> a_i;

  int cnt = n / 2;
  int max_skips = n - (2 * cnt - 1);
  // cerr << "n:" << n << " max_skips:" << max_skips << "\\n";

  fill_(dp, fail);
  dp[0][0][0] = 0;
  dp[0][1][0] = 0;
  dp[0][2][0] = 0;

  for (int i = 0; i < n; ++i) {
    // use i'th
    dp[i + 1][0][1] = dp[i][0][0] + a[i];
    dp[i + 1][1][1] = dp[i][1][0] + a[i];
    dp[i + 1][2][1] = dp[i][2][0] + a[i];

    // don't use i'th
    dp[i + 1][0][0] = dp[i][0][1];
    dp[i + 1][1][0] = max(dp[i][1][1], dp[i][0][0]);
    dp[i + 1][2][0] = max(dp[i][2][1], dp[i][1][0]);
  }

  auto ans = max(dp[n][max_skips][0], dp[n][max_skips][1]);
#if 0
    for (int sk = 0; sk < max_skips; ++sk) {
        auto pos = n - (max_skips - sk);
        ans = max({ans, dp[pos][sk][0], dp[pos][sk][1]});
    }
#endif
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using large = long long;
constexpr large fail = LLONG_MIN / 2;

auto sum_at_len = vector<large>();

static large solve(const int *const a, const int len, const int cnt) {
  int least = 2 * cnt - 1;
  if (len < least)
    return fail;

  if (cnt == 0)
    return 0;

  if (len == least) {
    auto &s = sum_at_len[len];
    if (s == fail) {
      large sum = 0LL;
      for (int i = 0; i < cnt; ++i)
        sum += a[2 * i];
      s = sum;
    }
    return s;
  }

  large case1 = solve(a + 2, len - 2, cnt - 1);
  if (case1 != fail)
    case1 += a[0];

  large case2 = solve(a + 1, len - 1, cnt);
  return max(case1, case2);
}

int main() {
  int n;
  cin >> n;
  auto a = vector<int>(n);
  for (auto &a_i : a)
    cin >> a_i;

  sum_at_len.assign(n + 10, fail);

  cout << solve(a.data(), n, n / 2);
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long dp[200010][3];
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  dp[0][0] = A[0];
  dp[1][1] = A[1];
  for (int i = 2; i < n; ++i) {
    dp[i][0] = dp[i - 2][0] + A[i];
    dp[i][1] = dp[i - 2][1] + A[i];
    dp[i][2] = dp[i - 2][2] + A[i];
    if (i > 2) {
      dp[i][1] = max(dp[i][1], dp[i - 3][0] + A[i]);
      dp[i][2] = max(dp[i][2], dp[i - 3][1] + A[i]);
    }
    if (i > 3) {
      dp[i][2] = max(dp[i][2], dp[i - 4][0] + A[i]);
    }
  }
  long long ans = max(dp[n - 1][1 + n % 2], dp[n - 2][0 + n % 2]);
  if (n > 2 && n % 2 == 1)
    ans = max(ans, dp[n - 3][0]);
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5, INF = 0xfeeddeadbeef, inf = INF >> 1;
int n, a[N];
map<int, int> dp[N];
int DP(int i, int k) {
  if (k > (i + 1 >> 1))
    return -inf;
  if (i <= 0)
    return 0;
  /// if(dp[i][k]) return dp[i][k];
  return dp[i][k] = max(DP(i - 2, k - 1) + a[i], DP(i - 1, k));
}
signed main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  cout << DP(n, n >> 1) << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long dp[200010][3];
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  dp[0][0] = A[0];
  dp[1][1] = A[1];
  for (int i = 2; i < n; ++i) {
    dp[i][0] = dp[i - 2][0] + A[i];
    dp[i][1] = dp[i - 2][1] + A[i];
    dp[i][2] = dp[i - 2][2] + A[i];
    if (i > 2) {
      dp[i][1] = max(dp[i][1], dp[i - 3][0] + A[i]);
      dp[i][2] = max(dp[i][2], dp[i - 3][1] + A[i]);
    }
    if (i > 3) {
      dp[i][2] = max(dp[i][2], dp[i - 4][0] + A[i]);
    }
  }
  long long ans = max(dp[n - 1][1 + n % 2], dp[n - 2][0 + n % 2]);
  if (n & 1)
    ans = max(ans, dp[n - 3][0]);
  printf("%lld\\n", ans);
  return 0;
}


Error: -6
8615254a-7f8e-4949-a77b-77b6b4723121.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02716/C++/s852636947.cpp:15: int main(): Assertion `n&1' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5, INF = 0xfeeddeadbeef, inf = INF >> 1;
int n, a[N];
map<int, int> dp[N];
int DP(int i, int k) {
  if (k > (i + 1 >> 1))
    return -inf;
  if (i <= 0)
    return 0;
  if (dp[i][k])
    return dp[i][k];
  return dp[i][k] = max(DP(i - 2, k - 1) + a[i], DP(i - 1, k));
}
signed main() {
  cin >> n;
  assert(n & 1);
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  cout << DP(n, n >> 1) << endl;
  return 0;
}
Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 5;
ll a[N], prfx[N], dp[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  prfx[1] = a[1];
  for (ll i = 3; i <= n; i += 2)
    prfx[i] = prfx[i - 2] + a[i];
  for (ll i = 2; i <= n; i++) {
    if (i & 1)
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    else
      dp[i] = max(prfx[i - 1], a[i] + dp[i - 2]);
  }
  cout << dp[n];
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5 + 5, inf = 1e17;
ll a[N], prfx[N], dp[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  prfx[1] = a[1];
  for (ll i = 3; i <= n; i += 2)
    prfx[i] = prfx[i - 2] + a[i];
  for (ll i = 2; i <= n; i++) {
    if (i & 1)
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    else
      dp[i] = max(prfx[i - 1], a[i] + dp[i - 2]);
  }
  cout << dp[n];
}
Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
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;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

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

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

  for (int i = 0; i < 200010; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        dp[i][j][k] = -LINF;
      }
    }
  }
  dp[0][0][0] = 0;
  dp[0][1][0] = 0;

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


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;

int n;
vector<ll> a;

ll dfs(int index, int rest) {
  if ((n - index) == 2 * rest - 1) {
    ll res = 0;
    for (int i = index; i < n; i += 2) {
      res += a[i];
    }
    return res;
  }
  if (index + 2 > n)
    return dfs(index + 1, rest);
  return max(a[index] + dfs(index + 2, rest - 1), dfs(index + 1, rest));
}

int main() {
  cin >> n;
  a.assign(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  cout << dfs(0, n / 2) << endl;
}
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 INF = 1001001001001001;

ll dp[200001];
ll a[200001];
ll dpall[200001];
int n;

ll init() {
  rep(i, 200001) dp[i] = -INF;
  rep(i, 200001) dpall[i] = -INF;
  return 0;
}

ll rec(int x, int k) {
  // cout << "x:" << x << "k:" << k << endl;
  if (k * 2 > x)
    return dpall[x];
  if (k == 0)
    return ll(0);
  if (dp[x] > -INF)
    return dp[x];

  ll res = -INF;

  ll temp = 0;
  temp += a[x - 1];
  temp += rec(x - 2, k - 1);
  // cout << "x-2:k-1 " << x-2 << " : "  << k-1 << "  " << temp << endl;
  res = max(res, temp);

  temp = 0;
  temp += a[x - 2];
  temp += rec(x - 3, k - 1);
  /*
  cout << "x-3:k-1 " << x-3 << " : "  << k-1 << "  " << temp << endl;
  cout << a[x-2] << " " << rec(x-3,k-1) << endl;
  */
  res = max(res, temp);

  if (x % 2 == 0)
    return dp[x] = res;

  temp = 0;
  temp = dpall[x - 2];
  // cout << "x-2:k " << x-2 << " : "  << k << "  " << temp << endl;
  res = max(res, temp);

  return dp[x] = res;
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];

  init();
  for (int i = 1; i <= n / 2; ++i) {
    if (i == 1) {
      dpall[1] = a[0];
      continue;
    }
    dpall[2 * i - 1] = dpall[2 * i - 3] + a[2 * i - 2];
  }

  /*
  cout << "dpall" << endl;
  for(int i=1; i<=n; i+=2){
      cout <<"i" << i <<" : "<<dpall[i] << endl;
  }*/

  cout << rec(n, n / 2) << endl;
}


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 INF = 1001001001001001;

ll dp[200001];
ll a[200001];
ll dpall[200001];
int n;

ll init() {
  rep(i, 200001) dp[i] = -INF;
  rep(i, 200001) dpall[i] = -INF;
}

ll rec(int x, int k) {
  // cout << "x:" << x << "k:" << k << endl;
  if (k * 2 > x)
    return dpall[x];
  if (k == 0)
    return ll(0);
  if (dp[x] > -INF)
    return dp[x];

  ll res = -INF;

  ll temp = 0;
  temp += a[x - 1];
  temp += rec(x - 2, k - 1);
  // cout << "x-2:k-1 " << x-2 << " : "  << k-1 << "  " << temp << endl;
  res = max(res, temp);

  temp = 0;
  temp += a[x - 2];
  temp += rec(x - 3, k - 1);
  /*
  cout << "x-3:k-1 " << x-3 << " : "  << k-1 << "  " << temp << endl;
  cout << a[x-2] << " " << rec(x-3,k-1) << endl;
  */
  res = max(res, temp);

  if (x % 2 == 0)
    return dp[x] = res;

  temp = 0;
  temp = dpall[x - 2];
  // cout << "x-2:k " << x-2 << " : "  << k << "  " << temp << endl;
  res = max(res, temp);

  return dp[x] = res;
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];

  init();
  for (int i = 1; i <= n / 2; ++i) {
    if (i == 1) {
      dpall[1] = a[0];
      continue;
    }
    dpall[2 * i - 1] = dpall[2 * i - 3] + a[2 * i - 2];
  }

  /*
  cout << "dpall" << endl;
  for(int i=1; i<=n; i+=2){
      cout <<"i" << i <<" : "<<dpall[i] << endl;
  }*/

  cout << rec(n, n / 2) << endl;
}
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>;
#define chmax(x, y) x = max(x, y)

const ll INF = 1e18;

ll dp[200005][4];

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;
  return 0;
}

Error: 0

Fixed submission:

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

const ll INF = 1e18;

ll dp[200005][4];

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][i] = -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;
  return 0;
}