atcoder#AGC015B. [AGC015B] Evilator

[AGC015B] Evilator

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 \leq |S| \leq 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.

UUD
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.

UUDUUDUD
77