atcoder#ARC121B. [ARC121B] RGB Matching

[ARC121B] RGB Matching

Score : 500500 points

Problem Statement

Snuke has 2N2N dogs, numbered 11 through 2N2N.

The cuteness of Dog ii is aia_i. The color of Dog ii is cic_i, which is R, G, or B; R stands for red, G stands for green, and B stands for blue.

Snuke has NN kennels, and wants to put two dogs in each kennel. Note that this means each dog will be in exactly one kennel.

When he puts two dogs in the same kennel, it will cause some dissatisfaction in that kennel. The level of dissatisfaction is represented as an integer. When Dogs ii and jj are in the same kennel, the dissatisfaction will be 00 if ci=cjc_i = c_j and aiaj|a_i - a_j| otherwise.

Find the minimum possible total dissatisfaction when putting two dogs in each of the NN kennels.

Constraints

  • 1N1051 \leq N \leq 10^{5}
  • 1ai10151 \leq a_i \leq 10^{15}
  • aia_i is an integer.
  • cic_i is R, G, or B.

Input

Input is given from Standard Input in the following format:

NN

a1a_{1} c1c_{1}

\vdots

a2Na_{2N} c2Nc_{2N}

Output

Print the minimum possible total dissatisfaction when putting two dogs in each of the NN kennels.

1
1 R
2 G
1
  • Dog 11 has the cuteness of 11, and Dog 22 has the cuteness of 22.
  • Since c1c2c_1 \neq c_2, the dissatisfaction will be 11.
1
1 B
2 B
0
  • Dog 11 has the cuteness of 11, and Dog 22 has the cuteness of 22.
  • Since c1=c2c_1 = c_2, the dissatisfaction will be 00.
10
585 B
293 B
788 B
222 B
772 G
841 B
115 R
603 G
450 B
325 R
851 B
205 G
134 G
651 R
565 R
548 B
391 G
19 G
808 B
475 B
0