Good Bye 2017
F
New Year and Rainbow Roads
Codeforces
2400
原题
← EG →
翻译中…

Roy and Biv have a set of $*n*$ points on the infinite number line.

翻译中…

Each point has one of 3 colors: red, green, or blue.

翻译中…

Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects.

翻译中…

They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly).

翻译中…

However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue.

翻译中…

Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected).

翻译中…

Help them compute the minimum cost way to choose edges to satisfy the above constraints.

翻译中…

Input

翻译中…

The first line will contain an integer $*n*$ ($1 ≤ *n* ≤ 300 000$), the number of points.

翻译中…

The next $*n*$ lines will contain two tokens $*p*_{*i*}$ and $*c*_{*i*}$ ($*p*_{*i*}$ is an integer, $1 ≤ *p*_{*i*} ≤ 10^{9}$, $*c*_{*i*}$ is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the $*i*$-th point and the color of the $*i*$-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.

翻译中…

Output

翻译中…

Print a single integer, the minimum cost way to solve the problem.

翻译中…

Note

翻译中…

In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.

翻译中…

样例

Input #1
4
1 G
5 R
10 B
15 G
Output #1
23
InputCopy OutputCopy
Input #2
4
1 G
2 R
3 B
10 G
Output #2
12
InputCopy OutputCopy
题目信息
题号
908F
所属比赛
时间限制
2 s
内存限制
256 MB
标签
图论
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。