#define eb emplace_back
#define pb push_back
#define pii pair<int,int>
#define sz(x) int((x).size())
#define ALL(x) (x).begin(),(x).end()
#define ln cout << '\n'
#define REP(i, a) for (int i = 0; i < int(a); i++)
#define FOR(i, a) for (int i = 1; i <= int(a); i++)
#define MEM(a,b) memset((a),(b),sizeof(a))
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
using namespace std;
#include <bits/stdc++.h>
using ll = long long;
using vi = vector<int>;
template<typename T> void rd(vector<T> &a) {for (auto &ele : a) cin >> ele;}
template<typename T> void writeln(vector<T> &a) {for (auto &ele : a) cout << ele << ' '; cout << "\n";}
#if __cplusplus > 201402L
template<typename... Args> void rd(Args&... args) {((cin >> args), ...);}
template<typename... Args> void write(Args... args) {((cout << args << " "), ...);}
template<typename... Args> void writeln(Args... args) {((cout << args << " "), ...); cout << "\n";}
#endif
const int maxn = 1e6 + 5;
ll v[maxn], w[maxn], k[maxn];
ll dp[2005];
struct item {
int v, k;
};
vector<item> b[2005];
signed main()
{
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
// int t;cin >> t;while(t--) solve();
int n, S;
cin >> S >> n;
FOR(i, n) {
int v, w, k;
rd(v, w, k);
b[w].pb({v, k});
}
for (int i = 1; i <= S; ++i) {
sort(ALL(b[i]), [](item & a, item & b) {
return a.v > b.v;
});
vector<pii> can_take; // (weight, value)
ll weight = 0, value = 0;
// writeln("weight");
for (auto [v, k] : b[i]) {
for (int c = 0; c < k; ++c) {
weight += i;
value += v;
if (weight >= S) break;
can_take.eb(weight, value);
}
if (weight >= S) break;
// writeln("item", weight, value);
}
if (!can_take.empty()) {
for (int j = S; j >= 1; --j) {
int l = -1, r = sz(can_take);
while (l + 1 < r) {
int mid = (l + r) >> 1;
if (can_take[mid].first <= j) l = mid;
else r = mid;
}
if (l >= 0 && l < sz(can_take)) {
auto [w, v] = can_take[l];
dp[j] = max(dp[j], dp[j - w] + v);
}
}
}
}
ll res = 0;
for (int i = 0; i <= S; ++i) {
res = max(res, dp[i]);
}
writeln(res);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |