Rainy Day
Time limit1sMemory limit1024 MB
Build skybridges so every building is reachable, where building i with degree k costs a_i*k^2, and minimize total cost.
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Graph, Greedy, Sorting
- Solved
- No attempts yet
Problem
Lee Hwan, the principal of PS High School, loves the school very much. So every day at lunchtime, Lee Hwan walks around all the buildings of the school. One rainy day, Lee Hwan wanted to walk around the school, but could not, because he hates the rain terribly.
Worried about the rain, Lee Hwan decided to install skybridges between buildings so that he can visit all the buildings of the school without getting rained on. But the students opposed the installation, afraid that they would be caught playing games if Lee Hwan walked around even on a rainy day. If skybridges connect building to other buildings, the students playing games in building have to watch skybridges for Lee Hwan's approach, so they have units of complaint.
Suppose that at lunchtime, 1 student plays games in building 1, 2 students in building 2, and 3 students in building 3. If skybridges are built as shown below, the students in buildings 2 and 3 each have 1 unit of complaint, and the student in building 1 has 4 units. Thus the students' total complaint is . It is impossible to build skybridges so that the complaint is less than 9.

PS High School actually has buildings, and in building , students play games. Since Lee Hwan gets hurt when the students' complaint is large, you must find the minimum complaint for Lee Hwan's sake.
Input
The first line gives the number of buildings .
The second line gives nonnegative integers , the number of students playing games in each building, separated by spaces.
Output
Print the minimum complaint on the first line.
Constraints
- All numbers in the input are integers.