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>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pill pair<int,ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
using namespace std;
const int N = 1e5 + 5;
const int S = 2e3 + 5;
const ll oo = 1e17;
template <typename T1, typename T2> bool maxi(T1 &a, T2 &b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 &b){if (a > b){a = b; return 1;} return 0;}
int n, s;
int v[N], w[N], k[N];
bool ar;
long long f[2][S], ans;
// ll btr(int i, int s_used){
// if (s_used > s) return -oo;
// if (i == n + 1) return 0;
// ll &res = f[i][s_used];
// if (res != -1) return res;
// int l = 1, r = k[i];
// res = btr(i + 1, s_used);
// ll compare;
// while(l <= r){
// int m = (l + r) >> 1;
// compare = btr(i + 1, s_used + w[i] * m) + v[i] * m;
// if (maxi(res, compare)){
// l = m + 1;
// } else r = m - 1;
// }
// return res;
// }
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> s >> n;
for (int i = 1; i <= n; i++){
cin >> v[i] >> w[i] >> k[i];
}
// mem(f, -1);
// cout << btr(1, 0);
for (int i = 1; i <= n; i++){
ar ^= 1;
for (int j = 0; j <= s; j++){
f[ar][j] = f[ar ^ 1][j];
int l = 1, r = k[i];
while (l <= r){
int m = (l + r) >> 1;
if (w[i] * m > j){
r = m - 1;
continue;
}
ll tmp = f[ar ^ 1][j - w[i] * m] + 1ll * v[i] * m;
if (maxi(f[ar][j], tmp)){
l = m + 1;
} else r = m - 1;
}
}
}
for (int j = 0; j <= s; j++)
maxi(ans, f[ar][j]);
cout << ans;
}
# | 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... |