PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#TP0000043. Buôn bán rượu ở Gergovia (Wine trading in Gergovia)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Buôn bán rượu ở Gergovia (Wine trading in Gergovia)
Wine trading in Gergovia
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
A street has consecutive houses. House has wine balance : a positive value means it sells that many bottles; a negative value means it needs to buy the opposite amount. The total balance is zero. Moving one bottle across one edge between adjacent houses costs one unit of work. Find the minimum total work needed to satisfy all houses.
Input
- The first line contains , the number of houses.
- The second line contains integers in street order.
Output
Print one integer: the minimum total transport work.
Subtasks
- Subtask 1 (20 points): ; for all ; .
- Subtask 2 (30 points): ; for all ; .
- Subtask 3 (50 points): ; for all ; .
Examples
Input
5
5 -4 1 -3 1
Output
9
Explanation
The running balances after successive houses are . Across the four boundaries, the necessary absolute flows are bottles. The total work is .