Under construction

Still being built. Things may change

Dongwoo, is his heart kind or cruel?

Interview

Time limit2sMemory limit1024 MB

Summary
Given a weighted tree of N villages with signed edge costs, find the minimum and maximum total path cost from one house point (on a village or along a road) to all villages.
Level

Medium5 of 10

Topics
Tree, DFS, Dynamic programming
Solved
No attempts yet

Input and output

The Korean club MatKor in the Cyber Defense department at Korea University invites one outside lecturer to hold a seminar on a regular basis. Dongwoo has run several MatKor seminars and found that when he explains something to the whole group, most members stop paying attention, so he has to explain things to each person one by one. From this semester on, the seminars are one-on-one. Dongwoo also found that a single seminar takes a great deal of effort from the lecturer, so for the lecturer's convenience he decided to hold only one seminar per day.

MatKor has NN members, and each member lives in a different village. There are N−1N-1 roads connecting the villages, and there is exactly one path between any two villages. Each road can be traveled in both directions. Whoever travels a road once has their hardship increased by cic_i. Some villages are unusual, so cic_i can be 00 or a negative integer, and then the hardship goes down.

Dongwoo wants to build a new house for the lecturer. The house can be built on an existing village or on a point along a road. If the house is built on a road, the hardship increase from that point to each of the two end villages is proportional to the distance and to the road's cic_i value. For example, suppose a road between village 1 and village 2 increases hardship by 5 when traveled from one end to the other. If the house is built at the point that divides the road in the ratio 1:21:2, the hardship increase from that point to village 1 is 53\frac{5}{3}, and the increase to village 2 is 103\frac{10}{3}.

Each day, the lecturer goes to the village of one member, holds a one-on-one seminar, and returns to the house. On the way back, the hardship does not change, no matter which roads were used. This is done once for every member. The lecturer uses a route from the house to each village that never uses the same road twice. Moving within the same village does not change the hardship.

Dongwoo may be kind or unkind, so he wants to place the house at a point that minimizes or maximizes the total hardship the lecturer feels. Find the minimum and maximum hardship the lecturer feels during the seminars.

Input

The first line contains the integer NN (2≤N≤5×1052 \le N \le 5\times 10^5), the number of members.

From the second line to the NN-th line, each line gives information about a road. Each line contains two distinct integers aia_i (1≤ai≤N1 \le a_i \le N) and bib_i (1≤bi≤N1 \le b_i \le N), the end villages of the road, and an integer cic_i (−107≤ci≤107-10^7 \le c_i \le 10^7), the change in hardship when the road is traveled. These are separated by spaces.

Output

Print the minimum and maximum hardship the lecturer feels during the seminars, separated by a space.

If the answer is an integer, print it as is. If the answer is a rational number that is not an integer, reduce it to an irreducible fraction pq{p\over q} (1≤∣p∣1 \le \lvert p\rvert, 2≤q2\le q, gcd(∣p∣,q)=1gcd(\lvert p\rvert,q)=1) and print it in the form pp/qq.

It can be proven that the answer is always rational.

Hint

Dongwoo is kind. That is the accepted view.

Examples3

  1. Example 1

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

    Input
    11
    1 2 3
    2 5 4
    2 6 -1
    1 4 -3
    4 8 1
    4 9 4
    4 10 7
    9 11 -5
    1 3 2
    3 7 5
    
    Expected output
    -3 77
    
  3. Example 3

    Input
    5
    1 2 3
    1 3 -1
    3 4 15
    1 5 3
    
    Expected output
    18 63