#abc369b. B - Piano 3

B - Piano 3

Score : 200200 points

问题陈述

高桥有一架钢琴,排列着 100100 个键。从左边数第 ii 个键被称为键 i

他将通过依次按下 NN 个键来演奏音乐。对于第 ii 次按下,他将按下键 AiA_i,如果 SiS_i 等于 L,则使用左手,如果 SiS_i 等于 R,则使用右手。

在开始演奏之前,他可以将双手放在任何他喜欢的键上,此时他的疲劳度00。在演奏过程中,如果他将一只手从键x移动到键y,疲劳度将增加 yx|y-x|(相反,除了移动手之外,疲劳度不会因任何原因增加)。要用手按下某个键,那只手必须放在那个键上。

找出演奏结束时可能的最小疲劳度。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

Takahashi has a piano with 100100 keys arranged in a row. The ii-th key from the left is called key ii.

He will play music by pressing NN keys one by one. For the ii-th press, he will press key AiA_i, using his left hand if Si=S_i= L, and his right hand if Si=S_i= R.

Before starting to play, he can place both of his hands on any keys he likes, and his fatigue level at this point is 0. During the performance, if he moves one hand from key xx to key yy, the fatigue level increases by yx|y-x| (conversely, the fatigue level does not increase for any reason other than moving hands). To press a certain key with a hand, that hand must be placed on that key.

Find the minimum possible fatigue level at the end of the performance.

Constraints

  • 1N1001 \leq N \leq 100
  • 1Ai1001 \leq A_i \leq 100
  • NN and AiA_i are integers.
  • SiS_i is L or R.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 S1S_1

A2A_2 S2S_2

\vdots

ANA_N SNS_N

Output

Print the minimum fatigue level at the end of the performance.

Sample Input 1

4
3 L
6 R
9 L
1 R

Sample Output 1

11

For example, the performance can be done as follows:

  • Initially, place the left hand on key 33 and the right hand on key 66.
  • Press key 33 with the left hand.
  • Press key 66 with the right hand.
  • Move the left hand from key 33 to key 99. The fatigue level increases by 93=6|9-3| = 6.
  • Move the right hand from key 66 to key 11. The fatigue level increases by 16=5|1-6| = 5.
  • Press key 99 with the left hand.
  • Press key 11 with the right hand.

In this case, the fatigue level at the end of the performance is 6+5=116+5 = 11, which is the minimum possible.

Sample Input 2

3
2 L
2 L
100 L

Sample Output 2

98

Sample Input 3

8
22 L
75 L
26 R
45 R
72 R
81 R
47 L
29 R

Sample Output 3

188
}