prithivMLmods/Coder-Stat huggingface dataset:
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;

void solve() {
  const int N = s.length();
  int ans = 0;
  while (true) {
    bool f = false;
    for (int i = 0; i + 1 < N; ++i)
      if (s[i] == 'B' && s[i + 1] == 'W') {
        swap(s[i], s[i + 1]);
        f = true;
        ans++;
        break;
      }
    if (!f)
      break;
  }
  cout << ans << endl;
}

int main() {
  cin >> s;
  solve();
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;

void solve() {
  const int N = s.length();
  long long ans = 0;
  int wcnt = 0;
  for (int i = N - 1; i >= 0; --i) {
    if (s[i] == 'B')
      ans += wcnt;
    else
      wcnt++;
  }
  cout << ans << endl;
}

int main() {
  cin >> s;
  solve();
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s, c;
  cin >> s;
  int count = 0;
  do {
    c = s;
    int i = 0;
    while (i < (int)s.length() - 1) {
      if (s[i] == 'B') {
        if (s[i + 1] == 'W') {
          s[i + 1] = 'B';
          s[i] = 'W';
          count++;
          i = i + 1;
        } else
          i = i + 1;
      } else
        i = i + 1;
      // cout << s << endl;
    }
  } while (c != s);
  cout << count;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s, c;
  cin >> s;
  long long count = 0, gcount = 0;
  for (int i = 0; i < (int)s.length(); i++) {
    if (s[i] == 'B')
      count++;
    else
      gcount += count;
  }
  cout << gcount;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mp make_pair
#define pb push_back
#define inf INT_MAX
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define mod INT_MAX
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  IOS string s;
  cin >> s;
  int cntb = 0, sum = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'W')
      sum += cntb;
    else
      cntb++;
  }
  cout << sum;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mp make_pair
#define pb push_back
#define inf INT_MAX
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define mod INT_MAX
int32_t main() {

  IOS string s;
  cin >> s;
  int cntb = 0, sum = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'W')
      sum += cntb;
    else
      cntb++;
  }
  cout << sum;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll ans = 0;
  string s;
  cin >> s;
  ll pos = 0;
  rep(i, 0, s.length()) {
    ll cntb = 0, cntw = 0, tmp = 0;
    if (s[i] == 'B') {
      while (s[i] == 'B') {
        tmp++;
        cntb++;
        i++;
      }
      while (s[i] == 'W') {
        cntw++;
        i++;
      }
    }
    // cout<<pos<<" "<<cntb<<" "<<cntw<<endl;
    ans += (cntb + pos) * cntw;
    pos += tmp;
    i--;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll ans = 0;
  string s;
  cin >> s;
  ll pos = 0;
  rep(i, 0, s.length()) {
    ll cntb = 0, cntw = 0, tmp = 0;
    if (s[i] == 'B') {
      while (s[i] == 'B') {
        tmp++;
        cntb++;
        i++;
      }
      while (s[i] == 'W') {
        cntw++;
        i++;
      }
    } else
      continue;
    // cout<<pos<<" "<<cntb<<" "<<cntw<<endl;
    ans += (cntb + pos) * cntw;
    pos += tmp;
    i--;
  }
  cout << ans;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  string N;
  cin >> N;
  ll sl = N.length();
  ll sw = 1, count = 0;
  while (sw == 1) {
    sw = 0;
    froop1(0, sl - 1) {
      if (N[i] == 'B' && N[i + 1] == 'W') {
        count++;
        N[i] = 'W';
        N[i + 1] = 'B';
        sw = 1;
      }
    }
  }
  cout << count << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  string N;
  cin >> N;
  ll sl = N.length();
  ll count = 0, c1 = 0;
  froop1(0, sl) {
    if (N[i] == 'B')
      c1++;
    else
      count += c1;
  }
  cout << count << endl;

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i, a, b) for (int i = a; i <= b; i++)

#define MOD 1000000007

void solve(int test_case) { // 220
  string s;
  cin >> s;
  int cnt = 0;
  while (1) {
    int ccnt = 0;
    for (int i = 0; i < (int)s.length() - 1; i++) {
      if (s[i] == 'B' && s[i + 1] == 'W')
        ccnt++, s[i] = 'W', s[i + 1] = 'B';
    }
    cnt += ccnt;
    if (ccnt == 0)
      break;
  }
  cout << cnt;
}

