hdu 1518

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

struct node{
int length;
}Len[21];
bool vis[21];
int n,len;

bool cmp(node a,node b){
return a.length>b.length;
}

bool Dfs(int l,int cout,int pas){

if(cout==4)
return true;
for(int i=pas;i<n;i++){
if(vis[i]==true)
continue;
if(len==l+Len[i].length){
vis[i]=true;
if(Dfs(0,cout+1,0))
return true;
vis[i]=false;
}
else if(len>(l+Len[i].length)){
vis[i]=true;
l+=Len[i].length;
if(Dfs(l,cout,i+1))
return true;
l-=Len[i].length;
vis[i]=false;
}
}
return false;
}

int main(){

int t;
cin>>t;
while(t--){
cin>>n;
len=0;
for(int i=0;i<n;i++){
cin>>Len[i].length;
len+=Len[i].length;
vis[i]=false;
}
if(len%4){
cout<<"no"<<endl;
continue;
}
len/=4;
sort(Len,Len+n,cmp);
if(len<Len[0].length){
cout<<"no"<<endl;
continue;
}
if(Dfs(0,0,0))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}

你可能感兴趣的:(hdu 1518)