Rainwater

Given stack heights across a 2D world, compute the total rainwater trapped between the blocks after heavy rain.

Medium5ArrayTwo pointersPrefix sumImplementationInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

Blocks are stacked in a two dimensional world. When it rains, rainwater collects between the blocks.

It rains more than enough. How much rainwater collects in total?

Input

The first line contains the height HH and the width WW of the two dimensional world. (1H,W5001 \le H, W \le 500)

The second line contains WW stack heights, given from the leftmost cell in order. Each height is an integer between 00 and HH.

Blocks are piled up from the floor, so no empty space appears inside a stack. You may assume that the floor of the two dimensional world is always closed.

Output

One cell of the two dimensional world holds 11 unit. Print the total amount of rainwater that collects.

If no rainwater collects at all, print 00.

Hint

Hint 1:

Hint 2:

Hint 3: