summaryrefslogtreecommitdiff
path: root/acwing/5.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-24 15:35:13 +0800
committercrupest <crupest@outlook.com>2021-02-24 15:35:13 +0800
commitbffd9c6c642e2d95ec1dafb69b03505f6fbddc0d (patch)
tree3c546b5f3541217f2d13b5186a353df69fa2d2ec /acwing/5.cpp
parent39ae14dadf02109368ffeca4351828b7a79907df (diff)
downloadsolutions-bffd9c6c642e2d95ec1dafb69b03505f6fbddc0d.tar.gz
solutions-bffd9c6c642e2d95ec1dafb69b03505f6fbddc0d.tar.bz2
solutions-bffd9c6c642e2d95ec1dafb69b03505f6fbddc0d.zip
Add acwing problem 5.
Diffstat (limited to 'acwing/5.cpp')
-rw-r--r--acwing/5.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/acwing/5.cpp b/acwing/5.cpp
new file mode 100644
index 0000000..e451a2d
--- /dev/null
+++ b/acwing/5.cpp
@@ -0,0 +1,51 @@
+#include <algorithm>
+#include <iostream>
+
+int N, V;
+int v[1001];
+int w[1001];
+int s[1001];
+int states[2001];
+
+void CompletePack(int v, int w) {
+ for (int j = v; j <= V; j++) {
+ states[j] = std::max(states[j], states[j - v] + w);
+ }
+}
+
+void ZeroOnePack(int v, int w) {
+ for (int j = V; j >= v; j--) {
+ states[j] = std::max(states[j], states[j - v] + w);
+ }
+}
+
+int main() {
+ std::cin >> N >> V;
+
+ for (int i = 1; i <= N; i++) {
+ std::cin >> v[i] >> w[i] >> s[i];
+ }
+
+ for (int i = 1; i <= N; i++) {
+ if (v[i] * s[i] >= V) {
+ CompletePack(v[i], w[i]);
+ } else {
+ int k = 1;
+ int amount = s[i];
+
+ while (k < amount) {
+ ZeroOnePack(k * v[i], k * w[i]);
+ amount -= k;
+ k *= 2;
+ }
+
+ if (amount != 0) {
+ ZeroOnePack(amount * v[i], amount * w[i]);
+ }
+ }
+ }
+
+ std::cout << states[V];
+
+ return 0;
+}