const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
const stack = [];
const answer = [];
for (let i = 1; i <= input[0]; i++) {
if (input[i].includes('push')) {
stack.push(input[i].split(' ')[1]);
} else if (input[i] === 'pop') {
if (stack.length === 0) {
answer.push(-1);
} else {
answer.push(stack.pop());
}
} else if (input[i] === 'size') {
answer.push(stack.length);
} else if (input[i] === 'empty') {
if (stack.length === 0) {
answer.push(1);
} else {
answer.push(0);
}
} else if (input[i] === 'top') {
if (stack.length === 0) {
answer.push(-1);
} else {
answer.push(stack[stack.length - 1]);
}
}
}
console.log(answer.join('\n'));
const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
const K = +input.shift();
const stack = [];
let answer = 0;
for (let i = 0; i < K; i++) {
if (input[i] !== '0') {
stack.push(input[i]);
} else {
stack.pop();
}
}
for (let i = 0; i < stack.length; i++) {
answer += +stack[i];
}
console.log(answer);
const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
const T = +input.shift();
const answer = new Array(T).fill(null);
let stack = 0;
for (let i = 0; i < T; i++) {
stack = 0;
const str = input[i].split('');
for (let j = 0; j < str.length; j++) {
if (str[j] === '(') {
stack += 1;
} else if (str[j] === ')' && stack === 0) {
answer[i] = 'NO';
} else {
stack -= 1;
}
}
if (stack === 0 && answer[i] === null) {
answer[i] = 'YES';
} else {
answer[i] = 'NO';
}
}
console.log(answer.join('\n'));
const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
const filteredInput = input.filter(el => el !== '.');
const answer = new Array(filteredInput.length).fill(null);
let stack = [];
for (let i = 0; i < answer.length; i++) {
stack = [];
const str = filteredInput[i].split('');
str.pop();
for (let j = 0; j < str.length; j++) {
if (str[j] === '[') {
stack.push('[');
} else if (str[j] === '(') {
stack.push('(');
} else if (str[j] === ']') {
if (stack.length === 0) {
answer[i] = 'no';
break;
}
if (stack[stack.length - 1] === '[') {
stack.pop();
} else {
answer[i] = 'no';
break;
}
} else if (str[j] === ')') {
if (stack.length === 0) {
answer[i] = 'no';
break;
}
if (stack[stack.length - 1] === '(') {
stack.pop();
} else {
answer[i] = 'no';
break;
}
}
}
if (answer[i] === null) {
if (stack.length === 0) {
answer[i] = 'yes';
} else {
answer[i] = 'no';
}
}
}
console.log(answer.join('\n'));
const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n').map(Number);
const n = +input.shift();
const stack = [];
let answer = '';
let count = 1;
let i = 0;
while (true) {
if (i === n) break;
if (count <= input[i]) {
stack.push(count);
count++;
answer += '+\n';
} else {
if (stack[stack.length - 1] !== input[i]) {
answer = 'NO';
break;
}
stack.pop();
i++;
answer += '-\n';
}
}
console.log(answer.trim());