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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1000000007;

int dp[5010][5010];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, L;
  cin >> N >> L;
  VI x(N), a(N);
  REP(i, N) cin >> x[i];
  REP(i, N) cin >> a[i];

  for (int d = 0; d < L; ++d)
    dp[0][d] = 1 + (a[0] == 0 ? 0 : d / a[0]);
  for (int i = 1; i < N; ++i) {
    int dx = x[i] - x[i - 1] - 1;
    if (a[i] == 0) {
      for (int j = 0; j < L; ++j)
        dp[i][j] = dp[i - 1][dx];
      continue;
    }
    for (int j = 0; j < a[i]; ++j)
      dp[i][j] = dp[i - 1][dx];
    for (int j = a[i]; j < L; ++j) {
      dp[i][j] =
          (int)((LL)dp[i][j - a[i]] + (LL)dp[i - 1][dx + j / a[i] * a[i]]) %
          MOD;
    }
  }

  cout << dp[N - 1][L - x[N - 1] - 1] << endl;
  /*
  REP(i,N){
        REP(j,L) cout << dp[i][j] << " ";
        cout << endl;
  }
  */
  return 0;
}

Error: MLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1000000007;

LL dp[5010][5010];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, L;
  cin >> N >> L;
  VI x(N), a(N);
  REP(i, N) cin >> x[i];
  REP(i, N) cin >> a[i];

  for (int d = 0; d < L; ++d)
    dp[0][d] = 1 + (a[0] == 0 ? 0 : d / a[0]);
  for (int i = 1; i < N; ++i) {
    int dx = x[i] - x[i - 1] - 1;
    if (a[i] == 0) {
      for (int j = 0; j < L; ++j)
        dp[i][j] = dp[i - 1][dx];
      continue;
    }
    for (int j = 0; j < a[i]; ++j)
      dp[i][j] = dp[i - 1][dx];
    for (int j = a[i]; j < L; ++j) {
      dp[i][j] = (dp[i][j - a[i]] + dp[i - 1][dx + j / a[i] * a[i]]) % MOD;
    }
  }

  cout << dp[N - 1][L - x[N - 1] - 1] << endl;
  /*
  REP(i,N){
        REP(j,L) cout << dp[i][j] << " ";
        cout << endl;
  }
  */
  return 0;
}