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. jQuery简单介绍

    一.jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交 ...

  2. ios 个人开发者账户 给其他团队用坑爹的教程

    最新版本的 ios  支持 3个开发者证书 和 3个发布者证书  ,如果是多余3台电脑设备要真机调试,就比较麻烦 (手机支持100个设备) 解决方案就是: 在别人的电脑上要成功安装,须具备两个文件: ...

  3. MongoDB官方文档结构

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

  4. kickstart配置LINUX无人值守选项--rootpw

    linux kickstart rootpw密码可以使用明文,也可以使用加密过的值(密码为:IPPBXADMINROOT) 注意:在这里要使用加密过的值,否则安全性就太低了 rootpw --iscr ...

  5. dp入门题目

    本文文旨,如题... 转载请注明出处... HDOJ 1176 免费馅饼 http://acm.hdu.edu.cn/showproblem.php?pid=1176 类似数塔,从底往上推,每次都是从 ...

  6. python连接hbase

    安装HBase HBase是一个构建在HDFS上的分布式列存储系统,主要用于海量结构化数据存储.这里,我们的目标只是为Python访问HBase提供一个基本的环境,故直接下载二进制包,采用单机安装.下 ...

  7. Java第三阶段学习(一、IO流------File类)

    一.IO概述: 把内存中的数据存入到硬盘(持久化设备)中叫做:输出(写)Output操作.JAVA软件往电脑硬盘上走叫输出. 把硬盘中的数据读取到到内存里叫做:输入(读)Input操作.电脑硬盘上往J ...

  8. 解析文本文件 "r" 与 "rb" 模式的区别(Python)

    r,rb 那么在读文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r':默认值,表示从文件读取数据.'b':表示要读写二进制数据 2.读文件 进行读文件操作时,直到读到文档结束符(E ...

  9. Rookey.Frame之数据库及缓存配置

    上一篇中讨论了Rookey.Frame框架菜单配置功能,这一节我们继续学习Rookey.Frame框架的数据库连接配置. 之前介绍了Rookey.Frame框架支持跨多数据库,并且支持读写分离,不过目 ...

  10. Java学习笔记之:Struts2.0 环境搭建

    一.介绍 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互. 二 ...