# XDU1172_01背包

## 题目描述

BlacKin and GKCY are going hiking together.
Besides their personal items, there are some items they share together.
They decided to devide those shared items into two parts.
So each of them has to carry their own items and a part of shared items.
To make it fair, they hope the weight difference between those two parts can be as small as possible.
Now they want to know how small it can be.

## 输入

Multiple test cases, please read until EOF
For each test case:
First line a single integer n (1 <= n <= 200), number of shared items.
Second line n integers, Wi (1 <= Wi <= 200), weight of each item.

## 输出

For each test case:
One line a single integer, the smallest difference between two part.

## 样例输入

1
7
3
1 2 3
3
11 1 2


## 样例输出

7
0
8
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n){
int v[n+1],w=0;
int dp[20010];
memset(dp,0x3f,sizeof(dp));
for(int i=1;i<=n;i++){
cin>>v[i];
w+=v[i];
}
dp[0]=0;
for(int i=1;i<=n;i++){
for(int j=w/2;j>=v[i];j--)
dp[j]=min(dp[j],dp[j-v[i]]+v[i]);
}
for(int i=w/2;i>=0;i--){
if(dp[i]<=w/2){
printf("%d\n",w-2*i);
break;
}
}
}
}
/*int main()
{
int n;
while(cin>>n){
int v[n+1],w=0;
int dp[n+1][20010];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
cin>>v[i];
w+=v[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=w/2;j++){
if(j<v[i]) dp[i][j]=dp[i-1][j];
else dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[i]]+v[i]);
}
}
int t=w-2*dp[n][w/2];
cout<<t<<endl;
}
}*/
/*int main()
{
int n;
while(cin>>n){
int v[n+1],w=0;
int dp[20010];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
cin>>v[i];
w+=v[i];
}
for(int i=1;i<=n;i++){
for(int j=w/2;j>=v[i];j--){
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
}
}
printf("%d\n",w-2*dp[w/2]);
}
}*/

posted @ 2017-04-21 17:04  despair_ghost  阅读(135)  评论(0编辑  收藏  举报