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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

int N, D[101], A[101];
map<vector<int>, int> encd;
map<int, vector<int>> dcd;

int dp
    [101]
    [5041]; // dp[x][y]:
            // encd[x][0],encd[x][1],...,encd[x][6]???????????§?£?????????¨???????????§???????????????(encd????????????i?????????????°??±???????id)
int taste[7];
int res, ans;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> D[i];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int K = min(N, 7);
  int cnt = 0;
  vector<int> v(K);
  for (int i = 0; i < K; i++)
    v[i] = i;
  do {
    encd[v] = cnt;
    dcd[cnt] = v;
    res = 0;
    for (int i = 0; i < K; i++)
      taste[i] = 0;
    for (int i = 0; i < K; i++) {
      int eat = K - 1 - v[i];
      res += taste[eat];
      for (int j = eat - D[eat]; j <= eat + D[eat]; j++) {
        if (0 <= j && j < K)
          taste[j] += A[eat];
      }
    }
    dp[K - 1][cnt] = res;
    cnt++;
  } while (next_permutation(v.begin(), v.end()));

  if (N <= 7) {
    for (int i = 0; i < cnt; i++) {
      ans = max(ans, dp[N - 1][i]);
    }
    cout << ans << endl;
    return 0;
  }
  // cnt=7!
  vector<int> V(7);
  for (int i = 6; i < N - 1; i++) { //??????dp
    for (int s = 0; s < cnt; s++) { //
      vector<int> G = dcd[s];       // G[??????]=i-(?°??±???????id)
      int V_SIZE;
      for (int nx = 0; nx < 8;
           nx++) { // i+1????°??±???????
                   // ?????´??´????????????nx??????????£???????
        V_SIZE = 0;
        res = 0;
        for (int now = 0; now < 7; now++) {
          int eat = G[now];
          if (now == nx) {
            V[V_SIZE] = 0;
            V_SIZE++;
          }
          if (now >= nx) { // i+1??????????????¨
            // i+1???????????????????????????
            if (D[i + 1] >= eat + 1)
              res += A[i + 1];
          } else { // i+1????????????
            // i-eat???????????????(i+1??????????????????)????????????
            if (D[i - eat] >= eat + 1)
              res += A[i - eat];
          }
          if (eat < 6) {
            V[V_SIZE] = eat + 1;
            V_SIZE++;
          }
        }
        if (nx == 7) {
          V[V_SIZE] = 0;
          V_SIZE++;
        }
        int news = encd[V];
        dp[i + 1][news] = max(dp[i + 1][news], dp[i][s] + res);
      }
    }
  }
  ans = 0;
  for (int i = 0; i < cnt; i++) {
    ans = max(dp[N - 1][i], ans);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll N, D[101], A[101];
map<vector<ll>, ll> encd;
map<ll, vector<ll>> dcd;

ll dp[100][5041];
ll taste[7];
ll res, ans;
int main() {
  cin >> N;
  rep(i, N) cin >> D[i];
  rep(i, N) cin >> A[i];
  ll K = min(N, 7);
  memset(dp, 0, sizeof(dp));
  ll cnt = 0;
  vector<ll> v(K);
  rep(i, K) v[i] = i;
  do {
    encd[v] = cnt;
    dcd[cnt] = v;
    res = 0;
    memset(taste, 0, sizeof(taste));
    rep(i, K) {
      ll eat = K - 1 - v[i];
      res += taste[eat];
      for (ll j = eat - D[eat]; j <= eat + D[eat]; j++) {
        if (0 <= j && j < K)
          taste[j] += A[eat];
      }
    }
    dp[K - 1][cnt] = res;
    cnt++;
  } while (next_permutation(v.begin(), v.end()));
  if (N <= 7) {
    rep(i, cnt) { ans = max(ans, dp[N - 1][i]); }
    cout << ans << endl;
    return 0;
  }
  vector<ll> V(7);
  for (ll i = 6; i < N - 1; i++) {
    for (ll s = 0; s < cnt; s++) { //?????????
      vector<ll> G = dcd[s];

      ll yoso = 0;
      for (ll nx = 0; nx < 8; nx++) { // i+1?????????nx??????
        res = 0;
        for (ll now = 0; now < 7; now++) {
          ll eat = G[now];
          if (now == nx) {
            V[yoso] = 0;
            yoso++;
          }
          if (now >= nx) {
            // i+1??????????????????
            if (D[i + 1] >= eat + 1)
              res += A[i + 1];
          } else {
            //?????£???????????????
            if (D[i - eat] >= eat + 1)
              res += A[i - eat];
          }
          if (eat < 6) {
            V[yoso] = eat + 1;
            yoso++;
          }
        }
        if (nx == 7) {
          V[yoso] = 0;
          yoso++;
        }
        ll news = encd[V];
        dp[i + 1][news] = max(dp[i + 1][news], dp[i][s] + res);
      }
    }
  }
  ans = 0;
  rep(i, cnt) { ans = max(dp[N - 1][i], ans); }
  cout << ans << endl;
  return 0;
}