选择第二个政党和第四个
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<deque> #include<set> #include<map> #include<ctime> #define LL long long #define inf 0x7ffffff #define pa pair<int,int> #define pi 3.1415926535897932384626433832795028841971 using namespace std; inline LL read() { LL x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } inline void write(LL a) { if (a<0){printf("-");a=-a;} if (a>=10)write(a/10); putchar(a%10+‘0‘); } inline void writeln(LL a){write(a);printf("\n");} int n,s; int a[500]; bool f[100010]; int main() { n=read(); for (int i=1;i<=n;i++)a[i]=read(),s+=a[i]; sort(a+1,a+n+1,greater<int>()); f[0]=1; for (int i=1;i<=n;i++) { for (int j=100000;j>=a[i];j--) if (j-a[i]<=s/2) { f[j]=f[j]|f[j-a[i]]; } } for (int i=100000;i>=0;i--) { if (f[i]) { printf("%d\n",i); return 0; } } }
原文:http://www.cnblogs.com/zhber/p/4167851.html