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 Rust (1.15.1) (judge verdict: Accepted)

use std::collections::{BTreeMap, BinaryHeap, VecDeque};

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let n: usize = sc.read();

    let mut graph = vec![vec![]; (1 << (n + 1)) - 1];
    let mut inverse = vec![vec![]; (1 << (n + 1)) - 1];
    for i in 0..((1 << n) - 1) {
        graph[i].push(i * 2 + 1);
        graph[i].push(i * 2 + 2);
        inverse[i * 2 + 1].push(i);
        inverse[i * 2 + 2].push(i);
    }
    //    eprintln!("{:?}", graph);

    let v: Vec<u64> = sc.vec(1 << n);
    let count = v.into_iter().fold(BTreeMap::new(), |mut map, v| {
        *map.entry(v).or_insert(0) += 1;
        map
    });

    let mut values = vec![0; graph.len()];
    let mut heap = BinaryHeap::new();
    heap.push((1 << n, 0));

    for (value, count) in count.into_iter().rev() {
        if heap.len() < count {
            println!("No");
            return;
        }
        let vs = (0..count).map(|_| heap.pop().unwrap()).collect::<Vec<_>>();
        for (mut size, mut cur) in vs.into_iter() {
            loop {
                values[cur] = value;
                if graph[cur].is_empty() {
                    break;
                }
                heap.push((size / 2, graph[cur][1]));
                cur = graph[cur][0];
                size /= 2;
            }
        }
    }

    println!("Yes");
}

fn get_min_leaf(root: usize, graph: &Vec<Vec<usize>>) -> usize {
    if graph[root].is_empty() {
        root
    } else {
        get_min_leaf(graph[root][0], graph)
    }
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}