Collecting one’s own plants for use as herbal medicines is perhaps one of the most self-empowering things a person can do, as it implies that they have taken the time and effort to learn about the uses and virtues of the plant and how it might benefit them, how to identify it in its native habitat or how to cultivate it in a garden, and how to prepare it as medicine. It also implies that a person has chosen to take responsibility for their own health and well being, rather than entirely surrender that faculty to another. Consider several different herbs. Each of them has a certain time which needs to be gathered, to be prepared and to be processed. Meanwhile a detailed analysis presents scores as evaluations of each herbs. Our time is running out. The only goal is to maximize the sum of scores for herbs which we can get within a limited time.
Input
There are at most ten test cases.
For each case, the first line consists two integers, the total number of different herbs and the time limit.
The i-th line of the following n line consists two non-negative integers. The first one is the time we need to gather and prepare the i-th herb, and the second one is its score.
The total number of different herbs should be no more than 100
All of the other numbers read in are uniform random and should not be more than 10^9
.
Output
For each test case, output an integer as the maximum sum of scores.
Sample Input
3 70
71 100
69 1
1 2
Sample Output
3
#include
#include
#include
#include
using namespace std;
const int maxn=105;
#define LL long long
int n,V;
LL sumv[maxn],sumw[maxn];
struct Node
{
int w,v;
bool operator<(const Node &temp)const
{
return w>temp.w;
}
};
Node bag[maxn];
LL ans=0;
void dfs(int pos,int limit,LL get) //limit剩下的背包重量
{
if(pos==n){
if(limit>=bag[pos].w)
ans=max(ans,(LL)get+bag[pos].v);
else
ans=max(ans,get);
}
if(sumw[pos]<=limit){ //剩下的背包的重量刚好装下剩下的所有的物品
ans=max(ans,get+sumv[pos]);
return;
}
if(get+sumv[pos]<=ans)//得到的最大的价值 大于 现在得到的价值加+剩下的所有物品的价值和
return;
dfs(pos+1,limit,get);
if(limit>=bag[pos].w)//能装下第pos个物品
dfs(pos+1,limit-bag[pos].w,get+bag[pos].v);
}
int main()
{
while(scanf("%d%d",&n,&V)!=EOF){
for(int i=1;i<=n;i++){
scanf("%d%d",&bag[i].w,&bag[i].v);
}
sort(bag+1,bag+1+n);
sumw[n+1]=0;
sumv[n+1]=0;
for(int i=n;i>=1;i--){
sumv[i]=sumv[i+1]+bag[i].v;
sumw[i]=sumw[i+1]+bag[i].w;
}
ans=0;
dfs(1,V,0);
printf("%lld\n",ans);
}
return 0;
}
/*
#include
#include
#include
using namespace std;
const int N=105;
int n,V;
long long maxvalue;
struct node
{
int w,v;
double wv;
}A[N];
bool cmp(node a,node b)
{
return a.wv>b.wv; //按价值比排序
}
void dfs(int index,long long sumw,long long sumv)
{
maxvalue=max(maxvalue,sumv);
if(index>n)
return; //搜索完所有点后返回
if(index+1<=n){
long long temp=(V-sumw)/A[index+1].w+1; //背包剩下的空间除以下一个物品的重量,看能放下多少个下一个物品
if(sumv+temp*A[index+1].vreturn;
}
if(A[index].w+sumw<=V) //能装下这一个,则dfs下一个
dfs(index+1,A[index].w+sumw,A[index].v+sumv);
dfs(index+1,sumw,sumv);
}
int main()
{
while(~scanf("%d%d",&n,&V))
{
for(int i=1; i<=n; i++)
{
scanf("%d%d",&A[i].w,&A[i].v);
A[i].wv=(double)A[i].v/A[i].w; //按比值排序
}
sort(A+1,A+n+1,cmp);
maxvalue=0;
dfs(1,0,0);
printf("%lld\n",maxvalue);
}
return 0;
}
*/