Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- struct El {
- int data;
- El * next;
- };
- int countt = 0;
- El * top = NULL;
- El * createEl(int x) {
- El * t = new El();
- t->data = x;
- t->next = NULL;
- return t;
- }
- void push(int x) {
- El * t = createEl(x);
- if (top == NULL) {
- top = t;
- return;
- }
- t->next = top;
- top = t;
- }
- void pop() {
- if (top == NULL) return;
- top = top->next;
- if (countt > 0) countt = countt - top->data;
- }
- int main() {
- int n, Q, x;
- cin >> n;
- for (int i = 0; i < n; i++) {
- cin >> Q;
- if (Q == 0) {
- pop();
- }
- if (Q == 1) {
- cin >> x;
- push(x);
- countt += x;
- }
- if (Q == 2) {
- cout << countt << "\n";
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment