Zb's birthday: Nanyang oj325 [DFS], zb Nanyang oj325dfs

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.