    class MaxStack {
        Stack<Integer> stack;
        Stack<Integer> max;

        /**
         * initialize your data structure here.
         */
        public MaxStack() {
            stack = new Stack<>();
            max = new Stack<>();
        }

        public void push(int x) {
            stack.push(x);
            if(max.isEmpty()||x>max.peek()){
                max.push(x);
            }else{
                max.push(max.peek());
            }
        }

        public int pop() {
            max.pop();
            return stack.pop();
        }

        public int top() {
            return stack.peek();
        }

        public int peekMax() {
            return max.peek();
        }

        public int popMax() {
            int num = max.peek();
            Stack<Integer> temp = new Stack<>();
            while (stack.peek()!=num){
                temp.push(pop());
            }
            pop();
            while (!temp.isEmpty()){
                push(temp.pop());
            }
            return num;
        }
    }

/**
 * Your MaxStack object will be instantiated and called as such:
 * MaxStack obj = new MaxStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.peekMax();
 * int param_5 = obj.popMax();
 */

//runtime:23 ms
//memory:41 MB
