Pour the juice

0

0 votes
Hard
Problem

You love drinking orange juice. But all you have is an infinite supply of the juice coming out of a pipe, and a container in the form of a rectangle of width W and length N. The rectangle is divided into N columns of length = 1 and has some barriers (columns of some height Ai) at certain places of width same as the  width of rectangle and length = 1. Since you cannot drink the juice directly from the pipe, you first pour it into the container. You have to find out the volume of juice that the container can hold.

For eg. given a container of width = 5 and the container columns are described as [3, 0, 0, 2, 0, 4] meaning that there is a barrier of height 3 at pos1, height 0 at pos2 and pos3, height 2 at pos4, height 0 at pos5 and height 4 at pos6 each of length 1. So the container can hold = (3 + 3 + 1 + 3) * (1) * (5) = 50 volume of juice.

Reference to sample input 1
Input = [3, 0, 0, 2, 0 ,4]

INPUT

  • The first line contains two space separated integers N and W, denoting the length and width of the container respectively.
  • The second line contains N space separated integers A[i], denoting the height of the barrier at ith position.

OUTPUT

  • Output a single line integer denoting the answer to the problem, i.e. the volume of juice that the container can hold.

CONSTRAINTS

  • 3 <= N <= 105
  • 1 <= W <= 10
  • 0 <= A[i] <= 1000 for each valid i

Subtask1 (20 points)

  • 3 <= N <= 1000

Subtask2 (80 points)

  • Original Constraints
Sample Input
12 6
1 5 0 2 3 8 12 0 1 4 12 11
Sample Output
246
Time Limit: 0.35
Memory Limit: 256
Source Limit:
Editor Image

?