#STK0000075. Phần tử lớn hơn tiếp theo (Next Greater Element)
Phần tử lớn hơn tiếp theo (Next Greater Element)
Next Greater Element
Source: Baekjoon Online Judge
Version: Phuoc Hung OJ Extended
Problem Statement
Given an array . For each position , its next greater element is the first element to the right whose value is greater than . If it does not exist, the answer at position is .
Input
The first line contains an integer . The second line contains integers .
Output
Print values. The -th value is the next greater element of , or if it does not exist.
Subtasks
- Subtask 1 (30 points): ; all other conditions are unchanged.
- Subtask 2 (70 points): , .
Examples
Input
4
3 5 2 7
Output
5 7 7 -1
Explanation
The array is .
- For , the first greater value to the right is , so the first answer is .
- For , the value is not greater than , while , so the second answer is .
- For , the immediately following value is , so the third answer is .
- For , there is no element to its right, so the answer is .
Therefore the output is 5 7 7 -1.