Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):

// Problem : E - Knapsack 2
// Contest : AtCoder - Educational DP Contest
// URL : https://atcoder.jp/contests/dp/tasks/dp_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 105
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int mp[105][100005];
int n, w;
pp arr[maxn];
int fun(int l, int wt) {
  if (l == 0 || wt == 0)
    return 0;
  if (mp[l][w] != -1)
    return mp[l][w];
  int x = 0;
  if (arr[l].ff <= wt)
    x = arr[l].ss + fun(l - 1, wt - arr[l].ff);
  x = max(x, fun(l - 1, wt));
  mp[l][wt] = x;
  return x;
}
void solve() {
  memset(mp, -1, sizeof(mp));
  cin >> n >> w;
  repe(i, 1, n) {
    int x, y;
    cin >> x >> y;
    arr[i] = {x, y};
  }
  cout << fun(n, w);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Fixed submission:

// Problem : E - Knapsack 2
// Contest : AtCoder - Educational DP Contest
// URL : https://atcoder.jp/contests/dp/tasks/dp_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 105
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int mp[105][100005];
int n, w;
pp arr[maxn];
int fun(int l, int wt) {
  if (l == 0 || wt == 0)
    return 0;
  if (mp[l][wt] != -1)
    return mp[l][wt];
  int x = 0;
  if (arr[l].ff <= wt)
    x = arr[l].ss + fun(l - 1, wt - arr[l].ff);
  x = max(x, fun(l - 1, wt));
  mp[l][wt] = x;
  return x;
}
void solve() {
  memset(mp, -1, sizeof(mp));
  cin >> n >> w;
  repe(i, 1, n) {
    int x, y;
    cin >> x >> y;
    arr[i] = {x, y};
  }
  cout << fun(n, w);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}