#C3484. Maximum Skyline Height with Parks
Maximum Skyline Height with Parks
Maximum Skyline Height with Parks
You are given a row of N buildings with various heights. Additionally, you are provided with the constant park height P. Between any two adjacent buildings, it is possible to insert a park. However, the effective height of a park inserted between building i and building i+1 is determined by the minimum of the two adjacent building heights, and then capped by the park height P (i.e. \( \min(P, \min(\text{height}_i,\,\text{height}_{i+1})) \)).
Your task is to calculate the maximum possible skyline height that can be achieved by either considering the building heights themselves or by utilizing the park option between any two buildings.
Input/Output Format: The input is given through standard input (stdin) and the output should be printed to standard output (stdout).
Note: If there is only one building, no park can be inserted and the answer is simply the height of that building.
inputFormat
The first line contains two space-separated integers N and P, where N represents the number of buildings and P is the park height limit. The second line contains N space-separated integers representing the heights of the buildings in order.
Constraints:
- 1 \( \leq N \leq 10^5 \)
- 0 \( \leq P \leq 10^9 \)
- Each building height is a positive integer.
outputFormat
Output a single integer which is the maximum skyline height that can be achieved.
The answer is the maximum value between the highest building and any park height computed as \( \min(P, \min(\text{height}_i,\,\text{height}_{i+1})) \) for all adjacent pairs \( i \) and \( i+1 \).
## sample5 3
3 1 4 1 5
5