Problem Description
Unfortunately YY gets ill, but he does not want to go to hospital. His girlfriend LMY gives him N kinds of medicine, which may be helpful. It is not a good idea to take all of them, since taking several different kinds of medicine may have side effect. Formally
speaking, for each subset S of the N kinds of medicine (excluding the empty set), it has a health value v(S). If YY chooses to take a combination T of the medicines, the final effect to his illness is the sum of health values of all non-empty subsets of T.



YY wants to be healthy as quickly as possible, so the final effect of the medicines he takes should be as large as possible. Of course, YY may choose taking nothing to have a zero final effect, if he is too unlucky to achieve a positive one…
 
Input
Input contains multiple test cases.



For each test case, the first line contains a positive integer N (N≤16), the number of different kinds of medicine YY received from LMY.



The second line contains a single integer M (0≤M≤2N).



M lines follow, representing a list of health values.



Each of the M lines contains 2 integers, s (1≤s<2N)and v (-10000≤v≤10000), indicating a subset of the N kinds of medicine and its health value. Write s in binary representation and add leading zeros if needed to make it exactly N binary digits. If the ith binary
digit of s is 1, then the subset it represents includes the ith kind of medicine; otherwise it does not.



It is guaranteed that no two lines of the list describe the same subset. All non-empty subsets that do not appear in the list have health value 0.



Input ends with N=0.
 
Output
For each test case, output one line with only one integer, the maximum final effect that can be achieved.
 
Sample Input
2
3
1 10
2 -1
3 100
0
 
Sample Output
109
 
Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
int dp[17][1<<17];
int n,m; int main()
{
int s,u,v;
while(~scanf("%d",&n)&&n)
{
memset(dp,0,sizeof(dp));
scanf("%d",&m);
int s=(1<<n)-1;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
dp[0][u]=v;
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=s;j++)
{
if(j&(1<<(i-1)))
dp[i][j]=dp[i-1][j]+dp[i-1][j-(1<<(i-1))];
else
dp[i][j]=dp[i-1][j];
}
}
int ans=-INT_MAX;
for(int i=0;i<=s;i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans);
}
return 0;
}

HDU 3217 Health(状压DP)的更多相关文章

  1. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  2. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  3. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  4. HDU - 5117 Fluorescent(状压dp+思维)

    原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...

  5. hdu 4114(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...

  6. HDU 3091 - Necklace - [状压DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  7. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  8. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  9. hdu 4628 Pieces 状压dp

    题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...

  10. HDU 5045 Contest(状压DP)

    Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...

随机推荐

  1. 重操JS旧业第八弹:面向对象与继承

    js里面没有语言语法层面的继承机制,但这并不意味着js就不能实现继承,利用js属性和方法动态性来模拟实现继承,通过总结大概有如下方法实现: 1 原型链继承 我们知道原型在对象中扮演着重要的角色,函数本 ...

  2. 调用QQ截图

    var SHExecInfo: SHELLEXECUTEINFO; begin //截图前隐藏主程序窗口 Form1.Hide; //等待截图执行完成 SHExecInfo.cbSize := siz ...

  3. jquery 下拉多选插件

    Jquery多选下拉列表插件jquery multiselect功能介绍及使用 Chosen 替代样式表 Bootstrap Chosen

  4. QT使用scrollarea显示图片,完美解决方案

    需求: 在界面上点击“显示图片”按钮,会调用scrollarea窗口显示图片,窗口大小能根据图片大小自动调整,但是最大为1024*768,图片过大就要有滚动条来显示 IDE环境: QT Creator ...

  5. cuzysdk购物模块 36kr+本期背景图

    说好的剧透,虽然来的稍微晚不少 cuzysdk(www.cuzy.com) 是一个手机淘宝客sdk,通过使用cuzy,可以获取taobao平台的推广商品数据,移动开发者把推广的商品数据呈现给用户,用户 ...

  6. 显示器 Linux 性能 18 (一个命令行工具传递)

    对于系统和网络管理员来说每天监控和调试Linux系统的性能问题是一项繁重的工作.在IT领域作为一名Linux系统的管理员工作5年后,我逐渐认识到监控和保持系统启动并执行是多么的不easy.基于此原因. ...

  7. PHP从数据库获取的下拉树

    <?php include "config.php"; include "mysql.php"; $db = new Mysql('test'); //几 ...

  8. OpenCV在矩阵上的卷积

    转载请注明出处!!!http://blog.csdn.net/zhonghuan1992 OpenCV在矩阵上的卷积 在openCV官网上说是戴面具,事实上就是又一次计算一下矩阵中的每个value,那 ...

  9. Java反射机制的使用方法

    Java的反射机制同意你在程序执行的过程中获取类定义的细节.有时候在程序执行的时候才得知要调用哪个方法,这时候反射机制就派上用场了. 获取类 类的获取方法有下面几种: forName().通过Clas ...

  10. Richard Stallman与洪峰谈黑客道培训实录_业界_科技时代_新浪网

    Richard Stallman与洪峰谈黑客道培训实录_业界_科技时代_新浪网 Richard Stallman与洪峰谈黑客道培训实录