Problem:
Score : 600 points

Problem Statement

We have one slime.

You can set the health of this slime to any integer value of your choice.

A slime reproduces every second by spawning another slime that has strictly less health. You can freely choose the health of each new slime. The first reproduction of our slime will happen in one second.

Determine if it is possible to set the healths of our first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals a multiset S.

Here S is a multiset containing 2^N (possibly duplicated) integers: S_1,~S_2,~...,~S_{2^N}.

Constraints

All values in input are integers.

1 \leq N \leq 18

1 \leq S_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_{2^N}

Output

If it is possible to set the healths of the first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals S, print Yes; otherwise, print No.

Sample Input 1

2
4 2 3 1

Sample Output 1

Yes

We will show one way to make the multiset of the healths of the slimes that will exist in 2 seconds equal to S.

First, set the health of the first slime to 4.

By letting the first slime spawn a slime whose health is 3, the healths of the slimes that exist in 1 second can be 4,~3.

Then, by letting the first slime spawn a slime whose health is 2, and letting the second slime spawn a slime whose health is 1, the healths of the slimes that exist in 2 seconds can be 4,~3,~2,~1, which is equal to S as multisets.

Sample Input 2

2
1 2 3 1

Sample Output 2

Yes

S may contain multiple instances of the same integer.

Sample Input 3

1
1 1

Sample Output 3

No

Sample Input 4

5
4 3 5 3 1 2 7 8 7 4 6 3 7 2 3 6 2 7 3 2 6 7 3 4 6 7 3 4 2 5 2 3

Sample Output 4

No

Solution in C (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include<stdio.h>
#include<stdlib.h>
#define ll long long
#define INF ((1LL<<62)-(1LL<<31))
//http://xoroshiro.di.unimi.it/
//乱数
#define ull unsigned long long
ull xorshiro[2]={0xcbe134025aac2d13,0x9e30d9a3f85a62c7};//適当な初期値
ull rnd(){
	ull s0=xorshiro[0],s1=xorshiro[1];
	s1^=s0;
	xorshiro[0]=((s0<<55)|(s0>> 9))^s1^(s1<<14);
	xorshiro[1]=((s1<<36)|(s1>>28));
	return xorshiro[0]+xorshiro[1];
}

typedef struct SETnode{
	struct SETnode*next,*pre,*up,*down;
	int cnt;
	ll x;
}SETnode;
typedef struct SET{
	int level;
	int cnt;
	SETnode*head;
}SET;

void SETinit(SET*s,int level){
	s->level=level;
	s->cnt=0;
	s->head=(SETnode*)calloc(level,sizeof(SETnode));
	for(int i=0;i<level;i++){
		SETnode*h=s->head+i;
		h->next=(SETnode*)calloc(1,sizeof(SETnode));
		if(i!=0)h->down=s->head+(i-1);
		if(i!=level-1)h->up=s->head+(i+1);
		h->x=-INF;	
	}
	for(int i=0;i<level;i++){
		SETnode*h=s->head+i;
		SETnode*t=h->next;
		t->pre=h;
		if(i!=0)t->down=h->down->next;
		if(i!=level-1)t->up=h->up->next;
		t->x=INF;
	}
}

SETnode*SETinssub(SETnode*crr,int level,ll x){
	while(crr->next->x < x)crr=crr->next;
	SETnode*d=0;
	if(level!=0)d=SETinssub(crr->down,level-1,x);
	if(level==0||(d!=0&&rnd()%2)){
		SETnode*new=calloc(1,sizeof(SETnode));
		new->pre=crr;
		new->next=crr->next;
		new->down=d;
		if(d)d->up=new;
		new->x=x;
		crr->next->pre=new;
		crr->next=new;
		return new;
	}else{
		return 0;
	}
}
void SETins(SET*s,ll x){
	int level=s->level;
	SETinssub(s->head+level-1,level-1,x);
	s->cnt++;
}
void SETdel(SET*s,ll x){
	SETnode*crr=s->head+(s->level)-1;
	while(crr){
		while(crr->next->x < x)crr=crr->next;
		if(crr->next->x == x)break;
		crr=crr->down;
	}
	if(!crr)return;
	crr=crr->next;
	while(crr){
		crr->pre->next=crr->next;
		crr->next->pre=crr->pre;
		SETnode*temp=crr->down;
		free(crr);
		crr=temp;
	}
	s->cnt--;
}

SETnode*SETlowerbound(SET*s,ll x){
	SETnode*crr=s->head+(s->level)-1;
	for(int i=s->level-1;i>=0;i--){
		while(crr->next->x < x)crr=crr->next;
		if(i)crr=crr->down;
	}
	return crr->next;
}
SETnode*SETupperbound(SET*s,ll x){
	SETnode*crr=s->head+(s->level)-1;
	for(int i=s->level-1;i>=0;i--){
		while(crr->next->x <= x)crr=crr->next;
		if(i)crr=crr->down;
	}
	return crr->next;
}
SETnode*SETtop(SET*s){return s->head->next;}
SETnode*SETend(SET*s){return SETlowerbound(s,INF);}

SET s;
ll a[1<<18];
int main(){
	int n;
	scanf("%d",&n);
	SETinit(&s,n);
	for(int i=0;i<1<<n;i++){
		int t;
		scanf("%d",&t);
		SETins(&s,-t);
	}
	ll x=SETtop(&s)->x;
	SETdel(&s,x);
	a[0]=x;
	for(int i=0;i<n;i++){
		for(int j=0;j<1<<i;j++){
			ll x=SETupperbound(&s,a[j])->x;
			if(x==INF){
				puts("No");
				return 0;
			}
			SETdel(&s,x);
			a[j+(1<<i)]=x;
		}
	}
	puts("Yes");
}