This page is still under construction.

Parts of this page are still being built. What you see may change.

Fluoride Dentist

Time limit3sMemory limit1024 MB

Summary
Given values a_i, Bjorn (the one with a_i=0) may reinsert himself anywhere; maximize the sum of i*a_i.
Level

Medium5 of 10

Topics
Array, Prefix sum, Greedy, Implementation
Solved
No attempts yet

Problem

Björn and n−1n-1 other people are standing in line to see the fluoride dentist. Different people find meeting the fluoride dentist scary to different degrees. The people are numbered from 11 to nn, and person ii stands in position ii in the line. Person ii also has a value a_ia\_i, which shows how reluctant the person is to meet the fluoride dentist. Person ii's happiness with their position in the line is i⋅a_ii \cdot a\_i. Some people may have a negative a_ia\_i, which means they actually want to meet the fluoride dentist and are therefore sad about having to wait.

Björn is the only person who is completely indifferent to meeting the fluoride dentist, that is, he is the only person with a_i=0a\_i = 0. He is also very kindhearted, so he decides to leave the line and then re-enter the line at some position so that the total happiness of everyone in the line becomes as large as possible. Write a program that, given the values a_ia\_i for all people, computes the maximum sum of happiness in the line when Björn stands in an optimal position.

Input

The first line contains an integer nn, the number of people in the line. The next line contains nn integers, where the iith integer is a_ia\_i. 1≤n≤1061 \leq n \leq 10^6, −1000≤a_i≤1000-1000 \leq a\_i \leq 1000.

Output

Print one line with a single integer: the maximum total happiness in the line.

Examples3

  1. Example 1

    Input
    3
    1 0 -2
    
    Expected output
    -3
    
  2. Example 2

    Input
    5
    0 -8 1 1 5
    
    Expected output
    24
    
  3. Example 3

    Input
    7
    2 -4 5 -3 0 -1 2
    
    Expected output
    7