Under construction

Still being built. Things may change

Cutting in Line

Interview

Time limit1sMemory limit1024 MB

Summary
Given each student's satisfaction, find the maximum total when each student either cuts to the front or joins the back of the line.
Level

Medium4 of 10

Topics
Prefix sum, Greedy, Array
Solved
No attempts yet

Problem

Today the lunch menu at Chamseulgi Cafeteria is chicken mayo rice bowl. Perhaps because of that, far more students than usual are lining up. Students have ticket numbers from 11 to NN. They line up in ticket order. The student with ticket 11 joins the line first, and their satisfaction is s_1s\_1. Starting from ticket 22, each student chooses one of the following two actions.

  • Stand at the front: The student stands at the front of the line, and student ii has satisfaction s_is\_i.
  • Stand at the back: The student stands at the back of the line, and student ii has satisfaction 00.

The way students line up can also change the satisfaction of students already in line.

  • If student jj (j>i)(j > i) is in front of student ii, student ii has been cut in front of, so student ii's satisfaction changes to −s_i-s\_i.
    • Note that it can change to −s_i-s\_i even if the existing satisfaction was 00.
  • If no student jj (j>i)(j > i) is in front of student ii, student ii has not been cut in front of, so student ii's satisfaction does not change.

Input

The first line contains the number of students NN.

The second line contains positive integers s_1s\_1, s_2s\_2, s_3s\_3, ⋯\cdots, s_Ns\_N, separated by spaces.

Output

Print the maximum possible total satisfaction on the first line.

Constraints

  • 1≤N≤100 0001 \le N \le 100\,000
  • 1≤s_i≤1091 \le s\_i \le 10^9
  • 1≤i≤N1 \le i \le N

Examples3

  1. Example 1

    Input
    2
    1 10
    
    Expected output
    9
    
  2. Example 2

    Input
    3
    10 1 1
    
    Expected output
    10
    
  3. Example 3

    Input
    4
    1 2 4 1
    
    Expected output
    1