Accepted Necklace

来源:互联网 发布:2016三毛淘宝小号网址 编辑:程序博客网 时间:2024/05/18 14:15

Problem 82 » Accepted Necklace 查看标程

Description

I have N precious stones, and plan to use K of them to make anecklace for my mother, but she won't accept a necklace which istoo heavy. Given the value and the weight of each precious stone,please help me find out the most valuable necklace my mother willaccept.

INPUT

Thefirst line of input is the number of cases.
For each case, the first line contains two integers N (N<= 20), the total number of stones, and K (K<= N), the exact number of stones to make anecklace.
Then N lines follow, each containing two integers: a(a<=1000), representing the value of each preciousstone, and b (b<=1000), its weight.
The last line of each case contains an integer W, the maximumweight my mother will accept, W <=1000.

OUTPUT

Foreach case, output the highest possible value of thenecklace.

SAMPLE INPUT

1 2 1 1 1 1 1 3 

SAMPLE OUTPUT

1 

HINT

#include<iostream>
using namespace std;
struct node
{
  int v;
  int w;
}p[21];
int n,W,k,s;
void dps(int v,int w,int num,int j)
{
   int i;
   if(w>W)
   return;
   if(num>k)
   return;
   if(s<v)
    s=v;
  for(i=j;i<n;i++)
   dps(v+p[i].v,w+p[i].w,num+1,i+1);

}
int main()
{
    intt,i;
 cin>>t;
 while(t--)
 {
  s=0;
 cin>>n>>k;
  for(i=0;i<n;i++)
  cin>>p[i].v>>p[i].w;
  cin>>W;
  dps(0,0,0,0);
 cout<<s<<endl;
 }

}

0 0
原创粉丝点击