Elevated Profits
Time limit2sMemory limit1024 MB
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.
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 cities in Latin America, each with its unique charm and a distinct integer from to called its popularity index.
W2M has provided her with transfers, connecting pairs of cities so that all 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 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 , and it increases by 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 be the popularity index of the city where the -th picture is taken. The profit is then
Marina needs your help. Given that the tour must start in city , determine the maximum profit she can achieve by planning the order of her city visits.
Input
The first line contains two integers () and (), the number of cities and the initial city of the tour.
Each of the next lines contains two integers and ( and ), meaning there is a transfer between cities and . 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.