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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m, l;
vi in, dp;
ll mm[2010][2010];
ll f(ll a, ll b) {
  if (mm[a][b] + 1)
    return mm[a][b];
  if (b == n - 1)
    return mm[a][b] = (dp[in[n - 1]] - dp[in[a] - 1]) / l;
  return mm[a][b] = min(f(a, b + 1) + (dp[in[b + 1]] - dp[in[b] - 1]) / l,
                        f(b, b + 1) + (dp[in[b + 1]] - dp[in[a] - 1]) / l);
}
int main() {
  cin >> n >> m >> l;
  rep(i, 2010) rep(j, 2010) mm[i][j] = -1;
  in = vi(n);
  dp = vi(m + 1);
  rep(i, n) cin >> in[i];
  rep(i, m) cin >> dp[i + 1];
  rep(i, m) dp[i + 1] += dp[i];
  sort(all(in));
  cout << f(0, 0) << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m, l;
vi in, dp;
vvi mm;
int f(int a, int b) {
  if (mm[a][b] + 1)
    return mm[a][b];
  if (b == n - 1)
    return mm[a][b] = (dp[in[n - 1]] - dp[in[a] - 1]) / l;
  return mm[a][b] = min(f(a, b + 1) + (dp[in[b + 1]] - dp[in[b] - 1]) / l,
                        f(b, b + 1) + (dp[in[b + 1]] - dp[in[a] - 1]) / l);
}
int main() {
  cin >> n >> m >> l;
  mm = vvi(n, vi(n, -1));
  in = vi(n);
  dp = vi(m + 1);
  rep(i, n) cin >> in[i];
  rep(i, m) cin >> dp[i + 1];
  rep(i, m) dp[i + 1] += dp[i];
  sort(all(in));
  cout << f(0, 0) << endl;
}