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 <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
ll linf = 1e15+1;
inline void scoobydoobydoo(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
ll s, n;
const int MAXN = 2e3+1;
vector<pair<ll, ll> > items[MAXN];
pair<ll, ll> elems[MAXN*MAXN]; // value, weight
int counter = 0;
ll mem[MAXN*30][MAXN];
ll dp(int index, int weight){
if (index == counter || weight == 0)return 0;
if (mem[index][weight])return mem[index][weight];
return mem[index][weight] = max((elems[index].second <= weight ? dp(index+1, weight-elems[index].second)+elems[index].first : 0), dp(index+1, weight));
}
int main(){
scoobydoobydoo();
cin >> s >> n;
for (int i = 0; i < n; i++){
int v, w, k; cin >> v >> w >> k;
items[w].push_back({v, k});
}
for (int i = 1; i < MAXN; i++)sort(rall(items[i]));
for (int i = 1; i < MAXN; i++){
int how = 0;
for (int j = 0; j < items[i].size() && how < s/i+1; j++){
int val = items[i][j].first;
int k = items[i][j].second;
for (int l = 0; l < k && how+l < s/i+1; l++){
elems[counter++] = {val, i};
}
how += k;
}
}
cout << dp(0, s) << endl;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:52:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int j = 0; j < items[i].size() && how < s/i+1; j++){
| ~~^~~~~~~~~~~~~~~~~
# | 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... |