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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#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 range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int x[5010], a[5010];
int dp[5010][5010];

const int mod = 1000000007;

int main(void) {
  int n, l;
  cin >> n >> l;
  rep(i, n) cin >> x[i];
  rep(i, n) cin >> a[i];

  dp[n][l] = 1;
  for (int i = n - 1; i >= 0; --i) {
    ll sum = 0;
    for (int j = l; j >= x[i]; --j)
      sum = (sum + dp[i + 1][j]) % mod;
    for (int j = x[i]; j <= l; ++j) {
      sum = (sum - dp[i + 1][j] + mod) % mod;
      if (a[i] != 0) {
        if ((j - x[i]) % a[i] == 0)
          dp[i][j] = sum % mod;
      } else {
        if ((j - x[i]) == 0)
          dp[i][j] = sum % mod;
      }
    }
  }
  ll ans = 0;
  rep(i, l) ans = (ans + dp[0][i]) % mod;
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#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 range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int x[5010], a[5010];
ll dp[5010][5010];

const int mod = 1000000007;

int main(void) {
  int n, l;
  cin >> n >> l;
  rep(i, n) cin >> x[i];
  rep(i, n) cin >> a[i];

  dp[n][l] = 1LL;
  for (int i = n - 1; i >= 0; --i) {
    ll sum = 0;
    for (int j = l; j >= x[i]; --j)
      sum = (sum + dp[i + 1][j]) % mod;
    for (int j = x[i]; j <= l; ++j) {
      sum = (sum - dp[i + 1][j] + mod) % mod;
      if (a[i] != 0) {
        if ((j - x[i]) % a[i] == 0)
          dp[i][j] = (dp[i][j] + sum) % mod;
      } else {
        if ((j - x[i]) == 0)
          dp[i][j] = (dp[i][j] + sum) % mod;
      }
    }
  }
  ll ans = 0;
  rep(i, l) ans = (ans + dp[0][i]) % mod;
  cout << ans << endl;
  return 0;
}