#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n;
vector<int> nivel, valori, before;
vector<bool> poti_reintoarce;
vector<int> reintoarce;
nivel.push_back(0);
valori.push_back(0);
before.push_back(-1);
poti_reintoarce.push_back(true);
reintoarce.push_back(0);
int raspuns = 0;
for (int i = 0; i < n; ++i) {
cin >> x;
if (x > 0) {
nivel.push_back(0);
valori.push_back(x);
before.push_back(-1);
poti_reintoarce.push_back(true);
reintoarce.push_back(nivel.size() - 1);
}
else {
int level = -x;
for (int j = (int)reintoarce.size() - 1; j >= 0; j--) {
int idx = reintoarce[j];
if (!poti_reintoarce[idx]) continue;
int lvl = nivel[idx];
if (lvl <= level - 1) {
poti_reintoarce[idx] = false;
nivel.push_back(level);
valori.push_back(level);
before.push_back(idx);
poti_reintoarce.push_back(true);
reintoarce.push_back(nivel.size() - 1);
int p = idx;
while (nivel[p] > 0 && before[p] != -1) p = before[p];
while (nivel[p] > 0 && !poti_reintoarce[p]) p = before[p];
while (nivel[p] > 0 && p != -1) {
poti_reintoarce[p] = true;
p = before[p];
}
break;
}
}
}
for (int j = nivel.size() - 1; j >= 0; j--) {
if (nivel[j] == 0 && poti_reintoarce[j]) {
cout << valori[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... |