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;}
void print(int x) {cout << x;}
void print(long long x) {cout << x;}
void print(unsigned x) {cout << x;}
void print(unsigned long long x) {cout << x;}
void print(double x) {cout << fixed << x;}
void print(long double x) {cout << fixed << x;}
void print(char x) {cout << "'" << x << "'";}
void print(string x) {cout << '"' << x << '"';}
void print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void print(const pair<T, V> &x) {cout << '{'; print(x.ff); cout << ", "; print(x.ss); cout << '}';}
template<typename T>
void print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? ", " : ""), print(i); cout << "}";}
void _print() {cout << "]" << endl;}
template <typename T, typename... V>
void _print(T t, V... v) {print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#ifdef TAP
#define bug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define bug(x...)
#endif
int _left = 0, _right;
struct segment_tree {
vt<ll> nodes, nho;
segment_tree(){}
void init(int n) {
nodes.assign((n+1)*4 +3, 0);
nho.assign((n+1)*4 +3, 0);
}
void upd(int root) {
if (nho[root] == 0) return;
ll y = nho[root];
nho[root] = 0;
nodes[root*2] += y;
nodes[root*2+1] += y;
nho[root*2] += y;
nho[root*2+1] += y;
}
void update(int i, int j, ll v, int root = 1, int left = _left, int right = _right) {
assert(root <= sz(nodes));
if (left >= i && right <= j) {
nodes[root] += v;
nho[root] += v;
return;
}
if (left > j || right < i) return;
upd(root);
int mid = (left+right)/2;
update(i, j, v, root*2, left, mid);
update(i, j, v, root*2+1, mid+1, right);
nodes[root] = max(nodes[root*2], nodes[root*2+1]);
}
ll query(int i, int j, int root = 1, int left = _left, int right = _right) {
assert(root <= sz(nodes));
if (left >= i && right <= j) return nodes[root];
if (left > j || right < i) return 0;
upd(root);
int mid = (left+right)/2;
return max(query(i, j, root*2, left, mid), query(i, j, root*2+1, mid+1, right));
}
};
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];
}
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];
segment_tree tree[w[i]];
fto(j, 0, w[i]-1) {
_right = (s/(w[i]+j));
tree[j].init(s/(w[i]+j));
}
fto(j, w[i], s) {
assert(j%w[i] <= w[i]);
ll sth = tree[j%w[i]].query((j/w[i])-1, (j/w[i])-1);
tree[j%w[i]].update((j/w[i])-1, (j/w[i])-1, -sth+f[li^1][j-w[i]]);
tree[j%w[i]].update(max(0LL, (j/w[i])-k[i]), (j/w[i])-1, v[i]);
f[li][j] = max(f[li][j], tree[j%w[i]].query(0, (j/w[i])-1));
}
}
ll ans = 0;
fto(j, 1, s) ans = max(ans, f[n&1][j]);
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... |