#P7985. [USACO21DEC] Paired Up P
[USACO21DEC] Paired Up P
Problem Description
There are a total of cows on a number line (). Each cow is either a Holstein or a Guernsey. For cow , its breed is , its position is (), and its weight is ().
Following Farmer John’s signal, some cows will form pairs such that:
- Each pair consists of one Holstein cow and one Guernsey cow whose positions differ by at most (); that is, .
- Each cow is either included in exactly one pair or belongs to no 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 , compute the minimum possible total weight of unpaired cows.
- If , compute the maximum possible total weight of unpaired cows.
Input Format
The first line contains , , and .
The next lines describe the cows. Line contains . The input guarantees that .
Output Format
Output the minimum or maximum possible total weight of unpaired cows.
2 5 4
G 1 1
H 3 4
G 4 2
H 6 6
H 8 9
16
1 5 4
G 1 1
H 3 4
G 4 2
H 6 6
H 8 9
6
2 10 76
H 1 18
H 18 465
H 25 278
H 30 291
H 36 202
G 45 96
G 60 375
G 93 941
G 96 870
G 98 540
1893
Hint
[Sample Explanation 1]
Cows and can be paired because their distance is , which does not exceed . This pairing is maximal because cow , the only remaining Guernsey, is at distance from cow and at distance from cow , both greater than . The total weight of unpaired cows is .
[Sample Explanation 2]
Cows and can be paired because their distance is , and cows and can be paired because their distance is . This pairing is maximal because only cow remains. The total weight of unpaired cows is the weight of the only unpaired cow, which is .
[Sample Explanation 3]
The answer for this example is .
[Constraints]
- Test cases 4-7 satisfy .
- Test cases 8-14 satisfy and .
- Test cases 15-22 satisfy .
Note: The memory limit for this problem is , which is twice the usual limit.
Translated by ChatGPT 5