class Solution {
public:
/**
* @param m: An integer m denotes the size of a backpack
* @param A & V: Given n items with size A[i] and value V[i]
* @return: The maximum value
*/
int backPackII(int m, vector<int> A, vector<int> V) {
// write your code here
if(A.size()==0) return 0;
vector<vector<int> > DP(A.size(), vector<int>(m+1, 0));
int ret = 0;
if(A[0]<=m)
DP[0][A[0]] = V[0];
for(int i=1;i<A.size();i++) {
for(int j=0;j<A[i];j++)
DP[i][j] = DP[i-1][j];
for(int j=A[i];j<=m;j++) {
DP[i][j] = max(DP[i-1][j-A[i]] + V[i], DP[i-1][j]);
ret = max(ret, DP[i][j]);
}
}
return ret;
}
};
public:
/**
* @param m: An integer m denotes the size of a backpack
* @param A & V: Given n items with size A[i] and value V[i]
* @return: The maximum value
*/
int backPackII(int m, vector<int> A, vector<int> V) {
// write your code here
if(A.size()==0) return 0;
vector<vector<int> > DP(A.size(), vector<int>(m+1, 0));
int ret = 0;
if(A[0]<=m)
DP[0][A[0]] = V[0];
for(int i=1;i<A.size();i++) {
for(int j=0;j<A[i];j++)
DP[i][j] = DP[i-1][j];
for(int j=A[i];j<=m;j++) {
DP[i][j] = max(DP[i-1][j-A[i]] + V[i], DP[i-1][j]);
ret = max(ret, DP[i][j]);
}
}
return ret;
}
};