Under construction

Still being built. Things may change

Janjetina

Time limit1.5sMemory limit512 MB

Summary
Count ordered city pairs in a tree whose path's maximum lamb weight minus the path length is at least k.
Level

Medium7 of 10

Topics
Tree, Divide and conquer, Sorting
Solved
No attempts yet

Problem

After restaurants all over Croatia closed because of the lockdown, Mr. Malnar was first overwhelmed with sadness. But he soon realized that there was no point in being sad, so he decided that as soon as the restaurants reopen, he will travel around Croatia and treat himself to the best lamb Croatian restaurants can offer.

Mr. Malnar knows about nn cities he could visit, labeled with integers from 11 through nn. He also knows about n−1n-1 two-way roads that connect these cities, so it is possible to travel between any two cities.

On every road there is a restaurant that serves lamb. Mr. Malnar knows how many kilograms of lamb he can order at each one.

He will choose two different cities xx and yy and travel from xx to yy via the shortest path, that is, the path that uses the fewest roads. He will stop at exactly one restaurant, the one where he can order the maximum amount of lamb (if several restaurants tie, he chooses any of them), and he will eat all the lamb he orders.

The length of a path is the number of roads it goes through. If he eats ww kilograms of lamb on a path of length ll, the path is satisfactory when w−l≥kw - l \ge k.

He wonders how many ordered pairs of distinct cities (x,y)(x, y) have a satisfactory shortest path between them. Calculate the answer for him.

Input

The first line contains integers nn and kk (1≤n,k≤1000001 \le n, k \le 100000), the number of cities and the satisfaction threshold.

Each of the following n−1n-1 lines contains three integers xx, yy and ww (1≤x,y≤n1 \le x, y \le n, x≠yx \ne y, 1≤w≤1000001 \le w \le 100000). This means there is a road connecting xx and yy, and a restaurant on that road where Mr. Malnar can order ww kilograms of lamb.

Output

Output the number of ordered pairs of distinct cities (x,y)(x, y) such that the shortest path from xx to yy is satisfactory.

Hint

Clarification of the third example:

The pairs are (1, 3), (3, 1), (1, 5), (5, 1), (3, 5), (5, 3), (4, 5) and (5, 4).

Examples3

  1. Example 1

    Input
    3 1
    1 2 3
    1 3 2
    
    Expected output
    6
    
  2. Example 2

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

    Input
    5 2
    1 2 2
    1 3 3
    3 4 2
    3 5 4
    
    Expected output
    8