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 <cstdio>
#include <vector>
#include <utility>
#include <cmath>
#include <algorithm>
#include <array>
#include <set>
#include <climits>
#include <map>
#include <memory>
#include <string>
#include <deque>
#include <queue>
#include <stack>
using namespace std;
#define input "knapsack.in"
#define output "knapsack.out"
#define int long long
#define double long double
typedef tuple<double, int, int, int> item;
int S, n;
vector<item> items;
vector<vector<int>> dp;
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen(input, "r", stdin);
//freopen(output, "w", stdout);
cin >> S >> n;
dp.resize(S+1, vector<int>(n+1, 0));
items.reserve(n);
for (int i=0;i<n;i++) {
int val, weight, amt;
cin >> val >> weight >> amt;
items.push_back({(double)(val)/weight, val, weight, amt});
}
sort(items.begin(), items.end(), [](item& a, item& b) -> bool {
return get<0>(a)<get<0>(b);
});
/* for (int i=0;i<n;i++) cout << get<0>(items[i]) << ' ' << get<1>(items[i]) <<
' ' << get<2>(items[i]) << ' ' << get<3>(items[i]) << endl; */
for (int i=0;i<n;i++) dp[get<2>(items[i])][i+1]=get<1>(items[i]);
for (int s=1;s<=S;s++) {
for (int i=1;i<=n;i++) {
dp[s][i]=max(dp[s-1][i], dp[s][i]);
int amt=get<3>(items[i-1]), wt=get<2>(items[i-1]), val=get<1>(items[i-1]);
for (int k=0;k<=s && k/wt<=amt;k+=wt) {
int used=k/wt*val;
dp[s][i]=max(dp[s][i], used+dp[s-k][i-1]);
}
}
}
cout << dp[S][n] << endl;
}
# | 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... |