Building a Graph
Time limit2sMemory limit512 MB
Build a connected graph (tree) with N nodes and N-1 edges, where each node's score depends on its degree, and maximize the total score.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
You build a graph with nodes and edges. The graph must be connected.
The figure below is a graph with nodes and edges.

An edge can connect two nodes. The degree of a node is the number of edges attached to it. In the figure above, A has degree 3 and B has degree 1.
The score of the graph is the sum of the scores of all nodes, and the score of a node is decided by its degree alone. Given the score for each degree, write a program that finds the largest score among the graphs that meet the conditions.
Input
The first line contains the number of nodes . ()
The second line contains scores by degree: the score of a node of degree 1, the score of a node of degree 2, ..., the score of a node of degree , in that order. Each score is an integer between 0 and 10,000.
If is 1, the second line is empty.
Output
Print the largest score among the graphs you can build.
If is 1, there are no edges and no score is given for degree 0, so print 0.