이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#define int long long
#define size() size() * 1ll
#define all(x) (x).begin(), (x).end()
#define allr(x, sz) (x) + 1, (x) + 1 + sz
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define MASK(x) (1LL<<(x))
#define BIT(x,i) (((x)>>(i))&1)
#define numbit(x) __builtin_popcountll(x)
using namespace std;
const int N = 1e5 + 1;
const int M = 2e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
template<class t>
bool mini(t &x,t y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
template<class t>
bool maxi(t &x,t y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
void file() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
return ;
}
int n, m;
int v[N], w[N], k[N];
int dp[N][M];
void DP(int i, vi &f) {
for (int j = w[i]; j <= m; j++) {
int l = 1, r = min(j / w[i], k[i]);
int vall = dp[i][j - w[i] * l] + v[i] * l;
int valr = dp[i][j - w[i] * r] + v[i] * r;
for (int t = 30; t; t--) {
int midl = (l + l + r) / 3;
int midr = (l + r + r) / 3;
vall = dp[i][j - w[i] * midl] + v[i] * midl;
valr = dp[i][j - w[i] * midr] + v[i] * midr;
maxi(f[j], vall);
maxi(f[j], valr);
if (vall <= valr) l = midl; else r = midr;
}
for (int it = l; it <= r; it++) {
maxi(f[j], dp[i][j - w[i] * it] + v[i] * it);
}
}
}
void solve(int test = -1) {
cin >> m >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i] >> w[i] >> k[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++) {
maxi(dp[i][j], dp[i - 1][j]);
}
vi f(m + 1, 0);
DP(i, f);
for (int j = w[i]; j <= m; j++) {
maxi(dp[i][j], f[j]);
}
// for (int j = 0; j <= m; j++) cout << dp[i][j] << " \n"[j == m];
}
cout << dp[n][m];
}
int32_t main() {
file();
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
solve(i);
}
return 0;
}
/*
20 3
5000 15 1
100 1 3
50 1 4
WRONG
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5000 5000 5000 5000 5000 5000
0 100 200 200 200 200 200 200 200 200 200 200 200 200 200 5000 5000 5200 5200 5200 5200
0 100 200 200 300 350 350 350 350 350 350 350 350 350 350 5000 5000 5200 5200 5300 5350
RIGHT
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5000 5000 5000 5000 5000 5000
0 100 200 300 300 300 300 300 300 300 300 300 300 300 300 5000 5100 5200 5300 5300 5300
0 100 200 300 350 400 450 500 500 500 500 500 500 500 500 5000 5100 5200 5300 5350 5400
*/
# | 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... |