P29765 [洛谷] [USACO21DEC] Paired Up G
提高
题目描述

Problem Description

There are a total of $N$ ($1 \le N \le 10^5$) cows on a number line. The position of the $i$-th cow is $x_i$ ($0 \leq x_i \leq 10^9$), and the weight of the $i$-th cow is $y_i$ ($1 \leq y_i \leq 10^4$).

According to Farmer John's signal, certain cows will form pairs such that:

  • Each pair contains two different cows $a$ and $b$ whose positions differ by no more than $K$ ($1 \le K \le 10^9$); that is, $|x_a - x_b| \le K$.

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

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

You need to determine the possible range of the sum of the weights of the unpaired cows. Specifically,

  • If $T = 1$, calculate the minimum sum of the weights of the unpaired cows.

  • If $T = 2$, calculate the maximum sum of the weights of the unpaired cows.

Input Format

The first line of input contains $T$, $N$, and $K$.

The following $N$ lines, the $i$-th line contains $x_i$ and $y_i$. The input guarantees that $0 \le x_1 < x_2 < \cdots < x_N \le 10^9$.

Output Format

Output the minimum or maximum sum of the weights of the unpaired cows.

Sample Input and Output

Sample Input #1

2 5 2
1 2
3 2
4 2
5 1
7 2

Sample Output #1

6

Sample Input #2

1 5 2
1 2
3 2
4 2
5 1
7 2

Sample Output #2

2

Sample Input #3

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

Sample Output #3

2470

Notes

Sample Explanation 1

In this example, cows $2$ and $4$ can be paired because their distance is $2$, which is no more than $K = 2$. This pairing scheme is maximal because the distances between cows $1$ and $3$, cows $3$ and $5$, and cows $1$ and $5$ are all greater than $K = 2$. The sum of the weights of the unpaired cows is $2 + 2 + 2 = 6$.

Sample Explanation 2

Here, cows $1$ and $2$ can be paired because their distance is $2 \leq K = 2$, and cows $4$ and $5$ can be paired because their distance is $2 \leq K = 2$. This pairing scheme is maximal because only cow $3$ remains unpaired. The sum of the weights of the unpaired cows is $2$.

Sample Explanation 3

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

Data Range

  • Test cases 4-8 satisfy $T = 1$.
  • Test cases 9-14 satisfy $T = 2$ and $N \le 5000$.
  • Test cases 15-20 satisfy $T = 2$.
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 5 2 1 2 3 2 4 2 5 1 7 2
样例输出
6