static void Main() { var n = int.Parse(Console.ReadLine()); var stack = new Stack <int>(); while (n-- > 0) { var query = Console.ReadLine().Split(); var type = query[0]; if (type == "1") { var x = int.Parse(query[1]); stack.Push(x); } else if (stack.Count > 0) { if (type == "2") { stack.Pop(); } else if (type == "3") { Console.WriteLine(stack.Max()); } else if (type == "4") { Console.WriteLine(stack.Min()); } } } Console.WriteLine(string.Join(", ", stack)); }
static void Main(string[] args) { long maxNum = long.MinValue; int numberOfCommands = int.Parse(Console.ReadLine()); Stack <long> stack = new Stack <long>(); for (int i = 0; i < numberOfCommands; i++) { var commandDate = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray(); var command = (commandDate[0]); if (command == 1) { stack.Push(commandDate[1]); if (commandDate[1] > maxNum) { maxNum = commandDate[1]; } } else if (command == 2) { if (stack.Peek() == maxNum) { stack.Pop(); if (stack.Count > 0) { maxNum = stack.Max(); } } else { stack.Pop(); } } if (stack.Count == 0) { maxNum = long.MinValue; } else if (command == 3) { Console.WriteLine(maxNum); } } }