#include<bits/stdc++.h>

#define ll long long
#define int long long
#define pb push_back
#define vec vector
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()

using namespace std;

const int MAX = 2e5 + 7;

ll dp[MAX * 4];
ll lazy[MAX * 4];


void push(int v, int tl, int tr) {
    dp[v] = min(dp[v], lazy[v]);
    if (tl == tr) {
        return;
    }
    lazy[v * 2] = min(lazy[v], lazy[v * 2]);
    lazy[v * 2 + 1] = min(lazy[v], lazy[v * 2 + 1]);
}

void upd(int v, int tl, int tr, int l, int r, ll val) {
    push(v, tl, tr);
    if (l > r) return;
    if (tl == l && tr == r) {
        lazy[v] = min(lazy[v], val);
        push(v, tl, tr);
        return;
    }
    int tm = (tl + tr) / 2;
    upd(v * 2, tl, tm, l, min(r, tm), val);
    upd(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
    dp[v] = min(dp[v * 2], dp[v * 2 + 1]);
}

ll oo = 2e18;

ll get(int v, int tl, int tr, int l, int r) {
    push(v, tl, tr);
    if (l > r) return oo;
    if (tl == l && tr == r) return dp[v];
    int tm = (tl + tr) / 2;
    return min(get(v * 2, tl, tm, l, min(r, tm)),
                get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void solve1(){
    int n;
    cin >> n;
    vector<ll> a(n + 1), c(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> c[i];
    vector<int> l(n + 1, 0), r(n + 1, n + 1);
    vector<int> st;
    for (int i = 1; i <= n; ++i) {
        while (sz(st) && a[st.back()] < a[i])
            st.pop_back();
        if (sz(st))
            l[i] = st.back();
        st.push_back(i);
    }
    st.clear();
    for (int i = n; i >= 1; --i) {
        while (sz(st) && a[st.back()] < a[i])
            st.pop_back();
        if (sz(st))
            r[i] = st.back();
        st.push_back(i);
    }
    upd(1, 0, n, 0, 0, 0);
    for (int i = 1; i <= n; i++) {
        //l
        int R = r[i] - 1;
        ll val = get(1, 0, n, i - 1, i - 1);
        val += c[i];
        upd(1, 0, n, i, R, val);
        cout << i << ' ' << R << ' ' << val << endl;
        //r
        int L = l[i];
        val = get(1, 0, n, L, i - 1);
        val += c[i];
        upd(1, 0, n, i, i, val);
        cout << i << ' ' << i << ' ' << val << endl;
    }
    for (int i = 0; i <= n; i++) cout << get(1, 0, n, i, i) << ' ';
}

int val[MAX];
vec < int > ans;
map < int, set < int > > mp;

void pipka (int v, int h) {
    if (h == 1) {
        ans.pb(val[v]);
        return;
    }
    val[v * 2] = val[v];
    if (mp[h - 1].empty()) {
        cout << -1;
        exit(0);
    }
    val[v * 2 + 1] = *mp[h - 1].begin();
    mp[h-1].erase(mp[h - 1].begin());
    pipka(v * 2, h - 1);
    pipka(v * 2 + 1, h - 1);
}

void solve() {
    int n;
    cin >> n;
    map < int, int > cnt;
    for (int i = 0, x; i < n; ++i) {
        cin >> x;
        cnt[x]++;
    }
    for (auto [v, kol] :cnt)
        mp[kol].insert(v);
    int h = __lg(n + 1);
    if (mp[h].empty()) {
        cout << -1;
        exit(0);
    }
    val[1] = *mp[h].begin();
    pipka(1, h);
    for (int i : ans)
        cout << i << " ";
}

int32_t main () {
//    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    solve();
}
