Problem p02716 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for (int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for (int i = int(ini); i >= int(lim); i--)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

const int MAX = 2e5 + 15;
const ll INF = (ll)1e18;
ll arr[MAX];
ll cnt[MAX];
ll memo[MAX][5];
bool visited[MAX][5];
int n;
int max_jumps;

ll roll(int idx, int jumped) {
  if ((n & 1) && idx == n && jumped == 0) {
    return 0;
  }
  if (idx > n) {
    return 0;
  }
  ll &ans = memo[idx][jumped];
  if (visited[idx][jumped]) {
    return ans;
  }
  visited[idx][jumped] = true;
  ans = roll(idx + 2, jumped) + arr[idx];
  if (jumped < max_jumps) {
    ans = max(ans, roll(idx + 1, jumped + 1));
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  fori(i, 1, n + 1) cin >> arr[i];
  arr[n + 1] = -INF;
  ford(i, n, 1) { cnt[i] = max(arr[i] + cnt[i + 2], arr[i + 1] + cnt[i + 3]); }
  memset(memo, -1, sizeof memo);
  if (n % 2 == 0) {
    max_jumps = 1;
    cout << roll(1, 0) << endl;
  } else {
    max_jumps = 2;
    cout << roll(1, 0) << endl;
  }
  return 0;
}


Error: -6
805af816-51fb-4840-83e6-c1f4c02cb536.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02716/C++/s506447994.cpp:74: int main(): Assertion `false' failed.


Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for (int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for (int i = int(ini); i >= int(lim); i--)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

const int MAX = 2e5 + 15;
const ll INF = (ll)1e18;
ll arr[MAX];
ll cnt[MAX];
ll memo[MAX][5];
bool visited[MAX][5];
int n;

ll roll(int idx, int jumped) {
  if (idx == n && jumped == 0) {
    return 0;
  }
  if (idx > n) {
    return 0;
  }
  ll &ans = memo[idx][jumped];
  if (visited[idx][jumped]) {
    return ans;
  }
  visited[idx][jumped] = true;
  ans = roll(idx + 2, jumped) + arr[idx];
  if (jumped < 2) {
    ans = max(ans, roll(idx + 1, jumped + 1));
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  fori(i, 1, n + 1) cin >> arr[i];
  arr[n + 1] = -INF;
  ford(i, n, 1) { cnt[i] = max(arr[i] + cnt[i + 2], arr[i + 1] + cnt[i + 3]); }
  if (n % 2 == 0) {
    cout << max(cnt[1], cnt[2]) << endl;
    assert(false);
  } else {
    memset(memo, -1, sizeof memo);
    cout << roll(1, 0) << endl;
  }
  return 0;
}