# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1083901 | tkvinhtruongquang | Knapsack (NOI18_knapsack) | C++14 | 46 ms | 3868 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>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define clz __builtin_clz
#define popcount __builtin_popcount
#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)
#define loop(i, j, n, k) for(int i = j; i <= n; i += k)
#define fast ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
template <typename T>
inline void print(T x) { cerr << x << endl; }
template <typename T>
inline void print(vector <T> x) { cerr << '['; loop(i, 0, sz(x) - 2, 1) cerr << x[i] << ", "; cerr << x.back() << ']' << endl; }
template <typename T>
inline bool maximize(T &a, const T &b) { return a < b ? (a = b) : 0; }
template <typename T>
inline bool minimize(T &a, const T &b) { return a > b ? (a = b) : 0; }
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using pss = pair<string, string>;
using vll = vector<ll>;
using pll = pair<ll, ll>;
#define debug(x) cerr << #x << " = "; print(x)
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
freopen("error.txt", "w", stderr);
}
int main() {
// setIO("task");
fast;
int cap, n; cin >> cap >> n;
map<int, vector<pii>> by_weight;
loop(i, 0, n - 1, 1) {
int v, w, a;
cin >> v >> w >> a;
by_weight[w].pb({v, a});
}
vll dp(cap + 1, 0); dp[0] = 0;
for(auto &[weight, items] : by_weight) {
sort(items.rbegin(), items.rend());
int copies = 1;
int item_indx = 0;
vll prefix(sz(items));
prefix[0] = items[0].se;
loop(i, 1, sz(items) - 1, 1)
prefix[i] = items[i].se += prefix[i - 1];
while(copies * weight <= cap && item_indx < sz(items)) {
for(int i = cap; i >= copies * weight; -- i) {
dp[i] = max(dp[i], dp[i - weight] + items[item_indx].fi);
}
copies ++;
if(copies > prefix[item_indx]) ++ item_indx;
}
}
cout << dp[cap] << '\n';
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... |