Submission #659959

#TimeUsernameProblemLanguageResultExecution timeMemory
659959chanhchuong123Knapsack (NOI18_knapsack)C++14
100 / 100
60 ms3756 KiB
#include <bits/stdc++.h>
using namespace std;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
  if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
  if (a < b) {a = b; return true;} return false;
}

const int N = 2020;
int s, n;
int dp[N];
vector<int> a[N];
vector<pair<int, int>> V[N];

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

  cin >> s >> n;
  for (int i = 1; i <= n; i++) {
    int v, w, k; cin >> v >> w >> k;
    V[w].push_back({v, k});
  }
  for (int i = 1; i <= s; i++) {
    int LIM = s / i; sort(all(V[i]));
    while (V[i].size() && LIM > 0) {
      if (V[i].back().se > 0) {
        a[i].push_back(V[i].back().fi);
        V[i].back().se--; LIM--;
      } else V[i].pop_back();
    }
  }
  for (int i = 1; i <= s; i++) for (int j: a[i]) {
    for (int k = s; k >= i; k--) maxi(dp[k], dp[k - i] + j);
  }
  cout << dp[s];
}

Compilation message (stderr)

knapsack.cpp:22:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   22 | main() {
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...