Under construction

Still being built. Things may change

Score of a Subsequence

Interview

Time limit2sMemory limit512 MB

Summary
Find the maximum over all contiguous subarrays of the weighted sum where the k-th element from the subarray start contributes k times its value.
Level

Medium5 of 10

Topics
Dynamic programming, Array, Greedy, Prefix sum
Solved
No attempts yet

Problem

The score of a sequence s=s1,s2,…,sns = s_1, s_2, \dots, s_n is ∑i=1ni×si\sum_{i=1}^{n} i \times s_i.

A contiguous subsequence is a sequence of its own, so the weights 1,2,3,…1, 2, 3, \dots count again from its first term. The score of the contiguous subsequence sl,sl+1,…,srs_l, s_{l+1}, \dots, s_r is ∑i=lr(i−l+1)×si\sum_{i=l}^{r} (i - l + 1) \times s_i.

Write a program that finds the largest score among the contiguous subsequences of ss. A subsequence of length 0 can be chosen, and its score is 0.

Input

The first line contains nn (1≤n≤200 0001 \le n \le 200\,000).

The second line contains s1,s2,…,sns_1, s_2, \dots, s_n. (∣si∣≤107|s_i| \le 10^7)

Output

Print the largest score among the contiguous subsequences of ss on the first line.

Examples3

  1. Example 1

    Input
    6
    5 -1000 1 -3 7 -8
    
    Expected output
    16
    
  2. Example 2

    Input
    5
    1000 1000 1001 1000 1000
    
    Expected output
    15003
    
  3. Example 3

    Input
    3
    -60 -70 -80
    
    Expected output
    0