def trap(height):
    stack=[]
    volume=0
    for i in range(len(height)):
        while stack and height[i]>height[stack[-1]]:
            top=stack.pop()
            if not len(stack):
                break
            distance=i-stack[-1]-1
            waters=min(height[i],height[stack[-1]])-height[top]
            volume+=distance*waters
        stack.append(i)
    return volume

height=list(map(int,input().split()))
print(trap(height))