Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define codefor                                                                \
  int test;                                                                    \
  cin >> test;                                                                 \
  while (test--)
#define vec_input(v)                                                           \
  for (auto it = v.begin(); it != v.end(); it++) {                             \
    cin >> *it;                                                                \
  }
#define vec_output(v)                                                          \
  for (auto it = v.begin(); it != v.end(); it++) {                             \
    if (it != v.begin())                                                       \
      cout << " ";                                                             \
    cout << *it;                                                               \
  }                                                                            \
  cout << endl;
#define vec_debug(v)                                                           \
  for (auto it = v.begin(); it != v.end(); it++) {                             \
    cerr << *it << " ";                                                        \
  }                                                                            \
  cerr << endl;
#define yn(ans) cout << (ans ? "Yes" : "No") << endl
#define YN(ans) cout << (ans ? "YES" : "NO") << endl
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;

int main() {
  string s;
  cin >> s;
  ll ans = 0, l = 0;
  rep(i, s.length() - 1) {
    while (s[i] == 'B' && s[i + 1] == 'W') {
      ans++;
      s[i] = 'W';
      s[i + 1] = 'B';
      i--;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define codefor                                                                \
  int test;                                                                    \
  cin >> test;                                                                 \
  while (test--)
#define vec_input(v)                                                           \
  for (auto it = v.begin(); it != v.end(); it++) {                             \
    cin >> *it;                                                                \
  }
#define vec_output(v)                                                          \
  for (auto it = v.begin(); it != v.end(); it++) {                             \
    if (it != v.begin())                                                       \
      cout << " ";                                                             \
    cout << *it;                                                               \
  }                                                                            \
  cout << endl;
#define vec_debug(v)                                                           \
  for (auto it = v.begin(); it != v.end(); it++) {                             \
    cerr << *it << " ";                                                        \
  }                                                                            \
  cerr << endl;
#define yn(ans) cout << (ans ? "Yes" : "No") << endl
#define YN(ans) cout << (ans ? "YES" : "NO") << endl
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;

int main() {
  string s;
  cin >> s;
  ll ans = 0, l = 0;
  rep(i, s.length()) {
    if (s[i] == 'W') {
      ans += i - l;
      l++;
    }
  }
  cout << ans << endl;
}