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

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

#ifdef _WIN32
#define scanfll(x) scanf("%I64d", x)
#define printfll(x) printf("%I64d", x)
#else
#define scanfll(x) scanf("%lld", x)
#define printfll(x) printf("%lld", x)
#endif
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repi(i, a, b)                                                          \\
  for (long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template <class T1, class T2> bool chmin(T1 &a, T2 b) {
  return b < a && (a = b, true);
}
template <class T1, class T2> bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vi = vector<int>;
using vvi = vector<vi>;
vll conv(vi &v) {
  vll r(v.size());
  rep(i, v.size()) r[i] = v[i];
  return r;
}
using P = pair<ll, ll>;

template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
  o << "(" << v.first << ", " << v.second << ")";
  return o;
}
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
  using s = int[];
  (void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
    -> basic_ostream<Ch, Tr> & {
  os << "(";
  print_tuple(os, t, gen_seq<sizeof...(Args)>());
  return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
  rep(i, v.size()) {
    rep(j, v[i].size()) o << v[i][j] << " ";
    cout << endl;
  }
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << '[';
  rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
  o << "]";
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it << (next(it) != m.end() ? ", " : "");
  o << "]";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it << (next(it) != m.end() ? ", " : "");
  o << "]";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const unordered_map<T, U> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it;
  o << "]";
  return o;
}
void printbits(ll mask, ll n) {
  rep(i, n) { cout << !!(mask & (1ll << i)); }
  cout << endl;
}
#define ldout fixed << setprecision(40)

static const double EPS = 1e-14;
static const long long INF = 1e18;
static const long long mo = 1e9 + 7;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, l;
  cin >> n >> m >> l;
  vi k(n);
  rep(i, n) {
    cin >> k[i];
    k[i]--;
  }
  vi s(m);
  rep(i, m) cin >> s[i];

  sort(all(k));

  vll ss(m + 1);
  rep(i, m) ss[i + 1] = ss[i] + s[i];

  function<ll(ll, ll)> dist = [&](ll i_, ll j_) {
    ll i = k[i_], j = k[j_];
    if (i > j)
      swap(i, j);
    return (ss[j + 1] - ss[i]) / l;
  };
  //    cout << ss << endl;

  static ll memo[2001][2001] = {};
  rep(i, 2001) rep(j, 2001) memo[i][j] = -1;
  function<ll(ll, ll)> f = [&](ll l, ll r) {
    //        cout << l << " " << r << endl;
    ll next = max(l, r) + 1;
    if (next >= n)
      return dist(l, r);
    if (memo[l][r] >= 0)
      return memo[l][r];

    return memo[l][r] =
               min(f(l, next) + dist(next, r), dist(next, l) + f(next, r));
  };

  cout << f(0, 0) << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

#ifdef _WIN32
#define scanfll(x) scanf("%I64d", x)
#define printfll(x) printf("%I64d", x)
#else
#define scanfll(x) scanf("%lld", x)
#define printfll(x) printf("%lld", x)
#endif
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repi(i, a, b)                                                          \\
  for (long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template <class T1, class T2> bool chmin(T1 &a, T2 b) {
  return b < a && (a = b, true);
}
template <class T1, class T2> bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vi = vector<int>;
using vvi = vector<vi>;
vll conv(vi &v) {
  vll r(v.size());
  rep(i, v.size()) r[i] = v[i];
  return r;
}
using P = pair<ll, ll>;

template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
  o << "(" << v.first << ", " << v.second << ")";
  return o;
}
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
  using s = int[];
  (void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
    -> basic_ostream<Ch, Tr> & {
  os << "(";
  print_tuple(os, t, gen_seq<sizeof...(Args)>());
  return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
  rep(i, v.size()) {
    rep(j, v[i].size()) o << v[i][j] << " ";
    cout << endl;
  }
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << '[';
  rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
  o << "]";
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it << (next(it) != m.end() ? ", " : "");
  o << "]";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it << (next(it) != m.end() ? ", " : "");
  o << "]";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const unordered_map<T, U> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it;
  o << "]";
  return o;
}
void printbits(ll mask, ll n) {
  rep(i, n) { cout << !!(mask & (1ll << i)); }
  cout << endl;
}
#define ldout fixed << setprecision(40)

static const double EPS = 1e-14;
static const long long INF = 1e18;
static const long long mo = 1e9 + 7;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, l;
  cin >> n >> m >> l;
  vll k(n);
  rep(i, n) {
    cin >> k[i];
    k[i]--;
  }
  vll s(m);
  rep(i, m) cin >> s[i];

  sort(all(k));

  vll ss(m + 1);
  rep(i, m) ss[i + 1] = ss[i] + s[i];

  function<ll(ll, ll)> dist = [&](ll i_, ll j_) {
    ll i = k[i_], j = k[j_];
    if (i > j)
      swap(i, j);
    return (ss[j + 1] - ss[i]) / l;
  };
  //    cout << ss << endl;

  map<P, ll> memo;
  function<ll(ll, ll)> f = [&](ll l, ll r) {
    //        cout << l << " " << r << endl;
    ll next = max(l, r) + 1;
    if (next >= n)
      return dist(l, r);
    if (memo.count(P(l, r)))
      return memo[P(l, r)];

    memo[P(l, r)] = min(f(l, next) + dist(next, r), dist(next, l) + f(next, r));
    return memo[P(l, r)];
  };

  /*
  cout << dist(0, 2) << endl;
  cout << dist(2, 4) << endl;
  cout << dist(4, 3) << endl;
  cout << dist(3, 0) << endl;
  */
  cout << f(0, 0) << endl;
  /*
  cout << f(0, 3) << endl;
  */

  return 0;
}