Each person has a supply or demand of fleas at unit-distance positions; find the minimum total delivery cost.
Medium5GreedyPrefix sumArrayInterviewNo attempts yetTime limit1sMemory limit128 MBPeople buy and sell fleas at a flea market. The total number of fleas people want to buy equals the total number of fleas people want to sell. Everyone stands in one line, and the distance between two neighboring people is 1. Nobody moves, so the courier Giyeong handles every delivery. Delivering fleas between two people costs (number of fleas delivered) × (distance between the two people). Find the smallest cost for Giyeong to deliver every flea to the people who want to buy.

The picture above shows five people whose trades are 500, -200, -400, 50, 50 in order. Person 3 wants to buy 400 fleas and person 1 wants to sell 500. If person 3 buys 400 fleas from person 1, the distance is 2, so the cost is 2×400=800. If Giyeong delivers 200 fleas from person 1 to person 2, then 300, 50 and 50 fleas from persons 1, 4 and 5 to person 3, the cost is 200+300×2+50+50×2=950, and no delivery plan is cheaper.
The first line contains the number of people N (1≤N≤100000).
The second line contains the number of fleas L (−1000≤L≤1000) each person trades, given in the order the people stand. A positive L marks a person who sells fleas, a negative L marks a person who buys fleas, and 0 marks a person who does not trade. The sum of all given L is 0.
Print the minimum cost for Giyeong to deliver every flea on one line. The answer can exceed the 32-bit integer range, so use a 64-bit integer type.