#K67582. Perform Operations on a Dynamic Array
Perform Operations on a Dynamic Array
Perform Operations on a Dynamic Array
You are given a series of operations to perform on an initially empty array. There are three types of operations:
- add x: Add the integer x to the array.
- remove x: Remove one occurrence of the integer x from the array if it exists.
- largest: Output the largest number in the array. If the array is empty, output -1.
Formally, if we denote the result of a largest
operation by \(R\), then
\[
R = \begin{cases}
\max\{a : a \text{ is in the array}\} & \text{if the array is non-empty},\\
-1 & \text{if the array is empty.}
\end{cases}
\]
Process the operations in order and print the result of every largest
operation.
inputFormat
The input is read from stdin
in the following format:
- The first line contains an integer n, the number of operations.
- The next n lines each contain a single operation, which is one of the following:
add x
(where x is an integer)remove x
(where x is an integer)largest
outputFormat
For every largest
operation, output the corresponding result to stdout
on a separate line.
10
add 5
add 3
largest
add 10
largest
remove 5
largest
remove 10
largest
remove 3
largest
5
10
10
3
-1
</p>