William and Summation

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 1.0s
Memory limit: 256M

Authors:
Problem type

William is given an array of N integers a1,a2,,an. He then must pick one contiguous segment [L,R] (1LRN) and multiply all values on that segment (aL,,aR) by 1.

His goal is such that after modifying this segment, the sum of the prefix sums is minimized. This value can be represented as:

i=1Nj=1iaj

Output the minimal value attainable under the given terms.

Input Specification

The first line consists of a single integer N (1N105).

The next line contains N space-separated integers a1,a2,,aN (1000ai1000).

Subtask 1 [20%]

1N2000

Subtask 2 [80%]

No further constraints.

Output Specification

Output a single integer, the minimal value attainable of the expression outlined above.

Sample Input 1

Copy
4
1 -2 100 -5

Sample Output 1

Copy
-207

Explanation for Sample 1

If the segment [3,3] was modified our array will become: [1,2,100,5].

The prefix sum of the modified array is:

[1,1,101,106]

Thus the answer would be the sum of the prefix sum of all elements:

ans=1+(1)+(101)+(106)=207

The value 207 is the minimum attainable answer.

Sample Input 2

Copy
5
-10 7 -2 -2 10

Sample Output 2

Copy
-78

Comments

There are no comments at the moment.