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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define REP(i, n) for (auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, n) for (ll i = s, i##_len = (ll)(n); i < i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);

  ll n, m, l;
  cin >> n >> m >> l;
  vl k(n);
  REP(i, n) cin >> k[i];
  vl s(m);
  REP(i, m) cin >> s[i];

  vl sum(m + 1);
  sum[0] = 0;
  REP(i, m) sum[i + 1] = sum[i] + s[i];

  sort(ALL(k));
  vvl dp(2, vl((n - 1) * 2 + 1, TEN(13)));
  dp[0][n - 1] = 0;
  ll ans = TEN(13);
  FOR(i, 1, n) {
    REP(j, (n - 1) * 2 + 1) dp[i % 2][j] = TEN(13);
    REP(j, (n - 1) * 2 + 1) {
      ll j_zero = j - (n - 1);
      ll times = abs(j_zero);
      if (times >= i || dp[(i - 1) % 2][j] == TEN(13))
        continue;
      if (i == n - 1) {
        MINC(ans, dp[(i - 1) % 2][j] +
                      (sum[k[i]] - sum[k[i - 1 - times] - 1]) / l +
                      (sum[k[i]] - sum[k[i - 1] - 1]) / l);
      } else {
        MINC(dp[i % 2][(n - 1) + (j_zero <= 0 ? 1 : -1)],
             dp[(i - 1) % 2][j] +
                 (sum[k[i]] - sum[k[i - 1 - times] - 1]) / l); // ???
        MINC(dp[i % 2][j + (j_zero <= 0 ? -1 : 1)],
             dp[(i - 1) % 2][j] +
                 (sum[k[i]] - sum[k[i - 1] - 1]) / l); // ??£?¶?
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define REP(i, n) for (auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, n) for (ll i = s, i##_len = (ll)(n); i < i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);

  ll n, m, l;
  cin >> n >> m >> l;
  vl k(n);
  REP(i, n) cin >> k[i];
  vl s(m);
  REP(i, m) cin >> s[i];

  vl sum(m + 1);
  sum[0] = 0;
  REP(i, m) sum[i + 1] = sum[i] + s[i];

  sort(ALL(k));
  vvl dp(n, vl((n - 1) * 2 + 1, TEN(13)));
  dp[0][n - 1] = 0;
  ll ans = TEN(13);
  FOR(i, 1, n) REP(j, (n - 1) * 2 + 1) {
    ll j_zero = j - (n - 1);
    ll times = abs(j_zero);
    if (times >= i || dp[i - 1][j] == TEN(13))
      continue;
    if (i == n - 1) {
      MINC(ans, dp[i - 1][j] + (sum[k[i]] - sum[k[i - 1 - times] - 1]) / l +
                    (sum[k[i]] - sum[k[i - 1] - 1]) / l);
    } else {
      MINC(dp[i][(n - 1) + (j_zero <= 0 ? 1 : -1)],
           dp[i - 1][j] + (sum[k[i]] - sum[k[i - 1 - times] - 1]) / l); // ???
      MINC(dp[i][j + (j_zero <= 0 ? -1 : 1)],
           dp[i - 1][j] + (sum[k[i]] - sum[k[i - 1] - 1]) / l); // ??£?¶?
    }
  }
  cout << ans << endl;

  return 0;
}