Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <stdlib.h>
#include <time.h>
#define pb push_back
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ull unsigned long long
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / __gcd((a), (b))
#define clr(x) x.clear()
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vi>
#define mp make_pair
#define hell 1000000007
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define ign cin.ignore();

#define o_set                                                                  \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define o_setll                                                                \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// member functions :
// 1. order_of_key(k) : number of elements strictly lesser than k
// 2. find_by_order(k) : k-th element in the set

using namespace std;
using namespace __gnu_pbds;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long 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 T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T 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

ll dp[20010];

ll wt[1010], val[1010];

ll n, W;

void KnapSack(ll n, ll W, ll maxi) {
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < W + maxi; j++) {
      for (ll k = 1;; k++) {
        if (j + k * wt[i] >= W + maxi)
          break;
        dp[j + k * wt[i]] = min(dp[j + k * wt[i]], k * val[i] + dp[j]);
      }
    }
}

int main() {
  IOS ll maxi = -1;
  for (int i = 0; i < 20010; i++) {
    dp[i] = 1e12;
  }
  dp[0] = 0;
  ll W, n;
  cin >> W >> n;
  for (int i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
    maxi = max(maxi, wt[i]);
  }
  KnapSack(n, W, maxi);
  ll mini = LLONG_MAX;
  for (ll i = W; i < W + maxi; i++) {
    mini = min(mini, dp[i]);
  }
  cout << mini << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <stdlib.h>
#include <time.h>
#define pb push_back
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ull unsigned long long
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / __gcd((a), (b))
#define clr(x) x.clear()
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vi>
#define mp make_pair
#define hell 1000000007
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define ign cin.ignore();

#define o_set                                                                  \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define o_setll                                                                \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// member functions :
// 1. order_of_key(k) : number of elements strictly lesser than k
// 2. find_by_order(k) : k-th element in the set

using namespace std;
using namespace __gnu_pbds;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long 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 T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T 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

ll dp[20010];

ll wt[1010], val[1010];

ll n, W;

void KnapSack(ll n, ll W, ll maxi) {
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < W + maxi; j++) {
      if (j + wt[i] >= W + maxi)
        break;
      dp[j + wt[i]] = min(dp[j + wt[i]], val[i] + dp[j]);
    }
}

int main() {
  IOS ll maxi = -1;
  for (int i = 0; i < 20010; i++) {
    dp[i] = 1e12;
  }
  dp[0] = 0;
  ll W, n;
  cin >> W >> n;
  for (int i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
    maxi = max(maxi, wt[i]);
  }
  KnapSack(n, W, maxi);
  ll mini = LLONG_MAX;
  for (ll i = W; i < W + maxi; i++) {
    mini = min(mini, dp[i]);
  }
  cout << mini << "\n";
  return 0;
}