Zb's birthday: Nanyang oj325 [DFS], zb Nanyang oj325dfs
Description
Today is the birthday of the acm team member zb on March 13, lunar July 5. Zb is training in Wuhan with C Xiaojia and never. He wanted to buy something for the two brothers to celebrate his birthday. After investigation, zb found that C Xiaojia and never both liked to eat watermelon, and they were a bunch of them, zb immediately decided to buy a pile of watermelon. When he was about to give the watermelon to C Xiaojia and never, he encountered a problem. never and C Xiaojia were not in the same place. They could only divide the watermelon into two heaps for them, to be fair to everyone, he wants to minimize the weight difference between the two stacks. The weight of each watermelon is known. Can you help him?
-
Input
-
Multiple groups of test data (<= 1500 ). Data ends with EOF
The first line inputs the number of watermelons N (1 ≤ N ≤ 20)
The second row has N numbers, W1 ,..., Wn (1 ≤ Wi ≤ 10000) represents the weight of each watermelon respectively
-
Output
-
Quality difference after the output is divided into two heaps
-
Sample Input
-
55 8 13 27 14
-
Sample output
-
3
#include <stdio.h>#define max(a,b) (a)>(b)?(a):(b)#define INF 0x3f3f3f3fint a[1010];int min,s;int N;void DFS(int t,int sum){if(t>N || sum>s/2) return ;min=max(min,sum);DFS(t+1,sum);DFS(t+1,sum+a[t]);}int main(){while(~scanf("%d",&N)){s=0;for(int i=0;i<N;++i){scanf("%d",a+i);s+=a[i];}min=-INF;DFS(0,0);printf("%d\n",s-min*2);}return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.