#include using namespace std; struct Node { int data; Node* next = nullptr; Node(int data, Node* next = nullptr) { this->data = data; this->next = next; } }; class Stack { public: Node* top = nullptr; void push(int number) { if (isEmpty()) { top = new Node(number); } else { Node* newTop = new Node(number, top); top = newTop; } } void pop() { if (!isEmpty()) { Node* oldTop = top; top = top->next; delete oldTop; } else { throw std::underflow_error("Stack is empty"); } } int peek() const { if (!isEmpty()) { return top->data; } else { throw std::underflow_error("Stack is empty"); } } bool isEmpty() const { return top == nullptr; } }; void PrintStack(Stack s) { if (s.isEmpty()) return; int x = s.peek(); s.pop(); PrintStack(s); cout << x << " "; s.push(x); } Stack s; int main() { int n; cin >> n; int num; if (n > 0) { cin >> num; s.push(num); } bool flag = 0; if (n > 1) { cin >> num; } for (int i = 2; i <= n; ) { while (s.peek() > 0) { if (s.peek() + num == 0) { if (!s.isEmpty()) { s.pop(); i++; if (i > n) { flag = 1; break; } cin >> num; continue; } } while (!s.isEmpty() && s.peek() + num < 0 && s.peek()>0) { if (!s.isEmpty()) s.pop(); } if (num > 0) { s.push(num); } if ((s.isEmpty() && num < 0) || (num < 0 && s.peek() < 0)) { s.push(num); } i++; if (i > n) { flag = 1; break; } cin >> num; } if (flag) break; while (s.peek() < 0) { s.push(num); i++; if (i > n) break; cin >> num; } } if (!s.isEmpty()) PrintStack(s); else cout << '\n'; }