-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL070.cpp
45 lines (38 loc) · 915 Bytes
/
SCTDL070.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @file SCTDL070.cpp
* @author long ([email protected])
* @brief similar to knapsack problem
* @version 0.1
* @date 2023-03-24
*
* @copyright Copyright (c) 2023
*
*/
#include<bits/stdc++.h>
#define MAX 26
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
//set up input
int n, w;
cin >> w >> n;
int value[MAX], weight[MAX], dp[n+1][w+1];
for(int i=1; i <= n; i++) {
cin >> weight[i];
value[i] = weight[i];
}
// set up table to find
memset(dp, 0, sizeof(dp));
for(int i=1 ; i <= n; i++) {
for(int j=1; j <= w; j++) {
dp[i][j] = dp[i-1][j];
if(j >= weight[i]) {
dp[i][j] = max(dp[i][j], dp[i-1][j-weight[i]] + value[i]);
}
}
}
cout << dp[n][w] << endl;
}
}