B - Piano 3 Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200200

問題文

高橋君は、横一列に並んだ 100100 個の鍵盤からなるピアノを持っています。 このピアノの左から ii 個目の鍵盤のことを鍵盤 ii と呼びます。

高橋君は今から NN 回にわたってこのピアノの鍵盤を一つずつ押すことでとある曲を演奏します。 ii 回目に押す鍵盤は鍵盤 AiA_i であり、それを押す手は Si=S_i= L のとき左手、Si=S_i= R のとき右手です。

演奏を始める前、高橋君は両手をそれぞれ好きな鍵盤の上に置くことができ、この時点での疲労度00 です。 演奏中、片方の手を鍵盤 xx の上から鍵盤 yy の上へと動かすと疲労度が yx|y-x| 増加します(逆に、手の移動以外で疲労度が増加することはありません)。 なお、ある手である鍵盤を押すためには、その手がその鍵盤の上に置かれている必要があります。

演奏が終了した時点での疲労度は最小でいくつになるか求めてください。

制約

  • 1N1001\leq N \leq 100
  • 1Ai1001\leq A_i \leq 100
  • N,AiN,A_i は整数
  • SiS_iL または R

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 S1S_1
A2A_2 S2S_2
\vdots
ANA_N SNS_N

出力

演奏が終了した時点での疲労度の最小値を出力せよ。


入力例 1Copy

Copy
4
3 L
6 R
9 L
1 R

出力例 1Copy

Copy
11

例えば以下のように演奏することができます。

  • 最初、左手を鍵盤 33 の上に、右手を鍵盤 66 の上に置いておく。
  • 左手で鍵盤 33 を押す。
  • 右手で鍵盤 66 を押す。
  • 左手を鍵盤 33 の上から鍵盤 99 の上へと動かす。疲労度が 93=6|9-3|=6 増加する。
  • 右手を鍵盤 66 の上から鍵盤 11 の上へと動かす。疲労度が 16=5|1-6|=5 増加する。
  • 左手で鍵盤 99 を押す。
  • 右手で鍵盤 11 を押す。

このとき、演奏が終了した時点での疲労度は 6+5=116+5=11 であり、これが最小です。


入力例 2Copy

Copy
3
2 L
2 L
100 L

出力例 2Copy

Copy
98

入力例 3Copy

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

出力例 3Copy

Copy
188

Score : 200200 points

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

Copy
4
3 L
6 R
9 L
1 R

Sample Output 1Copy

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

Copy
3
2 L
2 L
100 L

Sample Output 2Copy

Copy
98

Sample Input 3Copy

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

Sample Output 3Copy

Copy
188


2024-09-12 (Thu)
02:41:43 +09:00