#C4559. Tallest Student to the Right
Tallest Student to the Right
Tallest Student to the Right
You are given one or more datasets representing the heights of students standing in a row. For each dataset, the first integer \(n\) indicates the number of students. It is followed by \(n\) integers representing the heights of the students from left to right.
Your task is to, for each student at position \(i\) (0-based index), determine the height of the tallest student standing to the right of \(i\). Formally, for every valid index \(i\), compute \[ result[i] = \begin{cases} \max\{height[j] : j>i\} & \text{if such } j \text{ exists},\\ -1 & \text{otherwise}. \end{cases} \]
The input terminates with a dataset starting with 0 (which should not be processed).
inputFormat
The input consists of several datasets. Each dataset begins with an integer \(n\) \((n \ge 0)\) that indicates the number of students. This is followed by \(n\) integers representing the heights of the students.
The input is terminated by a line containing a single zero.
All input should be read from standard input (stdin).
outputFormat
For each dataset, output a single line containing \(n\) space-separated integers. Each integer corresponds to the height of the tallest student to the right of the current student. If there is no student to the right, output -1 for that position.
All output should be written to standard output (stdout).
## sample5
5 3 8 3 2
4
2 7 4 3
1
10
0
8 8 3 2 -1
7 4 3 -1
-1
</p>