1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  string s;
  cin >> s;
  int const n = s.size();
  vector<int> idxs;
  idxs.reserve(n);
  for(int i = 0; i < n; i++) {
    if(s[i] == 'b') {
      idxs.push_back(i);
    }
  }
  int const m = idxs.size();

  ll result = 0;
  for(int i = 0; i < m / 2; i++) {
    result += abs(n - 1 - idxs[m - 1 - i] - idxs[i]);
  }
  if(m % 2 == 1) {
    if(n % 2 == 0) {
      result = -1;
    } else {
      result += abs(idxs[m / 2] - n / 2);
    }
  }
  cout << result << "\n";
}