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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using E = System.Linq.Enumerable;
using System.Numerics;
using System.Collections;

partial class Solver
{
    public void Run()
    {
        var N = ni();
        var S = ni(1 << N);
        Array.Sort(S);
        Array.Reverse(S);

        bool ans = true;

        var set = new MultiSet<int>();

        foreach (var s in S)
        {
            set.Add(s);
        }

        var current = new List<int>();
        current.Add(S[0]);
        set.Remove(S[0]);

        while (current.Count < S.Length)
        {
            var added = new List<int>();

            foreach (var x in current)
            {
                var index = set.IndexOfLowerBound(x);
                index--;
                if (index < 0 || index >= set.Count)
                {
                    ans = false;
                    break;
                }
                int y = set.At(index);
                added.Add(y);
                set.Remove(y);
            }
            if (!ans) break;
            current.AddRange(added);
        }

        cout.WriteLine(ans ? "Yes" : "No");
    }

}

public partial class MultiSet<T>
{
    private Node root = null;
    private readonly IComparer<T> _comparer;

    public MultiSet() : this(Comparer<T>.Default)
    {
    }

    public MultiSet(IComparer<T> comparer)
    {
        _comparer = comparer;
    }

    virtual protected bool AllowMultiple { get { return true; } }

    public int Count { get { return root == null ? 0 : root.Count + root.LeftTotalCount + root.RightTotalCount; } }

    private Node FindNode(T value)
    {
        var node = root;
        while (node != null)
        {
            int cmp = _comparer.Compare(value, node.Value);
            if (cmp == 0)
            {
                return node;
            }
            node = (cmp < 0) ? node.Left : node.Right;
        }
        return null;
    }

    public bool Contains(T value)
    {
        var node = FindNode(value);
        return node != null;
    }

    public int CountOf(T value)
    {
        var node = FindNode(value);
        return node != null ? node.Count : 0;
    }

    public int IndexOfLowerBound(T value)
    {
        var node = root;
        int index = 0;
        while (node != null)
        {
            int cmp = _comparer.Compare(value, node.Value);
            if (cmp <= 0)
            {
                node = node.Left;
            }
            else
            {
                index += node.LeftTotalCount + node.Count;
                node = node.Right;
            }
        }
        return index;
    }

    public int IndexOfUpperBound(T value)
    {
        var node = root;
        int index = 0;
        while (node != null)
        {
            int cmp = _comparer.Compare(value, node.Value);
            if (cmp < 0)
            {
                node = node.Left;
            }
            else
            {
                index += node.LeftTotalCount + node.Count;
                node = node.Right;
            }
        }
        return index;
    }

    public T At(int index)
    {
        var node = root;
        while (node != null)
        {
            int leftCount = node.LeftTotalCount;
            if (leftCount <= index && index < leftCount + node.Count) return node.Value;
            if (index < leftCount)
            {
                node = node.Left;
            }
            else
            {
                index -= leftCount + node.Count;
                node = node.Right;
            }
        }
        throw new ArgumentOutOfRangeException("index");
    }

    public void Add(T value)
    {
        root = Add(root, value);
        root.Color = Node.BLACK;
    }

    private Node Add(Node h, T value)
    {
        if (h == null)
        {
            return new Node(value);
        }

        if (IsRed(h.Left) && IsRed(h.Right))
        {
            ColorFlip(h);
        }

        int cmp = _comparer.Compare(value, h.Value);
        if (cmp == 0)
        {
            if (AllowMultiple)
            {
                h.Count++;
                FixCount(h);
            }
        }
        else if (cmp <= 0)
        {
            h.Left = Add(h.Left, value);
            FixCount(h);
        }
        else
        {
            h.Right = Add(h.Right, value);
            FixCount(h);
        }

        if (IsRed(h.Right) && !IsRed(h.Left))
        {
            h = RotateLeft(h);
        }

        if (IsRed(h.Left) && IsRed(h.Left.Left))
        {
            h = RotateRight(h);
        }

        return h;
    }

    private Node RemoveLeftestNode(Node h)
    {
        if (h.Left == null)
        {
            return null;
        }

        if (!IsRed(h.Left) && !IsRed(h.Left.Left))
        {
            h = MoveRedLeft(h);
        }

        h.Left = RemoveLeftestNode(h.Left);
        return FixUp(h);
    }

    private bool TryReduceCount(Node h, T value, out int num)
    {
        num = 0;
        if (h == null)
        {
            return false;
        }

        int cmp = _comparer.Compare(value, h.Value);
        if (cmp == 0)
        {
            num = h.Count;
            if (h.Count > 1)
            {
                h.Count--;
                FixCount(h);
                return true;
            }
            return false;
        }
        if (TryReduceCount((cmp < 0) ? h.Left : h.Right, value, out num))
        {
            FixCount(h);
            return true;
        }
        return false;
    }


    public bool Remove(T value)
    {
        if (AllowMultiple)
        {
            int num;
            if (TryReduceCount(root, value, out num))
            {
                return true;
            }
            if (num == 0)
            {
                return false;
            }
        }
        else
        {
            if (!Contains(value))
            {
                return false;
            }
        }

        root = RemoveNode(root, value);
        if (root != null)
        {
            root.Color = Node.BLACK;
        }
        return true;
    }

