Interconnection
题号:NC252122
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

There are n villages, numbered from 1 to n. Initially, these n villages are all isolated.

Now, these n villages want to establish some **bi-directional** roads to achieve interconnection.

Building a road requires a certain cost. Specifically, building a bi-directional road between village i and village j costs (i^2+j^2). The construction of a bi-directional road will be completed jointly by the two villages connected by this road. However, the human resources that each village can provide are limited. Village i can participate in the construction of at most d_i roads. In other words, there can be at most d_i roads that have village i as one endpoint.

What is the minimum cost to make all n villages reachable to each other via roads?

输入描述:

The first line contains an integer n (2\leq n\leq 2\cdot10^5), denoting the number of villages.

The second line contains n integers d_1, d_2, ..., d_n (1\leq d_i\leq n-1), where d_i denotes the maximum number of roads that village i can participate in building.

输出描述:

Output a single integer, which is the minimum cost  to make all n villages reachable to each other via roads.

If it is impossible, then output "-1".
示例1

输入

复制
3
2 2 2

输出

复制
15
示例2

输入

复制
3
1 1 1

输出

复制
-1

说明

For the first example, the built roads are (1, 2) and (1, 3). Thus the cost is (1^2+2^2)+(1^2+3^2)=15.

For the second example, it is easy to verify that it is impossible to connect all three villages anyway.