# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1175318 | pete555 | Knapsack (NOI18_knapsack) | C++17 | 36 ms | 1984 KiB |
#include<bits/stdc++.h>
using namespace std;
#define pi pair<int,int>
#define ll long long
#define pb push_back
#define pf push_front
void fileIO(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const int MOD = 1e9+7;
int main()
{
cin.tie(0)->sync_with_stdio(false);
//fileIO("");
int S, n;
cin >> S >> n;
unordered_map<int,vector<pi>> m;
for(int i=0; i<n; i++){
int v, w, k;
cin >> v >> w >> k;
m[w].pb({v, k});
}
vector<int> dp(S+1);
for(auto I: m){ // 1 <= w <= S
sort(I.second.begin(), I.second.end(), greater<pi>());
int w = I.first;
for(int i=S; i>=0; i--){
int cnt = 0, idx = 0, used = 0, profit = 0;
while((cnt+1)*w <= i and idx < I.second.size()){
cnt++, used++;
profit += I.second[idx].first;
dp[i] = max(dp[i], dp[i-cnt*w] + profit);
if(used == I.second[idx].second){
used = 0;
idx++;
}
}
}
}
cout << dp[S] << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |