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

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

long long solve(size_t n) {
  vector<long long> A(n + 1, 0);
  for (size_t i = 1; i <= n; i++) {
    cin >> A[i];
  }

  vector<vector<long long>> dp(n + 1, vector<long long>(3, -1e15));
  dp[0][0] = 0;
  for (size_t i = 1; i <= n; i++) {
    if (i % 2 == 0) {
      {
        // j = 0
        dp[i][0] = max(dp[i][0], dp[i - 1][0]);
      }
      {
        // j = 1
        dp[i][1] = max(dp[i][1], dp[i - 1][1] + A[i]);
        dp[i][2] = max(dp[i][2], dp[i - 1][1]);
      }
      {
        // j = 2
        dp[i][2] = max(dp[i][2], dp[i - 1][2]);
      }
    } else {
      {
        // j = 0
        dp[i][0] = max(dp[i][0], dp[i - 1][0] + A[i]);
        dp[i][1] = max(dp[i][1], dp[i - 1][0]);
      }
      {
        // j = 1
        dp[i][1] = max(dp[i][1], dp[i - 1][1]);
      }
      {
        // j = 2
        dp[i][2] = max(dp[i][2], dp[i - 1][2] + A[i]);
      }
    }
  }
  if (n % 2 == 0) {
    return max(dp[n][0], dp[n][1]);
  } else {
    return max(dp[n][1], dp[n][2]);
  }
}

int main() {
  size_t N;
  cin >> N;

  cout << solve(N) << endl;
}


Error: 0

Fixed submission:

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

long long solve_even(size_t n) {
  vector<long long> ans(2, 0);
  long long A;
  for (size_t i = 0; i < n; i++) {
    cin >> A;
    ans[i % 2] += A;
  }
  return max(ans[0], ans[1]);
}
long long solve_odd(size_t n) {
  vector<long long> A(n + 1, 0);
  for (size_t i = 1; i <= n; i++) {
    cin >> A[i];
  }

  vector<vector<long long>> dp(
      n + 1, vector<long long>((n / 2) + 1 + 5, LONG_LONG_MIN));
  vector<vector<long long>> ep(
      n + 1, vector<long long>((n / 2) + 1 + 5, LONG_LONG_MIN));
  queue<pair<size_t, size_t>> q;
  q.push(make_pair(0, 0));
  dp[0][0] = 0;
  ep[0][0] = 0;
  for (size_t i = 1; i <= n; i++) {
    queue<pair<size_t, size_t>> tmp;
    while (!q.empty()) {
      size_t current_row = q.front().first;
      size_t used = q.front().second;
      q.pop();
      if (current_row == 0) {
        if (i % 2 == 0) {
          if (dp[i][used] < dp[i - 1][used]) {
            dp[i][used] = dp[i - 1][used];
            tmp.push(make_pair(0, used));
          }
        } else {
          if (dp[i][used + 1] < dp[i - 1][used] + A[i]) {
            dp[i][used + 1] = dp[i - 1][used] + A[i];
            tmp.push(make_pair(0, used + 1));
          }

          if (ep[i][used] < dp[i - 1][used]) {
            ep[i][used] = dp[i - 1][used];
            tmp.push(make_pair(1, used));
          }
        }
      } else {
        if (i % 2 == 0) {
          if (ep[i][used + 1] < ep[i - 1][used] + A[i]) {
            ep[i][used + 1] = ep[i - 1][used] + A[i];
            tmp.push(make_pair(1, used + 1));
          }

          if (dp[i][used] < ep[i - 1][used]) {
            dp[i][used] = ep[i - 1][used];
            tmp.push(make_pair(0, used));
          }
        } else {
          if (ep[i][used] < ep[i - 1][used]) {
            ep[i][used] = ep[i - 1][used];
            tmp.push(make_pair(1, used));
          }
        }
      }
    }
    q = tmp;
  }
  return max(dp.back()[n / 2], ep.back()[n / 2]);
}

int main() {
  size_t N;
  cin >> N;

  if (N % 2 == 0) {
    cout << solve_even(N) << endl;
  } else {
    cout << solve_odd(N) << endl;
  }
}