← Home
For problem statement at 0-999/300-399/370-379/374/problemD.txt this is a correct solution, but verifier at 0-999/300-399/370-379/374/verifierD.go ends with All tests passed can you fix the verifier? ```go
package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

type Node struct {
	val        int
	prio       int
	size       int
	left, right *Node
}

func getSize(n *Node) int {
	if n == nil {
		return 0
	}
	return n.size
}

func updateSize(n *Node) {
	if n != nil {
		n.size = 1 + getSize(n.left) + getSize(n.right)
	}
}

func rotateRight(root *Node) *Node {
	newRoot := root.left
	root.left = newRoot.right
	newRoot.right = root
	updateSize(root)
	updateSize(newRoot)
	return newRoot
}

func rotateLeft(root *Node) *Node {
	newRoot := root.right
	root.right = newRoot.left
	newRoot.left = root
	updateSize(root)
	updateSize(newRoot)
	return newRoot
}

func insert(root *Node, val int, prio int) *Node {
	if root == nil {
		return &Node{val: val, prio: prio, size: 1}
	}
	if val < root.val {
		root.left = insert(root.left, val, prio)
		if root.left.prio > root.prio {
			root = rotateRight(root)
		}
	} else if val > root.val {
		root.right = insert(root.right, val, prio)
		if root.right.prio > root.prio {
			root = rotateLeft(root)
		}
	}
	updateSize(root)
	return root
}

func delete(root *Node, val int) *Node {
	if root == nil {
		return nil
	}
	if val < root.val {
		root.left = delete(root.left, val)
	} else if val > root.val {
		root.right = delete(root.right, val)
	} else {
		if root.left == nil && root.right == nil {
			return nil
		} else if root.left == nil {
			root = root.right
		} else if root.right == nil {
			root = root.left
		} else {
			if root.left.prio > root.right.prio {
				root = rotateRight(root)
				root.right = delete(root.right, val)
			} else {
				root = rotateLeft(root)
				root.left = delete(root.left, val)
			}
		}
	}
	updateSize(root)
	return root
}

func findKth(root *Node, k int) int {
	if root == nil {
		return -1
	}
	leftSize := getSize(root.left)
	if k <= leftSize {
		return findKth(root.left, k)
	} else if k == leftSize+1 {
		return root.val
	} else {
		return findKth(root.right, k-leftSize-1)
	}
}

func collectInorder(root *Node) []int {
	var res []int
	stack := []*Node{}
	cur := root
	for cur != nil || len(stack) > 0 {
		for cur != nil {
			stack = append(stack, cur)
			cur = cur.left
		}
		cur = stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		res = append(res, cur.val)
		cur = cur.right
	}
	return res
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 0, 10000000), 10000000)
	scanner.Split(bufio.ScanWords)
	scanInt := func() int {
		scanner.Scan()
		x, _ := strconv.Atoi(scanner.Text())
		return x
	}
	n := scanInt()
	m := scanInt()
	a := make([]int, m+1)
	for i := 1; i <= m; i++ {
		a[i] = scanInt()
	}
	val := make([]int, n+1)
	cur_id := 0
	var root *Node
	for i := 0; i < n; i++ {
		x := scanInt()
		if x != -1 {
			cur_id++
			val[cur_id] = x
			root = insert(root, cur_id, int(rand.Int31n(1<<30)))
		} else {
			L := getSize(root)
			if L == 0 {
				continue
			}
			lo := 1
			hi := m
			for lo <= hi {
				mid := (lo + hi) / 2
				if a[mid] <= L {
					lo = mid + 1
				} else {
					hi = mid - 1
				}
			}
			k := hi
			if k == 0 {
				continue
			}
			toRemove := make([]int, 0, k)
			for j := 1; j <= k; j++ {
				pos := a[j]
				id := findKth(root, pos)
				toRemove = append(toRemove, id)
			}
			for _, id := range toRemove {
				root = delete(root, id)
			}
		}
	}
	if getSize(root) == 0 {
		fmt.Println("Poor stack!")
	} else {
		ids := collectInorder(root)
		var buf bytes.Buffer
		for _, id := range ids {
			buf.WriteByte(byte('0') + byte(val[id]))
		}
		fmt.Println(buf.String())
	}
}
```