#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

int find(int x)
{
    if (parent[x] == x) return x;
    return parent[x] = find(parent[x]);
}

void merge(int x, int y)
{
    x = find(x); y = find(y);

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);
    dist_parents.insert(x);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

    sort(srt.begin(), srt.end());

    R = *srt.begin();
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    for (int i = 1; i < N; i++) {

        if (CSZ > RSZ) {
            bool ok = true;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                RSZ = CSZ;
                R = val[inv[i]];
            }
        }

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

int find(int x)
{
    if (parent[x] == x) return x;
    return parent[x] = find(parent[x]);
}

void merge(int x, int y)
{
    x = find(x); y = find(y);

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

    sort(srt.begin(), srt.end());

    R = *srt.begin() + 1;
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    int skip = 1;

    for (int i = 1; i < N; i++) {

        if (i == skip && CSZ > RSZ) {
            bool ok = true;

            int mnv = 1 << 25, mxd = 0;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                mnv = min(mnv, sz[*x]);
                mxd = max(mxd, sz[*x]);

                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                RSZ = CSZ;
                R = val[inv[i - 1]] + 1;
            }

            skip = mxn - mnd + 1;
        }

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

int find(int x)
{
    if (parent[x] == x) return x;
    return parent[x] = find(parent[x]);
}

void merge(int x, int y)
{
    x = find(x); y = find(y);

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

    sort(srt.begin(), srt.end());

    R = *srt.begin() + 1;
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    int skip = 0;

    for (int i = 1; i < N; i++) {

        if (skip == 0 && CSZ > RSZ) {
            bool ok = true;

            int mnv = 1 << 25, mxd = 0;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                mnv = min(mnv, sz[*x]);
                mxd = max(mxd, sz[*x]);

                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                RSZ = CSZ;
                R = val[inv[i - 1]] + 1;
            }

            skip = mxd - mnv + 1;
        }

        skip--;

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

int find(int x)
{
    if (parent[x] == x) return x;
    return parent[x] = find(parent[x]);
}

void merge(int x, int y)
{
    x = find(x); y = find(y);

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);
    dist_parents.insert(x);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

    if (N == 1) {
        puts("0");
        return 0;
    }

    sort(srt.begin(), srt.end());

    R = *srt.begin();
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    for (int i = 1; i < N; i++) {

        if (CSZ > RSZ) {
            bool ok = true;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                RSZ = CSZ;
                R = val[inv[i - 1]] + 1;
            }
        }

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<pii> dist_parents;
vector<int> v, val, srt;

int find(int x)
{
    if (parent[x] == x) return x;
    return parent[x] = find(parent[x]);
}

void merge(int x, int y)
{
    x = find(x); y = find(y);

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase({sz[y], y});
	dist_parents.erase({sz[x], x});

    sz[x] += sz[y];
    parent[y] = x;

	dist_parents.insert({sz[x], x});
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

    sort(srt.begin(), srt.end());

    R = *srt.begin() + 1;
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert({1, inv[0]});

    for (int i = 1; i < N; i++) {

        if (CSZ > RSZ) {
			if (dist_parents.begin()->f == prev(dist_parents.end())->f) {
				RSZ = CSZ;
                R = val[inv[i - 1]] + 1;
			}
        }

        CSZ++; dist_parents.insert({1, inv[i]});

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

int find(int x)
{
    if (parent[x] == x) return x;
    return parent[x] = find(parent[x]);
}

void merge(int x, int y)
{
    x = find(x); y = find(y);

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);
    dist_parents.insert(x);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

    if (N == 1) {
        puts("0");
        return 0;
    }

    sort(srt.begin(), srt.end());

    R = *srt.begin();
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    for (int i = 1; i < N; i++) {

        if (CSZ > RSZ) {
            bool ok = true;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                RSZ = CSZ;
                R = val[inv[i]];
            }
        }

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

int find(int x)
{
    if (parent[x] == x) return x;
    return parent[x] = find(parent[x]);
}

void merge(int x, int y)
{
    x = find(x); y = find(y);

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);
    dist_parents.insert(x);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

    sort(srt.begin(), srt.end());

    R = *srt.begin() + 1;
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    for (int i = 1; i < N; i++) {

        if (CSZ > RSZ) {
            bool ok = true;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                RSZ = CSZ;
                R = val[inv[i - 1]] + 1;
            }
        }

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

int find(int x)
{
    if (parent[x] == x) return x;
    return parent[x] = find(parent[x]);
}

void merge(int x, int y)
{
    x = find(x); y = find(y);

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

    sort(srt.begin(), srt.end());

    R = *srt.begin() + 1;
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    for (int i = 1; i < N; i++) {

        if (CSZ > RSZ) {
            bool ok = true;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                RSZ = CSZ;
                R = val[inv[i - 1]] + 1;
            }
        }

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1000005;
ll A[maxn];
int ID[maxn];
multiset<int>pos, lon;

bool comp( const int &s1, const int &s2 ){
    return A[s1] > A[s2];
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    int N;
    cin >> N;

    ll sol = -1;
    int mayc = 1;
    for( int i = 1; i <= N; i++ ){
        cin >> A[i];
        sol = max(sol,A[i]);
        ID[i] = i;
    }

    sol++;

    sort( ID+1, ID+N+1, comp );

    pos.insert(0);
    pos.insert(N+1);
    lon.insert(N);

    for( int i = 1; i <= N; i++ ){
        auto nxt = pos.lower_bound( ID[i] );
        auto prev = --nxt;
        nxt++;
        int l = *nxt-*prev-1;
        if( l != 0 )
            lon.erase(lon.find(l));
        l = *nxt-ID[i]-1;
        if( l )
            lon.insert(l);
        l = ID[i]-*prev-1;
        if( l )
            lon.insert(l);
        pos.insert(ID[i]);
        if( !lon.empty() && *lon.begin() == *(--lon.end()) ){
            if( (int)lon.size() >= mayc ){
                mayc = lon.size();
                sol = min(sol,A[ ID[i] ]);
            }
        }
    }

    cout << sol;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;
int A[maxn], ID[maxn];
multiset<int>pos, lon;

bool comp( const int &s1, const int &s2 ){
    return A[s1] > A[s2];
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    int N;
    cin >> N;

    int sol = -1, mayc = 1;
    for( int i = 1; i <= N; i++ ){
        cin >> A[i];
        sol = max(sol,A[i])
        ID[i] = i;
    }

    sol++;

    sort( ID+1, ID+N+1, comp );

    pos.insert(0);
    pos.insert(N+1);
    lon.insert(N);

    for( int i = 1; i <= N; i++ ){
        auto nxt = pos.lower_bound( ID[i] );
        auto prev = --nxt;
        nxt++;
        int l = *nxt-*prev-1;
        if( l != 0 )
            lon.erase(lon.find(l));
        l = *nxt-ID[i]-1;
        if( l )
            lon.insert(l);
        l = ID[i]-*prev-1;
        if( l )
            lon.insert(l);
        pos.insert(ID[i]);
        if( !lon.empty() && *lon.begin() == *(--lon.end()) ){
            if( (int)lon.size() >= mayc ){
                mayc = lon.size();
                sol = A[ ID[i] ];
            }
        }
    }

    cout << sol;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;
int A[maxn], ID[maxn];
multiset<int>pos, lon;

bool comp( const int &s1, const int &s2 ){
    return A[s1] > A[s2];
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    int N;
    cin >> N;

    for( int i = 1; i <= N; i++ ){
        cin >> A[i];
        ID[i] = i;
    }

    sort( ID+1, ID+N+1, comp );

    pos.insert(0);
    pos.insert(N+1);
    lon.insert(N);

    int sol = -1, mayc = 0;
    for( int i = 1; i <= N; i++ ){
        auto nxt = pos.lower_bound( ID[i] );
        auto prev = --nxt;
        nxt++;
        int l = *nxt-*prev-1;
        if( l != 0 )
            lon.erase(lon.find(l));
        l = *nxt-ID[i]-1;
        if( l )
            lon.insert(l);
        l = ID[i]-*prev-1;
        if( l )
            lon.insert(l);
        pos.insert(ID[i]);
        if( !lon.empty() && *lon.begin() == *(--lon.end()) ){
            if( (int)lon.size() >= mayc ){
                mayc = lon.size();
                sol = A[ ID[i] ];
            }
        }
    }

    cout << sol;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1000005;
ll A[maxn];
int ID[maxn];
multiset<int>pos, lon;

bool comp( const int &s1, const int &s2 ){
    return A[s1] > A[s2];
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    int N;
    cin >> N;

    ll sol = -1;
    int mayc = 1;
    for( int i = 1; i <= N; i++ ){
        cin >> A[i];
        sol = max(sol,A[i]);
        ID[i] = i;
    }

    sol++;

    sort( ID+1, ID+N+1, comp );

    pos.insert(0);
    pos.insert(N+1);
    lon.insert(N);

    for( int i = 1; i <= N; i++ ){
        auto nxt = pos.lower_bound( ID[i] );
        auto prev = --nxt;
        nxt++;
        int l = *nxt-*prev-1;
        if( l != 0 )
            lon.erase(lon.find(l));
        l = *nxt-ID[i]-1;
        if( l )
            lon.insert(l);
        l = ID[i]-*prev-1;
        if( l )
            lon.insert(l);
        pos.insert(ID[i]);
        if( !lon.empty() && *lon.begin() == *(--lon.end()) ){
            if( (int)lon.size() >= mayc ){
                mayc = lon.size();
                sol = min(sol,A[ ID[i+1] ]+1);
            }
        }
    }

    cout << sol;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;
int A[maxn], ID[maxn];
multiset<int>pos, lon;

bool comp( const int &s1, const int &s2 ){
    return A[s1] > A[s2];
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    int N;
    cin >> N;

    int sol = -1, mayc = 1;
    for( int i = 1; i <= N; i++ ){
        cin >> A[i];
        sol = max(sol,A[i]);
        ID[i] = i;
    }

    sol++;

    sort( ID+1, ID+N+1, comp );

    pos.insert(0);
    pos.insert(N+1);
    lon.insert(N);

    for( int i = 1; i <= N; i++ ){
        auto nxt = pos.lower_bound( ID[i] );
        auto prev = --nxt;
        nxt++;
        int l = *nxt-*prev-1;
        if( l != 0 )
            lon.erase(lon.find(l));
        l = *nxt-ID[i]-1;
        if( l )
            lon.insert(l);
        l = ID[i]-*prev-1;
        if( l )
            lon.insert(l);
        pos.insert(ID[i]);
        if( !lon.empty() && *lon.begin() == *(--lon.end()) ){
            if( (int)lon.size() >= mayc ){
                mayc = lon.size();
                sol = A[ ID[i] ];
            }
        }
    }

    cout << sol;

    return 0;
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.StringTokenizer;

public class cf1200A
{
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
  public static void main(String[] args){
    FastReader f=new FastReader();
    int n =f.nextInt();
    TreeMap<Integer, Integer> map=new TreeMap<Integer, Integer>();
    Stack<Integer>q=new Stack<Integer>();
    for(int i=0;i<n;i++){
        int t=f.nextInt();
        map.put(t,i+1);
    }
    String str=f.next();
    for(int i=0;i<2*n;i++){
        if(str.charAt(i)=='0'){
            System.out.print(map.get(map.firstKey())+" ");
            q.add(map.get(map.firstKey()));
            map.remove(map.firstKey());
        }
        else{
            System.out.print(q.pop()+" ");

        }

    }
}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.StringTokenizer;

public class cf1200A
{
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
  public static void main(String[] args){
    FastReader f=new FastReader();
    int n =f.nextInt();
    TreeMap<Integer, Integer> map=new TreeMap<Integer, Integer>();
    Stack<Integer>q=new Stack<Integer>();
    for(int i=0;i<n;i++){
        int t=f.nextInt();
        map.put(t,i+1);
    }
    String str=f.next();
    for(int i=0;i<2*n;i++){
        if(str.charAt(i)=='0'){
            System.out.print(map.get(map.firstKey()));
            q.add(map.get(map.firstKey()));
            map.remove(map.firstKey());
        }
        else{
            System.out.print(q.pop());

        }

    }
}
}


import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Unstoppable solver = new Unstoppable();
	//	int t=in.nextInt();
	//	while(t-->0)
        solver.solve(in, out);
        out.close();
    }

    static class Unstoppable {

         public void solve(InputReader in, PrintWriter out) {
         int n=in.nextInt();
         int a[][]=new int[n][2];
         for(int i=0;i<n;i++){ a[i][1]=in.nextInt(); a[i][0]=i; }
         int ans[]=new int[2*n];
         char ch[]=in.next().toCharArray();
         Arrays.sort(a,new Comparator<int[]>(){
            public int compare(int a[],int b[]){
                return a[1]-b[1];
            }
         });
         int inn=-1;
         TreeMap<Integer,Integer> tm=new TreeMap<Integer,Integer>();
         for(int i=0;i<2*n;i++){
             if(ch[i]=='0'){
                 ans[i]=a[++inn][0]+1; tm.put(a[inn][1],ans[i]);
             }
             else{
                 int key=(int)tm.lowerKey(Integer.MAX_VALUE);
                 ans[i]=tm.get(key); tm.remove(key);
             }
         }
         for(int i=0;i<2*n;i++) out.print(ans[i]+" "); out.println("");
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public long nextLong(){
            return Long.parseLong(next());
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Unstoppable solver = new Unstoppable();
	//	int t=in.nextInt();
	//	while(t-->0)
        solver.solve(in, out);
        out.close();
    }

    static class Unstoppable {

         public void solve(InputReader in, PrintWriter out) {
         int n=in.nextInt();
         int a[][]=new int[n][2];
         for(int i=0;i<n;i++){ a[i][1]=in.nextInt(); a[i][0]=i; }
         int ans[]=new int[2*n];
         char ch[]=in.next().toCharArray();
         Arrays.sort(a,new Comparator<int[]>(){
            public int compare(int a[],int b[]){
                return a[1]-b[1];
            }
         });
         int inn=0;
         PriorityQueue<Integer> pq=new PriorityQueue<Integer>(n,(e1, e2) -> { return a[e1-1][1]-a[e2-1][1]; });
         for(int i=0;i<2*n;i++){
             if(ch[i]=='0'){
                 ans[i]=a[inn++][0]+1; pq.add(ans[i]);
             }
             else{
                 ans[i]=pq.poll();
             }
         }
         for(int i=0;i<2*n;i++) out.print(ans[i]+" "); out.println("");
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public long nextLong(){
            return Long.parseLong(next());
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Unstoppable solver = new Unstoppable();
	//	int t=in.nextInt();
	//	while(t-->0)
        solver.solve(in, out);
        out.close();
    }

    static class Unstoppable {

         public void solve(InputReader in, PrintWriter out) {
         int n=in.nextInt();
         int a[][]=new int[n][2];
         for(int i=0;i<n;i++){ a[i][1]=in.nextInt(); a[i][0]=i; }
         int ans[]=new int[2*n];
         char ch[]=in.next().toCharArray();
         Arrays.sort(a,new Comparator<int[]>(){
            public int compare(int a[],int b[]){
                return a[1]-b[1];
            }
         });
         int inn=0;
         PriorityQueue<Integer> pq=new PriorityQueue<Integer>(n,Collections.reverseOrder());
         for(int i=0;i<2*n;i++){
             if(ch[i]=='0'){
                 ans[i]=a[inn++][0]+1; pq.add(ans[i]);
             }
             else{
                 ans[i]=pq.poll();
             }
         }
         for(int i=0;i<2*n;i++) out.print(ans[i]+" "); out.println("");
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public long nextLong(){
            return Long.parseLong(next());
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Unstoppable solver = new Unstoppable();
	//	int t=in.nextInt();
	//	while(t-->0)
        solver.solve(in, out);
        out.close();
    }

    static class Unstoppable {

         public void solve(InputReader in, PrintWriter out) {
         int n=in.nextInt();
         int a[][]=new int[n][2];
         for(int i=0;i<n;i++){ a[i][1]=in.nextInt(); a[i][0]=i; }
         int ans[]=new int[2*n];
         char ch[]=in.next().toCharArray();
         Arrays.sort(a,new Comparator<int[]>(){
            public int compare(int a[],int b[]){
                return b[1]-a[1];
            }
         });
         int inn=0,ex=0;
         for(int i=0;i<2*n;i++){
             if(ch[i]=='0'){
                 ans[i]=a[inn++][0]+1;
             }
             else{
                 ans[i]=a[ex++][0]+1;
             }
         }
         for(int i=0;i<2*n;i++) out.print(ans[i]+" "); out.println("");
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public long nextLong(){
            return Long.parseLong(next());
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public final class BusCharactersCDF {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Scanner sc = new Scanner(br);
        int n = sc.nextInt();
        Pair arr[] = new Pair[n];
        int pos[] = new int[n];

        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            arr[i] = new Pair(x, i);
        }
        String s = sc.next();
        Arrays.sort(arr, new Comparator<Pair>() {
            @Override
            public int compare(Pair a, Pair b) {
                if (a.first < b.first) {
                    return -1;
                } else if (a.first > b.first) {
                    return 1;
                } else
                    return 0;
            }
        });
        ArrayList<Integer> l=new ArrayList<>();
        int last = -1;
        for (int i = 0; i < 2*n; i++) {
            if (s.charAt(i) == '0') {
                last++;
                int index = arr[last].second + 1;
                l.add(index);
                System.out.print(index+" ");
            }else{
                int latest=l.get(l.size()-1);
                l.remove(l.size()-1);
                System.out.print(latest+" ");
            }
        }

    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public final class BusCharactersCDF {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Scanner sc = new Scanner(br);
        int n = sc.nextInt();
        Pair arr[] = new Pair[n];
        int pos[] = new int[n];

        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            arr[i] = new Pair(x, i);
        }
        String s = sc.next();
        Arrays.sort(arr, new Comparator<Pair>() {
            @Override
            public int compare(Pair a, Pair b) {
                if (a.first < b.first) {
                    return -1;
                } else if (a.first > b.first) {
                    return 1;
                } else
                    return 0;
            }
        });
        ArrayList<Integer> l=new ArrayList<>();
        int last = -1;
        for (int i = 0; i < 2*n; i++) {
            if (s.charAt(i) == '0') {
                last++;
                int index = arr[last].second + 1;
                l.add(index);
                System.out.print(index+" ");
            }else{
                int latest=l.get(l.size()-1);
                l.remove(l.size()-1);
                System.out.print(latest+" ");
            }
        }

    }
}

class Pair {
    int first;
    int second;
    Pair(int first,int second){
        this.first=first;
        this.second=second;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
struct SEAT {
	int id, w;
	friend bool  operator < (SEAT &a, SEAT &b) {
		return a.w < b.w;
	}
};
deque<SEAT> has1, has2;
const int maxn = 410000;
char s[maxn * 2];
int main() {
	int n, w;
	while(~scanf("%d", &n)) {
		has1.clear();
		has2.clear();
		SEAT seat;
		for(int i = 0; i < n; i++) {
			seat.id = i + 1;
			scanf("%d", &w);
			seat.w = w;
			has2.push_back(seat);
		}
		int ans[maxn];
		sort(has2.begin(), has2.end());
		scanf("%s", s);
		for(int i = 0; i < 2 * n; i++) {
			if(s[i] == '0') {
				ans[i] = has2.front().id;
				has1.push_front(has2.front());
				has2.pop_front();
			} else {
				ans[i] = has1.front().id;
				has1.pop_front();
			}
		}
		for(int i = 0; i < 2 * n; i++) {
			printf("%d", ans[i]);
			if(i != 2 * n - 1) printf(" ");
		}
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
struct SEAT {
	int id, w;
	friend bool  operator < (SEAT &a, SEAT &b) {
		return a.w < b.w;
	}
};
deque<SEAT> has1, has2;
const int maxn = 210000;
char s[maxn];
int main() {
	int n, w;
	while(scanf("%d", &n)) {
		has1.clear();
		has2.clear();
		SEAT seat;
		for(int i = 0; i < n; i++) {
			seat.id = i + 1;
			scanf("%d", &w);
			seat.w = w;
			has2.push_back(seat);
		}
		int ans[maxn];
		sort(has2.begin(), has2.end());
		scanf("%s", s);
		for(int i = 0; i < 2 * n; i++) {
			if(s[i] == '0') {
				ans[i] = has2.front().id;
				has1.push_front(has2.front());
				has2.pop_front();
			} else {
				ans[i] = has1.front().id;
				has1.pop_front();
			}
		}
		for(int i = 0; i < 2 * n; i++) {
			printf("%d", ans[i]);
			if(i != 2 * n - 1) printf(" ");
		}
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
struct SEAT {
	int id, w;
	friend bool  operator < (SEAT &a, SEAT &b) {
		return a.w < b.w;
	}
};
deque<SEAT> has1, has2;
const int maxn = 210000;
char s[maxn * 2];
int main() {
	int n, w;
	while(~scanf("%d", &n)) {
		has1.clear();
		has2.clear();
		SEAT seat;
		for(int i = 0; i < n; i++) {
			seat.id = i + 1;
			scanf("%d", &w);
			seat.w = w;
			has2.push_back(seat);
		}
		int ans[maxn];
		sort(has2.begin(), has2.end());
		scanf("%s", s);
		for(int i = 0; i < 2 * n; i++) {
			if(s[i] == '0') {
				ans[i] = has2.front().id;
				has1.push_front(has2.front());
				has2.pop_front();
			} else {
				ans[i] = has1.front().id;
				has1.pop_front();
			}
		}
		for(int i = 0; i < 2 * n; i++) {
			printf("%d", ans[i]);
			if(i != 2 * n - 1) printf(" ");
		}
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
struct SEAT {
	int id, w;
	friend bool  operator < (SEAT &a, SEAT &b) {
		return a.w < b.w;
	}
};
deque<SEAT> has1, has2;
const int maxn = 210000;
char s[maxn];
int main() {
	int n, w;
	while(~scanf("%d", &n)) {
		has1.clear();
		has2.clear();
		SEAT seat;
		for(int i = 0; i < n; i++) {
			seat.id = i + 1;
			scanf("%d", &w);
			seat.w = w;
			has2.push_back(seat);
		}
		int ans[maxn];
		sort(has2.begin(), has2.end());
		scanf("%s", s);
		for(int i = 0; i < 2 * n; i++) {
			if(s[i] == '0') {
				ans[i] = has2.front().id;
				has1.push_front(has2.front());
				has2.pop_front();
			} else {
				ans[i] = has1.front().id;
				has1.pop_front();
			}
		}
		for(int i = 0; i < 2 * n; i++) {
			printf("%d", ans[i]);
			if(i != 2 * n - 1) printf(" ");
		}
		printf("\n");
	}
	return 0;
}


/***
	B16080414
	wrjlinkkkkkk
***/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>

#define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,x<<1
#define rson mid+1,r,x<<1|1
#define lowbit(x) ((x)&(-x))

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const int maxn = 6e5 + 100;
const int maxm = 5e4 + 100;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;

struct seat{
	int w;
	int id;
	bool operator < (const seat &a) const{
		return a.w < w;
	}
}st[maxn];
bool cmp(seat a, seat b){
	return a.w < b.w;

}
int Scan(){
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
int main(){
	int n;
	char s[maxn];
	int a[maxn];
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		//st[i].w=Scan();
		scanf("%d", st[i].w);
		st[i].id = i+1;
	}
	sort(st, st+n, cmp);
    getchar();
    scanf("%s", s);
    set<seat>ss;
    int p = 0;
    for(int i = 0; i < 2*n; i++){
    	if(s[i]=='0'){
    		ss.insert(st[p]);
    		printf("%d ", st[p++].id);
		}
	    else if(s[i] =='1'){
	    	printf("%d ", (*ss.begin()).id);
	    	ss.erase(ss.begin());
		}
	}
	return 0;
}
/*
3 3 1
1 2 2 2 2 2 3 3 3
*/


/***
	B16080414
	wrjlinkkkkkk
***/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>

#define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,x<<1
#define rson mid+1,r,x<<1|1
#define lowbit(x) ((x)&(-x))

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const int maxn = 6e5 + 100;
const int maxm = 5e4 + 100;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;

struct seat{
	int w;
	int id;
	bool operator < (const seat &a) const{
		return a.w < w;
	}
}st[maxn];
bool cmp(seat a, seat b){
	return a.w < b.w;

}
int Scan(){
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
int n;
char s[maxn];
int a[maxn];
int main(){

	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		//st[i].w=Scan();
		scanf("%d", st[i].w);
		st[i].id = i+1;
	}
	sort(st, st+n, cmp);
    getchar();
    scanf("%s", s);
    set<seat>ss;
    int p = 0;
    for(int i = 0; i < 2*n; i++){
    	if(s[i]=='0'){
    		ss.insert(st[p]);
    		printf("%d ", st[p++].id);
		}
	    else if(s[i] =='1'){
	    	printf("%d ", (*ss.begin()).id);
	    	ss.erase(ss.begin());
		}
	}
	return 0;
}
/*
3 3 1
1 2 2 2 2 2 3 3 3
*/


/***
	B16080414
	wrjlinkkkkkk
***/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>

#define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,x<<1
#define rson mid+1,r,x<<1|1
#define lowbit(x) ((x)&(-x))

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const int maxn = 2e5 + 100;
const int maxm = 5e4 + 100;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;

struct seat{
	int w;
	int id;
	bool operator < (const seat &a) const{
		return a.w < w;
	}
}st[maxn];
bool cmp(seat a, seat b){
	return a.w < b.w;

}
int main(){
	int n;
	char s[maxn];
	int a[maxn];
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &st[i].w);
		st[i].id = i+1;
	}
	sort(st, st+n, cmp);
    getchar();
    scanf("%s", s);
    set<seat>ss;
    int p = 0;
    for(int i = 0; i < 2*n; i++){
    	if(s[i]=='0'){
    		ss.insert(st[p]);
    		printf("%d ", st[p++].id);
		}
	    else if(s[i] =='1'){
	    	printf("%d ", (*ss.begin()).id);
	    	ss.erase(ss.begin());
		}
	}
	return 0;
}
/*
3 3 1
1 2 2 2 2 2 3 3 3
*/


/***
	B16080414
	wrjlinkkkkkk
***/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>

#define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,x<<1
#define rson mid+1,r,x<<1|1
#define lowbit(x) ((x)&(-x))

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const int maxn = 5e5 + 100;
const int maxm = 5e4 + 100;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;

struct seat{
	int w;
	int id;
	bool operator < (const seat &a) const{
		return a.w < w;
	}
}st[maxn];
bool cmp(seat a, seat b){
	return a.w < b.w;

}
int main(){
	int n;
	char s[maxn];
	int a[maxn];
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &st[i].w);
		st[i].id = i+1;
	}
	sort(st, st+n, cmp);
    getchar();
    scanf("%s", s);
    set<seat>ss;
    int p = 0;
    for(int i = 0; i < 2*n; i++){
    	if(s[i]=='0'){
    		ss.insert(st[p]);
    		printf("%d ", st[p++].id);
		}
	    else if(s[i] =='1'){
	    	printf("%d ", (*ss.begin()).id);
	    	ss.erase(ss.begin());
		}
	}
	return 0;
}
/*
3 3 1
1 2 2 2 2 2 3 3 3
*/


/***
	B16080414
	wrjlinkkkkkk
***/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>

#define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,x<<1
#define rson mid+1,r,x<<1|1
#define lowbit(x) ((x)&(-x))

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const int maxn = 6e5 + 100;
const int maxm = 5e4 + 100;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;

struct seat{
	int w;
	int id;
	bool operator < (const seat &a) const{
		return a.w < w;
	}
}st[maxn];
bool cmp(seat a, seat b){
	return a.w < b.w;

}
int Scan(){
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
int main(){
	int n;
	char s[maxn];
	int a[maxn];
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		st[i].w=Scan();
		st[i].id = i+1;
	}
	sort(st, st+n, cmp);
    getchar();
    scanf("%s", s);
    set<seat>ss;
    int p = 0;
    for(int i = 0; i < 2*n; i++){
    	if(s[i]=='0'){
    		ss.insert(st[p]);
    		printf("%d ", st[p++].id);
		}
	    else if(s[i] =='1'){
	    	printf("%d ", (*ss.begin()).id);
	    	ss.erase(ss.begin());
		}
	}
	return 0;
}
/*
3 3 1
1 2 2 2 2 2 3 3 3
*/


/***
	B16080414
	wrjlinkkkkkk
***/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>

#define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,x<<1
#define rson mid+1,r,x<<1|1
#define lowbit(x) ((x)&(-x))

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const int maxn = 8e5 + 100;
const int maxm = 5e4 + 100;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;

struct seat{
	int w;
	int id;
	bool operator < (const seat &a) const{
		return a.w < w;
	}
}st[maxn];
bool cmp(seat a, seat b){
	return a.w < b.w;

}
int Scan(){
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
int main(){
	int n;
	char s[maxn];
	int a[maxn];
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		st[i].w=Scan();
		st[i].id = i+1;
	}
	sort(st, st+n, cmp);
    getchar();
    scanf("%s", s);
    set<seat>ss;
    int p = 0;
    for(int i = 0; i < 2*n; i++){
    	if(s[i]=='0'){
    		ss.insert(st[p]);
    		printf("%d ", st[p++].id);
		}
	    else if(s[i] =='1'){
	    	printf("%d ", (*ss.begin()).id);
	    	ss.erase(ss.begin());
		}
	}
	return 0;
}
/*
3 3 1
1 2 2 2 2 2 3 3 3
*/


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
//#include<ext/pb_ds/detail/standard_policies.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;

#define F               first
#define S               second
#define pb              push_back
#define pp              pop_back()
#define UB              upper_bound
#define LB              lower_bound
#define ui              unsigned int
#define ll              long long int
#define ull             unsigned long long int
#define ld              double
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define vi              vector<int>
#define vll             vector<ll>
#define vc              vector<char>
#define vs              vector<string>
#define vpii            vector<pii>
#define vpll            vector<pll>
#define mpii            map<int,int>
#define mpll            map<ll,ll>
#define ump             unordered_map
#define uset            unordered_set
#define PQ              priority_queue

#define nl                  '\n'
#define pf                  printf
#define sfc(a)              scanf("%c",&a)
#define sfs(a)              scanf("%s",&a)
#define sfi1(a)             scanf("%d",&a)
#define sfi2(a,b)           scanf("%d %d",&a,&b)
#define sfi3(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfi4(a,b,c,d)       scanf("%d %d %d %d",&a,&b,&c,&d)
#define sfd1(a)             scanf("%lf",&a)
#define sfd2(a,b)           scanf("%lf %lf",&a,&b)
#define sfd3(a,b,c)         scanf("%lf %lf %lf",&a,&b,&c)
#define sfd4(a,b,c,d)       scanf("%lf %lf %lf %lf",&a,&b,&c,&d)
#define sfll1(a)            scanf("%lld",&a)
#define sfll2(a,b)          scanf("%lld %lld",&a,&b)
#define sfll3(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sfll4(a,b,c,d)      scanf("%lld %lld %lld %lld",&a,&b,&c,&d)

#define sqr(a)              ((a)*(a))
#define dist(x1,y1,x2,y2)   sqrt(sqr(x1-x2)+sqr(y1-y2))
#define rev(v)              reverse(v.begin(),v.end())
#define srt(v)              sort(v.begin(),v.end())
#define grtsrt(v)           sort(v.begin(),v.end(),greater<int>())
#define all(v)              v.begin(),v.end()
#define minv(v)             *min_element(v.begin(),v.end())
#define maxv(v)             *max_element(v.begin(),v.end())
#define cntv(v,a)           count(v.begin(),v.end(),a)
#define one(x)              __builtin_popcount(x)
#define valid(tx,ty)        tx>=0&&tx<r&&ty>=0&&ty<c
#define mem(arr,val)        memset(arr,val,sizeof(arr))
#define Unique(x)           x.erase(unique(x.begin(),x.end()),x.end())
#define fast                ios_base::sync_with_stdio(false),cin.tie(NULL)
#define forstl(it,v)        for(__typeof(v.begin()) it=v.begin();it!=v.end();++it)
#define loop(i,n)           for(int i=0;i<n;i++)
#define loop1(i,n)          for(int i=1;i<=n;i++)
#define _case               printf("Case %d: ",cs)

template <typename T> T gcd(T a, T b){while (b){T t = a % b;a = b;b = t;}return a;}
template <typename T> T lcm(T a, T b){return a / gcd(a, b) * b;}
template <typename T> T gcd(T a, T b, T c){return gcd(gcd(a, b), c);}
template <typename T> bool isPrime(T x){if (x<=4||x%2==0||x%3==0)return x == 2 || x == 3;for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0;return 1;}
template <typename T> bool bitcheck(T p,T pos){return (bool)(p&(1<<pos));}
template <typename T> T biton(T p,T pos){return p=p|(1<<pos);}
template <typename T> T bitoff(T p,T pos){return p=p&~(1<<pos);}
template <typename T> ll toint(T s) {ll p;stringstream ss(s); ss>>p; return p;}
template <typename T> string tostring(T n) {stringstream ss; ss << n; return ss.str();}
template <typename T> T POW(T b,T p) {T Ans=1; while(p){if(p&1)Ans=(Ans*b);b=(b*b);p>>=1;}return Ans;}
template <typename T> T BigMod(T b,T p,T Mod) {T Ans=1; while(p){if(p&1)Ans=(Ans*b)%Mod;b=(b*b)%Mod;p>>=1;}return Ans;}
template <typename T> T ModInverse(T p,T Mod) {return BigMod(p,Mod-2,Mod);}
//template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//template <typename T> using ordered_set2 = tree<pair <T, string>, null_type, less<pair <T, string>>, rb_tree_tag, tree_order_statistics_node_update>;
//ll histogram(ll arr[],ll n){ll top,i=0,sum=0,max_area=0;stack<ll>st;st.push(0);while(i<n){if(st.empty()||arr[st.top()]<=arr[i]) st.push(i++);else{top=st.top();st.pop();if(st.empty())sum=arr[top]*i;else sum=arr[top]*(i-st.top()-1);max_area=max(sum,max_area);}}while(!st.empty()){top=st.top();st.pop();if(st.empty()) sum=arr[top]*i;else sum=arr[top]*(i-st.top()-1);max_area=max(sum,max_area);}return max_area;}
//ll ext_gcd(ll a,ll b,ll *x,ll *y){if(a==0){*x=0;*y=1;return b;}ll x1,y1;ll gcd = ext_gcd(b%a,a,&x1,&y1);*x=y1-(b/a)*x1;*y=x1;return gcd;}
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); deb(_it, args); }
void deb(istream_iterator<string> it) {}
template<typename T, typename... Args> void deb(istream_iterator<string> it, T a, Args... args) {cerr << *it << " = " << a << endl;deb(++it, args...);}

const int fx[]      = {+0,+0,+1,-1,-1,+1,-1,+1};
const int fy[]      = {-1,+1,+0,+0,+1,+1,-1,-1};
const int mx        = 200000+10;
const int inf       = 0x3f3f3f3f;
const int mod       = 1e9+7;
const ld pi         = 2*acos(0.0);
const ld E          = 2.71828182845904523536;
const ld eps        = 1e-9;
//ll fact[mx];
//void factorial(){fact[0]=1;loop1(i,mx-1) fact[i]=(fact[i-1]*i)%mod;}
//ll nCr(ll n,ll r){return (fact[n]*ModInverse(((ll)fact[n-r]*(ll)fact[r])%mod,(ll)mod))%mod;}
//bool mark[mx];vector<ll>prime;
//void sieve(){memset(mark,true,sizeof(mark));for(ll i=3;i<=(ll)sqrt(mx);i+=2){if(mark[i]){for(ll j=i*i;j<=mx;j+=2*i) mark[j]=false;}}prime.push_back(2);for(ll i=3;i<=mx;i+=2) if(mark[i]) prime.push_back(i);}
///code start here
//PQ<ll>ami;
PQ<ll,vll,greater<ll> > ami;
PQ<ll>tumi;
mpll ind;
ll w;
int main(){
//    freopen("Input.txt","r",stdin); freopen("Output.txt","w",stdout);
    ll n,w;
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>w;
        //debug(w);
        ind[w]=1ll*i;
        //debug(n);
        ami.push(w);
    }

    //debug(n);
    string s;
    cin>>s;
    vll ans;
    for(auto it : s){
        if(it=='0'){
            ans.pb(ind[ami.top()]);
            tumi.push(ami.top());
            ami.pop();
        }
        else{
            ans.pb(ind[tumi.top()]);
            tumi.pop();
        }
    }
    for(auto it:ans){
        cout<<it<<" ";
    }
    cout<<nl;
    return 0;
}
/*

*/


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
//#include<ext/pb_ds/detail/standard_policies.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;

#define F               first
#define S               second
#define pb              push_back
#define pp              pop_back()
#define UB              upper_bound
#define LB              lower_bound
#define ui              unsigned int
#define ll              long long int
#define ull             unsigned long long int
#define ld              double
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define vi              vector<int>
#define vll             vector<ll>
#define vc              vector<char>
#define vs              vector<string>
#define vpii            vector<pii>
#define vpll            vector<pll>
#define mpii            map<int,int>
#define mpll            map<ll,ll>
#define ump             unordered_map
#define uset            unordered_set
#define PQ              priority_queue

#define nl                  '\n'
#define pf                  printf
#define sfc(a)              scanf("%c",&a)
#define sfs(a)              scanf("%s",&a)
#define sfi1(a)             scanf("%d",&a)
#define sfi2(a,b)           scanf("%d %d",&a,&b)
#define sfi3(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfi4(a,b,c,d)       scanf("%d %d %d %d",&a,&b,&c,&d)
#define sfd1(a)             scanf("%lf",&a)
#define sfd2(a,b)           scanf("%lf %lf",&a,&b)
#define sfd3(a,b,c)         scanf("%lf %lf %lf",&a,&b,&c)
#define sfd4(a,b,c,d)       scanf("%lf %lf %lf %lf",&a,&b,&c,&d)
#define sfll1(a)            scanf("%lld",&a)
#define sfll2(a,b)          scanf("%lld %lld",&a,&b)
#define sfll3(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sfll4(a,b,c,d)      scanf("%lld %lld %lld %lld",&a,&b,&c,&d)

#define sqr(a)              ((a)*(a))
#define dist(x1,y1,x2,y2)   sqrt(sqr(x1-x2)+sqr(y1-y2))
#define rev(v)              reverse(v.begin(),v.end())
#define srt(v)              sort(v.begin(),v.end())
#define grtsrt(v)           sort(v.begin(),v.end(),greater<int>())
#define all(v)              v.begin(),v.end()
#define minv(v)             *min_element(v.begin(),v.end())
#define maxv(v)             *max_element(v.begin(),v.end())
#define cntv(v,a)           count(v.begin(),v.end(),a)
#define one(x)              __builtin_popcount(x)
#define valid(tx,ty)        tx>=0&&tx<r&&ty>=0&&ty<c
#define mem(arr,val)        memset(arr,val,sizeof(arr))
#define Unique(x)           x.erase(unique(x.begin(),x.end()),x.end())
#define fast                ios_base::sync_with_stdio(false),cin.tie(NULL)
#define forstl(it,v)        for(__typeof(v.begin()) it=v.begin();it!=v.end();++it)
#define loop(i,n)           for(int i=0;i<n;i++)
#define loop1(i,n)          for(int i=1;i<=n;i++)
#define _case               printf("Case %d: ",cs)

template <typename T> T gcd(T a, T b){while (b){T t = a % b;a = b;b = t;}return a;}
template <typename T> T lcm(T a, T b){return a / gcd(a, b) * b;}
template <typename T> T gcd(T a, T b, T c){return gcd(gcd(a, b), c);}
template <typename T> bool isPrime(T x){if (x<=4||x%2==0||x%3==0)return x == 2 || x == 3;for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0;return 1;}
template <typename T> bool bitcheck(T p,T pos){return (bool)(p&(1<<pos));}
template <typename T> T biton(T p,T pos){return p=p|(1<<pos);}
template <typename T> T bitoff(T p,T pos){return p=p&~(1<<pos);}
template <typename T> ll toint(T s) {ll p;stringstream ss(s); ss>>p; return p;}
template <typename T> string tostring(T n) {stringstream ss; ss << n; return ss.str();}
template <typename T> T POW(T b,T p) {T Ans=1; while(p){if(p&1)Ans=(Ans*b);b=(b*b);p>>=1;}return Ans;}
template <typename T> T BigMod(T b,T p,T Mod) {T Ans=1; while(p){if(p&1)Ans=(Ans*b)%Mod;b=(b*b)%Mod;p>>=1;}return Ans;}
template <typename T> T ModInverse(T p,T Mod) {return BigMod(p,Mod-2,Mod);}
//template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//template <typename T> using ordered_set2 = tree<pair <T, string>, null_type, less<pair <T, string>>, rb_tree_tag, tree_order_statistics_node_update>;
//ll histogram(ll arr[],ll n){ll top,i=0,sum=0,max_area=0;stack<ll>st;st.push(0);while(i<n){if(st.empty()||arr[st.top()]<=arr[i]) st.push(i++);else{top=st.top();st.pop();if(st.empty())sum=arr[top]*i;else sum=arr[top]*(i-st.top()-1);max_area=max(sum,max_area);}}while(!st.empty()){top=st.top();st.pop();if(st.empty()) sum=arr[top]*i;else sum=arr[top]*(i-st.top()-1);max_area=max(sum,max_area);}return max_area;}
//ll ext_gcd(ll a,ll b,ll *x,ll *y){if(a==0){*x=0;*y=1;return b;}ll x1,y1;ll gcd = ext_gcd(b%a,a,&x1,&y1);*x=y1-(b/a)*x1;*y=x1;return gcd;}
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); deb(_it, args); }
void deb(istream_iterator<string> it) {}
template<typename T, typename... Args> void deb(istream_iterator<string> it, T a, Args... args) {cerr << *it << " = " << a << endl;deb(++it, args...);}

const int fx[]      = {+0,+0,+1,-1,-1,+1,-1,+1};
const int fy[]      = {-1,+1,+0,+0,+1,+1,-1,-1};
const int mx        = 200000+10;
const int inf       = 0x3f3f3f3f;
const int mod       = 1e9+7;
const ld pi         = 2*acos(0.0);
const ld E          = 2.71828182845904523536;
const ld eps        = 1e-9;
//ll fact[mx];
//void factorial(){fact[0]=1;loop1(i,mx-1) fact[i]=(fact[i-1]*i)%mod;}
//ll nCr(ll n,ll r){return (fact[n]*ModInverse(((ll)fact[n-r]*(ll)fact[r])%mod,(ll)mod))%mod;}
//bool mark[mx];vector<ll>prime;
//void sieve(){memset(mark,true,sizeof(mark));for(ll i=3;i<=(ll)sqrt(mx);i+=2){if(mark[i]){for(ll j=i*i;j<=mx;j+=2*i) mark[j]=false;}}prime.push_back(2);for(ll i=3;i<=mx;i+=2) if(mark[i]) prime.push_back(i);}
///code start here
PQ<ll>ami;
//PQ<ll,vll,greater<ll> > ami;
PQ<ll>tumi;
mpll ind;
ll w;
int main(){
//    freopen("Input.txt","r",stdin); freopen("Output.txt","w",stdout);
    ll n,w;
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>w;
        //debug(w);
        ind[w]=1ll*i;
        //debug(n);
        ami.push(w);
    }

    //debug(n);
    string s;
    cin>>s;
    vll ans;
    for(auto it : s){
        if(it=='0'){
            ans.pb(ind[ami.top()]);
            tumi.push(ami.top());
            ami.pop();
        }
        else{
            ans.pb(ind[tumi.top()]);
            tumi.pop();
        }
    }
    for(auto it:ans){
        cout<<it<<" ";
    }
    cout<<nl;
    return 0;
}
/*

*/


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
//#include<ext/pb_ds/detail/standard_policies.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;

#define F               first
#define S               second
#define pb              push_back
#define pp              pop_back()
#define UB              upper_bound
#define LB              lower_bound
#define ui              unsigned int
#define ll              long long int
#define ull             unsigned long long int
#define ld              double
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define vi              vector<int>
#define vll             vector<ll>
#define vc              vector<char>
#define vs              vector<string>
#define vpii            vector<pii>
#define vpll            vector<pll>
#define mpii            map<int,int>
#define mpll            map<ll,ll>
#define ump             unordered_map
#define uset            unordered_set
#define PQ              priority_queue

#define nl                  '\n'
#define pf                  printf
#define sfc(a)              scanf("%c",&a)
#define sfs(a)              scanf("%s",&a)
#define sfi1(a)             scanf("%d",&a)
#define sfi2(a,b)           scanf("%d %d",&a,&b)
#define sfi3(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfi4(a,b,c,d)       scanf("%d %d %d %d",&a,&b,&c,&d)
#define sfd1(a)             scanf("%lf",&a)
#define sfd2(a,b)           scanf("%lf %lf",&a,&b)
#define sfd3(a,b,c)         scanf("%lf %lf %lf",&a,&b,&c)
#define sfd4(a,b,c,d)       scanf("%lf %lf %lf %lf",&a,&b,&c,&d)
#define sfll1(a)            scanf("%lld",&a)
#define sfll2(a,b)          scanf("%lld %lld",&a,&b)
#define sfll3(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sfll4(a,b,c,d)      scanf("%lld %lld %lld %lld",&a,&b,&c,&d)

#define sqr(a)              ((a)*(a))
#define dist(x1,y1,x2,y2)   sqrt(sqr(x1-x2)+sqr(y1-y2))
#define rev(v)              reverse(v.begin(),v.end())
#define srt(v)              sort(v.begin(),v.end())
#define grtsrt(v)           sort(v.begin(),v.end(),greater<int>())
#define all(v)              v.begin(),v.end()
#define minv(v)             *min_element(v.begin(),v.end())
#define maxv(v)             *max_element(v.begin(),v.end())
#define cntv(v,a)           count(v.begin(),v.end(),a)
#define one(x)              __builtin_popcount(x)
#define valid(tx,ty)        tx>=0&&tx<r&&ty>=0&&ty<c
#define mem(arr,val)        memset(arr,val,sizeof(arr))
#define Unique(x)           x.erase(unique(x.begin(),x.end()),x.end())
#define fast                ios_base::sync_with_stdio(false),cin.tie(NULL)
#define forstl(it,v)        for(__typeof(v.begin()) it=v.begin();it!=v.end();++it)
#define loop(i,n)           for(int i=0;i<n;i++)
#define loop1(i,n)          for(int i=1;i<=n;i++)
#define _case               printf("Case %d: ",cs)

template <typename T> T gcd(T a, T b){while (b){T t = a % b;a = b;b = t;}return a;}
template <typename T> T lcm(T a, T b){return a / gcd(a, b) * b;}
template <typename T> T gcd(T a, T b, T c){return gcd(gcd(a, b), c);}
template <typename T> bool isPrime(T x){if (x<=4||x%2==0||x%3==0)return x == 2 || x == 3;for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0;return 1;}
template <typename T> bool bitcheck(T p,T pos){return (bool)(p&(1<<pos));}
template <typename T> T biton(T p,T pos){return p=p|(1<<pos);}
template <typename T> T bitoff(T p,T pos){return p=p&~(1<<pos);}
template <typename T> ll toint(T s) {ll p;stringstream ss(s); ss>>p; return p;}
template <typename T> string tostring(T n) {stringstream ss; ss << n; return ss.str();}
template <typename T> T POW(T b,T p) {T Ans=1; while(p){if(p&1)Ans=(Ans*b);b=(b*b);p>>=1;}return Ans;}
template <typename T> T BigMod(T b,T p,T Mod) {T Ans=1; while(p){if(p&1)Ans=(Ans*b)%Mod;b=(b*b)%Mod;p>>=1;}return Ans;}
template <typename T> T ModInverse(T p,T Mod) {return BigMod(p,Mod-2,Mod);}
//template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//template <typename T> using ordered_set2 = tree<pair <T, string>, null_type, less<pair <T, string>>, rb_tree_tag, tree_order_statistics_node_update>;
//ll histogram(ll arr[],ll n){ll top,i=0,sum=0,max_area=0;stack<ll>st;st.push(0);while(i<n){if(st.empty()||arr[st.top()]<=arr[i]) st.push(i++);else{top=st.top();st.pop();if(st.empty())sum=arr[top]*i;else sum=arr[top]*(i-st.top()-1);max_area=max(sum,max_area);}}while(!st.empty()){top=st.top();st.pop();if(st.empty()) sum=arr[top]*i;else sum=arr[top]*(i-st.top()-1);max_area=max(sum,max_area);}return max_area;}
//ll ext_gcd(ll a,ll b,ll *x,ll *y){if(a==0){*x=0;*y=1;return b;}ll x1,y1;ll gcd = ext_gcd(b%a,a,&x1,&y1);*x=y1-(b/a)*x1;*y=x1;return gcd;}
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); deb(_it, args); }
void deb(istream_iterator<string> it) {}
template<typename T, typename... Args> void deb(istream_iterator<string> it, T a, Args... args) {cerr << *it << " = " << a << endl;deb(++it, args...);}

const int fx[]      = {+0,+0,+1,-1,-1,+1,-1,+1};
const int fy[]      = {-1,+1,+0,+0,+1,+1,-1,-1};
const int mx        = 3e5+10;
const int inf       = 0x3f3f3f3f;
const int mod       = 1e9+7;
const ld pi         = 2*acos(0.0);
const ld E          = 2.71828182845904523536;
const ld eps        = 1e-9;
//ll fact[mx];
//void factorial(){fact[0]=1;loop1(i,mx-1) fact[i]=(fact[i-1]*i)%mod;}
//ll nCr(ll n,ll r){return (fact[n]*ModInverse(((ll)fact[n-r]*(ll)fact[r])%mod,(ll)mod))%mod;}
//bool mark[mx];vector<ll>prime;
//void sieve(){memset(mark,true,sizeof(mark));for(ll i=3;i<=(ll)sqrt(mx);i+=2){if(mark[i]){for(ll j=i*i;j<=mx;j+=2*i) mark[j]=false;}}prime.push_back(2);for(ll i=3;i<=mx;i+=2) if(mark[i]) prime.push_back(i);}
///code start here

PQ<ll>ami,tumi;
ll n,ind[mx];
int main(){
//    freopen("Input.txt","r",stdin); freopen("Output.txt","w",stdout);

    cin>>n;
    loop1(i,n){
        ll w;
        cin>>w;
        ind[w]=i;
        ami.push(-w);
    }
    string s;
    cin>>s;
    vll ans;
    for(auto it : s){
        if(it=='0'){
            ans.pb(ind[-ami.top()]);
            tumi.push(-ami.top());
            ami.pop();
        }
        else{
            ans.pb(ind[tumi.top()]);
            tumi.pop();
        }
    }
    for(auto it:ans){
        cout<<it<<" ";
    }
    cout<<nl;
    return 0;
}
/*

*/


//why are you stalking me?
#include <bits/stdc++.h>

using namespace std;

#define fillchar(a, s) memset((a), (s), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef pair<int, int> pii;
const int MAXS = 11111111;
const int MAXN = 755;

struct node {
	node *ch[2], *f;
	node *out;	//apparently he does this? idk. will mark every line with this as "SPECIAL"
	vector<int> has;
	bool done;	//may reach node multiple times, but this checks if all the stuff here was matched!
} nodes[MAXS], *null = nodes, *cnode = nodes + 1;

node *getnode() {
	node *res = cnode++;
	res -> ch[0] = res -> ch[1] = res -> f = null;
	res -> done = false;
	return res;
}

int N;
vector<bool> str[MAXN];
char buf[MAXS];
node *root;

node *move (node *n, bool c) {
	while (n -> ch[c] == null) {
		n = n -> f;
	}
	return n -> ch[c];
}

void makelinks() {
	queue<node*> que;
	root -> out = root;	//SPECIAL
	root -> done = true;
	for (int i = 0; i < 2; i++) {
		if (root -> ch[i] == null) {
			root -> ch[i] = root;
		} else {
			root -> ch[i] -> f = root;
			root -> ch[i] -> out = root;	//SPECIAL
			que.push(root -> ch[i]);
		}
	}

	while (!que.empty()) {
		node *cur = que.front();
		que.pop();
		for (int i = 0; i < 2; i++) {
			node *c = cur -> ch[i];
			if (c == null) {
				continue;
			}
			c -> f = move(cur -> f, i);
			c -> out = (c -> f -> has.empty() ? c -> f -> out : c -> f);	//SPECIAL
			que.push(c);
		}
	}
}

void addword (int which) {
	node *cur = root;
	for (bool b : str[which]) {
		node* &c = cur -> ch[b];
		if (c == null) {
			c = getnode();
		}
		cur = c;
	}
	cur -> has.push_back(which);
}

void make_ac() {
	for (int i = 0; i < N; i++) {
		addword(i);
	}
	makelinks();
}

vector<int> adj[MAXN];

void match_str (int ind, vector<bool> &v) {
	vector<node*> reset;
	node *cur = root;
	for (bool b : v) {
		cur = move(cur, b);
		node *cur2 = cur;
		while (!cur2 -> done) {
			reset.push_back(cur2);
			for (int i : cur2 -> has) {
				if (i != ind) {
					adj[i].push_back(ind);
				}
			}
			cur2 -> done = true;
			// cur2 = cur2 -> f;	//SPECIAL, commented out...
			cur2 = cur2 -> out;
		}
	}

	for (node *n : reset) {
		n -> done = false;
	}
}

/*
Read this: http://aleph.math.louisville.edu/teaching/2009FA-681/notes-091119.pdf

Proposition 2. The set of maximal elements of a finite poset S is a maximal antichain; likewise,
the set of minimal elements of a finite poset S is a maximal antichain.
*/

int rev[MAXN];
bool vis[MAXN];

bool dfs (int x) {
	if (vis[x]) {
		return false;
	}
	vis[x] = true;
	for (int v : adj[x]) {
		if (rev[v] == -1) {
			rev[v] = x;
			return true;
		} else {
			if (dfs(rev[v])) {
				rev[v] = x;
				return true;
			}
		}
	}
	return false;
}

int match() {
	int res = 0;
	fillchar(rev, -1);
	for (int i = 0; i < N; i++) {
		if (dfs(i)) {
			res++;
			fillchar(vis, 0);
		}
	}
	return res;
}

bool ans[MAXN];
bool lmatched[MAXN];

int main() {
	if (fopen("input.txt", "r")) {
		freopen("input.txt", "r", stdin);
	}
	scanf("%d", &N);

	for (int i = 0; i < N; i++) {
		scanf("%s", buf);
		for (int j = 0; buf[j]; j++) {
			str[i].push_back(buf[j] == 'b');
		}
	}

	root = getnode();
	make_ac();

	for (int i = 0; i < N; i++) {
		match_str(i, str[i]);
	}

	//NEXT STEP: FIND ANTICHAIN
	//sub1: find matching size
	printf("%d\n", N - match());

	//sub2: from matching, find covering. thank you section 3 http://theory.stanford.edu/~trevisan/cs261/lecture14.pdf
	for (int i = 0; i < N; i++) {
		if (rev[i] != -1) {
			lmatched[rev[i]] = true;
		}
	}

	//left side, unmatched
	for (int i = 0; i < N; i++) {
		if (!lmatched[i]) {
			ans[i] = true;
		}
	}

	//right side, matched
	for (int i = 0; i < N; i++) {
		if (rev[i] != -1) {
			ans[i] = true;
		}
	}

	//(right side, unmatched), has neighbor in (left side, matched)
	for (int i = 0; i < N; i++) {
		if (lmatched[i]) {
			for (int j : adj[i]) {
				if (rev[j] == -1) {
					ans[j] = true;
				}
			}
		}
	}

	//print ans
	for (int i = 0; i < N; i++) {
		if (!ans[i]) {
			printf("%d ", i + 1);
		}
	}

	// printf("\nTIME: %f s.\n", float(clock()) / CLOCKS_PER_SEC);	//screw optimizations...
	//QUESTIONS:
	//1. what is the "out" thing doing?
	//2. How did you think of finding antichain? maybe I abused the stuff too much...
}


//why are you stalking me?
#include <bits/stdc++.h>

using namespace std;

#define fillchar(a, s) memset((a), (s), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef pair<int, int> pii;
const int MAXS = 11111111;
const int MAXN = 755;

struct node {
	node *ch[2], *f;
	node *out;	//apparently he does this? idk. will mark every line with this as "SPECIAL"
	vector<int> has;
	bool done;	//may reach node multiple times, but this checks if all the stuff here was matched!
} nodes[MAXS], *null = nodes, *cnode = nodes + 1;

node *getnode() {
	node *res = cnode++;
	res -> ch[0] = res -> ch[1] = res -> f = null;
	res -> done = false;
	return res;
}

int N;
vector<bool> str[MAXN];
char buf[MAXS];
node *root;

node *move (node *n, bool c) {
	while (n -> ch[c] == null) {
		n = n -> f;
	}
	return n -> ch[c];
}

void makelinks() {
	queue<node*> que;
	root -> out = root;	//SPECIAL
	root -> done = true;
	for (int i = 0; i < 2; i++) {
		if (root -> ch[i] == null) {
			root -> ch[i] = root;
		} else {
			root -> ch[i] -> f = root;
			root -> ch[i] -> out = root;	//SPECIAL
			que.push(root -> ch[i]);
		}
	}

	while (!que.empty()) {
		node *cur = que.front();
		que.pop();
		for (int i = 0; i < 2; i++) {
			node *c = cur -> ch[i];
			if (c == null) {
				continue;
			}
			c -> f = move(cur -> f, i);
			c -> out = (c -> f -> has.empty() ? c -> f -> out : c -> f);	//SPECIAL
			que.push(c);
		}
	}
}

void addword (int which) {
	node *cur = root;
	for (bool b : str[which]) {
		node* &c = cur -> ch[b];
		if (c == null) {
			c = getnode();
		}
		cur = c;
	}
	cur -> has.push_back(which);
}

void make_ac() {
	for (int i = 0; i < N; i++) {
		addword(i);
	}
	makelinks();
}

vector<int> adj[2][MAXN];

void match_str (int ind, vector<bool> &v) {
	vector<node*> reset;
	node *cur = root;
	for (bool b : v) {
		cur = move(cur, b);
		node *cur2 = cur;
		while (!cur2 -> done) {
			reset.push_back(cur2);
			for (int i : cur2 -> has) {
				if (i != ind) {
					adj[0][i].push_back(ind);
					adj[1][ind].push_back(i);
				}
			}
			cur2 -> done = true;
			// cur2 = cur2 -> f;	//SPECIAL, commented out...
			cur2 = cur2 -> out;
		}
	}

	for (node *n : reset) {
		n -> done = false;
	}
}

/*
Read this: http://aleph.math.louisville.edu/teaching/2009FA-681/notes-091119.pdf

Proposition 2. The set of maximal elements of a finite poset S is a maximal antichain; likewise,
the set of minimal elements of a finite poset S is a maximal antichain.
*/

int match[2][MAXN];
bool vis[MAXN];

bool dfs (int x) {
	if (vis[x]) {
		return false;
	}
	vis[x] = true;
	for (int v : adj[0][x]) {
		if (match[1][v] == -1) {
			match[1][v] = x;
			match[0][x] = v;
			return true;
		} else {
			if (dfs(match[1][v])) {
				match[1][v] = x;
				match[0][x] = v;
				return true;
			}
		}
	}
	return false;
}

int max_match() {
	int res = 0;
	fillchar(match, -1);
	for (int i = 0; i < N; i++) {
		if (dfs(i)) {
			res++;
			fillchar(vis, 0);
		}
	}
	return res;
}

bool ans[MAXN];
int mright[MAXN];	//match, going from left to right
bool reach[2][MAXN][2];	//reach[side][id of vertex][previous color: 1 = matched, 0 = unmatched]

void dfsreach (int side, int vid, int pcolor) {
	if (reach[side][vid][pcolor]) {
		return;
	}

	reach[side][vid][pcolor] = true;
	//select the stuff that hasn't been reached
	for (int w : adj[side][vid]) {
		if (pcolor != (w == match[side][vid])) {
			dfsreach(!side, w, !pcolor);
		}
	}
}

int main() {
	if (fopen("input.txt", "r")) {
		freopen("input.txt", "r", stdin);
	}
	scanf("%d", &N);

	for (int i = 0; i < N; i++) {
		scanf("%s", buf);
		for (int j = 0; buf[j]; j++) {
			str[i].push_back(buf[j] == 'b');
		}
	}

	root = getnode();
	make_ac();

	for (int i = 0; i < N; i++) {
		match_str(i, str[i]);
	}
	//NEXT STEP: FIND ANTICHAIN
	//sub1: find matching size
	printf("%d\n", N - max_match());

	//sub2: from matching, find covering.
	//wikipedia says it all...

	//calculate "U" as described in wiki
	for (int i = 0; i < N; i++) {
		if (match[0][i] == -1) {
			dfsreach(0, i, 0);
			dfsreach(0, i, 1);
		}
	}

	for (int i = 0; i < N; i++) {
		//left side. now check if it is not reachable
		if (!reach[0][i][0] && !reach[0][i][1]) {
			ans[i] = true;
		}
	}

	for (int i = 0; i < N; i++) {
		//right side. now check if it is reachable
		if (reach[1][i][0] || reach[1][i][1]) {
			ans[i] = true;
		}
	}

	//print ans
	for (int i = 0; i < N; i++) {
		if (!ans[i]) {
			printf("%d ", i + 1);
		}
	}

	// printf("\nTIME: %f s.\n", float(clock()) / CLOCKS_PER_SEC);	//screw optimizations...
	//QUESTIONS:
	//1. what is the "out" thing doing?
	//2. How did you think of finding antichain? maybe I abused the stuff too much...
}


//why are you stalking me?
#include <bits/stdc++.h>

using namespace std;

#define fillchar(a, s) memset((a), (s), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef pair<int, int> pii;
const int MAXS = 11111111;
const int MAXN = 755;

struct node {
	node *ch[2], *f;
	vector<int> has;
	bool done;	//may reach node multiple times, but this checks if all the stuff here was matched!
} nodes[2 * MAXS], *null = nodes, *cnode = nodes + 1;

node *getnode() {
	return cnode++;
}

int init_nodes() {
	for (node &n : nodes) {
		n.ch[0] = n.ch[1] = n.f = null;
		n.done = false;
	}
	return 0;
}

const int INIT_NODES = init_nodes();

int N;
vector<bool> str[MAXN];
char buf[MAXS];
node *root;

node *move (node *n, bool c) {
	while (n -> ch[c] == null) {
		n = n -> f;
	}
	return n -> ch[c];
}

void makelinks() {
	queue<node*> que;
	root -> done = true;
	for (int i = 0; i < 2; i++) {
		if (root -> ch[i] == null) {
			root -> ch[i] = root;
		} else {
			root -> ch[i] -> f = root;
			que.push(root -> ch[i]);
		}
	}

	while (!que.empty()) {
		node *cur = que.front();
		que.pop();
		for (int i = 0; i < 2; i++) {
			node *c = cur -> ch[i];
			if (c == null) {
				continue;
			}
			c -> f = move(cur -> f, i);
			que.push(c);
		}
	}
}

void addword (int which) {
	node *cur = root;
	for (bool b : str[which]) {
		node* &c = cur -> ch[b];
		if (c == null) {
			c = getnode();
		}
		cur = c;
	}
	cur -> has.push_back(which);
}

void make_ac() {
	for (int i = 0; i < N; i++) {
		addword(i);
	}
	makelinks();
}

vector<int> adj[MAXN];

void match_str (int ind, vector<bool> &v) {
	vector<node*> reset;
	node *cur = root;
	for (bool b : v) {
		cur = move(cur, b);
		node *cur2 = cur;
		while (!cur2 -> done) {
			reset.push_back(cur2);
			for (int i : cur2 -> has) {
				if (i != ind) {
					adj[i].push_back(ind);
				}
			}
			cur2 -> done = true;
			cur2 = cur2 -> f;
		}
	}

	for (node *n : reset) {
		n -> done = false;
	}
}

int rev[MAXN];
bool vis[MAXN];

bool dfs (int x) {
	if (vis[x]) {
		return false;
	}
	vis[x] = true;
	for (int v : adj[x]) {
		if (rev[v] == -1) {
			rev[v] = x;
			return true;
		} else {
			if (dfs(rev[v])) {
				rev[v] = x;
				return true;
			}
		}
	}
	return false;
}

int match() {
	int res = 0;
	fillchar(rev, -1);
	for (int i = 0; i < N; i++) {
		if (dfs(i)) {
			res++;
			fillchar(vis, 0);
		}
	}
	return res;
}

struct union_find {
	int ufpar[MAXN];  //don't forget to init

	int find (int x) {
		return x == ufpar[x] ? x : ufpar[x] = find(ufpar[x]);
	}

	bool merge (int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) {
			return false;
		}
		ufpar[x] = y;
		return true;
	}
} uf;

int main() {
	if (fopen("input.txt", "r")) {
		freopen("input.txt", "r", stdin);
	}
	scanf("%d", &N);

	for (int i = 0; i < N; i++) {
		scanf("%s", buf);
		for (int j = 0; buf[j]; j++) {
			str[i].push_back(buf[j] == 'b');
		}
	}

	root = getnode();
	make_ac();

	for (int i = 0; i < N; i++) {
		match_str(i, str[i]);
	}

	//next step: max antichain...
	printf("%d\n", N - match());

	//come on man, it's too simple.
	iota(uf.ufpar, uf.ufpar + N, 0);

	for (int i = 0; i < N; i++) {
		if (rev[i] != -1) {
			uf.merge(i, rev[i]);
		}
	}

	for (int i = 0; i < N; i++) {
		if (i == uf.find(i)) {
			printf("%d ", i + 1);
		}
	}
}


#include<bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int MAXN=760;
const int MAXL=1E7+10;

char Pool[MAXL<<1],*s[MAXN],*cur=Pool;
int son[MAXL][2],fail[MAXL],rt,cnt,jmp[MAXL],ed[MAXL],n,a[MAXN];
bitset<MAXN> G[MAXN];
vector<int> Ans,tAns;

void insert(char *s,int id)
{
	int x=rt;
	for(int i=1;s[i];i++)
	{
		if(!son[x][s[i]-'a'])
			son[x][s[i]-'a']=++cnt;
		x=son[x][s[i]-'a'];
	}
	ed[x]=id;
}

void build()
{
	queue<int> q;
	for(int i=0;i<2;i++)
		if(son[rt][i])
			q.push(son[rt][i]);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		jmp[x]=ed[x]?ed[x]:jmp[fail[x]];
		for(int i=0;i<2;i++)
			if(son[x][i])
				fail[son[x][i]]=son[fail[x]][i],
				q.push(son[x][i]);
			else
				son[x][i]=son[fail[x]][i];
	}
}

void solve()
{
	tAns.clear();
	bitset<MAXN> tmp;
	for(int i=1;i<=n;i++)
		if(!tmp[a[i]])
		{
			tAns.push_back(a[i]);
			tmp|=G[a[i]];
		}
	if(tAns.size()>Ans.size())
		swap(Ans,tAns);
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		s[i]=cur;
		scanf("%s",s[i]+1);
		cur+=strlen(s[i]+1)+10;
		insert(s[i],i);
	}
	build();
	for(int i=1;i<=n;i++)
	{
		int x=rt,p;
		for(int j=1;s[i][j];j++)
		{
			x=son[x][s[i][j]-'a'],p=ed[x]==i?fail[x]:x;
			if(jmp[p])
				G[i][jmp[p]]=1;
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j])
				G[i]|=G[j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<i;j++)
			G[i][j]=G[i][j]|G[j][i],
			G[j][i]=G[i][j]|G[j][i];

	for(int i=1;i<=n;i++)
		a[i]=i;
	int T=500;
	while(T--)
	{
		random_shuffle(a+1,a+1+n);
		solve();
	}
	printf("%d\n",Ans.size());
	for(auto x:Ans)
		printf("%d ",x);
	puts("");
}


#include<bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int MAXN=760;
const int MAXL=1E7+10;

char Pool[MAXL<<1],*s[MAXN],*cur=Pool;
int son[MAXL][2],fail[MAXL],rt,cnt,jmp[MAXL],ed[MAXL],n,a[MAXN];
bitset<MAXN> G[MAXN];
vector<int> Ans,tAns;

void insert(char *s,int id)
{
	int x=rt;
	for(int i=1;s[i];i++)
	{
		if(!son[x][s[i]-'a'])
			son[x][s[i]-'a']=++cnt;
		x=son[x][s[i]-'a'];
	}
	ed[x]=id;
}

void build()
{
	queue<int> q;
	for(int i=0;i<2;i++)
		if(son[rt][i])
			q.push(son[rt][i]);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		jmp[x]=ed[x]?ed[x]:jmp[fail[x]];
		for(int i=0;i<2;i++)
			if(son[x][i])
				fail[son[x][i]]=son[fail[x]][i],
				q.push(son[x][i]);
			else
				son[x][i]=son[fail[x]][i];
	}
}

void solve()
{
	tAns.clear();
	bitset<MAXN> tmp;
	for(int i=1;i<=n;i++)
		if(!tmp[a[i]])
		{
			tAns.push_back(a[i]);
			tmp|=G[a[i]];
		}
	if(tAns.size()>Ans.size())
		swap(Ans,tAns);
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		s[i]=cur;
		scanf("%s",s[i]+1);
		cur+=strlen(s[i]+1)+10;
		insert(s[i],i);
	}
	build();
	for(int i=1;i<=n;i++)
	{
		int x=rt,p;
		for(int j=1;s[i][j];j++)
		{
			x=son[x][s[i][j]-'a'],p=ed[x]==i?fail[x]:x;
			if(jmp[p])
				G[i][jmp[p]]=1,G[jmp[p]][i]=1;
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j])
				G[i]|=G[j];
	for(int i=1;i<=n;i++)
		a[i]=i;
	int T=500;
	while(T--)
	{
		random_shuffle(a+1,a+1+n);
		solve();
	}
	printf("%d\n",Ans.size());
	for(auto x:Ans)
		printf("%d ",x);
	puts("");
}


#include<bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int MAXN=760;
const int MAXL=1E7+10;

bool vis[MAXN],tag[MAXN<<1];
char Pool[MAXL<<1],*s[MAXN],*cur=Pool;
int son[MAXL][2],fail[MAXL],rt,cnt,jmp[MAXL],ed[MAXL],n,mat[MAXN],matr[MAXN];
bitset<MAXN> G[MAXN];
vector<int> g[MAXN],g2[MAXN];

void insert(char *s,int id)
{
	int x=rt;
	for(int i=1;s[i];i++)
	{
		if(!son[x][s[i]-'a'])
			son[x][s[i]-'a']=++cnt;
		x=son[x][s[i]-'a'];
	}
	ed[x]=id;
}

void build()
{
	queue<int> q;
	for(int i=0;i<2;i++)
		if(son[rt][i])
			q.push(son[rt][i]);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		jmp[x]=ed[x]?ed[x]:jmp[fail[x]];
		for(int i=0;i<2;i++)
			if(son[x][i])
				fail[son[x][i]]=son[fail[x]][i],
				q.push(son[x][i]);
			else
				son[x][i]=son[fail[x]][i];
	}
}

bool match(int x)
{
	for(int v:g[x])
		if(!vis[v])
		{
			vis[v]=1;
			if(!mat[v]||match(mat[v]))
				return mat[v]=x,true;
		}
	return false;
}

void dfs(int x,int tp)//tp=0 left,tp=1 right
{
	if(tp==0)
	{
		tag[x]=1;
		if(!tag[matr[x]+n])
			dfs(matr[x],1);
	}
	else
	{
		tag[x+n]=1;
		for(int v:g2[x])
			if(v!=mat[x]&&!tag[v])
				dfs(v,0);
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		s[i]=cur;
		scanf("%s",s[i]+1);
		cur+=strlen(s[i]+1)+10;
		insert(s[i],i);
	}
	build();
	for(int i=1;i<=n;i++)
	{
		int x=rt,p;
		for(int j=1;s[i][j];j++)
		{
			x=son[x][s[i][j]-'a'],p=ed[x]==i?fail[x]:x;
			if(jmp[p])
				G[i][jmp[p]]=1;//,printf("%d -> %d\n",i,jmp[p]);
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j])
				G[i]|=G[j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j]&&i!=j)
				g[i].push_back(j),
				g2[j].push_back(i);
				//printf("%d -> %d\n",i,j);
	int fafa=0;
	for(int i=1;i<=n;i++)
	{
		memset(vis,0,sizeof vis);
		fafa+=match(i);
	}
	for(int i=1;i<=n;i++)
		if(mat[i])
			matr[mat[i]]=i;
	for(int i=1;i<=n;i++)
		if(!mat[i])
			dfs(i,1);
	int Ans=0;
	for(int i=1;i<=n;i++)
		if(!tag[i]&&tag[i+n])
			++Ans;
	printf("%d\n",Ans);
	for(int i=1;i<=n;i++)
		if(!tag[i]&&tag[i+n])
			printf("%d ",i);
	puts("");
}

//CZY AK IOI
//CZY TXDY


#include<bits/stdc++.h>
//CZY TXDY
typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int MAXN=760;
const int MAXL=1E7+10;

bool vis[MAXN],tag[MAXN<<1];
char Pool[MAXL<<1],*s[MAXN],*cur=Pool;
int son[MAXL][2],fail[MAXL],rt,cnt,jmp[MAXL],ed[MAXL],n,mat[MAXN],matr[MAXN];
bitset<MAXN> G[MAXN];
vector<int> g[MAXN],g2[MAXN];

void insert(char *s,int id)
{
	int x=rt;
	for(int i=1;s[i];i++)
	{
		if(!son[x][s[i]-'a'])
			son[x][s[i]-'a']=++cnt;
		x=son[x][s[i]-'a'];
	}
	ed[x]=id;
}

void build()
{
	queue<int> q;
	for(int i=0;i<2;i++)
		if(son[rt][i])
			q.push(son[rt][i]);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		jmp[x]=ed[x]?ed[x]:jmp[fail[x]];
		for(int i=0;i<2;i++)
			if(son[x][i])
				fail[son[x][i]]=son[fail[x]][i],
				q.push(son[x][i]);
			else
				son[x][i]=son[fail[x]][i];
	}
}

bool match(int x)
{
	for(int v:g[x])
		if(!vis[v])
		{
			vis[v]=1;
			if(!mat[v]||match(mat[v]))
				return mat[v]=x,true;
		}
	return false;
}

void dfs(int x,int tp)//tp=0 left,tp=1 right
{
	if(tp==0)
	{
		tag[x]=1;
		if(!tag[matr[x]+n])
			dfs(matr[x],1);
	}
	else
	{
		tag[x+n]=1;
		for(int v:g2[x])
			if(v!=mat[x]&&!tag[v])
				dfs(v,0);
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		s[i]=cur;
		scanf("%s",s[i]+1);
		cur+=strlen(s[i]+1)+10;
		insert(s[i],i);
	}
	build();
	for(int i=1;i<=n;i++)
	{
		int x=rt,p;
		for(int j=1;s[i][j];j++)
		{
			x=son[x][s[i][j]-'a'],p=ed[x]==i?fail[x]:x;
			if(jmp[p])
				G[i][jmp[p]]=1;//,printf("%d -> %d\n",i,jmp[p]);
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j])
				G[i]|=G[j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j]&&i!=j)
				g[i].push_back(j),
				g2[j].push_back(i);
				//printf("%d -> %d\n",i,j);
	int fafa=0;
	for(int i=1;i<=n;i++)
	{
		memset(vis,0,sizeof vis);
		fafa+=match(i);
	}
	for(int i=1;i<=n;i++)
		if(mat[i])
			matr[mat[i]]=i;
	for(int i=1;i<=n;i++)
		if(!mat[i])
			dfs(i,1);
	int Ans=0;
	for(int i=1;i<=n;i++)
		if(!tag[i]&&tag[i+n])
			++Ans;
	printf("%d\n",Ans);
	for(int i=1;i<=n;i++)
		if(!tag[i]&&tag[i+n])
			printf("%d ",i);
	puts("");
}

//CZY AK IOI
//CZY TXDY


#include<bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int MAXN=760;
const int MAXL=1E7+10;

bool vis[MAXN],tag[MAXN<<1];
char Pool[MAXL<<1],*s[MAXN],*cur=Pool;
int son[MAXL][2],fail[MAXL],rt,cnt,jmp[MAXL],ed[MAXL],n,mat[MAXN],matr[MAXN];
bitset<MAXN> G[MAXN];
vector<int> g[MAXN],g2[MAXN];

void insert(char *s,int id)
{
	int x=rt;
	for(int i=1;s[i];i++)
	{
		if(!son[x][s[i]-'a'])
			son[x][s[i]-'a']=++cnt;
		x=son[x][s[i]-'a'];
	}
	ed[x]=id;
}

void build()
{
	queue<int> q;
	for(int i=0;i<2;i++)
		if(son[rt][i])
			q.push(son[rt][i]);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		jmp[x]=ed[x]?ed[x]:jmp[fail[x]];
		for(int i=0;i<2;i++)
			if(son[x][i])
				fail[son[x][i]]=son[fail[x]][i],
				q.push(son[x][i]);
			else
				son[x][i]=son[fail[x]][i];
	}
}

bool match(int x)
{
	for(int v:g[x])
		if(!vis[v])
		{
			vis[v]=1;
			if(!mat[v]||match(mat[v]))
				return mat[v]=x,true;
		}
	return false;
}

void dfs(int x,int tp)//tp=0 left,tp=1 right
{
	if(tp==0)
	{
		tag[x]=1;
		if(!tag[matr[x]+n])
			dfs(matr[x],1);
	}
	else
	{
		tag[x+n]=1;
		for(int v:g2[x])
			if(v!=mat[x]&&!tag[v])
				dfs(v,0);
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		s[i]=cur;
		scanf("%s",s[i]+1);
		cur+=strlen(s[i]+1)+10;
		insert(s[i],i);
	}
	build();
	for(int i=1;i<=n;i++)
	{
		int x=rt,p;
		for(int j=1;s[i][j];j++)
		{
			x=son[x][s[i][j]-'a'],p=ed[x]==i?fail[x]:x;
			if(jmp[p])
				G[i][jmp[p]]=1;//,printf("%d -> %d\n",i,jmp[p]);
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j])
				G[i]|=G[j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j]&&i!=j)
				g[i].push_back(j),
				g2[j].push_back(i);
				//printf("%d -> %d\n",i,j);
	int fafa=0;
	for(int i=1;i<=n;i++)
	{
		memset(vis,0,sizeof vis);
		fafa+=match(i);
	}
	for(int i=1;i<=n;i++)
		if(mat[i])
			matr[mat[i]]=i;
	for(int i=1;i<=n;i++)
		if(!mat[i])
			dfs(i,1);
	int Ans=0;
	for(int i=1;i<=n;i++)
		if(!tag[i]&&tag[i+n])
			++Ans;
	printf("%d\n",Ans);
	for(int i=1;i<=n;i++)
		if(!tag[i]&&tag[i+n])
			printf("%d ",i);
	puts("");
}


#include<bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int MAXN=760;
const int MAXL=1E7+10;

char Pool[MAXL<<1],*s[MAXN],*cur=Pool;
int son[MAXL][2],fail[MAXL],rt,cnt,jmp[MAXL],ed[MAXL],n,a[MAXN];
bitset<MAXN> G[MAXN];
vector<int> Ans,tAns;

void insert(char *s,int id)
{
	int x=rt;
	for(int i=1;s[i];i++)
	{
		if(!son[x][s[i]-'a'])
			son[x][s[i]-'a']=++cnt;
		x=son[x][s[i]-'a'];
	}
	ed[x]=id;
}

void build()
{
	queue<int> q;
	for(int i=0;i<2;i++)
		if(son[rt][i])
			q.push(son[rt][i]);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		jmp[x]=ed[x]?ed[x]:jmp[fail[x]];
		for(int i=0;i<2;i++)
			if(son[x][i])
				fail[son[x][i]]=son[fail[x]][i],
				q.push(son[x][i]);
			else
				son[x][i]=son[fail[x]][i];
	}
}

void solve()
{
	tAns.clear();
	bitset<MAXN> tmp;
	for(int i=1;i<=n;i++)
		if(!tmp[a[i]])
		{
			tAns.push_back(a[i]);
			tmp|=G[a[i]];
		}
	if(tAns.size()>Ans.size())
		swap(Ans,tAns);
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		s[i]=cur;
		scanf("%s",s[i]+1);
		cur+=strlen(s[i]+1)+10;
		insert(s[i],i);
	}
	build();
	for(int i=1;i<=n;i++)
	{
		int x=rt;
		for(int j=1;s[i][j];j++)
		{
			x=son[x][s[i][j]-'a'];
			if(jmp[x])
				G[i][jmp[x]]=1,G[jmp[x]][i]=1;
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(G[i][j])
				G[i]|=G[j];
	for(int i=1;i<=n;i++)
		a[i]=i;
	int T=500;
	while(T--)
	{
		random_shuffle(a+1,a+1+n);
		solve();
	}
	printf("%d\n",Ans.size());
	for(auto x:Ans)
		printf("%d ",x);
	puts("");
}


#include <bits/stdc++.h>

struct Node {
    Node *go[2],*fail,*par;
    int id;

    Node(Node *_par = NULL) {
        go[0] = go[1] = fail = NULL;
        id = -1;
        par = _par;
    }
} pool[10000000 + 1],*alloc,*root;

char str[10000000 + 1];

const int N = 750 + 5;
bool graph[N][N];
int n;

void insert(char *s,int id) {
    Node *p = root;
    for ( ; *s; ++ s) {
        int c = *s - 'a';
        if (p->go[c] == NULL)
            p->go[c] = new(alloc ++) Node(p);
        p = p->go[c];
    }
    if (p->id != -1) {
        graph[p->id][id] = graph[id][p->id] = true;
    }
    p->id = id;
}

int vis[N],stmp;
int left[N];
bool mark[N][2];
bool matched[N];

void dfs(int u,int side) {
    if (mark[u][side])
        return ;
    mark[u][side] = true;
    if (side == 0) {
        for (int v = 0; v < n; ++ v) {
            if (graph[u][v])
                dfs(v,side ^ 1);
        }
    } else if (left[u] != -1) {
        dfs(left[u],side ^ 1);
    }
}

bool expath(int u) {
    for (int v = 0; v < n; ++ v) {
        if (graph[u][v] && vis[v] != stmp) {
            vis[v] = stmp;
            if (left[v] == -1 || expath(left[v])) {
                left[v] = u;
                return true;
            }
        }
    }
    return false;
}

int main() {
    alloc = pool;
    root = new(alloc ++) Node;
    scanf("%d",&n);
    for (int i = 0; i < n; ++ i) {
        scanf("%s",str);
        insert(str,i);
    }
    std::queue<Node*> que;
    que.push(root);
    while (!que.empty()) {
        Node *u = que.front(); que.pop();
        if (u->id != -1) {
            for (Node *v = u->par; v; v = v->par) {
                if (v->id != -1)
                    graph[u->id][v->id] = true;
            }
        }
        if (u->fail && u->fail->id != -1) {
            if (u->id != -1)
                graph[u->id][u->fail->id] = true;
            else
                u->id = u->fail->id;
        }
        for (int c = 0; c < 2; ++ c) {
            if (u->go[c] == NULL) continue;
            Node *f = u->fail;
            while (f && f->go[c] == NULL) f = f->fail;
            u->go[c]->fail = f == NULL ? root : f->go[c];
            que.push(u->go[c]);
        }
    }
    for (int k = 0; k < n; ++ k) {
        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                graph[i][j] |= graph[i][k] & graph[k][j];
            }
        }
    }
    for (int i = 0; i < n; ++ i)
        graph[i][i] = false;
    memset(left,-1,sizeof(left));
    for (int i = 0; i < n; ++ i) {
        stmp ++;
        matched[i] = expath(i);
    }
    for (int i = 0; i < n; ++ i) {
        if (!matched[i])
            dfs(i,0);
    }
    std::vector<int> answer;
    for (int i = 0; i < n; ++ i) {
        if (!(matched[i] && !mark[i][0] || left[i] != -1 && mark[i][1]))
            answer.push_back(i);
    }
    printf("%d\n",answer.size());
    for (int i = 0; i < answer.size(); ++ i) {
        if (i) putchar(' ');
        printf("%d",answer[i] + 1);
    }
    puts("");
}