Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
高橋君は、横一列に並んだ 個の鍵盤からなるピアノを持っています。 このピアノの左から 個目の鍵盤のことを鍵盤 と呼びます。
高橋君は今から 回にわたってこのピアノの鍵盤を一つずつ押すことでとある曲を演奏します。 回目に押す鍵盤は鍵盤 であり、それを押す手は L
のとき左手、 R
のとき右手です。
演奏を始める前、高橋君は両手をそれぞれ好きな鍵盤の上に置くことができ、この時点での疲労度は です。 演奏中、片方の手を鍵盤 の上から鍵盤 の上へと動かすと疲労度が 増加します(逆に、手の移動以外で疲労度が増加することはありません)。 なお、ある手である鍵盤を押すためには、その手がその鍵盤の上に置かれている必要があります。
演奏が終了した時点での疲労度は最小でいくつになるか求めてください。
制約
- は整数
- は
L
またはR
入力
入力は以下の形式で標準入力から与えられる。
出力
演奏が終了した時点での疲労度の最小値を出力せよ。
入力例 1Copy
4 3 L 6 R 9 L 1 R
出力例 1Copy
11
例えば以下のように演奏することができます。
- 最初、左手を鍵盤 の上に、右手を鍵盤 の上に置いておく。
- 左手で鍵盤 を押す。
- 右手で鍵盤 を押す。
- 左手を鍵盤 の上から鍵盤 の上へと動かす。疲労度が 増加する。
- 右手を鍵盤 の上から鍵盤 の上へと動かす。疲労度が 増加する。
- 左手で鍵盤 を押す。
- 右手で鍵盤 を押す。
このとき、演奏が終了した時点での疲労度は であり、これが最小です。
入力例 2Copy
3 2 L 2 L 100 L
出力例 2Copy
98
入力例 3Copy
8 22 L 75 L 26 R 45 R 72 R 81 R 47 L 29 R
出力例 3Copy
188
Score : points
Problem Statement
Takahashi has a piano with keys arranged in a row. The -th key from the left is called key .
He will play music by pressing keys one by one. For the -th press, he will press key , using his left hand if L
, and his right hand if 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 to key , the fatigue level increases by (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
- and are integers.
- is
L
orR
.
Input
The input is given from Standard Input in the following format:
Output
Print the minimum fatigue level at the end of the performance.
Sample Input 1Copy
4 3 L 6 R 9 L 1 R
Sample Output 1Copy
11
For example, the performance can be done as follows:
- Initially, place the left hand on key and the right hand on key .
- Press key with the left hand.
- Press key with the right hand.
- Move the left hand from key to key . The fatigue level increases by .
- Move the right hand from key to key . The fatigue level increases by .
- Press key with the left hand.
- Press key with the right hand.
In this case, the fatigue level at the end of the performance is , which is the minimum possible.
Sample Input 2Copy
3 2 L 2 L 100 L
Sample Output 2Copy
98
Sample Input 3Copy
8 22 L 75 L 26 R 45 R 72 R 81 R 47 L 29 R
Sample Output 3Copy
188