#include <bits/stdc++.h>
#define pb push_back
#define MAX 1000001
#define lim 1000
#define read freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int ary[50],target,n,maxsum,big;
vector<int>ans,temp;
int backtrack(int i,int sum)
{
if(i==n) return sum;
int a,b;
if(sum+ary[i]<=target) a=backtrack(i+1,sum+ary[i]);
else a=backtrack(i+1,sum);
b=backtrack(i+1,sum);
return max(a,b);
}
void backtrack2(int i,int sum)
{
if(i==n) return ;
if(ary[i]+sum==big)
{
temp.pb(ary[i]);
ans=temp;
temp.pop_back();
return ;
}
// if(ary[i]+sum>target) return ;
// int a,b;
if(sum+ary[i]<=target)
{
temp.pb(ary[i]);
backtrack2(i+1,sum+ary[i]);
temp.pop_back();
}
else backtrack2(i+1,sum);
backtrack2(i+1,sum);
}
int main()
{
// read
while(scanf("%d %d",&target,&n)==2)
{
for(int i=0; i<n; i++) scanf("%d",ary+i);
ans.clear();
temp.clear();
big=backtrack(0,0);
backtrack2(0,0);
// cout<<ans.size()<<endl;
for(int i=0; i<ans.size(); i++) printf("%d ",ans[i]);
printf("sum:%d\n",big);
}
return 0;
}
#define pb push_back
#define MAX 1000001
#define lim 1000
#define read freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int ary[50],target,n,maxsum,big;
vector<int>ans,temp;
int backtrack(int i,int sum)
{
if(i==n) return sum;
int a,b;
if(sum+ary[i]<=target) a=backtrack(i+1,sum+ary[i]);
else a=backtrack(i+1,sum);
b=backtrack(i+1,sum);
return max(a,b);
}
void backtrack2(int i,int sum)
{
if(i==n) return ;
if(ary[i]+sum==big)
{
temp.pb(ary[i]);
ans=temp;
temp.pop_back();
return ;
}
// if(ary[i]+sum>target) return ;
// int a,b;
if(sum+ary[i]<=target)
{
temp.pb(ary[i]);
backtrack2(i+1,sum+ary[i]);
temp.pop_back();
}
else backtrack2(i+1,sum);
backtrack2(i+1,sum);
}
int main()
{
// read
while(scanf("%d %d",&target,&n)==2)
{
for(int i=0; i<n; i++) scanf("%d",ary+i);
ans.clear();
temp.clear();
big=backtrack(0,0);
backtrack2(0,0);
// cout<<ans.size()<<endl;
for(int i=0; i<ans.size(); i++) printf("%d ",ans[i]);
printf("sum:%d\n",big);
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন