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)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

/// 2-3 Tree
/// Reference: https://en.wikipedia.org/wiki/2%E2%80%933_tree
/// https://www.slideshare.net/sandpoonia/23-tree
/// Verified by: ARC061-D (http://arc061.contest.atcoder.jp/submissions/1246386)
/// Codeforces 404E (http://codeforces.com/contest/785/submission/26747752), TLE
#[derive(Clone, Debug)]
enum TwoThreeTree<T> {
    Tip,
    Two(
        usize, // size
        T, // value
        Box<TwoThreeTree<T>>, // left
        Box<TwoThreeTree<T>>, // right
    ),
    Three(
        usize, // size
        T, // val1
        T, // val2
        Box<TwoThreeTree<T>>, // left
        Box<TwoThreeTree<T>>, // middle
        Box<TwoThreeTree<T>>, // right
    ),
}

impl<T: Ord> TwoThreeTree<T> {
    pub fn new() -> Self {
        TwoThreeTree::Tip
    }
    pub fn size(&self) -> usize {
        use TwoThreeTree::*;
        match *self {
            Tip => 0,
            Two(sz, _, _, _) => sz,
            Three(sz, _, _, _, _, _) => sz,
        }
    }
    fn node_two(x: T, left: Box<Self>, right: Box<Self>) -> Self {
        TwoThreeTree::Two(left.size() + right.size() + 1, x,
                          left, right)
    }
    fn node_three(x: T, y: T, left: Box<Self>, middle: Box<Self>, right: Box<Self>)
                  -> Self {
        TwoThreeTree::Three(left.size() + middle.size() + right.size() + 2,
                            x, y,
                            left, middle, right)
    }
    fn divide_four(t1: Box<Self>, v1: T, t2: Box<Self>, v2: T,
                       t3: Box<Self>, v3: T, t4: Box<Self>) -> (Self, Self, T) {
        (Self::node_two(v1, t1, t2), Self::node_two(v3, t3, t4), v2)
    }
    // Ok(x) -> ordinary tree
    // Err((t1, t2, v)) -> propagating v, whilst dividing the tree into t1, t2
    fn insert_sub(self, x: T) -> Result<Self, (Self, Self, T)> {
        use TwoThreeTree::*;
        match self {
            Tip => Err((Tip, Tip, x)),
            Two(size, val, left, right) => {
                match x.cmp(&val) {
                    std::cmp::Ordering::Equal =>
                        Ok(Two(size, val, left, right)),
                    std::cmp::Ordering::Less => {
                        match left.insert_sub(x) {
                            Ok(t) =>
                                Ok(Self::node_two(val, Box::new(t), right)),
                            Err((t1, t2, sub_up)) =>
                                Ok(Self::node_three(
                                    sub_up, val,
                                    Box::new(t1), Box::new(t2), right)),
                        }
                    },
                    std::cmp::Ordering::Greater => {
                        match right.insert_sub(x) {
                            Ok(t) =>
                                Ok(Self::node_two(val, left, Box::new(t))),
                            Err((t1, t2, sub_up)) =>
                                Ok(Self::node_three(
                                    val, sub_up,
                                    left, Box::new(t1), Box::new(t2))),
                        }
                    },
                }
            }
            Three(size, val1, val2, left, middle, right) => {
                if x == val1 || x == val2 {
                    return Ok(Three(size, val1, val2, left, middle, right));
                }
                if x < val1 {
                    match left.insert_sub(x) {
                        Ok(sub_tr) =>
                            Ok(Self::node_three(
                                val1, val2,
                                Box::new(sub_tr), middle, right)),
                        Err((t1, t2, sub_up)) => {
                            let (t1, t2, v) = Self::divide_four(
                                Box::new(t1), sub_up, Box::new(t2), val1,
                                middle, val2, right);
                            Err((t1, t2, v))
                        },
                    }
                } else if x < val2 {
                    match middle.insert_sub(x) {
                        Ok(sub_tr) =>
                            Ok(Self::node_three(
                                val1, val2,
                                left, Box::new(sub_tr), right)),
                        Err((t1, t2, sub_up)) => {
                            let (t1, t2, v) = Self::divide_four(
                                left, val1, Box::new(t1), sub_up,
                                Box::new(t2), val2, right);
                            Err((t1, t2, v))
                        },
                    }
                } else {
                    match right.insert_sub(x) {
                        Ok(sub_tr) =>
                            Ok(Self::node_three(
                                val1, val2,
                                left, middle, Box::new(sub_tr))),
                        Err((t1, t2, sub_up)) => {
                            let (t1, t2, v) = Self::divide_four(
                                left, val1, middle, val2,
                                Box::new(t1), sub_up, Box::new(t2));
                            Err((t1, t2, v))
                        },
                    }
                }
            }
        }
    }
    pub fn insert(self, x: T) -> Self {
        match self.insert_sub(x) {
            Ok(t) => t,
            Err((t1, t2, v)) =>
                Self::node_two(v, Box::new(t1), Box::new(t2)),
        }
    }
    fn get_min(&mut self) -> &mut T {
        use TwoThreeTree::*;
        match *self {
            Tip => panic!(),
            Two(_, ref mut val, ref mut left, _) => {
                if let &Tip = &left as &Self {
                    val
                } else {
                    left.get_min()
                }
            }
            Three(_, ref mut val, _, ref mut left, _, _) => {
                if let &Tip = &left as &Self {
                    val
                } else {
                    left.get_min()
                }
            }
        }
    }
    fn delete_sub(self, x: &T, mut leftist: bool)
                  -> (Result<Self, Box<Self>>, Option<T>) {
        use TwoThreeTree::*;
        match self {
            Tip => (Ok(Tip), None),
            Two(_, mut val, left, mut right) => {
                let mut cmp_result = x.cmp(&val);
                if leftist {
                    if let &Tip = &left as &Self {
                        cmp_result = std::cmp::Ordering::Equal;
                    } else {
                        cmp_result = std::cmp::Ordering::Less;
                    }
                }
                if cmp_result == std::cmp::Ordering::Equal {
                    if let Tip = *right {
                        return (Err(Box::new(Tip)), Some(val));
                    }
                    let inord_succ = right.get_min();
                    std::mem::swap(&mut val, inord_succ);
                    cmp_result = std::cmp::Ordering::Greater;
                    leftist = true;
                }
                match cmp_result {
                    std::cmp::Ordering::Equal => panic!(),
                    std::cmp::Ordering::Less => {
                        match left.delete_sub(x, leftist) {
                            (Ok(t), ret) =>
                                (Ok(Self::node_two(val, Box::new(t), right)), ret),
                            (Err(box_t), ret) => {
                                let right = *right; // Workaround: http://stackoverflow.com/questions/28466809/collaterally-moved-error-on-deconstructing-box-of-pairs
                                match right {
                                    Tip => panic!(),
                                    Two(_, rv, rleft, rright) => {
                                        let newtree =
                                            Self::node_three(val, rv, box_t, rleft, rright);
                                        (Err(Box::new(newtree)), ret)
                                    },
                                    Three(_, rv1, rv2, rleft, rmiddle, rright) => {
                                        // rotation
                                        let left = Self::node_two(val, box_t, rleft);
                                        let right = Self::node_two(rv2, rmiddle, rright);
                                        (Ok(Self::node_two(rv1, Box::new(left), Box::new(right))), ret)
                                    },
                                }
                            },
                        }
                    },
                    std::cmp::Ordering::Greater => {
                        match right.delete_sub(x, leftist) {
                            (Ok(t), ret) =>
                                (Ok(Self::node_two(val, left, Box::new(t))), ret),
                            (Err(box_t), ret) => {
                                let left = *left; // Workaround
                                match left {
                                    Tip => panic!(),
                                    Two(_, lv, lleft, lright) => {
                                        let newtree =
                                            Self::node_three(lv, val, lleft, lright, box_t);
                                        (Err(Box::new(newtree)), ret)
                                    },
                                    Three(_, lv1, lv2, lleft, lmiddle, lright) => {
                                        // rotation
                                        let left = Self::node_two(lv1, lleft, lmiddle);
                                        let right = Self::node_two(val, lright, box_t);
                                        (Ok(Self::node_two(lv2, Box::new(left), Box::new(right))), ret)
                                    },
                                }
                            },
                        }
                    },
                }
            }
            Three(_, mut val1, mut val2, left, mut middle, mut right) => {
                let mut cmp_result;
                if leftist {
                    if let &Tip = &left as &Self {
                        cmp_result = 1;
                    } else {
                        cmp_result = 0;
                    }
                } else if x < &val1 {
                    cmp_result = 0;
                } else if x == &val1 {
                    cmp_result = 1;
                } else if x < &val2 {
                    cmp_result = 2;
                } else if x == &val2 {
                    cmp_result = 3;
                } else {
                    cmp_result = 4;
                }
                if cmp_result == 1 {
                    if let Tip = *right {
                        return (Ok(Self::node_two(
                            val2, Box::new(Tip), Box::new(Tip))),
                                   Some(val1));
                    }
                    let inord_succ = middle.get_min();
                    std::mem::swap(&mut val1, inord_succ);
                    cmp_result = 2;
                    leftist = true;
                }
                if cmp_result == 3 {
                    if let Tip = *right {
                        return (Ok(Self::node_two(
                            val1, Box::new(Tip), Box::new(Tip))),
                                   Some(val2));
                    }
                    let inord_succ = right.get_min();
                    std::mem::swap(&mut val2, inord_succ);
                    cmp_result = 4;
                    leftist = true;
                }
                macro_rules! three_two {
                    ($ll:expr, $lv:expr, $lr:expr,
                     $val1:expr,
                     $ml:expr, $mv:expr, $mr:expr,
                     $val2:expr,
                     $rl:expr, $rv:expr, $rr:expr) => {
                        Self::node_three(
                            $val1,
                            $val2,
                            Box::new(Self::node_two($lv, $ll, $lr)),
                            Box::new(Self::node_two($mv, $ml, $mr)),
                            Box::new(Self::node_two($rv, $rl, $rr)))
                    }
                }
                match cmp_result {
                    0 => {
                        match left.delete_sub(x, leftist) {
                            (Ok(t), ret) =>
                                (Ok(Self::node_three(
                                    val1, val2,
                                    Box::new(t), middle, right)),
                                 ret),
                            (Err(t), ret) => {
                                match (*middle, *right) {
                                    (Three(_, mv1, mv2, mleft, mmiddle, mright),
                                     right) =>
                                        (Ok(Self::node_three(
                                            mv1, val2,
                                            Box::new(Self::node_two(val1, t, mleft)),
                                            Box::new(Self::node_two(mv2, mmiddle, mright)),
                                            Box::new(right))), ret),
                                    (Two(_, mv, mleft, mright),
                                     Three(_, rv1, rv2, rleft, rmiddle, rright)) =>
                                        (Ok(three_two!(
                                            t, val1, mleft,
                                            mv,
                                            mright, val2, rleft,
                                            rv1,
                                            rmiddle, rv2, rright)), ret),
                                    (Two(_, mv, mleft, mright),
                                     right) =>
                                        (Ok(Self::node_two(
                                            val2,
                                            Box::new(Self::node_three(
                                                val1, mv, t, mleft, mright)),
                                            Box::new(right))), ret),
                                    _ => panic!("Unexpected Tip"),
                                }
                            }
                        }
                    },
                    2 => {
                        match middle.delete_sub(x, leftist) {
                            (Ok(t), ret) =>
                                (Ok(Self::node_three(
                                    val1, val2,
                                    left, Box::new(t), right)),
                                 ret),
                            (Err(t), ret) => {
                                match (*left, *right) {
                                    (Three(_, lv1, lv2, lleft, lmiddle, lright),
                                     right) =>
                                        (Ok(Self::node_three(
                                            lv2, val2,
                                            Box::new(Self::node_two(lv1, lleft, lmiddle)),
                                            Box::new(Self::node_two(val1, lright, t)),
                                            Box::new(right))), ret),
                                    (Two(_, lv, lleft, lright),
                                     Three(_, rv1, rv2, rleft, rmiddle, rright)) =>
                                        (Ok(three_two!(
                                            lleft, lv, lright,
                                            val1,
                                            t, val2, rleft,
                                            rv1,
                                            rmiddle, rv2, rright)), ret),
                                    (Two(_, lv, lleft, lright),
                                     right) =>
                                        (Ok(Self::node_two(
                                            val2,
                                            Box::new(Self::node_three(
                                                lv, val1, lleft, lright, t)),
                                            Box::new(right))), ret),
                                    _ => panic!("Unexpected Tip"),
                                }
                            }
                        }
                    },
                    4 => {
                        match right.delete_sub(x, leftist) {
                            (Ok(t), ret) =>
                                (Ok(Self::node_three(
                                    val1, val2,
                                    left, middle, Box::new(t))),
                                 ret),
                            (Err(t), ret) => {
                                match (*left, *middle) {
                                    (left,
                                     Three(_, mv1, mv2, mleft, mmiddle, mright)) =>
                                        (Ok(Self::node_three(
                                            val1, mv2,
                                            Box::new(left),
                                            Box::new(Self::node_two(mv1, mleft, mmiddle)),
                                            Box::new(Self::node_two(val2, mright, t)))), ret),
                                    (Three(_, lv1, lv2, lleft, lmiddle, lright),
                                     Two(_, mv, mleft, mright)) =>
                                        (Ok(three_two!(
                                            lleft, lv1, lmiddle,
                                            lv2,
                                            lright, val1, mleft,
                                            mv,
                                            mright, val2, t)), ret),
                                    (left,
                                     Two(_, mv, mleft, mright)) =>
                                        (Ok(Self::node_two(
                                            val1,
                                            Box::new(left),
                                            Box::new(Self::node_three(
                                                mv, val2, mleft, mright, t)))), ret),
                                    _ => panic!("Unexpected Tip"),
                                }
                            }
                        }
                    },
                    _ => panic!("Unexpected case"),
                }
            },
        }
    }
    pub fn delete(self, x: &T) -> (Self, Option<T>) {
        let (t, ret) = self.delete_sub(x, false);
        match t {
            Ok(t) => (t, ret),
            Err(t) => (*t, ret),
        }
    }
    fn into_vec_sub(self, ret: &mut Vec<T>) {
        use TwoThreeTree::*;
        match self {
            Tip => (),
            Two(_, val, left, right) => {
                left.into_vec_sub(ret);
                ret.push(val);
                right.into_vec_sub(ret);
            },
            Three(_, val1, val2, left, middle, right) => {
                left.into_vec_sub(ret);
                ret.push(val1);
                middle.into_vec_sub(ret);
                ret.push(val2);
                right.into_vec_sub(ret);
            },
        }
    }
    pub fn find_index(&self, x: &T) -> (usize, bool) {
        use TwoThreeTree::*;
        match *self {
            Tip => (0, false),
            Two(_size, ref v, ref left, ref right) => {
                match x.cmp(v) {
                    std::cmp::Ordering::Equal => (left.size(), true),
                    std::cmp::Ordering::Less => left.find_index(x),
                    std::cmp::Ordering::Greater => {
                        let (res, found) = right.find_index(x);
                        (res + left.size() + 1, found)
                    },
                }
            },
            Three(_size, ref v1, ref v2, ref left, ref middle, ref right) => {
                if x == v1 {
                    return (left.size(), true);
                }
                if x == v2 {
                    return (left.size() + middle.size() + 1, true);
                }
                if x < v1 {
                    return left.find_index(x);
                }
                if x < v2 {
                    let (res, found) = middle.find_index(x);
                    return (left.size() + 1 + res, found);
                }
                let (res, found) = right.find_index(x);
                return (left.size() + middle.size() + 2 + res, found);
            }
        }
    }
    // Verified by https://atcoder.jp/contests/abc128/submissions/5654911
    pub fn at(&self, nth: usize) -> Option<&T> {
        use TwoThreeTree::*;
        match *self {
            Tip => None,
            Two(_size, ref v, ref left, ref right) => {
                if nth < left.size() {
                    left.at(nth)
                } else if nth == left.size() {
                    Some(v)
                } else {
                    right.at(nth - left.size() - 1)
                }
            },
            Three(_size, ref v1, ref v2, ref left, ref middle, ref right) => {
                if nth < left.size() {
                    left.at(nth)
                } else if nth == left.size() {
                    Some(v1)
                } else if nth < left.size() + 1 + middle.size() {
                    middle.at(nth - left.size() - 1)
                } else if nth == left.size() + 1 + middle.size() {
                    Some(v2)
                } else {
                    right.at(nth - left.size() - 1 - middle.size() - 1)
                }
            }
        }
    }
    pub fn into_vec(self) -> Vec<T> {
        let mut ret = Vec::with_capacity(self.size());
        self.into_vec_sub(&mut ret);
        ret
    }
}

fn naive(n: usize, mut s: Vec<i64>) -> bool {
    s.sort();
    let mut ans = vec![0; 1 << n];
    for bc in 0..n + 1 {
        for bits in 0..1usize << n {
            if bits.count_ones() as usize == bc {
                ans[bits] = s.pop().expect("2^n = 2^n");
            }
        }
    }
    for i in (0..n).rev() {
        for bits in 0..1 << i {
            if ans[bits] <= ans[bits | 1 << i] {
                return false;
            }
        }
    }
    true
}

fn correct(n: usize, s: Vec<i64>) -> bool {
    let mut tr = TwoThreeTree::new();
    for i in 0..1 << n {
        tr = tr.insert((s[i], i + 1));
    }
    let &ma = tr.at((1 << n) - 1).unwrap();
    tr = tr.delete(&ma).0;
    let mut cur = vec![ma.0];
    for _d in 0..n {
        let mut nxt = vec![];
        // greedily pick
        for &c in &cur {
            let idx = tr.find_index(&(c, 0)).0;
            if idx > 0 {
                let &val = tr.at(idx - 1).unwrap();
                tr = tr.delete(&val).0;
                nxt.push(val.0);
            } else {
                return false;
            }
        }
        cur.extend_from_slice(&nxt);
    }
    true
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        n: usize,
        s: [i64; 1 << n],
    }
    puts!("{}\n", if correct(n, s) { "Yes" } else { "No" });
}

#[allow(unused)]
fn find_counterexamples() {
    let n = 3;
    let ma = 10;
    let mut x: u64 = 1511;
    for _ in 0..10000 {
        let mut dat = vec![0; 1 << n];
        for i in 0..1 << n {
            x = x.wrapping_mul(11515153).wrapping_add(11);
            let val = (x / 63333) % ma;
            dat[i] = val as i64;
        }
        let ans_naive = naive(n, dat.clone());
        let ans_correct = correct(n, dat.clone());
        if ans_naive != ans_correct {
            debugln!("s = {:?} naive = {} correct = {}", dat, ans_naive, ans_correct);
        }
    }
}