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;
#define MODY ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pii pair<int , int>
#define A first
#define B second
#define pb push_back
int n , s;
vector<vector<pii>> vec(2001);
vector<pii> a;
int dp[22001][2001];
int knapsack(int i , int w){
if (i==-1) return 0 ;
if (~dp[i][w]) return dp[i][w];
int x = 0 , y = 0;
if (w>=a[i].A) x = knapsack(i-1 , w-a[i].A) + a[i].B;
y = knapsack(i-1 , w);
return dp[i][w] = max(x, y);
}
signed main(){
memset(dp , -1 , sizeof dp);
cin >> s >> n;
for (int i = 1 ; i<=n ; i++){
int v , w , k;
cin >> v >> w >> k;
vec[w].pb({v , k});
}
for (int i = 1 ; i<=s ; i++) sort(vec[i].begin() , vec[i].end() , greater<pii>());
for (int i = 1 ; i<=s ; i++){
int copies = 0;
for (auto j : vec[i]){
while(copies < s/i && j.B) { a.pb({i , j.A}); j.B--; copies++; }
}
}
cout << knapsack(a.size()-1 , s);
}
# | 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... |