Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
//*****************************believer******************************//

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef map<ll, ll> mi;

#define f first
#define ss second
#define ins insert
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pq priority_queue
#define sqr(n) floor(sqrt(n))
#define sz(v) ((ll)(v.size()))
#define trav(a, x) for (auto &a : x)
#define all(v) (v).begin(), (v).end()
#define arl(v) (v).rbegin(), (v).rend()
#define mem(a) memset(a, -1, sizeof(a))
#define ssort(v) sort((v).begin(), (v).end())
#define rsort(v) sort((v).rbegin(), (v).rend())
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define vmn(a) (*min_element(a.begin(), a.end()))
#define vmx(a) (*max_element(a.begin(), a.end()))
#define foo(i, a, b) for (ll i = (a); i < (b); ++i)
#define foor(i, a, b) for (ll i = (a); i >= (b); --i)

const ll N = 2e5 + 100;
ll wt[105], val[105];
ll n, w;
////this is the dp bw values and N which is different from the previous...
ll dp[N][105];

ll solve(ll x, ll num) {
  /// base condition;
  if (num == 0)
    return (x == 0) ? 0 : 1e12;

  if (val[num] > x)
    return dp[x][num] = solve(x, num - 1);

  else
    return dp[x][num] =
               min(solve(x, num - 1), solve(x - val[num], num - 1) + wt[num]);
}

ll solve1(ll x) { foor(i, x, 0) if (solve(i, n) <= w) return i; }

int main() {
  cin >> n >> w;
  ll maxx = 0;
  foo(i, 1, n + 1) {
    cin >> wt[i] >> val[i];
    maxx += val[i];
  }
  mem(dp);

  cout << solve1(maxx) << "\n";

  return 0;
}


Fixed submission:
//*****************************believer******************************//

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef map<ll, ll> mi;

#define f first
#define ss second
#define ins insert
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pq priority_queue
#define sqr(n) floor(sqrt(n))
#define sz(v) ((ll)(v.size()))
#define trav(a, x) for (auto &a : x)
#define all(v) (v).begin(), (v).end()
#define arl(v) (v).rbegin(), (v).rend()
#define mem(a) memset(a, -1, sizeof(a))
#define ssort(v) sort((v).begin(), (v).end())
#define rsort(v) sort((v).rbegin(), (v).rend())
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define vmn(a) (*min_element(a.begin(), a.end()))
#define vmx(a) (*max_element(a.begin(), a.end()))
#define foo(i, a, b) for (ll i = (a); i < (b); ++i)
#define foor(i, a, b) for (ll i = (a); i >= (b); --i)

const ll N = 2e5 + 100;
ll wt[105], val[105];
ll n, w;
////this is the dp bw values and N which is different from the previous...
ll dp[N][105];

ll solve(ll x, ll num) {
  /// base condition;
  if (num == 0)
    return (x == 0) ? 0 : 1e12;

  if (dp[x][num] != -1)
    return dp[x][num];

  if (val[num] > x)
    return dp[x][num] = solve(x, num - 1);

  else
    return dp[x][num] =
               min(solve(x, num - 1), solve(x - val[num], num - 1) + wt[num]);
}

ll solve1(ll x) { foor(i, x, 0) if (solve(i, n) <= w) return i; }

int main() {
  cin >> n >> w;
  ll maxx = 0;
  foo(i, 1, n + 1) {
    cin >> wt[i] >> val[i];
    maxx += val[i];
  }
  mem(dp);

  cout << solve1(maxx) << "\n";

  return 0;
}