# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1008415 | cpdreamer | Knapsack (NOI18_knapsack) | C++17 | 52 ms | 3752 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>
#include <ext/pb_ds/assoc_container.hpp>
#include <utility>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
const int max_n=INT_MAX;
const int inf=1e8;
typedef long long ll;
#define LLM LONG_LONG_MAX
#define pb push_back
#define F first
#define P pair
#define all(v) v.begin(),v.end()
#define V vector
#define S second
const long long MOD = 998244353; // 1e9 + 7
void file(){
freopen("input.txt.txt","r",stdin);
freopen("output.txt.txt","w",stdout);
}
void setio(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct product{
int value;
int weight;
int copies;
};
bool sorted(product a,product b){
return a.value>b.value;
}
void solve() {
int n,W;
cin>>W>>n;
V<V<int>>weights(W+1);
product A[n];
for(int i=0;i<n;i++){
cin>>A[i].value>>A[i].weight>>A[i].copies;
}
sort(A,A+n,sorted);
for(int i=0;i<n;i++){
int a=A[i].value,b=A[i].weight,c=A[i].copies;
int limit=(W/b)-(int)weights[b].size();
for(int j=0;j<min(limit,c);j++){
weights[b].pb(a);
}
}
ll dp[W+1];
memset(dp,0LL,sizeof(dp));
for(int i=1;i<=W;i++){
for(auto u:weights[i]){
for(int j=W;j>=i;j--){
dp[j]=max(dp[j],dp[j-i]+u);
}
}
}
cout<<*max_element(dp,dp+W+1)<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//file();
solve();
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... |