cswork / OAOpen practice workspace

AmazonOA #41

Maximize Total Memory Points

Estimated EasyJudge-ready

Topics (AI-labelled)GreedyIn-place Arrays

Read all chapters in any order; after reading each chapter, you score the sum of the memory values of all chapters read so far. Find the maximum possible sum of all these scores. Memory values may be negative.

Input

The first line contains n (1..100000), and the second line contains n memory values (−1000..1000).

Output

Output a single integer answer.

Example 1

Input:

3
3 4 5

Output:

26

Example 2

Input:

2
-2 -1

Output:

-4

Example 3

Input:

1
0

Output:

0

Example notes

Example 1: reading in the order 5, 4, 3 gives scores 5, 9, 12, for a total of 26. Example 2: reading −1 first and then −2 gives scores −1, −3, for a total of −4, which is larger than the −5 of the other order. Example 3: the total is 0.

Editorial

Loading solution…