Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
/**Bismillahir Rahmanir Raheem
    Author:Refatul Fahad**/

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

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define pil pair<int, ll>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl '\n'
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
const int N = 1e5 + 5;
const ll M = (ll)1e18;
const double pi = 2 * acos(0.0);
const double esp = 1e-9;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
ll cost[105], val[105], sum = 0ll;
int main() {

  // IOS;
  /// freopen("input.txt","r",stdin);
  /// freopen("output.txt","w",stdout);

  int test = 1;
  // cin>>test;
  for (int cs = 1; cs <= test; ++cs) {
    int n, w, ans = 0;
    cin >> n >> w;
    for (int i = 1; i <= n; ++i) {
      cin >> cost[i] >> val[i];
      sum += val[i];
    }
    vector<ll> dp(sum + 1, M);
    dp[0] = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = sum - val[i]; i >= 0; --i) {
        dp[j + val[i]] = min(dp[j + val[i]], dp[i] + cost[i]);
      }
    }
    for (int i = 1; i <= sum; ++i) {
      if (dp[i] <= w)
        ans = max(ans, i);
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
/**Bismillahir Rahmanir Raheem
    Author:Refatul Fahad**/

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

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define pil pair<int, ll>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl '\n'
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
const int N = 1e5 + 5;
const ll M = (ll)1e18;
const double pi = 2 * acos(0.0);
const double esp = 1e-9;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
ll cost[105], val[105], sum = 0ll;
int main() {

  // IOS;
  /// freopen("input.txt","r",stdin);
  /// freopen("output.txt","w",stdout);

  int test = 1;
  // cin>>test;
  for (int cs = 1; cs <= test; ++cs) {
    int n, w, ans = 0;
    cin >> n >> w;
    for (int i = 1; i <= n; ++i) {
      cin >> cost[i] >> val[i];
      sum += val[i];
    }
    vector<ll> dp(sum + 1, M);
    dp[0] = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = sum - val[i]; j >= 0; --j) {
        dp[j + val[i]] = min(dp[j + val[i]], dp[j] + cost[i]);
      }
    }
    for (int i = 1; i <= sum; ++i) {
      if (dp[i] <= w)
        ans = max(ans, i);
    }
    cout << ans << endl;
  }
  return 0;
}