#P1296. Gossiping Cows

    ID: 14583 Type: Default 1000ms 256MiB

Gossiping Cows

Gossiping Cows

You are given a dairy farm with n cows positioned in a line. The position of the i-th cow is given by an integer coordinate \(p_i\) (\(0 \le p_i \le 10^8\)). All cows produce sound at the same volume, but due to the decay of sound energy, a cow's gossip can only be heard by another cow if their distance is at most \(d\) (\(0 \le d \le 10^4\)). Two cows are said to be able to exchange gossip if \(|p_i - p_j| \le d\). Your task is to compute the number of distinct pairs of cows that can exchange gossip.

inputFormat

The first line contains two integers n and d, where n is the number of cows and d is the maximum distance the sound can travel. The second line contains n space-separated integers \(p_1, p_2, \ldots, p_n\) representing the positions of the cows.

outputFormat

Output a single integer: the number of pairs of cows that can exchange gossip.

sample

3 2
1 3 5
2