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

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

map<vector<int>, int> dp[101];
int n;
int D[100];
int A[100];

int calc(vector<int> &vec, int key) {
  bool flg = false;
  int res = 0;
  for (int i = 0; i < (int)vec.size(); i++) {
    if (vec[i] == key) {
      flg = true;
    } else if (flg) {
      if (key - vec[i] <= D[key]) {
        res += A[key];
      }
    } else {
      if (key - vec[i] <= D[vec[i]]) {
        res += A[vec[i]];
      }
    }
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> D[i];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  map<vector<int>, int>::iterator it;
  vector<int> vec, vec2;
  int cost;
  vec.push_back(0);
  dp[0][vec] = 0;

  for (int i = 0; i + 1 < n; i++) {

    for (it = dp[i].begin(); it != dp[i].end(); it++) {
      vec = it->first;
      cost = it->second;

      vec.push_back(i + 1);
      int size = vec.size();
      for (int j = size - 1; j >= 0; j--) {
        if (j + 1 < size)
          swap(vec[j], vec[j + 1]);
        vector<int> next = vec;
        vector<int>::iterator it2;
        it2 = find(next.begin(), next.end(), i + 1 - 7);
        if (it2 != next.end())
          next.erase(it2);
        int ncost = cost + calc(vec, i + 1);
        dp[i + 1][next] = max(dp[i + 1][next], ncost);
      }
    }
  }

  int ans = 0;
  for (it = dp[n - 1].begin(); it != dp[n - 1].end(); it++) {
    ans = max(ans, it->second);
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

map<vector<int>, int> dp[101];
int n;
int D[100];
int A[100];

int calc(vector<int> &vec, int key) {
  bool flg = false;
  int res = 0;
  for (int i = 0; i < (int)vec.size(); i++) {
    if (vec[i] == key) {
      flg = true;
    } else if (flg) {
      if (key - vec[i] <= D[key]) {
        res += A[key];
      }
    } else {
      if (key - vec[i] <= D[vec[i]]) {
        res += A[vec[i]];
      }
    }
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> D[i];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  map<vector<int>, int>::iterator it;
  vector<int> vec;
  int cost;
  vec.push_back(0);
  dp[0][vec] = 0;

  for (int i = 0; i + 1 < n; i++) {

    for (it = dp[i].begin(); it != dp[i].end(); it++) {
      vec = it->first;
      cost = it->second;

      if (vec.size() < 8) {
        vec.push_back(i + 1);
        dp[i + 1][vec] = max(dp[i + 1][vec], cost + calc(vec, i + 1));
        for (int j = i; j >= 0; j--) {
          swap(vec[j], vec[j + 1]);
          dp[i + 1][vec] = max(dp[i + 1][vec], cost + calc(vec, i + 1));
        }
      } else {
        vector<int>::iterator IT;
        IT = find(vec.begin(), vec.end(), i - 7);
        if (IT != vec.end()) {
          vec.erase(IT);
        } else {
          cout << 'a' << endl;
        }
        vec.push_back(i + 1);
        dp[i + 1][vec] = max(dp[i + 1][vec], cost + calc(vec, i + 1));
        for (int j = 6; j >= 0; j--) {
          swap(vec[j], vec[j + 1]);
          dp[i + 1][vec] = max(dp[i + 1][vec], cost + calc(vec, i + 1));
        }
      }
    }
  }

  int ans = 0;
  for (it = dp[n - 1].begin(); it != dp[n - 1].end(); it++) {
    ans = max(ans, it->second);
  }
  cout << ans << endl;
  return 0;
}