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

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k, s;
  int yatai[3001], cost[3001];
  int dp[3001];
  rep(i, 0, 3001) { dp[i] = 0; }
  cin >> n >> k >> s;
  rep(i, 0, n) { cin >> yatai[i] >> cost[i]; }
  int ans = 0;
  rep(i, 0, n) {
    for (int j = k; 0 <= (j - cost[i]); --j) {
      int a = j - cost[i];
      if ((a >= s) || (j <= s)) {
        dp[j] = max(dp[j], dp[a] + yatai[i]);
        // cout << j << " ";
      }
    }
  }
  printf("%d\\n", *max_element(dp, dp + k + 1));
  return 0;
}

Error: MLE

Fixed submission:


#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef pair<int, int> pii;
typedef vector<pii> vii;
int n, m, s;
int memo[3001][3001][2];
int yatai[3010], cost[3010];
int f(int nu, int cou) {
  if (memo[nu][cou][0] > -1)
    return memo[nu][cou][0];
  if (nu >= n || cou >= m)
    return 0;
  else {
    if ((cou + cost[nu]) >= m)
      return memo[nu][cou][0] = f(nu + 1, cou);
    else
      return memo[nu][cou][0] =
                 max(f(nu + 1, cou), f(nu + 1, cou + cost[nu]) + yatai[nu]);
  }
}
int fmi(int nu, int cou) {
  if (nu < 0 || cou < 0)
    return 0;
  else if (memo[nu][cou][1] > -1)
    return memo[nu][cou][1];
  else {
    if ((cou - cost[nu]) < 0)
      return memo[nu][cou][1] = fmi(nu - 1, cou);
    else
      return memo[nu][cou][1] =
                 max(fmi(nu - 1, cou), fmi(nu - 1, cou - cost[nu]) + yatai[nu]);
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  rep(i, 0, 3001) {
    rep(j, 0, 3001) {
      memo[i][j][0] = -1;
      memo[i][j][1] = -1;
    }
  }
  cin >> n >> m >> s;
  rep(i, 0, n) { cin >> yatai[i] >> cost[i]; }
  int ans = 0;
  rep(i, 0, n) {
    int an = f(i, s);
    // cout << an << " ";
    an += fmi(i - 1, s);
    // cout << an << " " << i << endl;
    ans = max(an, ans);
  }
  // cout << f(0,0) << endl;
  cout << ans << endl;
  return 0;
}