题目不难,深度优先搜索。代码是参考别人写的。
题目链接:http://acm.jlu.edu.cn/joj/showproblem.php?pid=2510
#include <iostream>
using namespace std;
int a[50], vist[55], n;
int sum = 0;
int MAXN = 0;
void dfs(int cur, int maxn)
{
int i;
if(cur == n)
{
if((sum - maxn)*maxn>(sum - MAXN)*MAXN)
{
MAXN = maxn;
return;
}
}
else
{
for(i = cur; i < n; i++)
{
if(!vist[i])
{
vist[i] = 1;
dfs(i+1,maxn + a[i]);
vist[i] = 0;
}
}
}
}
int main()
{
int i;
while(cin>>n)
{
memset(vist,0,sizeof(vist));
sum = 0;
for(i = 0; i < n; i++)
{
cin>>a[i];
sum += a[i];
}
MAXN = 0;
dfs(0, 0);
cout<<(sum-MAXN)*MAXN<<endl;
}
return 0;
}