3358高数Umaru系列(9)——哈士奇(DP)

Description

由于高数巨养的喵星人太傲娇了,要天天吃新鲜猫粮而且还经常欺负高数巨,所以高数巨决定买几条哈士奇尝尝鲜。这天高数巨来到了二手狗市场买哈士奇,高数巨看完了所有的哈士奇,记下了每条哈士奇的价格,并根据对它们的好感程度给它们每只都赋予了一个萌值。高数现在手里有X元,她想通过购买若干条哈士奇来获得尽可能多的萌值。现在给定高数巨手里的钱X以及N条哈士奇的价格和萌值,求高数巨最多可获得多少萌值

Input

 多组输入。

对于每组输入,第一行有两个整数N,X(1 < = N < = 100,1 < = X < = 1000),分别表示哈士奇的数量和高数巨的钱数

接下来的N行每行有两个整数Pi,Mi(1 < = Pi,Mi < = 100),分别表示第i条哈士奇的价格和萌值

Output

对于每组数据,输出一个整数,表示高数巨最多可以获得的萌值,每组输出占一行

Sample

Input 

2 100
50 20
60 40
3 100
20 55
20 35
90 95
1 10
20 50

Output 

40
95
0

Hint

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string>
 4 #include <string.h>
 5 #include <algorithm>
 6 #include <math.h>
 7 #include <map>
 8 #include <vector>
 9 
10 using namespace std;
11 
12 struct node
13 {
14     int jia, meng;
15 } ha[105];
16 
17 int re[1005];
18 
19 int main()
20 {
21     int n, x, i, j;
22     while(cin >> n >> x)
23     {
24         for(i=0; i<n; i++)
25         {
26             cin >> ha[i].jia >> ha[i].meng;
27         }
28         memset(re, 0, sizeof(re));
29         for(i=0; i<n; i++)
30         {
31             for(j=x; j>=ha[i].jia; j--)
32             {
33                 re[j] = max(re[j], re[j-ha[i].jia] + ha[i].meng);
34             }
35         }
36         cout << re[x] << endl;
37     }
38 
39     return 0;
40 }
原文地址:https://www.cnblogs.com/0xiaoyu/p/14089913.html