Problem:
Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

1 \leq |S| \leq 2\times 10^5

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6

Solution in Go (1.14.1) (judge verdict: Time Limit Exceeded)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextFloat64() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func nextString() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(nil, 100000000)

	s := nextString()
	n := len(s)

	ss := make([]int, n)
	for i, v := range s {
		if v == 'B' {
			ss[i] = 1 // Black
		} else {
			ss[i] = 0 // White
		}
	}

	tmp := 0
	ans := 0
	for i := 0; i < n; i++ {

		if ss[i] == 1 {
			if tmp == -1 {
				tmp = i
			}
		} else {
			if tmp > -1 {
				l := ss[0:tmp]
				m := ss[tmp : i+1]

				r := make([]int, len(ss)-i-1)
				copy(r, ss[i+1:])

				m = append(m[len(m)-1:], m[:len(m)-1]...)

				ans += len(m) - 1
				i = i - (len(m) + 1)
				if i < 0 {
					i = 0
				}

				mid := append(m, r...)

				ss = append(l, mid...)
			}
			tmp = -1
		}
	}
	fmt.Println(ans)
}