← 返回 jpmorgan 的题目列表About Shipping (Minimum operations to make two consecutive consignments equal)
类型:online_judge
Problem: About Shipping (Minimum operations to equalize two consecutive consignments)
A shop has n item types. The quantity of the i-th item type is quantity[i] (1-based indexing).
The items must be shipped in two consecutive consignments split by an index j:
The first consignment contains item types [1, 2, ..., j]
The second consignment contains item types [j+1, ..., n]
You may choose any j such that 1 <= j < n, so both consignments are non-empty. An item type cannot be split across consignments.
The shopkeeper wants the total quantities of the two consignments to be equal. To achieve this, you may perform operations on any quantity[i]:
In one operation, increase or decrease a single quantity[i] by 1.
You may apply any number of operations to any elements.
Each quantity[i] must remain positive at all times (i.e., quantity[i] >= 1).
Compute the minimum number of operations required to make the two consignment sums equal, assuming you also choose the split index j optimally.
Input
Integer n
Integer array quantity of length n with quantity[i] >= 1
Output
Return a 64-bit integer (long int / long long): the minimum operations needed.
Example
n = 3
quantity = [1, 4, 4]
Increase quantity[3] by 1 to get [1, 4, 5], then split at j = 2:
First consignment sum: 1 + 4 = 5
Second consignment sum: 5
Answer: 1.
Example
Input
3
1 4 4
Output
1