#P7987. [USACO21DEC] Paired Up G

[USACO21DEC] Paired Up G

Problem Description

There are a total of NN (1N1051\le N\le 10^5) cows on a number line. The position of the ii-th cow is xix_i (0xi1090 \leq x_i \leq 10^9), and the weight of the ii-th cow is yiy_i (1yi1041 \leq y_i \leq 10^4).

According to Farmer John’s signal, some cows will form pairs such that:

  • Each pair contains two different cows aa and bb whose positions differ by at most KK (1K1091\le K\le 10^9); that is, xaxbK|x_a-x_b|\le K.

  • Each cow is either included in exactly one pair or does not belong to any pair.

  • The pairing is maximal; that is, no two unpaired cows can form a pair.

You need to find the possible range of the total weight of unpaired cows. Specifically:

  • If T=1T=1, compute the minimum possible total weight of unpaired cows.

  • If T=2T=2, compute the maximum possible total weight of unpaired cows.

Input Format

The first line contains TT, NN, and KK.

The following NN lines each contain xix_i and yiy_i. The input guarantees that 0x1<x2<<xN1090\le x_1< x_2<\cdots<x_{N}\le 10^9.

Output Format

Output the minimum or maximum possible total weight of unpaired cows.

2 5 2
1 2
3 2
4 2
5 1
7 2
6
1 5 2
1 2
3 2
4 2
5 1
7 2
2
2 15 7
3 693
10 196
12 182
14 22
15 587
31 773
38 458
39 58
40 583
41 992
84 565
86 897
92 197
96 146
99 785
2470

Hint

Sample Explanation 1

In this example, cows 22 and 44 can be paired because their distance is 22, which does not exceed K=2K = 2. This pairing is maximal because the distance between cows 11 and 33 is 33, the distance between cows 33 and 55 is 33, and the distance between cows 11 and 55 is 66, all greater than K=2K = 2. The total weight of unpaired cows is 2+2+2=62 + 2 + 2 = 6.

Sample Explanation 2

Here, cows 11 and 22 can be paired because their distance is 2K=22 \leq K = 2, and cows 44 and 55 can be paired because their distance is 2K=22 \leq K = 2. This pairing is maximal because only cow 33 remains. The total weight of unpaired cows is 22.

Sample Explanation 3

The answer for this example is 693+992+785=2470693+992+785=2470.

Constraints

  • Test cases 4–8 satisfy T=1T=1.
  • Test cases 9–14 satisfy T=2T=2 and N5000N\le 5000.
  • Test cases 15–20 satisfy T=2T=2.

Translated by ChatGPT 5