alexjercan/bugnet huggingface dataset:
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
#define EPS (long double)1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};

using namespace std;

int main() {
  ll n, w;
  cin >> n >> w;
  vector<P> itm(n);
  REP(i, n) cin >> itm[i].first >> itm[i].second;
  ll dp[n + 1][100 * n + 1];
  REP(i, 100 * n + 1) dp[0][i] = INF * INF;
  dp[0][0] = 0;
  FOR(i, 1, n + 1) {
    REP(j, 100 * n + 1) {
      if (j >= itm[i - 1].first)
        dp[i][j] = min(dp[i - 1][j],
                       dp[i - 1][j - itm[i - 1].first] + itm[i - 1].second);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  ll res = 0;
  REP(i, 100 * n + 1) {
    if (dp[n][i] <= w)
      res = i;
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
#define EPS (long double)1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};

using namespace std;

int main() {
  int n, w;
  cin >> n >> w;
  vector<P> itm(n);
  REP(i, n) cin >> itm[i].first >> itm[i].second;
  ll dp[n + 1][w + 1];
  REP(i, w + 1) dp[0][i] = 0;
  FOR(i, 1, n + 1) {
    REP(j, w + 1) {
      if (j >= itm[i - 1].second)
        dp[i][j] = max(dp[i - 1][j],
                       dp[i - 1][j - itm[i - 1].second] + itm[i - 1].first);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w] << endl;
}
Problem p02320 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>
using namespace std;

int n, wt;
int v[100], w[100], m[100], f[10001];
long a[10001], d[10001];

int main() {
  cin >> n >> wt;

  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i] >> m[i];

  memset(a, 0, sizeof(a));
  memset(f, 0, sizeof(f));
  memset(d, 0, sizeof(d));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < w[i]; j++) {
      int s = 0, t = 0;
      for (int k = 0; k * w[i] + j <= wt; k++) {
        int x = a[k * w[i] + j] - k * v[i];
        while (s < t && d[t - 1] <= x)
          t--;
        f[t] = k;
        d[t++] = x;
        a[k * w[i] + j] = d[s] + k * v[i];
        if (f[s] == k - m[i])
          s++;
      }
    }
  }

  cout << a[wt] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;

int n, wt;
int v[101], w[101], m[101], f[10001];
long a[101][10001];

int main() {
  cin >> n >> wt;

  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i] >> m[i];

  memset(a, 0, sizeof(a));

  for (int i = 1; i <= n; i++) {
    memset(f, 0, sizeof(f));
    for (int j = 1; j <= wt; j++) {
      a[i][j] = a[i - 1][j];
      for (int k = 1; k <= m[i]; k++) {
        if (j >= w[i] * k && k + f[j - w[i] * k] <= m[i]) {
          a[i][j] = max(a[i - 1][j - w[i] * k] + v[i] * k, a[i][j]);
          if (a[i - 1][j - w[i] * k] + v[i] * k > a[i][j])
            f[j] = f[j - w[i] * k] + k;
        }
      }
    }
  }

  cout << a[n][wt] << endl;

  return 0;
}
Problem p02320 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int N, W, v_, w, m, x = 0;
  scanf("%d%d", &N, &W);
  vector<pair<int, int>> v;
  for (; N--;) {
    scanf("%d%d%d", &v_, &w, &m);
    for (int i = 1; i < m; m -= i, i *= 2)
      v.emplace_back(v_ * i, w * i);
    if (m)
      v.emplace_back(v_ * m, w * m);
  }
  vector<int> bag(W + 1);
  bag[0] = 1;
  for (auto &e : v) {
    x += e.second;
    if (x > W)
      x = W;
    for (int j = x; j >= e.second; j--)
      if (bag[j - e.second] && bag[j] < bag[j - e.second] + e.first)
        bag[j] = bag[j - e.second] + e.first;
  }
  for (int i = 0; i < W; i++)
    bag[i + 1] = max(bag[i + 1], bag[i]);
  printf("%d\\n", bag[W] - 1);
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int N, W, v_, w, m, x = 0;
  scanf("%d%d", &N, &W);
  vector<pair<int, int>> v;
  for (; N--;) {
    scanf("%d%d%d", &v_, &w, &m);
    for (int i = 1; i < m; m -= i, i *= 2)
      v.emplace_back(v_ * i, w * i);
    if (m)
      v.emplace_back(v_ * m, w * m);
  }
  vector<int> bag(W + 1);
  bag[0] = 1;
  for (auto &e : v) {
    x += e.second;
    for (int j = x; j >= e.second; j--)
      if (bag[j - e.second] && bag[j] < bag[j - e.second] + e.first)
        bag[j] = bag[j - e.second] + e.first;
  }
  for (int i = 0; i < W; i++)
    bag[i + 1] = max(bag[i + 1], bag[i]);
  printf("%d\\n", bag[W] - 1);
}
Problem p02320 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const size_t INF = 1uLL << 57;

size_t binary_knapsack(vector<size_t> &w, vector<size_t> &v, size_t c) {
  // for small-capacity knapsack
  vector<size_t> dp(c + 1);
  size_t n = w.size();
  for (size_t i = 0; i < n; i++)
    for (size_t j = c; j >= w[i]; --j)
      if (dp[j] < dp[j - w[i]] + v[i])
        dp[j] = dp[j - w[i]] + v[i];

  return dp[c];
}

size_t nlimited_knapsack(vector<size_t> &w, vector<size_t> &v,
                         vector<size_t> &m, size_t c) {
  size_t n = w.size();
  vector<size_t> _w, _v;
  for (size_t i = 0; i < n; ++i) {
    for (size_t j = 1; m[i] > 0; j <<= 1) {
      size_t k = min(m[i], j);
      m[i] -= k;
      _w.push_back(w[i] * k);
      _v.push_back(v[i] * k);
    }
  }

  return binary_knapsack(_w, _v, c);
}

int main() {
  size_t N, W;
  scanf("%zu %zu", &N, &W);

  vector<size_t> v(N), w(N), m(N);
  for (size_t i = 0; i < N; ++i) {
    scanf("%zu %zu %zu", &v[i], &w[i], &m[i]);
  }

  printf("%zu\\n", nlimited_knapsack(w, v, m, W));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const size_t INF = 1uLL << 57;

size_t binary_knapsack(vector<size_t> &w, vector<size_t> &v, size_t c) {
  // for small-capacity knapsack
  vector<size_t> dp(c + 1);
  size_t n = w.size();
  for (size_t i = 0; i < n; i++)
    for (size_t j = c; j >= w[i]; --j)
      if (dp[j] < dp[j - w[i]] + v[i])
        dp[j] = dp[j - w[i]] + v[i];

  return dp[c];
}

int main() {
  size_t N, W;
  scanf("%zu %zu", &N, &W);

  vector<size_t> v, w;
  for (size_t i = 0; i < N; ++i) {
    size_t _v, _w, m;
    scanf("%zu %zu %zu", &_v, &_w, &m);
    for (size_t j = 0; j < m; ++j) {
      v.push_back(_v);
      w.push_back(_w);
    }
  }

  printf("%zu\\n", binary_knapsack(w, v, W));
  return 0;
}
Problem p02320 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define endl '\\n'

typedef long long ll;
typedef pair<int, ll> pii;

const ll oo = 0x3f3f3f3f3f3f3f3f;

struct monotonic_queue {
  deque<pii> dq;

  void insert(int p, ll v) {
    while (!dq.empty() && dq.back().second <= v)
      dq.pop_back();
    dq.push_back({p, v});
  }

  void erase(int p) {
    while (!dq.empty() && dq.front().first < p)
      dq.pop_front();
  }

  ll max() { return dq.empty() ? -oo : dq.front().second; }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, w;
  cin >> n >> w;

  ll vi;
  vector<ll> best(w + 1);
  for (int i = 0, wi, ci; i < n; ++i) {
    cin >> vi >> wi >> ci;
    vector<monotonic_queue> mq(wi);

    int disp = wi * ci;
    for (int j = 0, curk = 0; j <= w; ++j, curk = j % wi) {
      ll nv = j / wi * vi;

      mq[curk].erase(j - disp);
      ll good = max(best[j], mq[curk].max() + nv);
      mq[curk].insert(j, best[j] - nv);
      best[j] = good;
    }
  }

  cout << *max_element(best.begin(), best.end()) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int dp[10000 + 1];
vector<int> v, w, m;

int main() {
  int N, W;
  int ans = 0;
  cin >> N >> W;
  for (int i = 0; i < N; ++i) {
    int tmp_v, tmp_w, tmp_m;
    cin >> tmp_v >> tmp_w >> tmp_m;
    for (int j = 0; j < tmp_m; ++j) {
      v.push_back(tmp_v);
      w.push_back(tmp_w);
    }
  }
  for (int i = 0; i < v.size(); ++i) {
    for (int j = W; j >= 0; --j) {
      if (j + w[i] <= W)
        dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]);
    }
  }
  for (int i = W; i >= 0; --i)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
Problem p02320 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>

#define int long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

signed main() {
  int N, W;
  cin >> N >> W;
  vector<int> v(N), w(N), m(N);
  rep(i, N) cin >> v[i] >> w[i] >> m[i];

  vector<int> vv, ww;
  rep(i, N) {
    for (int j = 0; j <= 13; j++) {
      int x = pow(2, j);
      if (x > m[i]) {
        break;
      }
      vv.push_back(v[i] * x);
      ww.push_back(w[i] * x);
      m[i] -= x;
    }
    for (int j = 13; j >= 0; j--) {
      int x = pow(2, j);
      if (x > m[i]) {
        continue;
      }
      vv.push_back(v[i] * x);
      ww.push_back(w[i] * x);
      m[i] -= x;
    }
  }

  int nn = vv.size();
  vector<int> dp(W + 1, 0);
  rep(i, nn) {
    vector<int> tmp(W + 1);
    for (int j = W; j >= 0; j--) {
      if (j < ww[i] || dp[j - ww[i]] == -1) {
        tmp[j] = dp[j];
        continue;
      }
      tmp[j] = max(dp[j], dp[j - ww[i]] + vv[i]);
    }
    swap(dp, tmp);
  }

  int ans = 0;
  rep(i, W + 1) {
    if (ans < dp[i]) {
      ans = dp[i];
    }
  }
  cout << ans << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>

#define int long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

signed main() {
  int N, W;
  cin >> N >> W;
  vector<int> v(N), w(N), m(N);
  rep(i, N) cin >> v[i] >> w[i] >> m[i];

  vector<int> vv, ww;
  rep(i, N) {
    for (int j = 0; j <= 13; j++) {
      int x = pow(2, j);
      if (x > m[i]) {
        break;
      }
      vv.push_back(v[i] * x);
      ww.push_back(w[i] * x);
      m[i] -= x;
    }
    for (int j = 13; j >= 0; j--) {
      int x = pow(2, j);
      if (x > m[i]) {
        continue;
      }
      vv.push_back(v[i] * x);
      ww.push_back(w[i] * x);
      m[i] -= x;
    }
  }

  int nn = vv.size();
  vector<vector<int>> dp(nn + 1, vector<int>(W + 1, -1));
  dp[0][0] = 0;
  rep(i, nn) {
    for (int j = W; j >= 0; j--) {
      if (j < ww[i] || dp[i][j - ww[i]] == -1) {
        dp[i + 1][j] = dp[i][j];
        continue;
      }
      dp[i + 1][j] = max(dp[i][j], dp[i][j - ww[i]] + vv[i]);
    }
  }

  int ans = 0;
  rep(i, W + 1) {
    if (ans < dp[nn][i]) {
      ans = dp[nn][i];
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02320 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int n, W;
int v[10001], w[10001], m[10001];
int dp[2][10001];
int ct = 0;

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    // press
    while (c > 0) {
      for (int i = 1; i <= c; (i <<= 1)) {
        v[ct] = i * a, w[ct] = i * b;
        c -= i, ct++;
      }
    }
  }
  for (int i = 0; i < ct; i++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[(i + 1) % 2][j] = dp[i % 2][j];
      else
        dp[(i + 1) % 2][j] = max(dp[i % 2][j], dp[i % 2][j - w[i]] + v[i]);
    }
  }
  cout << dp[ct % 2][W] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int n, W;
int v[10001], w[10001], m[10001];
int dp[10001][10001];
int ct = 0;

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    // press
    while (c > 0) {
      for (int i = 1; i <= c; (i <<= 1)) {
        v[ct] = i * a, w[ct] = i * b;
        c -= i, ct++;
      }
    }
  }
  for (int i = 0; i < ct; i++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }
  }
  cout << dp[ct][W] << endl;
  return 0;
}
Problem p02321 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
ll dp[1 << 22] = {};
int main() {
  ll n, W, a, b;
  vector<ll> v[2], w[2];
  cin >> n >> W;
  for (ll i = 0; i < n; i++) {
    cin >> a >> b;
    v[i % 2].push_back(a);
    w[i % 2].push_back(b);
  }
  vector<P> an[2];
  for (ll cn = 0; cn < 2; cn++)
    for (ll i = 0; i < (1 << v[cn].size()); i++) {
      ll suw = 0, suv = 0;
      for (ll j = 0; j < v[cn].size(); j++) {
        if ((1 & (i >> j)) == 1) {
          suw += w[cn][j];
          suv += v[cn][j];
        }
      }
      an[cn].push_back(P(suw, suv));
    }
  sort(an[0].begin(), an[0].end());
  ll ma = 0;
  for (int i = 0; i < an[0].size(); i++) {
    ma = max(ma, an[0][i].second);
    dp[i] = ma;
  }
  ll ans = 0;
  for (int i = 0; i < an[1].size(); i++) {
    ll vv = an[1][i].second;
    ll ww = an[1][i].first;
    ll lb = -1, ub = an[0].size(), mid;
    while (ub - lb > 1) {
      mid = (ub + lb) / 2;
      if (an[0][mid].first + ww <= W)
        lb = mid;
      else
        ub = mid;
    }
    if (lb != -1)
      ans = max(ans, vv + dp[lb]);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
ll dp[(1 << 20) + 1] = {};
int main() {
  ll n, W, a, b;
  vector<ll> v[2], w[2];
  cin >> n >> W;
  for (ll i = 0; i < n; i++) {
    cin >> a >> b;
    v[n % 2].push_back(a);
    w[n % 2].push_back(b);
  }
  vector<P> an;
  for (ll i = 0; i < (1 << v[0].size()); i++) {
    ll suw = 0, suv = 0;
    for (ll j = 0; j < v[0].size(); j++) {
      if ((1 & (i >> j)) == 1) {
        suw += w[0][j];
        suv += v[0][j];
      }
    }
    an.push_back(P(suw, suv));
  }
  sort(an.begin(), an.end());
  ll ma = 0;
  for (int i = 0; i < an.size(); i++) {
    ma = max(ma, an[i].second);
    dp[i] = ma;
  }
  ll ans = 0;
  for (ll i = 0; i < (1 << v[1].size()); i++) {
    ll ww = 0, vv = 0;
    for (ll j = 0; j < v[1].size(); j++) {
      if ((1 & (i >> j)) == 1) {
        ww += w[1][j];
        vv += v[1][j];
      }
    }
    ll lb = -1, ub = an.size(), mid;
    while (ub - lb > 1) {
      mid = (ub + lb) / 2;
      if (an[mid].first + ww <= W)
        lb = mid;
      else
        ub = mid;
    }
    if (lb != -1)
      ans = max(ans, vv + dp[lb]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02321 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N, W;
ll v[40], w[40];
vector<P> x, y;

void func(int si, int ti, vector<P> &x) {
  int size = ti - si;
  for (int bit = 0; bit < (1 << size); bit++) {
    P p = P(0, 0);
    for (int i = 0; i < size; i++) {
      if (bit >> i & 1) {
        p.first += w[si + i];
        p.second += v[si + i];
      }
    }
    x.push_back(p);
  }
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> v[i] >> w[i];

  func(0, N / 2, x);
  sort(x.begin(), x.end());
  func(N / 2, N, y);
  sort(y.begin(), y.end());

  ll target = 0, maxm = -(1LL << 60), ans = 0;

  for (int i = (int)x.size() - 1; i >= 0; i--) {
    P p = x[i];
    while (target < (int)y.size() && p.first + y[target].first <= W) {
      maxm = max(maxm, y[target].second);
      target++;
    }
    ans = max(ans, maxm + p.second);
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pl;

const int MAX_N = 40;

ll v[MAX_N];
ll w[MAX_N];
vector<pl> x, y;

void solve(int s, int t, vector<pl> &x) {
  int z = t - s;
  rep(i, 1 << z) {
    P p = P(0, 0);
    rep(j, z) {
      if (i & (1 << j)) {
        p.first += w[s + j];
        p.second += v[s + j];
      }
    }
    x.push_back(p);
  }
}

int main() {
  int n;
  ll lm;
  cin >> n >> lm;
  rep(i, n) { cin >> v[i] >> w[i]; }
  solve(0, n / 2, x);
  solve(n / 2, n, y);
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  ll t = 0;
  ll maxv = 0;
  ll ans = 0;
  for (int i = x.size() - 1; i >= 0; i++) {
    while (t < y.size() && x[i].first + y[t].first <= lm) {
      maxv = max(maxv, y[i].second);
      t++;
    }
    ans = max(ans, x[i].second + maxv);
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02322 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct Item {
  ll v, w, m;
  Item() {}
  explicit Item(ll v, ll w, ll m) : v(v), w(w), m(m) {}
  bool operator<(const Item &item) const { return v * item.w < item.v * w; }
};

constexpr ll MAX_N = 50;
constexpr ll MAX_V = 50;
constexpr ll LIMIT = MAX_N * MAX_N * MAX_V;

ll N, W;
ll dp[LIMIT + 1];

int main() {

  cin >> N >> W;

  vector<Item> items;
  for (ll i = 0; i < N; ++i) {
    ll v, w, m;
    cin >> v >> w >> m;
    items.emplace_back(v, w, m);
  }
  sort(items.rbegin(), items.rend());

  // knapsack dp
  fill(dp, dp + LIMIT + 1, LLONG_MAX / 2);
  dp[0] = 0;
  for (ll i = 0; i < N; ++i) {
    ll v = items[i].v;
    ll w = items[i].w;
    ll m = min(items[i].m, MAX_V);
    for (ll k = 0; m > 0; ++k) {
      ll use = min(m, 1LL << k);
      for (ll j = LIMIT - v * use; j >= 0; --j) {
        dp[j + v * use] = min(dp[j + v * use], dp[j] + w * use);
      }
      m -= use;
    }
  }

  // greedy
  ll ans = 0;
  for (ll i = 0; i <= LIMIT; ++i) {
    ll sum = i;
    ll cap = W - dp[i];
    if (cap < 0)
      continue;
    for (Item item : items) {
      if (item.m < MAX_V)
        continue;
      ll use = min(cap / item.w, item.m - MAX_V);
      sum += use * item.v;
      cap -= use * item.w;
    }
    ans = max(ans, sum);
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int MAX_N = 100;
const int MAX_W = 10000;

ll N, W;
ll v[MAX_N], w[MAX_N], m[MAX_N];

ll dp[MAX_W + 1];

int main() {
  cin >> N >> W;
  for (ll i = 0; i < N; ++i)
    cin >> v[i] >> w[i] >> m[i];

  fill(dp, dp + MAX_W + 1, -1);
  dp[0] = 0;
  for (ll i = 0; i < N; ++i) {
    ll lim = m[i];
    for (ll k = 0; lim > 0; ++k) {
      ll use = min(lim, 1LL << k);
      for (ll j = W - use * w[i]; j >= 0; --j) {
        if (dp[j] == -1)
          continue;
        dp[j + use * w[i]] = max(dp[j + use * w[i]], dp[j] + use * v[i]);
      }
      lim -= use;
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= W; ++i)
    ans = max(ans, dp[i]);
  cout << ans << endl;

  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

constexpr int INF = 1e9;
int V, E;
vector<vector<int>> dp, dist;

int dfs(int vis, int v) {
  if (vis == ((1 << V) - 1))
    return v ? INF : 0;
  int &res = dp[vis][v];
  if (res > 0)
    return res;
  res = INF;
  for (int i = 0; i < V; i++) {
    if ((vis >> i) & 1)
      continue;
    res = min(res, dist[v][i] + dfs(vis | (1 << i), i));
  }
  return res;
}

int main() {
  cin >> V >> E;
  dist = vector<vector<int>>(V, vector<int>(V, INF));
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    dist[s][t] = d;
  }
  dp = vector<vector<int>>(1 << V, vector<int>(V, -1));
  int res = dfs(0, 0);
  cout << (res < INF ? res : -1) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

constexpr int INF = 1e9;
int V, E;
vector<vector<int>> dp, dist;

int dfs(int vis, int v) {
  if (vis == ((1 << V) - 1))
    return v ? INF : 0;
  int &res = dp[vis][v];
  if (res < INF)
    return res;
  for (int i = 0; i < V; i++) {
    if ((vis >> i) & 1)
      continue;
    res = min(res, dist[v][i] + dfs(vis | (1 << i), i));
  }
  return res;
}

int main() {
  cin >> V >> E;
  dist = vector<vector<int>>(V, vector<int>(V, INF));
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    dist[s][t] = d;
  }
  dp = vector<vector<int>>(1 << V, vector<int>(V, INF));
  int res = dfs(0, 0);
  cout << (res < INF ? res : -1) << endl;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int v, e;
int memo[16][100000];
vector<vector<pair<int, int>>> graph;

int dp(int n, int c, int bit) {

  if (bit == (pow(2, v) - 1)) {
    for (int i = 0; i < graph[n].size(); i++) {
      if (graph[n][i].first == 0) {
        return graph[n][i].second;
      }
    }
    return 1000000;
  }

  if (memo[n][bit] != 0) {
    return memo[n][bit];
  }

  int a = 1000000;

  for (int i = 0; i < graph[n].size(); i++) {
    if ((bit & (1 << graph[n][i].first)) == 0) {
      a = min(a, dp(graph[n][i].first, c + 1, bit | (1 << graph[n][i].first)) +
                     graph[n][i].second);
    }
  }

  return memo[n][bit] = a;
}

int main() {
  cin >> v >> e;
  graph.resize(v);
  for (int i = 0; i < e; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    pair<int, int> p = make_pair(t, d);
    graph[s].push_back(p);
  }

  int ans = dp(0, 0, 1);

  if (ans >= 20000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int v, e;
int memo[100000];
vector<vector<pair<int, int>>> graph;

int dp(int n, int c, int bit) {

  if (bit == (pow(2, v) - 1)) {
    for (int i = 0; i < graph[n].size(); i++) {
      if (graph[n][i].first == 0) {
        return memo[bit] = graph[n][i].second;
      }
    }
    return 1000000;
  }

  // if(memo[bit] != 0){
  //   return memo[bit];
  // }

  if (graph[n].size() == 0) {
    return 1000000;
  }

  int a = 20000;

  for (int i = 0; i < graph[n].size(); i++) {
    if ((bit & (1 << graph[n][i].first)) == 0) {
      a = min(a,
              dp(graph[n][i].first, c + 1, (bit | (1 << graph[n][i].first))) +
                  graph[n][i].second);
    }
  }

  return memo[bit] = a;
}

int main() {
  cin >> v >> e;
  graph.resize(v);
  for (int i = 0; i < e; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    pair<int, int> p = make_pair(t, d);
    graph[s].push_back(p);
  }

  int ans = dp(0, 0, 1);

  if (ans >= 20000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}