J - Watashi's BG

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Appoint description:
 

Description

Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer training camp, students are divided into several groups and each day one of the groups will design some problems to hold a contest. Today students of Group C are required to design the problems, and they spent the whole night to check to test data which made them very tired. Watashi decides to give some money as a reward to group C so that they can buy the lunch for free.

There are N days in the training schedule, and all students have booked their lunch for N days so we know how much money they will spend in each day. Now the leader of group C needs to decide how to use Watashi's money. Since the money is limited, it may not be possible that they can have free lunch every day. So each day the leader can choose to pay for the whole group's lunch by themselves or use Watashi's money. Of course, the leader wants to spend Watashi's money as much as possible, but he is too busy to write a program to calculate the maximum money he can spend from Watashi's reward. Can you help him?

Input

The input contains multiple test cases ( no more than 50 test cases ).
In each test case, first there are two integer, N ( 1 <= N <=30 ) , which is the number of training days, M ( 0 <= M <=10000000 ) , which is the reward money from Watashi.
Then there is a line containing N positive integers with the ith integer indicating the money group C need to pay for the lunch of the ith day. All these integers are no more than 10000000 and integers are seperated by a space.

Output

For each test case, output one line with an integer which is the maximum money group C can spend from Watashi's reward

Sample Input

3 10
8 4 5

Sample Output

9

本来是一个很简单的背包问题的,背包容量太大,就T了

于是就只有DFS然后搜索,注意剪枝

bool cmp(int i,int j)
{
return i>j;
}
int ans=;
int a[];
int n,m;
int vis[];
void dfs(int i,int v)
{
if(ans==m)
return;
if(v>m)
return;
if(v==m)
{
ans=v;
return;
}
int sum=;
for(int j=i+;j<n;j++)
{
if(!vis[j])
sum+=a[j];
}
if(v+sum<=ans)
return;
ans=max(ans,v);
for(int j=i+;j<n;j++)
{
if(vis[j])
continue;
vis[j]=;
dfs(j,v+a[j]);
vis[j]=;
}
}
int main()
{
while(cin>>n>>m)
{
ans=;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
for(int i=;i<n;i++)
{
vis[i]=;
dfs(i,a[i]);
vis[i]=;
}
cout<<ans<<endl;
}
}

ZOJ 3631 Watashi's BG DFS的更多相关文章

  1. [ZOJ 3631] Watashi's BG

    Watashi's BG Time Limit: 3 Seconds      Memory Limit: 65536 KB Watashi is the couch of ZJU-ICPC Team ...

  2. zoj 3620 Escape Time II dfs

    题目链接: 题目 Escape Time II Time Limit: 20 Sec Memory Limit: 256 MB 问题描述 There is a fire in LTR ' s home ...

  3. ZOJ 4124 拓扑排序+思维dfs

    ZOJ - 4124Median 题目大意:有n个元素,给出m对a>b的关系,问哪个元素可能是第(n+1)/2个元素,可能的元素位置相应输出1,反之输出0 省赛都过去两周了,现在才补这题,这题感 ...

  4. ZOJ 1002 Fire Net(dfs)

    嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...

  5. ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的 ...

  6. ZOJ 1008 Gnome Tetravex(DFS)

    题目链接 题意 : 将n*n个正方形进行排列,需要判断相邻的正方形的相邻三角形上边的数字是不是都相等. 思路 : 只知道是个深搜,一开始不知道怎么搜,后来看了题解才明白,就是说不是自己去搜,而是将给定 ...

  7. zoj 2734 Exchange Cards【dfs+剪枝】

    Exchange Cards Time Limit: 2 Seconds      Memory Limit: 65536 KB As a basketball fan, Mike is also f ...

  8. ZOJ 1709 Oil Deposits(dfs,连通块个数)

    Oil Deposits Time Limit: 2 Seconds      Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...

  9. ZOJ - 3816 Generalized Palindromic Number dfs

    Generalized Palindromic Number Time Limit: 2 Seconds                                     Memory Limi ...

随机推荐

  1. weblogic更改端口

    两种方式: 1.访问console控制台页面,进入“环境\服务器\需要修改端口的服务器如AdminServer”,修改监听端口,保存并激活更改即可: 2.修改配置文件,进入weblogic的域目录,如 ...

  2. mac上安装完成node,就升级好了npm,之后的设置

    1.打开终端输入: npm config list 找到npmrc globalconfig /usr/local/etc/npmrc 2.打开npmrc sudo vim /usr/local/et ...

  3. MongoDB官方文档结构

    本文展示MongoDB 3.6.4.0的官方Server文档的结构图——一眼可见完整的知识脉络图.不过,MongoDB除了Server的文档外,还有DRIVERS.CLOUD.TOOLS.DUIDES ...

  4. handlermethodargumentresolver

    http://www.cnblogs.com/fangjian0423/p/springMVC-request-param-analysis.html http://www.cnblogs.com/f ...

  5. Python 文件IO

    Python 文件I/O 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写到标准输出如下: #!/u ...

  6. git clone命令使用

    git clone命令使用 分类: 项目构建2013-06-26 15:43 38660人阅读 评论(2) 收藏 举报 GitClone git clone 命令参数: usage: git clon ...

  7. 安装window系统

    安装服务器系统,进入windowpe后将iso中sources,bootmgr,和boot拷贝到C盘,执行bootsect.exe  /nt60  c:,调试froad13的consle win8 改 ...

  8. android解决AVD中文路径无法启动问题

    在as中新建一个AVD,然而启动时却报错,总之是不能找到中文路径 然后这个虚拟设备被默认安装在了C盘我的用户李敏啊,而我用户名是中文名导致无法识别 解决办法,使用链接文件格式修改虚拟设备配置路径, 比 ...

  9. Elasticsearch: 权威指南---基础入门

    1.查看方式:GETURL:http://10.10.6.225:9200/?pretty pretty 在任意的查询字符串中增加pretty参数.会让Elasticsearch美化输出JSON结果以 ...

  10. HTML小工具

    一般可能用的到的符号代码: 符号 HTML 符号 HTML     & & < < > > ⁄ ⁄ " " ¸ ¸ ° ° ½ ½ ¼ ¼ ...