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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

string S;

//[l,r)
int f(int l, int r) {
    if (l + 1 == r)return 0;
    if (l + 2 == r) {
        if (S[l] == 'B' && S[l + 1] == 'W') {
            S[l] = 'W';
            S[l + 1] = 'B';
            return 1;
        }
        return 0;
    }
    int m = (l + r) / 2;
    int ans = 0;
    if (S[m - 1] == 'B' && S[m] == 'W') {
        S[m - 1] = 'W';
        S[m] = 'B';
        ans += 1;
    }
    ans += f(l, m) + f(m, r);
    if (S[m - 1] == 'B' && S[m] == 'W') {
        S[m - 1] = 'W';
        S[m] = 'B';
        ans += 1;
        int r1 = m - 2;
        for (int r1 = m - 2; r1 >= l; --r1) {
            if (S[r1] == 'B')ans++;
            else break;
        }
        for (int r2 = m + 1; r2 < r; ++r2) {
            if (S[r2] == 'W')ans++;
            else break;
        }
    }
    return ans;
}

signed main() {
    cin >> S;
    cout << f(0, S.size()) << endl;
    return 0;
}