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;
#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() {
ll dp[maxn];
vector<pll> items[maxn];
dp[0] = 0;
forr(i, 1, n)
items[w[i]].pb(mp(v[i], k[i]));
forr(i, 0, s) {
if (!items[i].size())
continue;
sort(all(items[i]), greater<pll>());
int idx = 0;
forr(j, 0, s / i) {
if (idx >= items[i].size())
break;
ford(k, s, i)
dp[k] = max(dp[k], dp[k - i] + items[i][idx].fi);
if (--items[i][idx].se == 0)
idx++;
}
}
cout << dp[s];
}
}
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:98:25: 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]
98 | if (idx >= items[i].size())
| ~~~~^~~~~~~~~~~~~~~~~~
# | 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... |