2019/10/8 今日头条笔试第3题

2019/10/8 今日头条笔试第3题

在这里插入图片描述

#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;
}

猜你喜欢

转载自blog.csdn.net/zhc_24/article/details/82977099