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

mt19937 rng;

struct node
{
    int lson, rson;
    int x, y;

    node(): lson(-1), rson(-1), x(-1), y(-1) {}

    node(const int x_): lson(-1), rson(-1),
                        x(x_), y(rng()) {}
};

struct treap_manager
{
    vector<node> v;

    int merge(int l, int r)
    {
        if (l == -1)
            return r;
        if (r == -1)
            return l;

        if (v[l].y > v[r].y)
        {
            v[l].rson = merge(v[l].rson, r);
            return l;
        }
        else
        {
            v[r].lson = merge(l, v[r].lson);
            return r;
        }
    }

    pair<int, int> split(int t, int x)
    {
        if (t == -1)
            return {-1, -1};

        if (v[t].x < x)
        {
            int l, r;
            tie(l, r) = split(v[t].rson, x);
            v[t].rson = l;
            return {t, r};
        }
        else
        {
            int l, r;
            tie(l, r) = split(v[t].lson, x);
            v[t].lson = r;
            return {l, t};
        }
    }

    bool find(int &t, const int x)
    {
        int l, m, r;
        tie(l, m) = split(t, x);
        tie(m, r) = split(m, x + 1);

        const bool ok = (m != -1);
        t = merge(l, merge(m, r));
        return ok;
    }

    int get_new_node(const int x)
    {
        v.push_back(node(x));
        return int(v.size()) - 1;
    }

    void insert(int &t, const int x)
    {
        int l, r;
        tie(l, r) = split(t, x);
        const int m = get_new_node(x);
        t = merge(l, merge(m, r));
    }

    void erase(int &t, const int x)
    {
        int l, m, r;
        tie(l, m) = split(t, x);
        tie(m, r) = split(m, x + 1);
        t = merge(l, r);
    }

    int find_min(int t)
    {
        assert(t != -1);
        while (v[t].lson != -1)
            t = v[t].lson;
        return v[t].x;
    }
};

void solve(istream &cin = std::cin,
           ostream &cout = std::cout)
{
    treap_manager manager;
    int root = -1;

    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int a;
        cin >> a;

        if (a > 0)
        {
            a *= n;
            a += i;

            manager.insert(root, a);
        }
        else
        {
            const int to_erase = manager.find_min(root);
            manager.erase(root, to_erase);
            cout << to_erase / n << endl;
        }
    }
}

int main() {
#ifdef LOCAL
    ifstream fin("input.txt");
    ofstream fout("output.txt");
    solve(fin, fout);
#else
    solve(cin, cout);
#endif
}
