Problem:
Score : 700 points

Problem Statement

Aoki loves numerical sequences and trees.

One day, Takahashi gave him an integer sequence of length N, a_1, a_2, ..., a_N, which made him want to construct a tree.

Aoki wants to construct a tree with N vertices numbered 1 through N, such that for each i = 1,2,...,N, the distance between vertex i and the farthest vertex from it is a_i, assuming that the length of each edge is 1.

Determine whether such a tree exists.

Constraints

2 ≦ N ≦ 100

1 ≦ a_i ≦ N-1

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If there exists a tree that satisfies the condition, print Possible. Otherwise, print Impossible.

Sample Input 1

5
3 2 2 3 3

Sample Output 1

Possible

The diagram above shows an example of a tree that satisfies the conditions. The red arrows show paths from each vertex to the farthest vertex from it.

Sample Input 2

3
1 1 2

Sample Output 2

Impossible

Sample Input 3

10
1 2 2 2 2 2 2 2 2 2

Sample Output 3

Possible

Sample Input 4

10
1 1 2 2 2 2 2 2 2 2

Sample Output 4

Impossible

Sample Input 5

6
1 1 1 1 1 5

Sample Output 5

Impossible

Sample Input 6

5
4 3 2 3 4

Sample Output 6

Possible

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

use std::cmp;
fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.read();
    let a: Vec<usize> = sc.read_vec(n);
    let max: usize = a.iter().map(|&i| i).max().unwrap();

    if n > 2 && max == 1 {
        println!("Impossible");
        return;
    }

    let mut count = vec![0; max + 1];
    for &a in &a {
        count[a] += 1;
    }

    for i in 0..(max + 1) {
        if i < (max + 1) / 2 {
            if count[i] > 0 {
                println!("Impossible");
                return;
            }
        } else {
            if i * 2 != max && count[i] < 2 {
                println!("Impossible");
                return;
            } else if i * 2 == max && count[i] != 1 {
                println!("Impossible");
                return;
            }
        }
    }

    let mut tree = vec![vec![]; n];
    {
        let mut count = count.clone();
        for i in 0..max {
            tree[i].push(i + 1);
            tree[i + 1].push(i);
        }
        for i in 0..(max + 1) {
            count[max_dist(&tree, i, n, 0)] -= 1;
        }

        let mut t = max + 1;
        for d in 0..(max + 1) {
            for _ in 0..count[d] {
                tree[d - 1].push(t);
                tree[t].push(d - 1);
                t += 1;
            }
        }
        assert_eq!(t, n);
    }
    {
        let mut check = vec![0; max + 1];
        for i in 0..n {
            check[max_dist(&tree, i, n, 0)] += 1;
        }

        for i in 0..(max + 1) {
            assert_eq!(count[i], check[i]);
        }
    }

    println!("Possible");
}

fn max_dist(tree: &Vec<Vec<usize>>, v: usize, p: usize, depth: usize) -> usize {
    if tree[v].len() == 1 && tree[v][0] == p {
        return depth;
    }
    let mut max = 0;
    for &to in &tree[v] {
        if to == p {
            continue;
        }
        max = cmp::max(max_dist(tree, to, v, depth + 1), max);
    }
    return max;
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner {
        Scanner {
            ptr: 0,
            length: 0,
            buf: vec![0; 1024],
            small_cache: vec![0; 1024],
        }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool {
        b == b'\n' || b == b'\r' || b == b'\t' || b == b' '
    }

    fn read_vec<T>(&mut self, n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        (0..n).map(|_| self.read()).collect()
    }

    fn read<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)])
                    .parse()
                    .unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}