Algorithm training_1049 (Luogu)

 

 Complete code:

#include <iostream>
#include <cstdio>
using namespace std;
int m, n; // Data scale
int f [20010]; // This is the state (related to the residual value), indicating that the volume is f [j] The minimum remaining value
int w [40]; // The volume used to hold each backpack
int main () {
int i, j; // The backpack problem is generally that the volume of the first i items is limited by j The best
cin >> m >> n; // Data scale
for (i = 1; i <= n; i ++) {
cin >> w [i];
}
for (i = 1; i <= n; i ++) {
for (j = m; j> = w [i]; j--) {
// Note: here must be from m to w [i], otherwise an object will be loaded into the box multiple times, see example 1
if (f [j] <f [j-w [i]] + w [i]) {
f [j] = f [j-w [i]] + w [i];
}
}
}
cout << m-f [m];
}

Guess you like

Origin www.cnblogs.com/WAsbry/p/12683624.html