1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
/** nyoj: 654*/
//c header
#include <cstdlib>
#include <cstdio>
#include <cstring>
//cpp header
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define M 1100000 //Max bag's Capacity
#define N 120 //Max Item's amount
#define CLS(x,v) memset(x,v,sizeof(x))
typedef pair<int,int> ppi;
/**Cap is the bag's Capacity; SumCost is the sum of Item's cost*/
int dp[M],Cap,SumCost;
/** first is cost ,second is weight*/
int cmp(ppi x,ppi y)
{
//return true will be Swap elements
return x.first>y.first;
}
int ZeroOnePack(int cost,int weight)
{
SumCost-=cost;
int bound=max(Cap-SumCost,cost);
for(int v=Cap;v>=bound;--v)
dp[v]=max(dp[v],dp[v-cost]+weight);
}
int solve(vector<ppi> &Items)
{
CLS(dp,0);
for(int i=0;i<Items.size();i++)
ZeroOnePack(Items[i].first,Items[i].second);
//return Answer
return dp[Cap];
}
int main()
{
int T,n,cost,weight;
vector<ppi>Items;
//large input take effect obviously
ios::sync_with_stdio(false);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&Cap);
SumCost=0;
Items.clear();
for(int i=0;i<n;i++)
{
scanf("%d%d",&cost,&weight);
SumCost+=cost;
Items.push_back(make_pair(cost,weight));
}
sort(Items.begin(),Items.end(),cmp);
printf("Max experience: %d\n",solve(Items));
}
return 0;
}
|