This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// @tarche
// Knapsack
// oj.uz
// Singapore National Olympiad in Informatics 2018
// https://oj.uz/problem/view/NOI18_knapsack
// 2024-07-21
#include <bits/stdc++.h>
#define forn(i, n) for (tint i = 0; i < tint(n); i++)
#define forsn(i, s, n) for (tint i = s; i < tint(n); i++)
#define dforn(i, n) for (tint i = tint(n) - 1; i >= 0; i--)
#define dforsn(i, s, n) for (tint i = tint(n) - 1; i >= s; i--)
#define sz(x) tint(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define DBG(x) cerr << #x << " = " << x << endl
using namespace std;
#ifndef SMALL_TINT
using tint = long long;
#else
using tint = int;
#endif
using pii = pair<tint, tint>;
using vi = vector<tint>;
using vii = vector<pii>;
using vb = vector<bool>;
inline void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
inline string YN(bool x, string y = "YES", string n = "NO") { return (x ? y : n); }
template <typename T>
inline void chmax(T &lhs, T rhs) {
lhs = max(lhs, rhs);
}
template <typename T>
inline void chmin(T &lhs, T rhs) {
lhs = min(lhs, rhs);
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p);
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v);
template <typename T, size_t N>
ostream &operator<<(ostream &os, const array<T, N> &v);
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
forn(i, sz(v)) {
if (i > 0) os << ", ";
os << v[i];
}
os << "]";
return os;
}
template <typename T, size_t N>
ostream &operator<<(ostream &os, const array<T, N> &v) {
os << "[";
forn(i, N) {
if (i > 0) os << ", ";
os << v[i];
}
os << "]";
return os;
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &lhs, const pair<T, U> &rhs) {
return {lhs.first + rhs.first, lhs.second + rhs.second};
}
int main() {
fastIO();
tint s, n;
cin >> s >> n;
map<pii, tint> freq;
forn(i, n) {
tint v, w, k;
cin >> v >> w >> k;
freq[{w, v}] += k;
}
vii items;
for (auto [x, k] : freq) {
auto [w, v] = x;
if (k == 0 || w > s) continue;
if (k % 2 == 0) items.push_back(x), k--;
items.push_back(x), k--;
v *= 2, w *= 2, k /= 2;
if (k > 0) freq[{w, v}] += k;
}
vi dp(s + 1);
for (const auto &[w, v] : items) dforn(i, (s + 1) - w) chmax(dp[i + w], dp[i] + v);
cout << *max_element(all(dp)) << '\n';
}
# | 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... |