    private Node RemoveNode(Node h, T value)
    {
        if (_comparer.Compare(value, h.Value) < 0)
        {
            if (h.Left == null)
            {
                throw new KeyNotFoundException("value");
            }

            if (!IsRed(h.Left) && !IsRed(h.Left.Left))
            {
                h = MoveRedLeft(h);
            }
            h.Left = RemoveNode(h.Left, value);
            FixCount(h);
        }
        else
        {
            if (IsRed(h.Left))
            {
                h = RotateRight(h);
            }

            if (h.Right == null)
            {
                if (_comparer.Compare(value, h.Value) == 0)
                {
                    return null;
                }

                throw new KeyNotFoundException("value");
            }

            if (!IsRed(h.Right) && !IsRed(h.Right.Left))
            {
                h = MoveRedRight(h);
            }

            if (_comparer.Compare(value, h.Value) == 0)
            {
                var minNode = GetLeftestNode(h.Right);
                h.Value = minNode.Value;
                h.Count = minNode.Count;
                h.Right = RemoveLeftestNode(h.Right);
                FixCount(h);
            }
            else
            {
                h.Right = RemoveNode(h.Right, value);
                FixCount(h);
            }
        }

        return FixUp(h);
    }


    private static Node MoveRedLeft(Node h)
    {
        ColorFlip(h);

        if (IsRed(h.Right.Left))
        {
            h.Right = RotateRight(h.Right);
            FixCount(h);
            h = RotateLeft(h);
            ColorFlip(h);
        }

        return h;
    }

    private static Node MoveRedRight(Node h)
    {
        ColorFlip(h);

        if (IsRed(h.Left.Left))
        {
            h = RotateRight(h);
            ColorFlip(h);
        }

        return h;
    }

    static private Node RotateLeft(Node h)
    {
        Node x = h.Right;
        h.Right = x.Left;
        x.Left = h;

        x.Color = h.Color;
        h.Color = Node.RED;
        FixCount(h);
        FixCount(x);
        return x;
    }

    private static Node RotateRight(Node h)
    {
        Node x = h.Left;
        h.Left = x.Right;
        x.Right = h;

        x.Color = h.Color;
        h.Color = Node.RED;
        FixCount(h);
        FixCount(x);
        return x;
    }

    private static bool IsRed(Node h)
    {
        return h != null && h.Color == Node.RED;
    }

    static private void ColorFlip(Node h)
    {
        h.Color = !h.Color;
        h.Left.Color = !h.Left.Color;
        h.Right.Color = !h.Right.Color;
    }

    static private Node GetLeftestNode(Node h)
    {
        while (h.Left != null)
        {
            h = h.Left;
        }
        return h;
    }

    static private Node FixUp(Node h)
    {
        if (IsRed(h.Right))
        {
            h = RotateLeft(h);
        }

        if (h.Left != null && IsRed(h.Left) && IsRed(h.Left.Left))
        {
            h = RotateRight(h);
        }

        if (IsRed(h.Left) && IsRed(h.Right))
        {
            ColorFlip(h);
        }
        FixCount(h);
        return h;
    }

    static private void FixCount(Node node)
    {
        node.TotalCount = node.Count + node.LeftTotalCount + node.RightTotalCount;
    }

    public void Debug()
    {
        if (root != null)
        {
            root.Debug();
        }
    }

    private class Node
    {
        public const bool RED = true;
        public const bool BLACK = false;
        public T Value;
        public bool Color = RED;
        public Node Left = null;
        public Node Right = null;
        public int Count = 1;
        public Node(T value)
        {
            this.Value = value;
        }

        public int TotalCount = 1;
        public int LeftTotalCount { get { return Left == null ? 0 : Left.TotalCount; } }
        public int RightTotalCount { get { return Right == null ? 0 : Right.TotalCount; } }

        public void Debug(string indent = "")
        {
            Console.WriteLine(indent + ":" + this.Value + " - " + Count + " - " + TotalCount);
            if (Left != null)
            {
                Left.Debug(indent + "L");
            }
            if (Right != null)
            {
                Right.Debug(indent + "R");
            }
        }
    }
}

// PREWRITEN CODE BEGINS FROM HERE
partial class Solver : Scanner
{
    public static void Main(string[] args)
    {
#if LOCAL
        new Solver(Console.In, Console.Out).Run();
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver(Console.In, Console.Out).Run();
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader)
    {
        this.cin = reader;
        this.cout = writer;
    }
    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer)
    {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

public static class LinqPadExtension
{
    static public T Dump<T>(this T obj)
    {
#if LOCAL
        return LINQPad.Extensions.Dump(obj);
#else
        return obj;
#endif
    }
}
public class Scanner
{
    private readonly TextReader Reader;
    private readonly Queue<string> TokenQueue = new Queue<string>();
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    public Scanner()
        : this(Console.In)
    {
    }

    public Scanner(TextReader reader)
    {
        this.Reader = reader;
    }

    public int NextInt() { return int.Parse(Next(), ci); }
    public long NextLong() { return long.Parse(Next(), ci); }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size)
    {
        var array = new string[size];
        for (int i = 0; i < size; i++) array[i] = Next();
        return array;
    }
    public int[] NextIntArray(int size)
    {
        var array = new int[size];
        for (int i = 0; i < size; i++) array[i] = NextInt();
        return array;
    }

    public long[] NextLongArray(int size)
    {
        var array = new long[size];
        for (int i = 0; i < size; i++) array[i] = NextLong();
        return array;
    }

    public double[] NextDoubleArray(int size)
    {
        var array = new double[size];
        for (int i = 0; i < size; i++) array[i] = NextDouble();
        return array;
    }

    public string Next()
    {
        if (TokenQueue.Count == 0)
        {
            if (!StockTokens()) throw new InvalidOperationException();
        }
        return TokenQueue.Dequeue();
    }

    public bool HasNext()
    {
        if (TokenQueue.Count > 0)
            return true;
        return StockTokens();
    }
    static readonly char[] _separator = new[] { ' ' };
    private bool StockTokens()
    {
        while (true)
        {
            var line = Reader.ReadLine();
            if (line == null) return false;
            var tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length == 0) continue;
            foreach (var token in tokens)
                TokenQueue.Enqueue(token);
            return true;
        }
    }
}