Laziness

Time limit1sMemory limit512 MB

Summary
Cut a bar of total length into given pieces with cut cost xy and minimize total cost; the cost is fixed at (sum of squares differences)/2, so just read the lengths.
Level

Hard8 of 10

Topics
Math, Greedy, Implementation
Solved
No attempts yet

Problem

For some reason, Hyunwoo has ended up needing nn iron bars of lengths a1,…,ana_1, \dots, a_n. But all he had was a single iron bar of length a1+⋯+ana_1 + \cdots + a_n. Hyunwoo will cut this bar himself to make the nn bars he originally needed. Cutting a bar of length x+yx+y into two bars of lengths xx and yy costs xyxy, the product of the lengths of the two bars he wants to make. Hyunwoo wants to cut this bar at minimum cost to obtain the nn bars of lengths a1,…,ana_1, \dots, a_n.

However, Hyunwoo is not sure how much this cost will be. So he has offered to raise your Code Jam contest score by 30 points if you write a program that computes the minimum cost of cutting the bar. How about it?

Input

The first line gives the integer nn, the number of iron bars Hyunwoo wants. (1≤n≤500,0001 \le n \le 500{,}000)

The second line gives the integers a1,…,ana_1, \dots, a_n, the lengths of the iron bars Hyunwoo wants. (1≤ai≤1011 \le a_i \le 101)

Output

Print the minimum cost for Hyunwoo to obtain the nn iron bars he needs.

Examples2

  1. Example 1

    Input
    4
    3 5 4 2
    
    Expected output
    71
    
  2. Example 2

    Input
    10
    12 43 22 51 2 55 8 21 98 50
    
    Expected output
    55164