2019/10/8 今日头条笔试第3题
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/90b414121d7f998a5a9fc273a9d26b01.png)
#include <iostream>
#include <vector>
using namespace std;
int helper(int n,int m,vector<vector<int>>& dp,vector<vector<int>>& arm,int c,int x,int cur_m) {
if(x>=c) return 0;
if(cur_m<m&&n-arm[x][0]) {
dp[x][n] = max(dp[x+1][n]?dp[x+1][n]:helper(n,m,dp,arm,c,x+1,cur_m),dp[x+1][n-arm[x][0]]?dp[x+1][n-arm[x][0]]+arm[x][1]:
helper(n-arm[x][0],m,dp,arm,c,x+1,cur_m+1)+arm[x][1]);
}
else
dp[x][n] = dp[x+1][n]?dp[x+1][n]:helper(n,m,dp,arm,c,x+1,cur_m);
return dp[x][n];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin >> n >> m;
vector<vector<int>> arm;
int x,y,c = 0;
while(cin >> x >> y) {
vector<int> temp;
temp.push_back(x);
temp.push_back(y);
arm.push_back(temp);
++c;
}
int res = 0;
vector<vector<int>> dp(c,vector<int> (n,0));
res = helper(n,m,dp,arm,c,0,0);
cout << res << endl;
return 0;
}