This page is still under construction.

Parts of this page are still being built. What you see may change.

Soldering

Time limit2sMemory limit128 MB

Summary
Given a tree, cover its edges with paths (wires) that may meet at soldered points, minimizing the sum of squared path lengths.
Level

Medium7 of 10

Topics
Tree, Dynamic programming, Greedy
Solved
No attempts yet

Problem

The cows are playing with wires! The soldering technique the cows have learned attaches the end of one wire to the middle of another wire. (Soldering two wires end to end is not allowed.) Several wires may be soldered onto the same point.

Using this technique, the cows want to build an impressive structure. The structure is a tree made of NN connected vertices (1≤N≤50,0001 \le N \le 50{,}000) and N−1N-1 unit-length edges. Each edge is given by two integers AA and BB (1≤A≤N1 \le A \le N, 1≤B≤N1 \le B \le N, A≠BA \ne B), the numbers of the vertices at its two ends.

To build the structure the cows must buy wires. Longer wires are more expensive: a wire of length LL costs L×LL \times L. Wires may not be cut, nor spliced together into longer wires.

Given the blueprint of the structure, compute the minimum cost to build it by soldering wires.

Note: for 50% of the test data, N<2,000N < 2{,}000.

Input

  • The first line contains the integer NN.
  • Each of the next N−1N-1 lines contains two integers AA and BB describing an edge.

Output

Print, on a single line, the minimum cost to build the structure. The answer may exceed the range of a 32-bit integer.

Hint

Consider a star in which every vertex is connected directly to vertex 1: you can join two edges into a single wire of length 2 and use a length-1 wire for each remaining edge. With 6 vertices the cost is 22+12×3=72^2 + 1^2 \times 3 = 7.

Examples3

  1. Example 1

    Input
    6
    1 2
    1 3
    1 4
    1 5
    1 6
    
    Expected output
    7
    
  2. Example 2

    Input
    2
    1 2
    
    Expected output
    1
    
  3. Example 3

    Input
    3
    1 2
    2 3
    
    Expected output
    4