#USACOC2211B. Paired Up

Paired Up

Description

There are a total of NN cows on the number line, each ofwhich is a Holstein or a Guernsey. The breed of the ii-th cow is given by bi{H,G}b_i\in \{{\tt H},{\tt G}\}, the location of the ii-th cow is given by xix_i, and the weight of the ii-th cow is given by yiy_i.

At Farmer John's signal, some of the cows will form pairs such that

  • Every pair consists of a Holstein hh and a Guernsey gg whose locations arewithin KK of each other; that is, xhxgK|x_h-x_g|\le K.
  • Every cow is either part of a single pair or not part of a pair.
  • The pairing is maximal; that is, no two unpaired cows can form apair.

It's up to you to determine the range of possible sums of weights of theunpaired cows. Specifically,

  • If T=1T=1, compute the minimum possible sum of weights of the unpairedcows.
  • If T=2T=2, compute the maximum possible sum of weights of theunpaired cows.
  • 1N50001\le N\le 5000
  • 1K1091\le K\le 10^9
  • 0xi1090 \leq x_i \leq 10^9
  • 1yi1051 \leq y_i \leq 10^5

Input Format

The first input line contains TT, NN, and KK.

Following this are NN lines, the ii-th of which contains bi,xi,yib_i,x_i,y_i. It isguaranteed that 0x1<x2<<xN1090\le x_1< x_2< \cdots< x_N\le 10^9.

Output Format

The minimum or maximum possible sum of weights of the unpaired cows.

2 5 4
G 1 1
H 3 4
G 4 2
H 6 6
H 8 9
2 5 4
G 1 1
H 3 4
G 4 2
H 6 6
H 8 9

Cows 22 and 33 can pair up because they are at distance 11, which is at mostK=4K = 4. This pairing is maximal, because cow 11, the only remaining Guernsey,is at distance 55 from cow 44 and distance 77 from cow 55, which are morethan K=4K = 4. The sum of weights of unpaired cows is1+6+9=161 + 6 + 9 = 16.

1 5 4
G 1 1
H 3 4
G 4 2
H 6 6
H 8 9
1 5 4
G 1 1
H 3 4
G 4 2
H 6 6
H 8 9

Cows 11 and 22 can pair up because they are at distance 2K=42 \leq K = 4, andcows 33 and 55 can pair up because they are at distance 4K=44 \leq K = 4. Thispairing is maximal because only cow 44 remains. The sum of weights ofunpaired cows is the weight of the only unpaired cow, which is simply 66.

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
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

Scoring

  • Test cases 4-7 satisfy T=1T=1.
  • Test cases 8-14 satisfy T=2T=2 and N300N\le 300.
  • Test cases 15-22 satisfy T=2T=2.

Note: the memory limit for this problem is 512MB, twice the default.

Problem Credit

Problem credits: Benjamin Qi