Submission #951544

# Submission time Handle Problem Language Result Execution time Memory
951544 2024-03-22T05:54:42 Z xuvxuv Knapsack (NOI18_knapsack) C++17
100 / 100
56 ms 8172 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define all(x) x.begin(),x.end()
#define vpii vector<pair<int,int>>
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mii;
const int Prime1= 1000000007;
const int Prime2= 998244353;


long long binpow(long long a, long long b, long long m) {
 a %= m;
long long res = 1;
while (b > 0) {
if (b & 1){
res = res * a % m;}
a = a * a % m;
b >>= 1;
}
return res;
 }
 
 
vector<int> hp(int n){
vector<int>h(n,0) ;
for(int i=2;i<n;i++){
h[i]=i;
}
for( int i=2;i*i<n;i++){
if(h[i]==i){
for(int  j=i;j<n;j+=i){
h[j]=i;
}
}
}
return h;
}
 
 
 
 
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
 
    int s,n;
    cin>>s>>n;
    vector<pair<int,pii>>v;
    int dp[s+1]={0};
    vector<pii>wei[s+1];
    for(int i=0;i<n;i++){
        int val,w,k;
        cin>>val>>w>>k;
        v.push_back({val,{w,k}});
        wei[w].push_back({val,k});
    }
    vector<int>fw[s+1];
    for(int i=1;i<=s;i++){
        int num=((s+i-1)/i);
        sort(all(wei[i]));
        while(num>0&&  wei[i].size()>0){
            if(wei[i].back().second>0){
                wei[i].back().second--;
                fw[i].push_back(wei[i].back().first);
                if(wei[i].back().second==0){
                    wei[i].pop_back();
                }
                num--;
            }
        }

    }
    for(int i=1;i<=s;i++){
        for(auto c:fw[i]){
            for(int j=s;j>=0;j--){
                if(j-i<0)break;
                if(j-i==0){
                    dp[j]=max(dp[j],c);
                    continue;
                }
                if(dp[j-i]>0){
                    dp[j]=max(dp[j],dp[j-i]+c);
                }
            }
        }
    }
    int mx=*max_element(dp,dp+s+1);
    cout<<mx<<endl;




return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 6 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 2 ms 452 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 6 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 2 ms 452 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 25 ms 6884 KB Output is correct
36 Correct 29 ms 7140 KB Output is correct
37 Correct 29 ms 5604 KB Output is correct
38 Correct 26 ms 6924 KB Output is correct
39 Correct 30 ms 8172 KB Output is correct
40 Correct 56 ms 8120 KB Output is correct
41 Correct 39 ms 6412 KB Output is correct
42 Correct 44 ms 6928 KB Output is correct
43 Correct 51 ms 6416 KB Output is correct
44 Correct 52 ms 6416 KB Output is correct
45 Correct 29 ms 7180 KB Output is correct
46 Correct 29 ms 6620 KB Output is correct
47 Correct 32 ms 7520 KB Output is correct
48 Correct 41 ms 7172 KB Output is correct
49 Correct 25 ms 7952 KB Output is correct
50 Correct 43 ms 6668 KB Output is correct