This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int n, a[5005], undo[5005], active[5005];
set<pii> st;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    active[0] = 1;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        active[i] = 1;
        if(a[i] < 0)
        {
            int cur = i;
            for (int j = i - 1; j >= 1; j--)
                if(active[j] && min(0, a[j]) > a[cur])
                {
                    // cerr << cur << " undo " << j << '\n';
                    undo[cur] = j;
                    active[j] ^= 1;
                    cur = j;
                    if(!undo[cur])
                        break;
                    // cerr << cur << " redo " << undo[cur] << '\n';
                    active[undo[cur]] ^= 1;
                    cur = undo[cur];
                }
        }
        for (int j = i; j >= 0; j--)
            if(active[j] && a[j] >= 0)
            {
                cout << a[j] << '\n';
                break;
            }
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |