【DFS】(选不选)蓝桥杯第十四届-买瓜

 

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=35,INF=0x3f3f3f3f;
LL p[N];
LL a[N];
int n;
LL m;
int ans=INF;
void dfs(int u,LL sum,int cnt){
	if(sum==m){
		ans=min(ans,cnt);
		return ;
	}
	if(cnt>=ans||u>n||sum+p[u]<m||sum>m) return ;
	dfs(u+1,sum+a[u],cnt);
	dfs(u+1,sum+a[u]/2,cnt+1);
	dfs(u+1,sum,cnt);
}
int main(){
	cin>>n>>m;
	m*=2;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i]*=2;
	}
	sort(a+1,a+1+n,greater<int>());
	for(int i=n;i>=1;i--) p[i]=p[i+1]+a[i];
	dfs(1,0,0);
	if(ans==INF) cout<<"-1"<<endl;
	else cout<<ans<<endl;
	return 0;
}

相关推荐

  1. 三国游戏(

    2024-03-10 22:40:02       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 22:40:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 22:40:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 22:40:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 22:40:02       20 阅读

热门阅读

  1. excel 如何自动调整行间距 vba

    2024-03-10 22:40:02       20 阅读
  2. 【48天笔试强训】day8

    2024-03-10 22:40:02       22 阅读
  3. https 加密解密过程是什么?

    2024-03-10 22:40:02       21 阅读
  4. for-in,for-of

    2024-03-10 22:40:02       20 阅读
  5. GPT的磁盘管理

    2024-03-10 22:40:02       23 阅读
  6. volatile

    2024-03-10 22:40:02       21 阅读
  7. HTML5- 拖拽功能

    2024-03-10 22:40:02       20 阅读
  8. Flask基于配置文件添加项目config配置

    2024-03-10 22:40:02       19 阅读