Problem:
Score : 700 points

Problem Statement

There is an integer sequence of length 2^N: A_0, A_1, ..., A_{2^N-1}. (Note that the sequence is 0-indexed.)

For every integer K satisfying 1 \leq K \leq 2^N-1, solve the following problem:

Let i and j be integers. Find the maximum value of A_i + A_j where 0 \leq i < j \leq 2^N-1 and (i or j) \leq K.
Here, or denotes the bitwise OR.

Constraints

1 \leq N \leq 18

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 ... A_{2^N-1}

Output

Print 2^N-1 lines.
In the i-th line, print the answer of the problem above for K=i.

Sample Input 1

2
1 2 3 1

Sample Output 1

3
4
5

For K=1, the only possible pair of i and j is (i,j)=(0,1), so the answer is A_0+A_1=1+2=3.

For K=2, the possible pairs of i and j are (i,j)=(0,1),(0,2).
When (i,j)=(0,2), A_i+A_j=1+3=4. This is the maximum value, so the answer is 4.

For K=3, the possible pairs of i and j are (i,j)=(0,1),(0,2),(0,3),(1,2),(1,3),(2,3) .
When (i,j)=(1,2), A_i+A_j=2+3=5. This is the maximum value, so the answer is 5.

Sample Input 2

3
10 71 84 33 6 47 23 25

Sample Output 2

81
94
155
155
155
155
155

Sample Input 3

4
75 26 45 72 81 47 97 97 2 2 25 82 84 17 56 32

Sample Output 3

101
120
147
156
156
178
194
194
194
194
194
194
194
194
194

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(non_snake_case)]

use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Formatter};
use std::io::*;
use std::ops::*;
use std::*;

// -----------------------------------------------
// Framework
// -----------------------------------------------

#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($t:ty) =>
        (rl().parse::<$t>().unwrap());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($arg:expr),*) => {
        #[cfg(debug_assertions)]
        {
            let entries = [$(&stringify!([$arg]:), &$arg as &Debug),*];
            stderr().write_fmt(format_args!("{:#?}\n", entries)).unwrap();
        }
    };
}

#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    buf.trim_right().to_owned()
}

trait IteratorExt: Iterator + Sized {
    fn vec(self) -> Vec<Self::Item> {
        self.collect()
    }
}

impl<T: Iterator> IteratorExt for T {}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Rev<T>(pub T);

impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> Ordering {
        other.0.cmp(&self.0)
    }
}

// -----------------------------------------------
// Solution
// -----------------------------------------------

fn exp() {
    let N = 4;
    let P = 1 << N;

    let mut prev = HashSet::new();

    for K in 1..P {
        let mut S = Vec::new();

        for i in 0..P {
            for j in i + 1..P {
                let s = i | j;
                if s > K {
                    continue;
                }

                S.push((i, j));
            }
        }

        S.sort();
        println!(
            "K={}\n{}\n",
            K,
            S.iter()
                .cloned()
                .filter(|x| !prev.contains(x))
                .map(|(i, j): (usize, usize)| format!("({}, {})", i, j))
                .vec()
                .join(" ")
        );

        prev = S.into_iter().collect();
    }
}

fn core() {
    let stdout = std::io::stdout();
    let mut stdout = stdout.lock();
    let mut p = move |n: i64| stdout.write_fmt(format_args!("{}\n", n)).unwrap();

    let N = read!(usize);
    let A = read![[i64]];

    let P = 1 << N;

    // T[n] = max A_i for i in n
    let mut T = vec![0; P];

    T[0] = A[0];
    T[1] = max(A[0], A[1]);
    T[2] = max(A[0], A[2]);

    let mut prev = A[0] + A[1];

    // K = 1
    p(prev);

    for K in 2..P {
        let mut ma = prev;

        let mut h = 1;
        while (1 << h) <= K {
            h += 1;
        }
        assert!((1 << h) >= K);

        let mut mask = 0;
        for i in (0..h).rev() {
            mask = mask | (1 << i);
            let s = K & !mask;

            // A_i + A_K (i in s)
            ma = max(ma, T[s] + A[K]);

            {
                let i = K & !mask;
                let j = K & mask;
                ma = max(ma, T[i] + T[j]);
            }
        }

        p(ma);
        prev = ma;
    }
}

pub fn main() {
    let stdout = std::io::stdout();
    let mut stdout = stdout.lock();
    let mut p = move |n: i64| stdout.write_fmt(format_args!("{}\n", n)).unwrap();

    let N = read!(usize);
    let P = 1 << N;
    let A = read![[i64]];

    let mut dp = vec![vec![]; P];
    dp[0] = vec![Rev((A[0], 0))];

    for s in 0..P {
        dp[s].push(Rev((A[s], s)));
        dp[s].sort();
        dp[s].dedup();
        while dp[s].len() > 2 {
            dp[s].pop();
        }

        let dps = dp[s].clone();

        for h in 0..N {
            let t = s | (1 << h);

            for x in dps.iter().cloned() {
                dp[t].push(x);
            }
            dp[t].sort();
            dp[t].dedup();
            while dp[t].len() > 2 {
                dp[t].pop();
            }
        }
    }

    let mut ma = 0;
    for k in 1..P {
        let mut heap = std::mem::replace(&mut dp[k], Vec::new());
        heap.sort();
        let Rev((a, _)) = heap.pop().unwrap();
        let Rev((b, _)) = heap.pop().unwrap();
        ma = max(ma, a + b);
        p(ma);
    }

    return;
}