Drain the Waters

0

0 votes
Easy
Problem

The Dothraki Khalasar led by Khal Drogo have departed Pentos and are heading east. On their way, they found a city that recently suffered fom severe flooding. The flodding was so severe that it filled the city with water to it's maximum capacity. Khal Drogo decides to help the people of the city by completely draining the waters. He wants to get an estimate of the amount of water filled in the city.

The city is represented in the form of a one dimensional array of buildings where each element of the array is equal to the height of the building at that point. The width of each building is same.

Find the amount of water trapped in the city.

INPUT

The first line contains a single integer n (0 ≤ n ≤ 106)  — the number of buildings.

The second line contains n space separated integers a1, a2, .... ,an (0 ≤ ai ≤ 106) — the height of each building.

OUTPUT

Print a single integer  — the number of units of water that's filled in the city.

Sample Input
10
1 4 2 3 6 7 8 9 5 17
6
Sample Output
6
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Editor Image

?