This page is still under construction.

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

Gastronomic Event

Time limit2sMemory limit2048 MB

Summary
Assign ratings 1 to n to the rooms of a tree so that the number of edge-following paths with increasing ratings is maximized, and print that maximum.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Greedy
Solved
No attempts yet

Problem

The SWERC organizers want to hold a gastronomic event.

The event takes place in a building with nn rooms connected by n−1n - 1 corridors, where each corridor joins two rooms and it is possible to go from any room to any other room.

In each room, you must set up a tasting of a typical Italian dish. There are nn dishes, rated from 11 to nn by quality, where nn is the best rating. The nn dishes have distinct ratings.

Assign the nn dishes to the nn rooms so that the number of pleasing tours is maximal. A pleasing tour is a nonempty sequence of rooms such that:

  • Each room in the sequence is connected to the next room in the sequence by a corridor.
  • The dish ratings along the sequence are increasing.

If you assign the dishes optimally, what is the maximum number of pleasing tours?

Input

The first line contains an integer nn (2≤n≤10000002 \le n \le 1000000), the number of rooms.

The second line contains n−1n - 1 integers p2,p3,⋯ ,pnp_2, p_3, \cdots, p_n (1≤pi<i1 \le p_i < i). Each pip_i means there is a corridor between room ii and room pip_i. It is guaranteed that the building lets you go from any room to any other room.

Output

Print the maximum number of pleasing tours.

Examples2

  1. Example 1

    Input
    5
    1 2 2 2
    
    Expected output
    13
    
  2. Example 2

    Input
    10
    1 2 3 4 3 2 7 8 7
    
    Expected output
    47