int main() {

  ////// FILE BASED IO////
  // freopen("wormhole.in", "r", stdin);
  // freopen("wormhole.out", "w", stdout);
  ///////////////

  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  REP(i, 1, t) { solve(i); }
  return 0;
}


Fixed submission:
/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i, a, b) for (int i = a; i <= b; i++)

#define MOD 1000000007

void solve(int test_case) { // 220
  string s;
  cin >> s;
  ll cnt = 0;
  int b = 0;
  REP(i, 0, s.length() - 1) {
    if (s[i] == 'W')
      cnt += b;
    else
      b++;
  }
  cout << cnt;
}

int main() {

  ////// FILE BASED IO////
  // freopen("wormhole.in", "r", stdin);
  // freopen("wormhole.out", "w", stdout);
  ///////////////

  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  REP(i, 1, t) { solve(i); }
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#define ll long long
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii, int>
#define rep(i, n) for (int(i) = (0); i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define mod (1000000000 + 7)
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define ss second
#define ff first
using namespace std;

int lg2(ll n) {
  int res = -1;
  while (n) {
    n >>= 1;
    res++;
  }
  return res;
}

int main() {
  // HOW DO I REDUCE THIS TO O(Lg(N)) ???
  ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  ll ans = 0;
  std::vector<int> pos;
  ll n = s.length();
  rep(i, s.length()) if (s[i] == 'W') pos.pb(n - i);
  sort(all(pos));
  reverse(all(pos));
  /*for(auto it : pos)
          cout << it << " " ;
  cout << endl ;*/
  rep(i, pos.size()) ans += (n-- - pos[i]);
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#define ll long long
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii, int>
#define rep(i, n) for (int(i) = (0); i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define mod (1000000000 + 7)
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define ss second
#define ff first
using namespace std;

int lg2(ll n) {
  int res = -1;
  while (n) {
    n >>= 1;
    res++;
  }
  return res;
}

int main() {
  // HOW DO I REDUCE THIS TO O(Lg(N)) ???
  // ios::sync_with_stdio(0);
  string s;
  cin >> s;
  ll ans = 0;
  std::vector<int> pos;
  ll n = s.length();
  rep(i, s.length()) if (s[i] == 'W') pos.pb(n - i);
  sort(all(pos));
  reverse(all(pos));
  /*for(auto it : pos)
          cout << it << " " ;
  cout << endl ;*/
  rep(i, pos.size()) ans += (n-- - pos[i]);
  cout << ans;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
const double pi = acos(-1.0);
const ll mod = pow(10, 9) + 7;
const ll INF = pow(2, 31) - 1;
typedef pair<int, int> P;
typedef vector<int> vi;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  ll lc = (a / g) * b;
  return lc;
}

int main() {
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int ans = 0;
  while (s.find("BBW") != std::string::npos) {
    int place0 = s.find("BBW");
    s.replace(place0, 3, "WBB");
    ans += 2;
  }
  while (s.find("BW") != std::string::npos) {
    int place = s.find("BW");
    s.replace(place, 2, "WB");
    ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
const double pi = acos(-1.0);
const ll mod = pow(10, 9) + 7;
const ll INF = pow(2, 31) - 1;
typedef pair<int, int> P;
typedef vector<int> vi;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  ll lc = (a / g) * b;
  return lc;
}

int main() {
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  ll ans = 0;
  ll cnt = 0;
  rep(i, s.size()) {
    if (s[i] == 'W') {
      ans += i - cnt;
      cnt++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <tuple>
using namespace std;

typedef long long int ll;
int main() {
  string S;
  cin >> S;
  int n = S.size();
  ll ans = 0;
  bool frag = 1;
  for (int i = 0; i < 10000; i++) {
    for (int j = 0; j < n; j++) {
      if (S[j] == 'B' && S[j + 1] == 'W') {
        S[j] = 'W';
        S[j + 1] = 'B';
        // cout << S << endl;
        ans++;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <tuple>
using namespace std;

typedef long long int ll;
int main() {
  string S;
  cin >> S;
  int n = S.size();
  ll ans = 0;
  vector<int> cnt(n);
  for (int i = 0; i < n - 1; i++) {
    if (S[i] == 'B') {
      cnt[i + 1] = cnt[i] + 1;
    } else {
      cnt[i + 1] = cnt[i];
    }
  }
  for (int i = 0; i < n; i++) {
    if (S[i] == 'W') {
      ans += cnt[i];
    }
  }
  cout << ans << endl;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;

int main() {
  string s;
  cin >> s;

  LL cnt = 0;
  for (int i = 1; i < s.size(); i++) {
    if (s.at(i) == 'W') {
      for (int j = 0; j < i; j++) {
        if (s.at(j) == 'B') {
          cnt++;
        }
      }
    }
  }

  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;

int main() {
  string s;
  cin >> s;

  LL cnt = 0, bcnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == 'W' && i > 0) {
      cnt += bcnt;
    } else if (s.at(i) == 'B') {
      bcnt++;
    }
  }

  cout << cnt << endl;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  string S;
  cin >> S;
  int num = 0;
  for (int i = 0; i < S.size() - 1; ++i) {
    if (S[i] == 'B' && S[i + 1] == 'W') {
      S[i] = 'W';
      S[i + 1] = 'B';
      ++num;
      i -= 2;
      if (i == -2)
        ++i;
    }
  }
  cout << num << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  string S;
  cin >> S;
  long long num = 0;
  long long numW = 0;
  for (long long i = 0; i < S.size(); ++i) {
    if (S[i] == 'B') {

    } else {
      num += (i - numW);
      ++numW;
    }
  }
  cout << num << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using lli = long long int;
ll MM = 1000000000;
ll mod = MM + 7;
#define INF (ll)1e18
#define pi acos(-1.0)
int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vector<ll> a;
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'B')
      a.push_back(i + 1);
  }
  reverse(a.begin(), a.end());
  for (ll i = 0; i < n; i++) {
    cnt += n - a[i];
    n--;
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using lli = long long int;
ll MM = 1000000000;
ll mod = MM + 7;
#define INF (ll)1e18
#define pi acos(-1.0)
int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vector<ll> a;
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'B')
      a.push_back(i + 1);
  }
  reverse(a.begin(), a.end());
  for (ll i = 0; i < a.size(); i++) {
    cnt += n - a[i];
    n--;
  }
  cout << cnt << endl;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <iostream>

using namespace std;

int main() {
  string s;
  cin >> s;
  int c = 0, change;
  do {
    change = 0;
    for (int i = 0, n = s.length(); i < n - 1; i++) {
      if (s[i] == 'B' && s[i + 1] == 'W') {
        s[i] = 'W';
        s[i + 1] = 'B';
        c++;
        change++;
      }
    }
  } while (change);

  cout << c << endl;
}


Fixed submission:

#include <iostream>

using namespace std;

int main() {
  string s;
  cin >> s;
  unsigned long long c = 0, cb = 0;
  for (int i = 0, n = s.length(); i < n; i++) {
    if (s[i] == 'B')
      cb++;
    else
      c += cb;
  }
  cout << c << endl;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template <typename T> void printv(vector<T> const &s) {
  REP(i, SZ(s)) { cout << s[i] << " "; }
  cout << endl;
}

int main() {
  cin.tie(0);
  cout << setprecision(10);

  string s;
  cin >> s;
  int cnt = 0;
  bool upd = true;
  while (upd) {
    upd = false;
    REP(i, SZ(s) - 1) {
      if (s[i] == 'B' && s[i + 1] == 'W') {
        s[i] = 'W';
        s[i + 1] = 'B';
        cnt++;
        upd = true;
      }
    }
    FOR(i, SZ(s) - 1, 1) {
      if (s[i] == 'W' && s[i - 1] == 'B') {
        s[i] = 'B';
        s[i - 1] = 'W';
        cnt++;
        upd = true;
      }
    }
  }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template <typename T> void printv(vector<T> const &s) {
  REP(i, SZ(s)) { cout << s[i] << " "; }
  cout << endl;
}

int main() {
  cin.tie(0);
  cout << setprecision(10);

  string s;
  cin >> s;
  ll cnt = 0;
  ll cntb = 0;
  RFOR(i, SZ(s) - 1, 0) {
    if (s[i] == 'B') {
      cnt += SZ(s) - 1 - i - cntb;
      cntb++;
    }
  }
  cout << cnt << endl;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char s[12000];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  int tot = 1;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'W') {
      ans += i - tot;
      tot++;
    }
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char s[1200000];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  int tot = 1;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'W') {
      ans += i - tot;
      tot++;
    }
  }
  printf("%lld\n", ans);
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 100000
#define LL long long
int n;
char s[MAXN + 5];
int cnt;
LL ans;
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'W')
      cnt++, ans += i - cnt;
  }
  printf("%lld", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 500000
#define LL long long
int n;
char s[MAXN + 5];
int cnt;
LL ans;
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'W')
      cnt++, ans += i - cnt;
  }
  printf("%lld", ans);
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <list>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;

  cin >> s;

  list<pair<char, int>> l;
  int len = s.length();

  int i = 0;
  while (i < len) {
    int c = 0;
    char tmp = s[i];
    while (s[i] == tmp) {
      c++;
      i++;
    }
    l.push_back(pair<char, int>(tmp, c));
  }
  l.pop_front();
  l.pop_back();

  int cnt = 0;
  int tmpb = 0;

  for (auto p = l.begin(); p != l.end(); p++) {
    if (p->first == 'B') {
      tmpb += p->second;
    } else {
      cnt += tmpb;
    }
  }

  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <list>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;

  cin >> s;

  unsigned long long tmpb = 0;
  unsigned long long cnt = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'B') {
      tmpb++;
    } else {
      cnt += tmpb;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define rREP(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  string s;
  cin >> s;
  bool is = true;
  int ans = 0;
  while (is) {
    is = false;
    rep(i, s.size() - 1) {
      if (s[i] == 'B' && s[i + 1] == 'W') {
        swap(s[i], s[i + 1]);
        ++ans;
        is = true;
      }
    }
  }
  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define rREP(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  string s;
  cin >> s;
  ll w = 0, ans = 0;
  rep(i, s.size()) {
    if (s[i] == 'W') {
      ans += i - w;
      ++w;
    }
  }
  cout << ans << "\n";

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

int main() {
#ifdef ONPC
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int n = (int)s.size();
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (i + 1 < n && s[i] == 'B' && s[i + 1] == 'W') {
      s[i] = 'W', s[i + 1] = 'B';
      i = max(-1, i - 2);
      ans++;
    }
  }
  cout << ans << '\n';
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

int main() {
#ifdef ONPC
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int n = (int)s.size();
  int cnt = 0;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'B') {
      ans += n - i - cnt - 1;
      cnt++;
    }
  }
  cout << ans << '\n';
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  string s;
  cin >> s;
  int cnt = 0;
  bool a = true;
  while (a) {
    a = false;
    rep(i, s.size()) {
      if (s[i] == 'B' && s[i + 1] == 'W') {
        s[i] = 'W';
        s[i + 1] = 'B';
        cnt++;
        a = true;
      }
    }
  }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  string s;
  cin >> s;
  ll cnt = 0;
  int Bcount = 0;
  rep(i, s.size()) {
    if (s[i] == 'B')
      Bcount++;
    else
      cnt += Bcount;
  }
  cout << cnt << endl;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

char str[100010];

int main(void) {

  scanf("%s", str);
  int len = strlen(str);

  int bcnt = 0;
  int wcnt = 0;

  for (int i = 0; i < len; i++) {
    if (str[i] == 'B') {
      bcnt++;
    } else {
      wcnt++;
    }
  }

  if (!bcnt || !wcnt) {
    printf("0");
    return 0;
  }

  ll ans = 0;
  for (int i = 0; i < len; i++) {
    if (str[i] == 'W')
      ans += i;
  }
  for (int i = 0; i < wcnt; i++) {
    ans -= i;
  }

  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

char str[200010];

int main(void) {

  scanf("%s", str);
  int len = strlen(str);

  int bcnt = 0;
  int wcnt = 0;

  for (int i = 0; i < len; i++) {
    if (str[i] == 'B') {
      bcnt++;
    } else {
      wcnt++;
    }
  }

  if (!bcnt || !wcnt) {
    printf("0");
    return 0;
  }

  ll ans = 0;
  for (int i = 0; i < len; i++) {
    if (str[i] == 'W')
      ans += i;
  }
  for (int i = 0; i < wcnt; i++) {
    ans -= i;
  }

  cout << ans;

  return 0;
}
Problem p03200 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()[::-1]
c = 0
while S.find("WB") != -1:
    S = S.replace("WB", "BW", 1)
    c += 1
print(c)


Fixed submission:
S = input()
S = S[::-1]
y = -1


def x():
    global y
    y += 1
    return y


c = [i - x() for i in range(len(S)) if S[i] == "B"]
print(sum(c))
Problem p03200 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
ans = 0
prev = None
while prev != ans:
    prev = ans
    ans += S.count("BW")
    S = S.replace("BW", "WB")
print(ans)


Fixed submission:
S = input()
ans = 0
black = 0
for c in S:
    if c == "B":
        black += 1
    else:
        ans += black
print(ans)
Problem p03200 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
reverse_count = 0
while "BW" in s:
    reverse_count += s.count("BW")
    s = s.replace("BW", "WB")
print(reverse_count)


Fixed submission:
s = list(input())
W_count = 0
ans = 0
for i in range(len(s)):
    if s[i] == "W":
        ans += i - W_count
        W_count += 1
print(ans)
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;

  cin >> S;

  int sz = S.size();
  int ans = 0;
  for (int i = 0; i < sz - 1; i++) {
    if (S[i] == 'B' && S[i + 1] == 'W') {
      S[i] = 'W';
      S[i + 1] = 'B';
      if (i > 0 && S[i - 1] == 'B' && S[i] == 'W') {
        i -= 2;
      }
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;

  cin >> S;

  int sz = S.size();
  long long ans = 0;
  int bcnt = 0;
  for (int i = 0; i < sz; i++) {
    if (S[i] == 'B') {
      bcnt++;
    } else {
      ans += bcnt;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

typedef unsigned uint;
typedef long long Int;
typedef unsigned long long UInt;

const int INF = 1001001001;
const Int INFLL = 1001001001001001001LL;

template <typename T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
double fin() {
  double x;
  scanf("%lf", &x);
  return x;
}
Int lin() {
  Int x;
  scanf("%lld", &x);
  return x;
}

char S[100050];

int main() {
  scanf("%s", S);
  const int n = strlen(S);

  Int res = 0;
  int blk = 0;
  for (int i = 0; i < n; ++i) {
    if (S[i] == 'B') {
      ++blk;
    } else {
      res += blk;
    }
  }
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

typedef unsigned uint;
typedef long long Int;
typedef unsigned long long UInt;

const int INF = 1001001001;
const Int INFLL = 1001001001001001001LL;

template <typename T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
double fin() {
  double x;
  scanf("%lf", &x);
  return x;
}
Int lin() {
  Int x;
  scanf("%lld", &x);
  return x;
}

char S[300050];

int main() {
  scanf("%s", S);
  const int n = strlen(S);

  Int res = 0;
  int blk = 0;
  for (int i = 0; i < n; ++i) {
    if (S[i] == 'B') {
      ++blk;
    } else {
      res += blk;
    }
  }
  printf("%lld\n", res);
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define ALL(A) A.begin(), A.end()

int main() {
  string s;
  cin >> s;

  int counter = 0;

  while (s.at(0) == 'W') {
    s.erase(s.begin());
  }

  int j = 0;

  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == 'W') {
      counter += (i - j);
      j++;
    }
  }

  cout << counter << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define ALL(A) A.begin(), A.end()

int main() {
  string s;
  cin >> s;

  ll counter = 0;

  int j = 0;

  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == 'W') {
      counter += (i - j);
      j++;
    }
  }

  cout << counter << endl;

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const ll MOD = 1e9 + 7;

int N;
char buf[114514];
LL ans = 0;
LL cnt = 0;

int main() {
  scanf("%s", buf);
  N = strlen(buf);

  rrep(i, N) {
    if (buf[i] == 'B') {
      ans += cnt;
    } else if (buf[i] == 'W') {
      cnt++;
    } else
      assert(0);
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const ll MOD = 1e9 + 7;

int N;
char buf[314514];
LL ans = 0;
LL cnt = 0;

int main() {
  scanf("%s", buf);
  N = strlen(buf);

  rrep(i, N) {
    if (buf[i] == 'B') {
      ans += cnt;
    } else if (buf[i] == 'W') {
      cnt++;
    } else
      assert(0);
  }
  printf("%lld\n", ans);
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
inline long long mod(long long n, long long m) { return (n % m + m) % m; }
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
  string s;
  cin >> s;
  int n = s.size();
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 'B' && s[i + 1] == 'W') {
      swap(s[i], s[i + 1]);
      i -= 2;
      ans++;
    }
    // cout<<s<<endl;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
inline long long mod(long long n, long long m) { return (n % m + m) % m; }
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
  string s;
  cin >> s;
  int n = s.size();
  long long ans = 0;
  long long ww = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'W')
      ww++;
    if (s[i] == 'B')
      ans += ww;
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define intt long long

int main() {
  std::string s;
  std::cin >> s;
  intt N = s.size();
  intt count = 0;
  for (int i = N - 2; i >= 0; --i) {
    if (s[i + 1] == 'W' && s[i] == 'B') {
      std::swap(s[i], s[i + 1]);
      ++count;
      i += 2;
    }
  }
  std::cout << count << std::endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define intt long long

int main() {
  std::string s;
  std::cin >> s;
  intt N = s.size();
  intt count = 0;
  intt num = 0;
  for (int i = 0; i < N; ++i) {
    if (s[i] == 'W') {
      count += num;
    } else
      ++num;
  }
  std::cout << count << std::endl;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
/*----------------by syr----------------*/
/*
  -----  -----  -----
  |   |  |---|    _/
  |   |  | \_    /
  -----  |   \  -----

  |---\  \   /  |\  /|
  |   |   \_/   | \/ |
  |   |   / \   |    |
  |---/  /   \  |    |
*/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define x0 x0123456789
#define y0 y0123456789
#define x1 x1234567890
#define y1 y1234567890
#define x2 x2345678901
#define y2 y2345678901

using namespace std;

typedef double db;
typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 1e5 + 5;

int n, cnt;
char s[maxn];
ll ans;

int main() {
  scanf("%s", s);
  n = strlen(s);
  FOR(i, 0, n) if (s[i] == 'W') ans += cnt;
  else ++cnt;
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
/*----------------by syr----------------*/
/*
  -----  -----  -----
  |   |  |---|    _/
  |   |  | \_    /
  -----  |   \  -----

  |---\  \   /  |\  /|
  |   |   \_/   | \/ |
  |   |   / \   |    |
  |---/  /   \  |    |
*/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define x0 x0123456789
#define y0 y0123456789
#define x1 x1234567890
#define y1 y1234567890
#define x2 x2345678901
#define y2 y2345678901

using namespace std;

typedef double db;
typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 2e5 + 5;

int n, cnt;
char s[maxn];
ll ans;

int main() {
  scanf("%s", s);
  n = strlen(s);
  FOR(i, 0, n) if (s[i] == 'W') ans += cnt;
  else ++cnt;
  printf("%lld\n", ans);
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#define INF 10e10
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n, m) for (int i = m; i < n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
// 昇順sort
#define sorti(x) sort(x.begin(), x.end())
// 降順sort
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())

int main() {
  string s;
  cin >> s;
  ll res = 0;
  for (int i = 1; i < s.size(); ++i) {
    if (s[i] == 'W' and s[i - 1] == 'B') {
      for (int j = i; j >= 1; --j) {
        if (s[j] == 'W' and s[j - 1] == 'B') {
          s[j] = 'B';
          s[j - 1] = 'W';
          res++;
        } else {
          break;
        }
      }
    }
  }
  cout << res << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#define INF 10e10
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n, m) for (int i = m; i < n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
// 昇順sort
#define sorti(x) sort(x.begin(), x.end())
// 降順sort
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())

int main() {
  string s;
  cin >> s;
  ll res = 0, b = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'B')
      b++;
    else if (s[i] == 'W') {
      res += b;
    }
  }
  cout << res << endl;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main() {
  string S;
  cin >> S;
  ll ans = 0;
  for (int i = 0; i < S.size() - 1; i++) {
    if (S[i] == 'B' && S[i + 1] == 'W') {
      S[i] = 'W', S[i + 1] = 'B';
      i = max(-1, i - 2);
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main() {
  string S;
  cin >> S;
  ll ans = 0;
  ll cnt = 0;
  REP(i, S.size()) {
    if (S[i] == 'B') {
      cnt++;
    } else {
      ans += cnt;
    }
  }
  cout << ans << endl;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}
template <class t> using vc = vector<t>;
template <class t> using vvc = vc<vc<t>>;
using pi = pair<int, int>;
using vi = vc<int>;
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int SIZE(string s) { return (int)s.size(); }

int main() {
  cout << fixed << setprecision(20);
  ll ans = 0;
  string s;
  cin >> s;
  int si = SIZE(s);
  rep(i, si - 1) {
    rep(j, si - 1 - i) {
      if (s[j] == 'B' && s[j + 1] == 'W') {
        swap(s[j], s[j + 1]);
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}
template <class t> using vc = vector<t>;
template <class t> using vvc = vc<vc<t>>;
using pi = pair<int, int>;
using vi = vc<int>;
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int SIZE(string s) { return (int)s.size(); }

int main() {
  cout << fixed << setprecision(20);
  ll ans = 0;
  string s;
  cin >> s;
  int si = SIZE(s);
  int cnt = 0;
  rep(i, si) {
    if (s[i] == 'B')
      cnt++;
    else
      ans += cnt;
  }
  cout << ans << endl;
  return 0;
}