Mock CCO '18 Contest 1 Problem 6 - A Combining Problem

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 0.3s
Memory limit: 16M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Given a list of N integers, we can take two adjacent integers, remove both of them, and insert the larger of the two where the two integers originally were. This incurs cost equal to the larger of the two integers. Compute the minimum cost needed to reduce this list to having just one integer.

Constraints

1 \le N \le 10^6

0 \le a_i \le 10^9

For at most 30% of marks, N \le 500.

For at most 50% of marks, N \le 20\,000.

Input Specification

The first line will contain a single integer, N.

Each of the next N lines will contain an integer a_i, the integers of the list in order.

Output Specification

Output the minimum cost.

Sample Input

3
1
2
3

Sample Output

5

Comments


  • -6
    p1geon  commented on Jan. 4, 2020, 10:17 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.