Garden
Time limit3sMemory limit512 MB
Given sorted tree heights, each day the leftmost tree with the smallest positive gap to its right grows by 1. For each K, find the tallest minus shortest height after K days.
- Level
Hard8 of 10
- Topics
- Greedy, Binary search, Array
- Solved
- No attempts yet
Problem
Hyeonwook's garden in front of his house has trees (). Each tree height is an integer from to , and the trees are planted so that their heights are non-decreasing. If is the height of the -th tree, then always holds.
Each day, a tree grows by if it meets both conditions:
- It is strictly shorter than the tree directly to its right ().
- Among such trees, the height difference with the tree directly to its right is the smallest.
If several trees qualify, only the leftmost one grows. In other words, at most one tree grows per day. For example, suppose the heights are . The trees with the smallest difference to their right neighbor are the 2nd and 3rd trees, both with difference . Of these, the leftmost is the 2nd tree, so its height grows by .
Starting today, Hyeonwook wants to know the difference between the tallest and the shortest tree after days (). Given queries (), write a program that answers each query.
Input
The first line contains and ().
The second line contains tree heights separated by spaces (). The input always satisfies .
Each of the next lines contains one query. Every is an integer with .
Output
For each query, print on its own line the difference between the heights of the tallest and the shortest tree after days.