Alien Invader Yooni
InterviewTime limit1sMemory limit1024 MB
Given building heights, each day every tallest building drops by 1; find the total number of lasers fired over D days.
- Level
Medium5 of 10
- Topics
- Sorting, Greedy, Simulation
- Solved
- No attempts yet
Problem
Yooni, an alien, invades Ulsan, the central city of the world, to conquer the Earth. There are buildings in a row in Ulsan, and the height of the -th building from the left is .
Yooni has a plan to destroy Ulsan. Every day, Yooni rides a UFO over the city and fires a laser at the tallest building. A building hit by the laser becomes 1 shorter. If several buildings are tied for the tallest height that day, Yooni fires a laser at every one of them. If all buildings have already reached height , Yooni stops firing lasers.
Yooni has followed the plan faithfully for days so far. Find the total number of lasers Yooni fired during those days.
Input
The first line contains the number of buildings and the number of days that Yooni carried out the plan, separated by a space. (, )
The second line contains integers , the heights of the buildings, separated by spaces. ()
Output
Print the total number of lasers Yooni fired during the days. (Note that the answer may exceed the 32-bit integer range.)