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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inc(l, i, r) for (int i = l; i <= r; i++)
#define dec(r, i, l) for (int i = r; i >= l; i--)
#define mid ((l + r) >> 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define ___ putchar(10);
#define N 100100
using namespace std;
typedef long long ll;

ll ans;
int n, tmp;
char s[N];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  inc(1, i, n) {
    if (s[i] == 'B')
      ++tmp;
    else
      ans += tmp;
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define inc(l, i, r) for (int i = l; i <= r; i++)
#define dec(r, i, l) for (int i = r; i >= l; i--)
#define mid ((l + r) >> 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define ___ putchar(10);
#define N 200200
using namespace std;
typedef long long ll;

ll ans;
int n, tmp;
char s[N];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  inc(1, i, n) {
    if (s[i] == 'B')
      ++tmp;
    else
      ans += tmp;
  }
  printf("%lld\n", ans);
}
Problem p03200 (C++)

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

int main() {

  string S;
  cin >> S;

  int count = 0;
  bool flag = true;
  while (flag) {
    flag = false;
    for (int i = 0; i < S.length() - 1; i++) {
      if (S[i] == 'B' && S[i + 1] == 'W') {
        S[i] = 'W';
        S[i + 1] = 'B';
        flag = true;
        count++;
        break;
      }
    }
  }

  cout << count << endl;

  return 0;
}

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

int main() {

  string S;
  cin >> S;

  long long count = 0;
  int left = 0;
  for (int i = 0; i < S.length(); i++) {
    if (S[i] == 'W') {
      count += i - left;
      left++;
    }
  }

  cout << count << endl;

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
// #include <cstdlib>
#include <cstring>

// #include <map>
// #include <set>
// #include <queue>
// #include <string>
// #include <vector>
// #include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

const int MAXN = 100005;
const int P = 1000000007;
const int INF = 1000000007;
const double eps = 1e-6;

inline int getint() {
  int r = 0;
  bool b = true;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      b = false;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 1) + (r << 3) + c - '0';
    c = getchar();
  }
  return b ? r : -r;
}

char str[MAXN];
int n, cnt;
LL ans;

int main() {
  scanf("%s", str + 1);
  n = strlen(str + 1);
  for (int i = n; i >= 1; --i) {
    if (str[i] == 'B')
      ans += cnt;
    else
      ++cnt;
  }
  printf("%lld", ans);
}


Fixed submission:
#include <cstdio>
// #include <cstdlib>
#include <cstring>

// #include <map>
// #include <set>
// #include <queue>
// #include <string>
// #include <vector>
// #include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

const int MAXN = 200005;
const int P = 1000000007;
const int INF = 1000000007;
const double eps = 1e-6;

inline int getint() {
  int r = 0;
  bool b = true;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      b = false;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 1) + (r << 3) + c - '0';
    c = getchar();
  }
  return b ? r : -r;
}

char str[MAXN];
int n, cnt;
LL ans;

int main() {
  scanf("%s", str + 1);
  n = strlen(str + 1);
  for (int i = n; i >= 1; --i) {
    if (str[i] == 'B')
      ans += cnt;
    else
      ++cnt;
  }
  printf("%lld", ans);
}
Problem p03200 (C++)

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

// typedef
//------------------------------------------
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int MOD = 1000000007;

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// main code

int main(int argc, char const *argv[]) {
  string s;
  cin >> s;
  int ans = 0;
  for (int i = s.size() - 2; i >= 0; i--) {
    if (i == s.size() - 1)
      continue;
    if (s[i] == 'B' and s[i + 1] == 'W') {
      s[i] = 'W';
      s[i + 1] = 'B';
      ans++;
      i += 2;
    }
  }
  cout << ans << endl;
  return 0;
}


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

// typedef
//------------------------------------------
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int MOD = 1000000007;

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// main code

