#include #include //#include using std::vector; int optimalWeight(int W, vector &w2) { int j = w2.size(); // int value[W + 1][j + 1]; vector< vector > value(W + 1, vector(j + 1)); for (int i = 0; i < j + 1; i++) { for (int w = 0; w < W + 1; w++) { if (i == 0 || w == 0) { value[w][i] = 0; continue; } value[w][i] = value[w][i - 1]; if (w2[i - 1] <= w) { int val = value[w - w2[i - 1]][i - 1] + w2[i - 1]; if (value[w][i] < val) { value[w][i] = val; } } } } return value[W][j]; } //TEST(Knapsack, t1) { // vector w = { 1, 4, 8 }; // int W = 10; // ASSERT_EQ(9, optimalWeight(W, w)); //} int main() { int n, W; std::cin >> W >> n; vector w(n); for (int i = 0; i < n; i++) { std::cin >> w[i]; } std::cout << optimalWeight(W, w) << '\n'; }