Problem p00520 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

#define SZ (8)
map<long long, int> dp[2];
int A[110];
int D[110];
int N;

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

  // cout << dfs(0,0) << endl;
  dp[0][0] = 0;
  int answer = -1;
  for (int pos = 0; pos <= N; pos++) {
    dp[(pos + 1) % 2].clear();
    for (map<long long, int>::iterator it = dp[pos % 2].begin();
         it != dp[pos % 2].end(); ++it) {
      long long slot_ = it->first;
      int cost = it->second;

      int sz = min(pos, SZ);
      if (pos >= N) {
        int sum = 0;
        for (int i = 0; i < sz; i++) {
          int id1 = slot_ >> (i * 7) & 127;
          for (int k = i + 1; k < sz; k++) {
            int id2 = slot_ >> (k * 7) & 127;
            if (id1 - D[id1] <= id2 && id2 <= id1 + D[id1]) {
              sum += A[id1];
            }
          }
        }
        answer = max(sum + cost, answer);
        continue;
      }
      int ans = 0;
      int sum = 0;
      // release
      if (pos >= SZ) {
        int m = pos - SZ;
        for (int i = 0; i < sz; i++) {
          int id1 = slot_ >> (i * 7) & 127;
          if (id1 == m) {
            for (int k = 0; k < i; k++) {
              int id2 = slot_ >> (k * 7) & 127;
              if (id2 - D[id2] <= id1) {
                sum += A[id2];
              }
            }
            for (int k = i + 1; k < sz; k++) {
              int id2 = slot_ >> (k * 7) & 127;
              if (id2 <= id1 + D[id1]) {
                sum += A[id1];
              }
            }
            long long mask1 = (1ll << (i * 7)) - 1;
            long long mask2 = ~((1ll << ((i + 1) * 7)) - 1);
            long long lower = slot_ & mask1;
            long long upper = slot_ & mask2;
            slot_ = lower + (upper >> 7);
            sz--;
            break;
          }
        }
      }
      // adding
      for (int i = 0; i <= sz; i++) {
        long long t = slot_;
        long long mask1 = (1ll << (i * 7)) - 1;
        long long mask2 = ~((1ll << (i * 7)) - 1);
        long long lower = slot_ & mask1;
        long long upper = slot_ & mask2;
        t = lower + (upper << 7) + (((1ll) * pos) << (i * 7));
        dp[(pos + 1) % 2][t] = max(dp[(pos + 1) % 2][t], cost + sum);
      }
    }
  }
  cout << answer << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

#define SZ (8)
map<long long, int> dp[110];
int A[110];
int D[110];
int N;

int view(long long oh, int sz) {
  for (int i = 0; i < sz; i++) {
    cout << (oh >> (i * 7) & 127) << " ";
  }
  cout << endl;
}
int dfs(int pos, long long slot_) {
  long long key = slot_;
  if (dp[pos].count(key)) {
    return dp[pos][key];
  }
  int sz = min(pos, SZ);
  if (pos >= N) {

    int sum = 0;

    for (int i = 0; i < sz; i++) {
      int id1 = slot_ >> (i * 7) & 127;
      for (int k = i + 1; k < sz; k++) {
        int id2 = slot_ >> (k * 7) & 127;
        if (id1 - D[id1] <= id2 && id2 <= id1 + D[id1]) {
          sum += A[id1];
        }
      }
    }
    return dp[pos][key] = sum;
  }
  int ans = 0;
  int sum = 0;
  int fl = 0;
  // release

  if (pos >= SZ) {
    int m = pos - SZ;
    for (int i = 0; i < sz; i++) {
      int id1 = slot_ >> (i * 7) & 127;
      if (id1 == m) {
        for (int k = 0; k < i; k++) {
          int id2 = slot_ >> (k * 7) & 127;
          if (id2 - D[id2] <= id1) {
            sum += A[id2];
          }
        }
        for (int k = i + 1; k < sz; k++) {
          int id2 = slot_ >> (k * 7) & 127;
          if (id2 <= id1 + D[id1]) {
            sum += A[id1];
          }
        }
        long long mask1 = (1ll << (i * 7)) - 1;
        long long mask2 = ~((1ll << ((i + 1) * 7)) - 1);
        long long lower = slot_ & mask1;
        long long upper = slot_ & mask2;
        slot_ = lower + (upper >> 7);
        fl = 1;
        break;
      }
    }
  }

  // adding

  for (int i = 0; i <= sz - fl; i++) {
    long long t = slot_;
    long long mask1 = (1ll << (i * 7)) - 1;
    long long mask2 = ~((1ll << (i * 7)) - 1);
    long long lower = slot_ & mask1;
    long long upper = slot_ & mask2;
    t = lower + (upper << 7) + (((1ll) * pos) << (i * 7));
    ans = max(ans, dfs(pos + 1, t));
  }
  return dp[pos][key] = ans + sum;
}

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

  cout << dfs(0, 0) << endl;
}