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;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
#define file "test"
#define forr(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define forf(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
const int maxn = 2e3 + 10;
const int MOD = 1e9 + 7;
const ll inf = 1e18;
const int oo = 1e9 + 7;
const float eps = 1e-6;
ll n, s, v[maxn], w[maxn], k[maxn];
namespace sub1 {
bool check() {
return n == 1;
}
void solve() {
cout << v[1] * min(s / w[1], k[1]);
}
}
namespace sub2 {
bool check() {
if (n <= 100) {
bool kt = 1;
forr(i, 1, n)
if (k[i] != 1) {
kt = 0;
break;
}
return kt;
}
return 0;
}
void solve() {
ll dp[maxn];
memset(dp, 0, sizeof dp);
forr(i, 1, n)
ford(j, s, w[i])
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
cout << dp[s];
}
}
namespace sub3 {
bool check() {
if (n <= 100) {
bool kt = 1;
forr(i, 1, n)
if (k[i] > 10) {
kt = 0;
break;
}
return kt;
}
return 0;
}
void solve() {
ll dp[maxn];
memset(dp, 0, sizeof dp);
forr(i, 1, n)
ford(j, s, w[i])
forr(take, 1, k[i])
if (w[i] * take <= j)
dp[j] = max(dp[j], dp[j - w[i] * take] + v[i] * take);
cout << dp[s];
}
}
namespace full {
void solve() {
vpll obj[maxn];
long long dp[2001];
memset(dp, 0, sizeof dp);
for (int i = 1; i <= n; i++)
obj[w[i]].pb({v[i], k[i]});
for (int i = 0; i <= s; i++) {
if (obj[i].size() == 0)
continue;
sort(all(obj[i]), greater<pair<ll, ll>>());
int id = 0;
for (int j = 0; j * i < s; j++) {
if (id >= obj[i].size()) break;
for (int k = s; k >= i; k--) {
dp[k] = max(dp[k], dp[k - i] + obj[i][id].first);
}
--obj[i][id].second;
if (obj[i][id].second == 0) ++id;
}
}
cout << dp[s] << endl;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen(file".in", "r", stdin);
//freopen(file".out", "w", stdout);
cin >> s >> n;
forr(i, 1, n)
cin >> v[i] >> w[i] >> k[i];
if (sub1::check()) return sub1::solve(), 0;
if (sub2::check()) return sub2::solve(), 0;
if (sub3::check()) return sub3::solve(), 0;
return full::solve(), 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void full::solve()':
knapsack.cpp:99:20: 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]
99 | if (id >= obj[i].size()) break;
| ~~~^~~~~~~~~~~~~~~~
# | 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... |