#C4096. Priority Messaging System

    ID: 47596 Type: Default 1000ms 256MiB

Priority Messaging System

Priority Messaging System

You are tasked with implementing a messaging system that processes two types of operations:

  • SEND: Send a message with an associated integer priority.
  • RECEIVE: Retrieve and remove the message with the highest priority.

If two messages share the same priority, the message that was sent earlier should be retrieved first. In case a RECEIVE operation is executed when there are no messages, output NO_MESSAGES.

The input begins with an integer n indicating the number of operations. The following n lines each contain an operation in one of the two forms:

  • SEND x message where x is an integer representing the priority and message is the content.
  • RECEIVE

Process each operation in the order given and for each RECEIVE command, output the retrieved message on a new line.

inputFormat

The input is read from standard input (stdin) and consists of:

  1. An integer n on the first line, representing the number of operations.
  2. n subsequent lines, each being either a SEND operation of the form SEND x message or a RECEIVE operation.

outputFormat

For each RECEIVE operation, output the result on a separate line to standard output (stdout). If there are no messages to receive, output NO_MESSAGES.

## sample
5
SEND 1 Hello
SEND 3 World
RECEIVE
SEND 2 !
RECEIVE
World

!

</p>