# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
680434 | sz1218 | Knapsack (NOI18_knapsack) | C++14 | 159 ms | 35180 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
const int NN = 1e6+3;
int n, S;
ll dp[2003][2003];
int main()
{
//setIO("");
cin >> S >> n;
int V, W, K;
map<int, vector<pi>> by_weight;
for(int j=0;j<n;j++){
cin >> V >> W >> K;
by_weight[W].push_back({V, K});
}
int a = 1;
for(auto&[w, item] : by_weight){
sort(item.rbegin(), item.rend());
for(int i=0;i<=S;i++){
dp[a][i] = dp[a-1][i];
int ta = 0;
int copies = 0;
int cur_used = 0;
ll profit = 0;
while((copies+1) * w <= i && ta < item.size()){
copies++;
profit += item[ta].first;
dp[a][i] = max(dp[a][i], dp[a-1][i-copies*w] + profit);
cur_used++;
if(cur_used == item[ta].second){
cur_used = 0;
ta++;
}
}
}
a++;
}
ll ans = 0;
for(int i=1;i<=S;i++){
ans = max(ans, dp[a-1][i]);
}
cout << ans << endl;
return 0;
}
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... |