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 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
/* void swap(int* a, int* b) { */
/*   int tmp; */
/*   tmp = *a; */
/*   *a = b; */
/*   *b = tmp; */
/* } */

int main(){
  //long long int N, M,i,j,Q,k, A, B, T;
  long long int x[20] ={0};
  long long int v[20] ={0};
  long long int ans[20] ={0};
  int flag = 0;
  //long long int K, S, cnt=0, cnt2=0;
  char str[120] = {0};
  long long int len[120];
  int lenstr;
  long long int L[200000];
  long long int c[200000];
  long long int t[100000];
  int i;
  char S[300000] = {0};
  int SS[300000] = {0};
  //long long int A[100000];
  long long int cnt = 0;
  int max, min, min_cost=9999;
  scanf("%s", S);
  int N = strlen(S);
  //printf("%d\n", N);
  for (i = 0; i < N; i++) {
    switch(S[i]) {
    case 'W':
      SS[i] = 1;
      break;
    case 'B':
      SS[i] = 0;
      break;
    default:
      puts("error");
      return -1;
    }
  }

  for (i = 0; i < N-1; i++) {
    //printf("i : %d\n", i);
    if (SS[i] == 0 && SS[i+1] == 1) {
      SS[i] = 1;
      SS[i+1] = 0;
      cnt++;
      //printf("%d, %d\n", i, i+1);
      if (i >= 2) {
        i -= 2;
      }
      else {
        i = -1;
      }
    }
  }
  printf("%d\n", cnt);
  
  //scanf("%lld", &N);
  /* scanf("%lld%lld", &N, &T); */

  /* for (i = 0; i < N; i++) { */
  /*   scanf("%lld%lld", &c[i], &t[i]); */
  /* } */
  /* for (i = 0; i < N; i++) { */
  /*   if (T >= t[i]) { */
  /*     if (min_cost > c[i]) { */
  /*       min_cost = c[i];  */
  /*     } */

  /*   } */

  /* } */
  /* if (min_cost != 9999) { */
  /* printf("%d\n", min_cost); */
  /* }else{ */
  /*   puts("TLE"); */
  /* } */
  /* /\* if (N == 2) { *\/ */
  /* /\*   scanf("%lld%lld", &A, &B); *\/ */

  /* /\*   printf("%lld\n", A+B); *\/ */

  /* } */
  /* else {puts("Hello World");} */
  
  /* for (i = 0; i < N; i++) { */
  /*   scanf("%lld", &A[i]); */
  /* } */

  /* for (k = 0; k < N; k++) { */
  /*   min =  100000; */
  /*   max = -100000;   */
  /*   for (i = 0; i < N; i++) { */
  /*     if (i == k) ; */
  /*     else { */
  /*       if (A[i] < min) min = A[i]; */
  /*       if (A[i] > max) max = A[i]; */
  /*     } */
  /*   } */

  /*   //printf("%d, %d\n", min, max); */

  /*   if (min >= 0) { */
  /*     printf("%d\n", max*2); */
  /*   } */
  /*   else if (max < 0) { */
  /*     printf("%d\n", min*2); */
  /*   } */
  /*   else { */
  /*     printf("%d\n", 2*(max-min)); */
  /*   } */
    
  /* } */
  //printf("%d, %d\n", min, max);

  
  /*  scanf("%lld", &M); */
  /*  scanf("%lld", &Q); */
  
 /*  for (i = 0; i < M; i++) { */
 /*    scanf("%lld", &L[i]); */
 /*    scanf("%lld", &R[i]); */
 /*  } */
 /*  for (i = 0; i < Q; i++) { */
 /*    scanf("%lld", &p[i]); */
 /*    scanf("%lld", &q[i]); */
 /*  } */



 /*  for (i = 0; i < Q; i++) { */
 /*    for(j = 0; j < M; j++) { */
 /*      if (p[i] <= L[j]) { */
 /*        if (R[j] <= q[i]) { */
 /*          cnt++; */
 /*        } */
 /*      } */
 /*    } */
 /*    printf("%lld\n", cnt); */
 /*    cnt = 0; */
 /*  } */
 /* /\*  for (i  = 0; i < 26; i++) { */
 /*   for (j = 0; j < 15; j++) { */
 /*     if ((4*i) + (7*j) == N) { */
 /*       printf("Yes\n"); */
 /*       goto L1; */
 /*     } */
 /*   } */
 /*  } */
 /*  printf("No\n"); */
 /* L1: */
  /* scanf("%lld", &K); */

  /* if (N % K == 0) printf("%lld\n", 0); */
  /* else {printf("1\n");} */
  /* for(i=0; i < N; i++) { */
  /*   scanf("%lld", &x[i]); */
  /* } */
  /* for(i=0; i < N; i++) { */
  /*   scanf("%lld", &c[i]); */
  /* } */
  /* for(i=0; i < N; i++) { */
  /*   scanf("%lld", &v[i]); */
  /* } */
  /* long long int sum=0; */
  /* for(i=0; i < N; i++) { */
  /*   sum += x[i]; */
  /* } */
  /* for(i=0; i < N; i++) { */
  /*   if (sum < c[i]) { */
  /*     v[i] = 0; */
  /*   } */
  /* } */


  /* for (i=0; i<N-1; i++) { */
  /*   int s; */
  /*   puts(""); */
  /*   for(s=0; s < N; s++) { */
  /*     printf("%lld ", x[s]); */
  /*   } */
  /*   puts(""); */
  /*   for(s=0; s < N; s++) { */
  /*     printf("%lld ", c[s]); */
  /*   } */
  /*   puts(""); */
  /*   for(s=0; s < N; s++) { */
  /*     printf("%lld ", v[s]); */
  /*   } */
  /*   puts(""); */
    
  /*   flag = 1; */
  /*   long long int aa, bb, cc; */
  /*   aa = Takahashi; */
  /*   while (flag == 1) {//購入 */
  /*     int j, t=0; */
  /*     flag = 0; */
  /*     for (j = 0; j < N; j++) { */
  /*       if (v[t] <= v[j] && Takahashi - c[j] >= 0 && v[j] != 0) { */
  /*         t = j; */
  /*         flag=1; */
  /*       } */
  /*     } */
  /*     //tは購入できる一番価値の高い販売中の賞品の添 */
  /*     //flag：購入できるものがあったら1 */
  /*     if (flag == 1 ){ */
  /*       Takahashi -= c[t]; */
  /*       ans[i] += v[t]; */
  /*       c[t] = v[t] = 0; */
  /*     } */
  /*   } */

  /*   Takahashi = aa; */
  /*   Takahashi += x[i]; */
  /*   x[i] = 0; */
  /*   printf("Takahasji : %lld\n", Takahashi); */

    
  /*   flag = 0; */
  /*   int j,t=0; */
  /*   for (j = 0; j < N; j++) { */
  /*     if (Takahashi >= c[j] && v[t] <= v[j]) { */
  /*       t = j; */
  /*       flag = 1; */
  /*     } */
  /*   } */
  /*   if (flag == 0) { */
  /*     for (j = 0; j < N; j++) { */
  /*       if (v[t] <= v[j]) { */
  /*         t = j; */
  /*         flag = 1; */
  /*       } */
  /*     } */
  /*   } */
  /*   if (flag == 0) puts("error"); */
  /*   printf("中止 : [%d]%lld\n", t, c[t]); */
  /*   v[t] = c[t] = 0; */
  /* } */

  /* int t = 0; */
  /* for (i = 0; i < N; i++) { */
  /*   printf("%d\n", ans[i]); */
  /*   if (ans[t] < ans[i]) { */
  /*     t=i; */
  /*   } */
  /* } */
  /* printf("%lld\n", ans[t]); */
  
  /* /\* scanf("%lld%lld", &X, &K); *\/ */
  /* /\* T=1; *\/ */
  /* /\* for (i = 0; i < K; i++) { *\/ */
  /* /\*   T*=10; *\/ */
  /* /\* } *\/ */
  /* /\* L=X/T; *\/ */
  /* /\* L++; *\/ */
  /* /\* L*=T; *\/ */
  /* /\* printf("%lld\n", L); *\/ */




  //long long int X, A, B, C;
  /* //long long int i, j, k, cnt = 0; */
  /* long long int N, max; */
  /* long long int a[2][110]; */


  /* int flag = 0; */
  /* int i; */
  /* char AA[10]; */
  /* scanf("%s", AA); */
  /* for (i = 0; i < 5; i++) { */

  /*   switch(i) { */
  /*   case 0: */
  /*     if (AA[i] != 'y') { */
  /*       flag = -1; */
  /*     } */
  /*     break; */
  /*   case 1: */
  /*     if (AA[i] != 'a') { */
  /*       flag = -1; */
  /*     } */
  /*     break; */
  /*   case 2: */
  /*     if (AA[i] != 'h') { */
  /*       flag = -1; */
  /*     } */
  /*     break; */

  /*   case 4: */
  /*     if (AA[i-1] != AA[i]) { */
  /*       flag = -1; */
  /*     } */
  /*   } */
  /* } */

  /* if (flag == -1) {puts("NO");} */
  /* else {puts("YES");} */
  /* scanf("%lld", &N); */

  /* for (i = 0; i < 2; i++) { */
  /*   for (j = 0; j < N; j++) { */
  /*     scanf("%lld", &a[i][j]); */
  /*   } */
  /* } */

  /* for (i = 1; i <= N; i++) { */
  /*   cnt = 0; */
  /*   for (j = 0; j < i; j++) { */
  /*     cnt += a[0][j]; */
  /*   } */
  /*   for (k = j-1; k < N; k++) { */
  /*     cnt += a[1][k]; */
  /*   } */
  /*   if (cnt > max) {max = cnt;} */
  /*   //printf("aa%lld\n", cnt); */
  /* } */
  
  /* printf("%lld\n", max); */
  
  /* scanf("%ld%ld%ld%ld", &A, &B, &C, &X); */
  /* for (i = 0; i <= A; i++) { */
  /*   for (j = 0; j <= B; j++) { */
  /*     for (k = 0; k <= C; k++) { */
  /*       if (500*i + 100*j + 50*k == X) {cnt++;} */
  /*     } */
  /*   } */
  /* } */
  //printf("%d\n", cnt);
  return 0;
}