#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 1007;
int n, c, a[maxn], ans;
LL sum[maxn];
void dfs(int cnt, int w) { //cnt下标,w剩余容量
if(w < 0) return ;
if(cnt == 1) {
if(w >= a[1]) w -= a[1];
ans = min(ans, w);
return ;
}
if(w >= sum[cnt]) {
w -= sum[cnt];
ans = min(ans, w);
return ;
}
dfs(cnt-1, w-a[cnt]);
dfs(cnt-1, w);
}
int main() {
while(~scanf("%d%d", &n, &c)) {
ans = (1<<30)+1;
sum[0] = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
sum[i] = sum[i-1] + a[i];
}
if(c < a[1]) {
printf("0\n");
continue;
}
int cnt = n;
for(int i = n; i >= 1; --i) {
if(a[i] <= c) {
cnt = i; break;
}
}
dfs(cnt, c);
printf("%d\n", c-ans);
}
return 0;
}
POJ 3172 (dfs写0-1背包)
最新推荐文章于 2019-08-09 21:34:22 发布
本文介绍了一个解决背包问题的递归算法实现。通过定义物品数量数组和容量限制,使用深度优先搜索策略来寻找最优解,最小化剩余背包容量。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Dify
AI应用
Agent编排
Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中
4604

被折叠的 条评论
为什么被折叠?



