描述
输入格式
以后N行,每行一个正整数表示G[i],G[i]<= 2^31-1。
输出格式
测试样例1
输入
20 5
7
5
4
18
1
输出
19
备注
对于40%的数据 W<=2^26
对于100%的数据 N<=45 W<=2^31-1
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define LL long long 8 #define HK_Reporter main 9 using namespace std; 10 const int mxn=60; 11 LL read(){ 12 LL x=0,f=1; char ch=getchar(); 13 while(ch<'0' || ch>'9')ch=getchar(); 14 if(ch=='-'){f=-1;ch=getchar();} 15 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 16 return x*f; 17 } 18 int n; 19 LL w; 20 LL a[mxn]; 21 LL res[5000000],cnt; 22 int find(LL x){ 23 int l=1,r=cnt; 24 int mid; 25 while(l<=r){ 26 mid=(l+r)>>1; 27 if(res[mid]<x)l=mid+1; 28 else if(res[mid]>x) r=mid-1; 29 } 30 return l-1; 31 } 32 LL ans=0;int mid; 33 int bignews=0; 34 void dfs(int now,int limit,LL wt){ 35 bignews++; 36 if(bignews>10000000){ 37 printf("%lld\n",ans); 38 exit(0); 39 } 40 if(now>limit){ 41 if(limit==mid){ 42 res[++cnt]=wt; 43 return; 44 } 45 int tmp=find(w-wt); 46 ans=max(ans,wt+res[tmp]); 47 return; 48 } 49 dfs(now+1,limit,wt);//不选 50 if(wt+a[now]<=w)dfs(now+1,limit,wt+a[now]);//选 51 return; 52 } 53 int HK_Reporter(){ 54 w=read();n=read(); 55 int i,j; 56 for(i=1;i<=n;i++){ 57 a[i]=read(); 58 } 59 mid=n/2; 60 dfs(1,mid,0); 61 sort(res+1,res+cnt+1); 62 dfs(mid+1,n,0); 63 printf("%lld\n",ans); 64 return 0; 65 }