int main(int argc, char const *argv[]) {
  string s;
  cin >> s;
  LL ans = 0;
  LL count = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == 'W') {
      count++;
    } else {
      ans += count;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric> // accumulate(v.begin(), v.end(), 0)
#include <vector>

using namespace std;
#define ll long long

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric> // accumulate(v.begin(), v.end(), 0)
#include <vector>

using namespace std;
#define ll long long

int main() {
  string S;
  cin >> S;
  int N = (int)S.size();
  ll ans = 0;
  int end = N - 1;
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == 'B') {
      ans += end - i;
      end--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
int main() {
  ll c = 0, ans = 0;
  char *str = new char[20005];
  scanf("%s", str);
  int len = strlen(str);
  rep(i, 0, len) {
    if (str[i] == 'B')
      c++;
    else
      ans += c;
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
int main() {
  ll c = 0, ans = 0;
  char *str = new char[200005];
  scanf("%s", str);
  int len = strlen(str);
  rep(i, 0, len) {
    if (str[i] == 'B')
      c++;
    else
      ans += c;
  }
  printf("%lld", ans);
  return 0;
}
Problem p03200 (C++)

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

#define N 100010
char s[N];

int main() {
  scanf("%s", s);
  int n = strlen(s);
  long long ans = 0, c = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'B')
      ++c;
    else
      ans += c;
  }
  cout << ans << endl;
  return 0;
}

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

#define N 200010
char s[N];

int main() {
  scanf("%s", s);
  int n = strlen(s);
  long long ans = 0, c = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'B')
      ++c;
    else
      ans += c;
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  string str;
  long long wn = 0;
  long long ans = 0;

  cin >> str;

  for (int i = str.length() - 1; i >= 0; i++) {
    if (str[i] == 'W')
      wn++;
    else
      ans += wn;
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  string str;
  long long wn = 0;
  long long ans = 0;

  cin >> str;

  for (int i = str.length() - 1; i >= 0; i--) {
    if (str[i] == 'W')
      wn++;
    else
      ans += wn;
  }

  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

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

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

int main() {
  string s;
  cin >> s;
  long long cnt = 0, ans = 0;
  for (int i = 0; i < s.size(); i++) {
    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 lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44

using namespace std;

string str;

int main() {
  // ifstream cin("A.in");
  // ofstream cout("A.out");
  // int ;
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> str;
  int n = str.size();
  int ans = 0;
  int pos = 0;
  while (pos < n - 1) {
    if (str[pos] == 'B' && str[pos + 1] == 'W') {
      str[pos] = 'W', str[pos + 1] = 'B';
      ans++;
    }
    if (pos > 0 && str[pos - 1] == 'B' && str[pos] == 'W') {
      pos--;
    } else {
      pos++;
    }
  }
  cout << ans;
  // cin.close();
  // cout.close();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44

using namespace std;

string str;

int main() {
  // ifstream cin("A.in");
  // ofstream cout("A.out");
  // int ;
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> str;
  ll ans = 0, cnt = 0;
  for (auto it : str) {
    cnt += (it == 'B');
    ans += cnt * (it == 'W');
  }
  cout << ans;
  // cin.close();
  // cout.close();
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  char s[200005] = {};
  long int ans = 0;
  scanf("%s\n", s);
  for (int i = strlen(s) - 2; i >= 0; i--) {
    int j = i;
    while (s[j] == 'B' && s[j + 1] == 'W') {
      s[j] = 'W';
      s[j + 1] = 'B';
      j++;
      ans++;
    }
  }
  printf("%ld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  char s[200005] = {};
  long int ans = 0;
  scanf("%s\n", s);
  int j = strlen(s) - 1;
  for (int i = strlen(s) - 1; i >= 0; i--) {
    if (s[i] == 'B') {
      ans += j - i;
      j--;
    }
  }
  printf("%ld\n", ans);
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
const double EPS = 1e-10;
// cout << fixed << std::setprecision(9)
// memset(a, 0, sizeof(a));
//--------------------------

string s;
int B[20005];

int main() {
  cin >> s;
  if (s[0] == 'B')
    B[0] = 1;
  for (int i = 1; i < s.size(); i++) {
    B[i] = B[i - 1];
    if (s[i] == 'B')
      B[i]++;
  }

  ll ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      ans += B[i];
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
const double EPS = 1e-10;
// cout << fixed << std::setprecision(9)
// memset(a, 0, sizeof(a));
//--------------------------

string s;
int B[200005];

int main() {
  cin >> s;
  if (s[0] == 'B')
    B[0] = 1;
  for (int i = 1; i < s.size(); i++) {
    B[i] = B[i - 1];
    if (s[i] == 'B')
      B[i]++;
  }

  ll ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      ans += B[i];
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03200 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
#define mod 1000000007
#define PI 3.14159265
#define endl '\n'
#define MAX

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string str;
  cin >> str;
  int n = str.size();
  int ans = 0;
  string temp = str;
  sort(allr(temp));
  while (str != temp) {
    for (int i = n - 2; i >= 0; i--) {
      if (str[i] == 'B' && str[i + 1] == 'W') {
        ans++;
        swap(str[i], str[i + 1]);
      }
    }
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
#define mod 1000000007
#define PI 3.14159265
#define endl '\n'
#define MAX

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string str;
  cin >> str;
  ll ans = 0, c = 0;
  for (auto it : str) {
    if (it == 'W') {
      ans += c;
    } else {
      c++;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#define ll long long
#define int ll

int ans;

string S;
int a[30000];
int len;
int Count;

signed main() {
  cin >> S;

  len = S.size();

  for (int i = 0; i < len; i++) {
    if (S[i] == 'B') {
      a[i + 1] = 1;
    }
  }

  for (int i = len; i != 0; i--) {
    if (a[i] == 1) {
      ans += Count;
    } else {
      Count++;
    }
  }

  cout << ans << endl;

  //	system("pause");
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#define ll long long
#define int ll

int ans;

string S;
int a[300000];
int len;
int Count;

signed main() {
  cin >> S;

  len = S.size();

  for (int i = 0; i < len; i++) {
    if (S[i] == 'B') {
      a[i + 1] = 1;
    }
  }

  for (int i = len; i != 0; i--) {
    if (a[i] == 1) {
      ans += Count;
    } else {
      Count++;
    }
  }

  cout << ans << endl;

  //	system("pause");
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  long long f = 0;
  int d = 0;
  long long sum = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'B' && s[i + 1] == 'W') {
      sum++;
      s[i] = 'W';
      s[i + 1] = 'B';
      if (i > 0)
        i -= 2;
    }
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  long long f = 0;
  int d = 0;
  long long sum = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'W') {
      sum += f;
    } else {
      f++;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
using namespace std;
using ll = long long;

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

  vector<int> cum_cnt(s.size(), 0);
  rep(i, 0, s.size() + 1) cum_cnt[i + 1] = cum_cnt[i] + int(s[i] == 'B');

  int ans = 0;
  rep(i, 0, s.size()) if (s[i] == 'W') ans += cum_cnt[i];

  cout << ans << endl;
  return 0;
}

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

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

  int cnt = 0;
  ll ans = 0;
  for (auto c : s)
    if (c == 'B')
      ++cnt;
    else
      ans += cnt;
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const long long INF = 1e18;
#define len(x) ((int)(x).size())
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define foreps(i, m, n) for (int i = m; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
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;
}

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

  ll ans = 0;
  for (int i = 0; i < len(S) - 1;) {
    if (S[i] == 'B' && S[i + 1] == 'W') {
      swap(S[i], S[i + 1]);
      ++ans, i = 0;
    } else
      i++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const long long INF = 1e18;
#define len(x) ((int)(x).size())
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define foreps(i, m, n) for (int i = m; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
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;
}

int main() {
  string S;
  cin >> S;
  ll ans = 0, num = 0;
  rep(i, len(S)) {
    if (S[i] == 'W')
      ans += num;
    else
      ++num;
  }
  cout << ans << endl;
}
Problem p03200 (C++)

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REP(i, x, n) for (int i = x; i < n; i++)

int main() {
  string s;
  cin >> s;
  int ans = 0;
  int i = 0;
  while (1) {
    if (s[i] == 'B' && s[i + 1] == 'W') {
      ans++;
      swap(s[i], s[i + 1]);
      i = 0;
    } else {
      i++;
    }
    if (i == s.length())
      break;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REP(i, x, n) for (int i = x; i < n; i++)

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
using ulli = unsigned long long int;
#define debug(x) cerr << #x << " : " << x << endl

namespace atcoder {
void judge_outer(bool judge_exp, string ok = "Yes", string ng = "No") {
  if (judge_exp) {
    cout << ok << endl;
  } else {
    cout << ng << endl;
  }
}

template <typename T> T ncr(T left, T right) {
  right = min(left - right, right);
  T ans = 1;
  for (T i = 1; i <= right; i++) {
    ans = ans * (left - right + i) / i;
  }
  return ans;
}

const int LIMIT = 1000000007;
const int tenfive = 100000;

} // namespace atcoder

using namespace atcoder;

int main() {
  string s;
  cin >> s;
  lli ans = 0;

  for (int i = s.size() - 1; i > 0; i--) {
    for (int j = i; j > 0; j--) {
      if (s.substr(j - 1, 2) == "BW") {
        ans++;
        swap(s[j], s[j - 1]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
using ulli = unsigned long long int;
#define debug(x) cerr << #x << " : " << x << endl

namespace atcoder {
void judge_outer(bool judge_exp, string ok = "Yes", string ng = "No") {
  if (judge_exp) {
    cout << ok << endl;
  } else {
    cout << ng << endl;
  }
}

template <typename T> T ncr(T left, T right) {
  right = min(left - right, right);
  T ans = 1;
  for (T i = 1; i <= right; i++) {
    ans = ans * (left - right + i) / i;
  }
  return ans;
}

const int LIMIT = 1000000007;
const int tenfive = 100000;

} // namespace atcoder

using namespace atcoder;

int main() {
  string s;
  cin >> s;
  lli ans = 0;
  int b_count = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'B')
      b_count++;
    else
      ans += b_count;
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
#define put(n) cout << (n) << endl
#define rep(i, N) for (int(i) = 0; (i) < (N); ++(i))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)

int main() {
  string S;
  cin >> S;
  int ans = 0;
  bool ok = true;

  while (ok) {
    bool p = false;
    rep(i, S.size() - 1) {
      if (S[i] == 'B' && S[i + 1] == 'W') {
        S[i] = 'W';
        S[i + 1] = 'B';
        ++ans;
        p = true;
      }
    }
    if (!p)
      ok = false;
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
#define put(n) cout << (n) << endl
#define rep(i, N) for (int(i) = 0; (i) < (N); ++(i))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)

int main() {
  string S;
  cin >> S;
  long long ans = 0;
  long long cnt = 0;
  for (int i = 0; i < S.size(); ++i) {
    if (S[i] == 'B')
      ++cnt;
    if (S[i] == 'W')
      ans += cnt;
  }

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

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

int main() {
  string S;
  cin >> S;
  int64_t ans = 0;
  while (true) {
    bool flag = false;
    for (int i = 0; i < S.size() - 1; i++) {
      if (S.substr(i, 2) == "BW") {
        ans++;
        flag = true;
        S.at(i) = 'W';
        S.at(i + 1) = 'B';
      }
    }
    if (!flag) {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  string S;
  cin >> S;
  int64_t ans = 0;
  int count = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) == 'B') {
      count++;
    } else {
      ans += count;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <random>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

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

  // bool flag = 0;
  int ans = 0;
  int k = 0;
  for (int i = 0; i < S.size() - 1; i++) {
    if (S.at(i) == 'B' && S.at(i + 1) == 'W') {
      S.at(i) = 'W';
      S.at(i + 1) = 'B';
      ans++;
      if (i != 0) {
        k = i - 1;
        while (1) {
          if (S.at(k) == 'B' && S.at(k + 1) == 'W') {
            S.at(k) = 'W';
            S.at(k + 1) = 'B';
            ans++;
            if (k != 0) {
              k--;
            } else {
              break;
            }
          } else {
            break;
          }
        }
      }
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <random>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

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

  ll n = 0;
  ll ans = 0;
  for (ll i = 0; i < S.size(); i++) {
    if (S.at(i) == 'W') {
      ans += i - n;
      n++;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s.substr(i, 2) == "BW") {
      s[i] = 'W';
      s[i + 1] = 'B';
      ans++;
      for (int k = 1;; k++) {
        if (i != 0 && s[i - k] == 'B') {
          ans++;
          s[i - k] = 'W';
          s[i - k + 1] = 'B';
        } else {
          break;
        }
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  long long ans = 0, ans1 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'B') {
      ans1++;
    } else {
      ans += ans1;
    }
  }
  cout << ans << endl;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):

// A - Irreversible operation

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

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

  if (S.size() == 1) {
    cout << 0 << endl;
    return 0;
  }

  int ans = 0;

  if (S[0] == 'B' && S[1] == 'W') {
    S[0] = 'W';
    S[1] = 'B';
    ans++;
  }

  int i = 1;
  while (i < S.size() - 1) {
    if (S[i] == 'B' && S[i + 1] == 'W') {
      S[i] = 'W';
      S[i + 1] = 'B';
      ans++;
      if (S[i - 1] == 'B')
        i--;
      else
        i++;
    } else {
      i++;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:

// A - Irreversible operation

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

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

  if (S.size() == 1) {
    cout << 0 << endl;
    return 0;
  }

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

  cout << ans << endl;

  return 0;
}
Problem p03200 (C++)

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

using namespace std;

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


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

using namespace std;

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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
long long n, a[100001], b[100001], ans = 0;

string s;

void gb(int l, int r) {
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  gb(l, mid);
  gb(mid + 1, r);
  int i = l, j = mid + 1, top = l - 1, k;
  while (i <= mid && j <= r) {
    if (a[i] > a[j]) {
      b[++top] = a[j];
      ans += mid - i + 1;
      j++;
    } else {
      b[++top] = a[i];
      i++;
    }
  }
  for (k = i; k <= mid; k++)
    b[++top] = a[k];
  for (k = j; k <= r; k++)
    b[++top] = a[k];
  for (k = l; k <= r; k++)
    a[k] = b[k];
  return;
}

int main() {
  cin >> s;
  n = s.length();
  for (int k = 1; k <= n; k++)
    a[k] = (s[k - 1] == 'B');
  gb(1, n);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
long long n, a[250001], b[250001], ans = 0;

string s;

void gb(int l, int r) {
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  gb(l, mid);
  gb(mid + 1, r);
  int i = l, j = mid + 1, top = l - 1, k;
  while (i <= mid && j <= r) {
    if (a[i] > a[j]) {
      b[++top] = a[j];
      ans += mid - i + 1;
      j++;
    } else {
      b[++top] = a[i];
      i++;
    }
  }
  for (k = i; k <= mid; k++)
    b[++top] = a[k];
  for (k = j; k <= r; k++)
    b[++top] = a[k];
  for (k = l; k <= r; k++)
    a[k] = b[k];
  return;
}

int main() {
  cin >> s;
  n = s.length();
  for (int k = 1; k <= n; k++)
    a[k] = (s[k - 1] == 'B');
  gb(1, n);
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, ini, n) for (int i = ini; i < n; i++)
#define _rep(i, ini, n) for (int i = ini; i >= n; i--)
#define ToEnd(a) a.begin(), a.end()
uint64_t MOD = 1000000007;

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

  uint64_t ans = 0;
  while (1) {
    int idx = S.find("BW");
    if (idx == string::npos)
      break;
    S.replace(idx, 2, "WB");
    ans++;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, ini, n) for (int i = ini; i < n; i++)
#define _rep(i, ini, n) for (int i = ini; i >= n; i--)
#define ToEnd(a) a.begin(), a.end()
uint64_t MOD = 1000000007;

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

  uint64_t ans = 0, wCnt = 0;
  ;
  for (auto s : S) {
    if (s == 'B')
      wCnt++;
    else
      ans += wCnt;
  }

  cout << ans << endl;
}
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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int LCM(int a, int b) { return (a * b) / GCD(a, b); }
int abs(int x) { return x > 0 ? x : -x; }

// const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
// const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  string s;
  cin >> s;
  int sum = 0;
  int len;
  while (1) {
    len = (int)s.size();
    if (len == 0)
      break;
    auto pos = s.rfind("B");
    if (pos != -1) {
      sum += len - ((int)pos + 1);
      s[pos] = 'W';
    }
    s.pop_back();
  }

  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int LCM(int a, int b) { return (a * b) / GCD(a, b); }
int abs(int x) { return x > 0 ? x : -x; }

// const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
// const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  string s;
  cin >> s;
  ll sum = 0;
  int len = (int)s.size();
  vector<int> index_b;
  for (int i = 0; i < len; i++) {
    if (s[i] == 'B')
      index_b.push_back(i);
  }

  reverse(ALL(index_b));
  len--;
  for (auto x : index_b) {
    sum += (len--) - x;
  }

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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

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

  uint64_t count = 0;
  rep(i, s.size() - 1) {
    if (s[i] == 'B' && s[i + 1] == 'W') {
      s[i] = 'W';
      s[i + 1] = 'B';
      count++;
      while (s[i - 1] == 'B') {
        s[i - 1] = 'W';
        s[i] = 'B';
        count++;
        --i;
      }
    }
  }

  std::cout << count << std::endl;
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

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

  uint64_t count = 0;
  uint64_t black = 0;
  rep(i, s.size()) {
    if (s[i] == 'W') {
      count += black;
    } else {
      black++;
    }
  }

  std::cout << count << std::endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

int main() {
  std::string s;
  std::cin >> s;
  int cnt = 0;
  size_t p = 0;
  while ((p = s.find("BW")) != std::string::npos) {
    s.replace(p, 2, "WB", 2);
    cnt++;
  }
  std::cout << cnt << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>

int main() {
  std::string s;
  std::cin >> s;
  unsigned long long cnt = 0;
  int b = 0;

  for (auto &c : s) {
    if (c == 'W') {
      cnt += b;
    } else { // 'B'
      b++;
    }
  }
  std::cout << cnt << std::endl;
  return 0;
}
Problem p03200 (C++)

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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int cnt = 0;
  for (int j = 0;; j++) {
    bool bw = true;
    for (int i = 0; i < n - 1; i++) {
      if (s[i] == 'B' && s[i + 1] == 'W') {
        cnt++;
        s[i] = 'W';
        s[i + 1] = 'B';
        bw = false;
      }
    }
    if (bw)
      break;
  }

  cout << cnt;
}

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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  long long int cnt = 0, w[n] = {};
  if (s[0] == 'W')
    w[0]++;
  for (int i = 1; i < n; i++) {
    if (s[i] == 'W')
      w[i] = w[i - 1] + 1;
    else
      w[i] = w[i - 1];
  }

  for (int i = 0; i < n; i++) {
    if (s[i] == 'B')
      cnt += w[n - 1] - w[i];
  }

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

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

typedef long long ll;
const int MOD = 1e9 + 7;
typedef vector<ll> vll;
typedef vector<int> vi;
const int maxn = 100 + 10;

int main() {
  string s;
  cin >> s;
  int ans = 0;
  int left = 0;
  int right = 0;
  while (left < s.length() && right < s.length()) {
    while (left < s.length() && s[left] == 'W')
      ++left;
    if (left >= s.length())
      break;
    right = left + 1;
    while (right < s.length() && s[right] == 'B')
      ++right;
    if (right >= s.length())
      break;
    ans += right - left;
    swap(s[left], s[right]);
    ++left;
    ++right;
  }
  cout << ans << endl;
  return 0;
}


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

typedef long long ll;
const int MOD = 1e9 + 7;
typedef vector<ll> vll;
typedef vector<int> vi;
const int maxn = 100 + 10;

int main() {
  string s;
  cin >> s;
  ll ans = 0;
  ll cnt = 0;
  for (auto c : s) {
    if (c == 'B')
      ++cnt;
    else
      ans += cnt;
  }
  cout << ans << endl;
  return 0;
}
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 reps(i, n) for (int i = 1; i <= (n); i++)
#define lol long long
#define SUM(n) ((n) + 1) * (n) / 2 // 1〜nまでの総和を求める式
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) fixed << setprecision(x + 1) // 小数点桁数を指定する
#define abs(x, y) max(x, y) - min(x, y)
#define all(v) v.begin(), v.end()
#define UPDight(a, b) (a + b - 1) / b // 小数点切り上げ
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = int(1e9) + 7;
using namespace std;
using pii = pair<int, int>;
typedef vector<int> vit;

// 最大公約数
lol gcd(lol x, lol y) {
  if (x < y)
    swap(x, y);
  lol r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}

// 最小公倍数
lol lcm(lol x, lol y) {
  lol a = x * y;
  return (a / gcd(x, y));
}

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  string s;
  cin >> s;
  lol ans = 0;
  while (true) {
    bool nasu = false;
    for (int i = s.size() - 1; i > 0; i--) {
      if (s[i] == 'W' && s[i - 1] == 'B') {
        ans++;
        swap(s[i], s[i - 1]);
        nasu = true;
      }
    }
    //    cout << s << '\n';
    if (!nasu) {
      break;
    }
  }
  cout << ans << '\n';
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define lol long long
#define SUM(n) ((n) + 1) * (n) / 2 // 1〜nまでの総和を求める式
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) fixed << setprecision(x + 1) // 小数点桁数を指定する
#define abs(x, y) max(x, y) - min(x, y)
#define all(v) v.begin(), v.end()
#define UPDight(a, b) (a + b - 1) / b // 小数点切り上げ
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = int(1e9) + 7;
using namespace std;
using pii = pair<int, int>;
typedef vector<int> vit;

// 最大公約数
lol gcd(lol x, lol y) {
  if (x < y)
    swap(x, y);
  lol r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}

// 最小公倍数
lol lcm(lol x, lol y) {
  lol a = x * y;
  return (a / gcd(x, y));
}

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  string s;
  cin >> s;
  lol ans = 0, nasu = 0;
  rep(i, s.size()) {
    if (s[i] == 'W') {
      ans += nasu;
    } else {
      nasu++;
    }
  }
  cout << ans << '\n';
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
ll MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
  string s;
  cin >> s;
  int ans = 0;
  bool continue_flg = false;
  while (1) {
    continue_flg = false;
    rep(i, s.length() - 1) {
      if (s[i] == 'B' && s[i + 1] == 'W') {
        ++ans;
        continue_flg = true;
        s[i] = 'W';
        s[i + 1] = 'B';
      }
    }
    if (!continue_flg)
      break;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
ll MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
  string s;
  cin >> s;
  int cnt = 0;
  ll ans = 0;
  rep(i, s.length()) {
    if (s[i] == 'B')
      ++cnt;
    else
      ans += cnt;
  }
  cout << ans << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \
  if (flg) {                                                                   \
    cout << "YES" << endl;                                                     \
  } else {                                                                     \
    cout << "NO" << endl;                                                      \
  }
#define MAX_N 1002
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pdi;
typedef pair<ll, int> Pli;
typedef pair<P1, ll> P2;
const ll INF = 100000000000000001;
struct edge {
  int to, cost;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct Road {
  double cost;
  int a, b;
};

int main() {

  string s;
  ll total = 0;

  cin >> s;
  ll o = 0;
  rep(i, s.size() - 1) {
    o = 0;
    rep(j, s.size() - 1) {
      if (s[j] == 'B' && s[j + 1] == 'W') {
        s[j] = 'W';
        s[j + 1] = 'B';
        total++;
      } else {
        o++;
      }
    }
    if (o == s.size()) {
      break;
    }
  }
  cout << total << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \
  if (flg) {                                                                   \
    cout << "YES" << endl;                                                     \
  } else {                                                                     \
    cout << "NO" << endl;                                                      \
  }
#define MAX_N 1002
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pdi;
typedef pair<ll, int> Pli;
typedef pair<P1, ll> P2;
const ll INF = 100000000000000001;
struct edge {
  int to, cost;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct Road {
  double cost;
  int a, b;
};

int main() {

  string s;
  ll total = 0;

  cin >> s;
  ll o = 0;

  for (ll i = 0; i < s.size() + 1; i++) {
    if (s[i] == 'W') {
      o++;
      total += (i + 1) - o;
    }
  }
  cout << total << endl;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define poi 100010
#define int long long
using namespace std;
bool b[poi];
inline int re() {
  char c = getchar();
  int x = 0, k = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * k;
}
void wr(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    wr(x / 10);
  putchar(x % 10 + '0');
}
signed main() {
  int ans = 0, n = 0, c, sum = 0;
  while ((c = getchar()) != '\n')
    b[++n] = (c == 'B');
  for (int i = 1; i <= n; i++)
    if (!b[i])
      ans += sum;
    else
      sum++;
  wr(ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define poi 2001000
#define int long long
using namespace std;
bool b[poi];
inline int re() {
  char c = getchar();
  int x = 0, k = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * k;
}
void wr(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    wr(x / 10);
  putchar(x % 10 + '0');
}
signed main() {
  int ans = 0, n = 0, c, sum = 0;
  while ((c = getchar()) != '\n')
    b[++n] = (c == 'B');
  for (int i = 1; i <= n; i++)
    if (!b[i])
      ans += sum;
    else
      sum++;
  wr(ans);
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  string s;
  cin >> s;
  int N;
  N = s.length();
  int x[N];
  for (int i = 0; i < N; i++) {
    if (s[i] == 'B') {
      x[i] = 0;
    } else if (s[i] == 'W') {
      x[i] = 1;
    }
  }
  bool flag = true;
  int a;
  int start = 0;
  int goal = 1;
  int counter = 0;
  while (flag) {
    while (x[start] == 1) {
      start += 1;
    }
    while (x[N - goal] == 0) {
      goal += 1;
    }
    if (start > N - goal) {
      break;
    }
    for (int i = start; i < N - goal; i++) {
      a = x[i + 1] - x[i];
      if (a == 1) {
        x[i + 1] = 0;
        x[i] = 1;
        flag = true;
        counter += 1;
        break;
      } else {
        flag = false;
      }
    }
  }
  cout << counter;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  string s;
  cin >> s;
  int N;
  N = s.length();
  int x[N];
  for (int i = 0; i < N; i++) {
    if (s[i] == 'B') {
      x[i] = 0;
    } else if (s[i] == 'W') {
      x[i] = 1;
    }
  }
  long a = 0;
  long counter = 0;
  for (int i = 0; i < N; i++) {
    if (x[i] == 0) {
      a += 1;
    } else if (x[i] == 1) {
      counter += a;
    }
  }
  cout << counter;
  return 0;
}