B - Evilator Editorial /

Time Limit: 2 sec / Memory Limit: 256 MiB

配点 : 400400

問題文

すけぬ君は、NN 階建てのビルを建てました。ビルにはエレベーターが 11 基あり、すべての階に止まります。

すけぬ君は、各階に上下の方向ボタンを設置しましたが、うっかりしていたため、どの階にも上向きか下向きの片方のボタンしかありません。 そのため、どの階からも上か下のどちらかにしか進むことができません。 SiS_iU のとき ii 階には上向きのボタンしかなく、上にしか進めないことを、D のとき下向きのボタンしかなく、 下にしか進めないことを表します。

ある階から目的の階へと移動したい住民は、仕方がないので必要があれば他の階を経由して目的の階へと向かうことにしました。 全ての階の順序対 (i,j)(i,j) についての、ii 階から jj 階へ行くときのエレベーターに乗る回数の最小値の合計を求めてください。

制約

  • 2S1052 ≦ |S| ≦ 10^5
  • SiS_iU または D である
  • S1S_1U である
  • SSS_{|S|}D である

入力

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

S1S2...SSS_1S_2...S_{|S|}

出力

全ての階の順序対 (i,j)(i,j) についての、ii 階から jj 階へ行くときのエレベーターに乗る回数の最小値の合計を出力せよ。


入力例 1Copy

Copy
UUD

出力例 1Copy

Copy
7

11 階から 22 階までは、11 回エレベーターに乗れば行くことができます。

11 階から 33 階までは、11 回エレベーターに乗れば行くことができます。

22 階から 11 階までは、22 回エレベーターに乗れば行くことができます。

22 階から 33 階までは、11 回エレベーターに乗れば行くことができます。

33 階から 11 階までは、11 回エレベーターに乗れば行くことができます。

33 階から 22 階までは、11 回エレベーターに乗れば行くことができます。

これらの合計は 77 なので、77 を出力します。


入力例 2Copy

Copy
UUDUUDUD

出力例 2Copy

Copy
77

Score : 400400 points

Problem Statement

Skenu constructed a building that has NN floors. The building has an elevator that stops at every floor.

There are buttons to control the elevator, but Skenu thoughtlessly installed only one button on each floor - up or down. This means that, from each floor, one can only go in one direction. If SiS_i is U, only "up" button is installed on the ii-th floor and one can only go up; if SiS_i is D, only "down" button is installed on the ii-th floor and one can only go down.

The residents have no choice but to go to their destination floors via other floors if necessary. Find the sum of the following numbers over all ordered pairs of two floors (i,j)(i,j): the minimum number of times one needs to take the elevator to get to the jj-th floor from the ii-th floor.

Constraints

  • 2S1052 ≤ |S| ≤ 10^5
  • SiS_i is either U or D.
  • S1S_1 is U.
  • SSS_{|S|} is D.

Input

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

S1S2...SSS_1S_2...S_{|S|}

Output

Print the sum of the following numbers over all ordered pairs of two floors (i,j)(i,j): the minimum number of times one needs to take the elevator to get to the jj-th floor from the ii-th floor.


Sample Input 1Copy

Copy
UUD

Sample Output 1Copy

Copy
7

From the 11-st floor, one can get to the 22-nd floor by taking the elevator once.

From the 11-st floor, one can get to the 33-rd floor by taking the elevator once.

From the 22-nd floor, one can get to the 11-st floor by taking the elevator twice.

From the 22-nd floor, one can get to the 33-rd floor by taking the elevator once.

From the 33-rd floor, one can get to the 11-st floor by taking the elevator once.

From the 33-rd floor, one can get to the 22-nd floor by taking the elevator once.

The sum of these numbers of times, 77, should be printed.


Sample Input 2Copy

Copy
UUDUUDUD

Sample Output 2Copy

Copy
77


2025-06-22 (Sun)
03:24:37 +00:00