This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
00 00 11 00 00 111111 00000 111111 000000
00 00 1111 0000 11 00 11 11 000000
0000 11 11 00 11 00000 111111 00
00 00 11111111 0000 11 00 11 11 00
00 00 11 11 00 00 111111 00000 11 11 00
**/
#include<bits/stdc++.h>
using namespace std;
void setIO(string s) {
freopen((s+".inp").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
void setIOusaco(string s) {
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
#define fto(i, a, b) for(int i = a; i <= b; ++i)
#define fdto(i, a, b) for(int i = a; i >= b; --i)
#define bugarr(a, i, j) cout << #a << "{" << i << "..." << j << "}:"; fto(k, i, j-1) cout << a[k] << ", "; cout << a[j] << endl;
#define ll long long
#define db double
#define ldb long double
#define ii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vt vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(i, a) for(auto &i : a)
#define sz(a) (int)a.size()
#define fast ios::sync_with_stdio(false); cin.tie(0)
template<typename T, typename V>
bool ckmin(T &a, V b) {return (b < a)? a = b, true : false;}
template<typename T, typename V>
bool ckmax(T &a, V b) {return (b > a)? a = b, true : false;}
int main() {
fast;
ll s, n;
cin >> s >> n;
vt<ll> v(n+1), w(n+1), k(n+1);
fto(i, 1, n) {
cin >> v[i] >> w[i] >> k[i];
}
if (n <= 2000) {
vt<vt<ll> > f(2, vt<ll>(s+1));
fto(i, 1, n) {
int li = i&1;
fto(j, 1, s) f[li][j] = f[li^1][j];
fto(sl, 1, min(k[i], s)) {
fto(j, w[i]*sl, s) {
f[li][j] = max(f[li][j], f[li^1][j-w[i]*sl] + v[i]*sl);
}
}
}
ll ans = 0;
fto(j, 1, s) ans = max(ans, f[n&1][j]);
cout << ans << '\n';
}
else {
vt<vt<pair<ll, int>> > f(2, vt<pair<ll, int> >(s+1));
fto(i, 1, n) {
int li = i&1;
fto(j, 1, s) {
f[li][j].ff = f[li^1][j].ff;
f[li][j].ss = 0;
}
fto(j, w[i], s) {
if (f[li][j-w[i]].ss < k[i]) {
if (ckmax(f[li][j].ff, f[li][j-w[i]].ff+v[i])) {
f[li][j].ss = f[li][j-w[i]].ss+1;
}
}
}
}
ll ans = 0;
fto(j, 1, s) ans = max(ans, f[n&1][j].ff);
cout << ans << '\n';
}
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:14:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | freopen((s+".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp: In function 'void setIOusaco(std::string)':
knapsack.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |