FIRING - Firing
About N birds in a row, each bird i-th has power a[i]. If we shoot bird i-th, bird (i-1)-th, bird i-th and bird (i+1)-th will fight, so we lost a[i-1]+a[i]+a[i+1] energy (that a[0]=0, a[n+1]=0). And then, bird i+1 will stand near bird i-1.
John will shoot the bird in descending order of power. If at the time, we have many birds that power is maximum, Jonh'll shoot the bird i-th such that i is smallest possible. Let me know total energy John lost to shoot all bird.
Input
The first line is a integer: N. (1≤N≤1000000).
The next line is N integer: a[1],a[2],...,a[n]. (1≤a[i]≤1000000000).
Output
Only integer: Result.
Example
Input: 6
1 4 2 3 6 5
Output: 38
hide comments
Phạm Bá Thái:
2014-12-08 12:37:35
@Min_25 Yes, I see. But your solution is O(N.LogN) because you sorted. My solution did not sort, it's really O(N) :) |
|
Min_25:
2014-12-05 07:03:20
@Phạm Bá Thái
|
|
Phạm Bá Thái:
2014-12-05 00:49:44
Min_25's solustion was TLE :) |
|
Phạm Bá Thái:
2014-12-05 00:44:29
Oh, yes. I edited my problem
|
|
Min_25:
2014-12-05 00:42:45
@Phạm Bá Thái
|
Added by: | Phạm Bá Thái |
Date: | 2013-10-21 |
Time limit: | 1s-1.5s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | Mine |