Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //UVa Q10364
- #include <bits/stdc++.h>
- using namespace std;
- int n,m, cnt, vis;
- int arr[20];
- bool rec[1<<20];
- int clen, tlen;//current, target
- bool yes;
- bool cmp(int a, int b){return a > b;}
- void dfs(){
- //cout << clen << ' ' << tlen << ' ' << cnt << endl;
- if(cnt == 3){
- yes = 1;
- return;
- }
- int one = -1;
- for(int i = 0; i < m; ++i){
- if(!(vis & (1<<i)) && arr[i] <= tlen - clen){
- one = i;
- break;
- }
- }
- if(one == -1)return;
- if(arr[one] == tlen - clen){//match
- clen = 0;
- vis += (1<<one);
- ++cnt;
- if(!rec[vis]){
- dfs();
- rec[vis] = 1;
- }
- --cnt;
- vis -= (1<<one);
- clen = tlen-arr[one];
- }else{
- for(one; one < m && !yes; ++one){
- if(!(vis & (1<<one))){
- clen += arr[one];
- vis += (1<<one);
- if(!rec[vis]){
- dfs();
- rec[vis] = 1;
- }
- vis -= (1<<one);
- clen -=arr[one];
- }
- }
- }
- }
- int32_t main(){
- cin >> n;
- while(n--){
- vis = cnt = clen = tlen = 0;
- yes = 0;
- memset(rec, 0, sizeof(rec));
- cin >> m;
- for(int i = 0; i < m; ++i){
- cin >> arr[i];
- tlen += arr[i];
- }
- if(tlen % 4 != 0){
- cout << "no\n";
- }else{
- sort(arr, arr+m, cmp);
- tlen /= 4;
- dfs();
- if(yes)cout << "yes\n";
- else cout << "no\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement