H. Distribution

H.  Distribution

Time Limit: 1500MS

Description

One day, Obama dreamed that he and Clinton went together to steal jewellery, got n pieces of the jewellerys. How to divide stolen jewellerys became a problem,because the estimated value of each pieces of the jewellery are not the same to them.If the total value of the jewellerys Obama received according to his standard is not the same as the total value of jewellerys Clinton received according to Clinton’s standard, it will lead to a contradiction; what is more, they will fight with each other. Finally, they asked their leader---myj to help them to divide these stolen jewellerys.Your task is to judge whether myj can help them divide those jewellerys successfully.

 

Input

There are multiple test cases. The first line of each test case contains a positive integer n(0<n≤40), indicating there are n stolen jewellerys There are n lines followed. The (i+1)-th line contains two numbers ai and oi(0≤ai,oi≤200),indicting the i-th stolen jewellery’s value in Obama and Clinton’s standards respectively.

Output

For each test case, print a line containing a word “YES” if myj can divide the stolen jewellerys successfully, otherwise print “NO”.

Sample Input

Sample Output

4

50 38

17 32

29 21

5 6

NO

 

主意:ai和oi分别为两个人对珠宝的估计价

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int a[44],b[44];
int n;
bool flag;
class Node
{
public:
      int a,b;   
}node[44];
void DFS( int t, int sum1 , int sum )
{
    if( sum1 >= sum || t == n ) 
    {
        if( sum1 == sum ) flag = true;
        return ;
    }
    if( flag ) return;
    DFS( t + 1 , sum1 + node[t].a , sum - node[t].b );//if(sum1<sum)
    DFS( t + 1 , sum1 , sum );//if(sum1>sum)
}
bool cmp( Node x, Node y )
{
    return x.b > y.b;   
}
int main( )
{
    while( scanf( "%d",&n )==1 )
    {
         int sum = 0;
         for( int i = 0 ; i < n ; i ++ )
         {
              scanf( "%d %d",&node[i].a,&node[i].b );   
              sum += node[i].b;   
         }   
         sort( node , node + n , cmp );
         flag = false;
         int sum1 = 0 ;
         DFS( 0 ,sum1, sum );
         if( flag )
         printf( "YES\n" );
         else printf( "NO\n" ); 
    }
    return 0;   
}

posted @ 2012-08-06 13:52  jiai  Views(210)  Comments(0Edit  收藏  举报