Under construction

Still being built. Things may change

Elevated Profits

Time limit2sMemory limit1024 MB

Summary
Given a tree of cities and a start city R, maximize the sum of visit index times popularity, where each newly visited city must neighbor an already visited one.
Level

Medium7 of 10

Topics
Tree, Greedy, Sorting
Solved
No attempts yet

Problem

Marina, a digital influencer who loves traveling the world, is embarking on a promotional tour for a women's clothing brand called W2M (From Woman to Woman Marina). Marina's journey takes her through NN cities in Latin America, each with its unique charm and a distinct integer from 11 to NN called its popularity index.

W2M has provided her with N−1N - 1 transfers, connecting pairs of cities so that all NN cities are reachable. Marina can traverse these connections as many times as she pleases.

Marina's mission is to display the brand's dresses in each of the NN cities. Each time she visits a city for the first time, she must select a dress she hasn't worn before and capture the city's essence in a social media post. Every new picture she shares attracts followers, creating anticipation for the next one. The anticipation value for her first picture is 11, and it increases by 11 for each subsequent picture.

Marina can revisit any city as often as desired, but a new picture must only be posted on her initial visit to a city. Her goal is to maximize the profit of her tour, which is the sum of the anticipation value of each picture multiplied by the popularity index of the city where the picture is taken. More precisely, let pip_i be the popularity index of the city where the ii-th picture is taken. The profit is then

∑i=1Ni×pi=1×p1+2×p2+⋯+N×pN\sum_{i=1}^{N}{i \times p_i} = 1 \times p_1 + 2 \times p_2 + \cdots + N \times p_N

Marina needs your help. Given that the tour must start in city p1=Rp_1 = R, determine the maximum profit she can achieve by planning the order of her city visits.

Input

The first line contains two integers NN (1≤N≤3×1051 \le N \le 3 \times 10^5) and RR (1≤R≤N1 \le R \le N), the number of cities and the initial city of the tour.

Each of the next N−1N - 1 lines contains two integers UU and VV (1≤U,V≤N1 \le U, V \le N and U≠VU \ne V), meaning there is a transfer between cities UU and VV. It is guaranteed that every city can be reached using the transfers.

Output

Output a single line with an integer, the maximum profit Marina can achieve on her promotional tour.

Examples2

  1. Example 1

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

    Input
    1 1
    
    Expected output
    1