题解:AT_abc390_e [ABC390E] Vitamin Balance
先将
最后再枚举三种维生素对应热量消耗,找出答案。
时间复杂度为
#include <bits/stdc++.h>
#define range(i, b, e) for (auto i(b); i != (e); i++)
#define rep(i, b, e) for (auto i(b); i <= (e); i++)
#define req(i, b, e) for (auto i(b); i >= (e); i--)
using namespace std;
typedef long long LL;
const int N = 5e3, V = 5e3;
int n, v;
LL DP[3][V+5];
LL ans;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> v;
for (int i = 1, vit, typ, ka; i <= n; i++) {
cin >> typ >> vit >> ka;
typ--; //原编号为 1 2 3,变为 0 1 2。
req(k, v, ka) DP[typ][k] = max(DP[typ][k], DP[typ][k-ka] + vit); //分开处理。
}
rep(i, 1, v) rep(j, 1, v-i) {
int k = v-i-j; //确定了其中两个的热量,剩下的一个的热量也能确定。
ans = max(ans, min({DP[0][i], DP[1][j], DP[2][k]}));
}
cout << ans;
return 0;
}