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 = 1e5 + 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;
vector <int> v, w, k;
bool ar;
vector <vector<ll>> f;
ll ans;
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> s >> n;
v.assign(n, 0);
w.assign(n, 0);
k.assign(n, 0);
for (int i = 0; i < n; i++){
cin >> v[i] >> w[i] >> k[i];
}
f.assign(2, vector <ll> (s + 1, 0));
for (int i = 0; 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... |