Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
/* -----KAUN_MEET--------*/

#include <bits/stdc++.h>
using namespace std;
#define int long long int
int mod = 1000000007;
// int maxi=100;
int maxi = 1e18 + 5;
#define P get<0>
#define D get<1>
#define T get<2>
#define C get<3>
#define PB push_back
#define MP make_pair
#define BS binary_search
#define LB lower_bound
#define UB upper_bound
#define F first
#define S second
#define gcd __gcd
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

void __print(int x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename Th, typename V> void __print(const pair<Th, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename Th> void __print(const Th &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename Th, typename... V> void _print(Th t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif
int n;
int w;
vector<pair<int, int>> p;
int ans[105][100000 + 10];
int answer(int i, int val) {
  if (val <= 0) {
    ans[i][val] = 0;
    return 0;
  }

  int j, k, l;

  if (i >= n)
    return maxi;

  int costi = p[i].F;
  int valui = p[i].S;

  if (i == 0) {

    if (valui == val) {
      ans[i][val] = costi;
    } else {
      ans[i][val] = maxi;
    }

    return ans[i][val];
  }
  int ans1 = answer(i - 1, val);
  int ans2;
  if (val < valui) {
    ans2 = maxi;
  } else {
    ans2 = answer(i - 1, val - valui) + costi;
  }
  ans[i][val] = min(ans1, ans2);
  return ans[i][val];
}
void binary_rock() {
  cin >> n;
  cin >> w;
  int i, j, k, l;
  int sum = 0;
  for (i = 0; i < n; i++) {
    cin >> j >> k;
    p.PB({j, k});
    sum += k;
  }
  sort(p.begin(), p.end());
  for (i = 0; i < n; i++) {
    for (j = 0; j <= sum; j++)
      ans[i][j] = -1;
  }
  int yy = 0;
  for (i = 0; i <= sum; i++) {
    if (answer(n - 1, i) <= w) {
      yy = i;
    }
  }
  cout << yy << endl;

  // for(i=0;i<n;i++){
  //     for(j=0;j<=sum;j++)
  //     {
  //         cout<<ans[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
}

signed main() {
  fastio clock_t t1 = clock();

  int t = 1;
  // cin >> t;
  while (t--) {
    binary_rock();
  }

  clock_t t2 = clock();
  // cout << "Time-Taken: " << ((t2 - t1) / (double)CLOCKS_PER_SEC) << endl;
  // cout << CLOCKS_PER_SEC << endl;
  return 0;
}

Fixed submission:
/* -----KAUN_MEET--------*/

#include <bits/stdc++.h>
using namespace std;
#define int long long int
int mod = 1000000007;
// int maxi=100;
int maxi = 1e18 + 5;
#define P get<0>
#define D get<1>
#define T get<2>
#define C get<3>
#define PB push_back
#define MP make_pair
#define BS binary_search
#define LB lower_bound
#define UB upper_bound
#define F first
#define S second
#define gcd __gcd
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

void __print(int x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename Th, typename V> void __print(const pair<Th, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename Th> void __print(const Th &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename Th, typename... V> void _print(Th t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif
int n;
int w;
vector<pair<int, int>> p;
int ans[105][100000 + 10];
int answer(int i, int val) {
  if (val <= 0) {
    ans[i][val] = 0;
    return 0;
  }

  int j, k, l;

  if (i >= n)
    return maxi;
  if (ans[i][val] != -1)
    return ans[i][val];

  int costi = p[i].F;
  int valui = p[i].S;

  if (i == 0) {

    if (valui == val) {
      ans[i][val] = costi;
    } else {
      ans[i][val] = maxi;
    }

    return ans[i][val];
  }
  int ans1 = answer(i - 1, val);
  int ans2;
  if (val < valui) {
    ans2 = maxi;
  } else {
    ans2 = answer(i - 1, val - valui) + costi;
  }
  ans[i][val] = min(ans1, ans2);
  return ans[i][val];
}
void binary_rock() {
  cin >> n;
  cin >> w;
  int i, j, k, l;
  int sum = 0;
  for (i = 0; i < n; i++) {
    cin >> j >> k;
    p.PB({j, k});
    sum += k;
  }
  sort(p.begin(), p.end());
  for (i = 0; i < n; i++) {
    for (j = 0; j <= sum; j++)
      ans[i][j] = -1;
  }
  int yy = 0;
  for (i = 0; i <= sum; i++) {
    if (answer(n - 1, i) <= w) {
      yy = i;
    }
  }
  cout << yy << endl;

  // for(i=0;i<n;i++){
  //     for(j=0;j<=sum;j++)
  //     {
  //         cout<<ans[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
}

signed main() {
  fastio clock_t t1 = clock();

  int t = 1;
  // cin >> t;
  while (t--) {
    binary_rock();
  }

  clock_t t2 = clock();
  // cout << "Time-Taken: " << ((t2 - t1) / (double)CLOCKS_PER_SEC) << endl;
  // cout << CLOCKS_PER_SEC << endl;
  return 0;
}