happy

Topic content:
Not buying tickets to the Olympic Games made YF sad. In order to make himself happy, he went to chat with people around him. Every time he chatted with someone, he would consume a certain amount of physical strength, but he would get a certain amount of happiness. YF tries to make himself as happy as possible, but once his stamina is exhausted (zero or negative), he just hangs up and has no joy at all. Now that Yk has 100 HP initially, how much happiness can he get at most?
enter description
The data is divided into multiple groups, for each group of data: the first row is n, which means there are n (0<n<21) friends of YK. The second line represents the physical effort spent chatting with each person, and the third line represents the happiness value each person can provide. The input ends with a 0.

output description
For each set of outputs, output a value, the maximum happiness value that YK can get.

input sample
3
1 21 79
20 30 25
4
100 100 100 100
1 2 3 4
0

Sample output
50
0

code
/*
Ideas:
Similar to the backpack problem, the stamina value has been increased from 1 to 99
*/

#include <iostream>
using namespace std;

#define max(a, b) a > b ? a : b
 
int main(){
	int n, i, j;
	int e [100], h [100];
	int D[100][100];
	 
	cin >> n;
	
	while(n != 0){
		for(i = 1; i <= n; i++){
			cin >> e[i];
		}
		for(i = 1; i <= n; i++){
			cin >> h[i];
		}
		
		for(i = 1; i <= n; i++){
			for(j = 1; j < 100; j++){
				if(j < e[i]){//
					D[i][j] = D[i - 1][j];
				}else{
					D[i][j] = max(D[i - 1][j], D[i - 1][j - e[i]] + h[i]);
				}
			}
		}
		
		cout << D[n][99] << endl; 	
			
		cin >> n;
	}
	
	
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325786438&siteId=291194637