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 problembecause 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<n40), indicating there are n stolen jewellerys There are n lines followed. The (i+1)-th line contains two numbers ai and oi(0ai,oi200),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

 

 

代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
int flag;
int n;
struct node
{
  int a,b;    
}T[45];

bool cmp(node A,node B)
{
  return A.b>B.b;  
}

void DFS(int t,int sum1,int sum)
{
   if(flag)
   return ;
   if(sum1>=sum||t==n)
   {
      if(sum1==sum) 
   flag=1;
   return ;     
   }   
   DFS(t+1,sum1+T[t].a,sum-T[t].b);//选第t件;
   DFS(t+1,sum1,sum);//不选第t件;
}

int main()
{

   while(scanf("%d",&n)!=-1)
   {
      flag=0;
   int sum1=0,sum=0;
   for(int i=0;i<n;i++)
   {
      scanf("%d%d",&T[i].a,&T[i].b);
   sum+=T[i].b;   
      }
      sort(T,T+n,cmp);
   DFS(0,sum1,sum); 
   if(flag)
   printf("YES\n");
   else
   printf("NO\n");      
   }  
   return 0;
}