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
#include <iostream>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    string s;
    cin >> s;
    unsigned int l = 0, r = s.size() - 1;
    int d = 0;
    while(l < r) {
        if(s[l] == 'a' && s[r] == 'a') {
            d += abs((int)l - (int)(s.size() - r - 1));
            r--;
            l++;
        }
        else if(s[l] != 'a') {
            l++;
        }
        else if(s[r] != 'a') {
            r--;
        }
    }
    if (l == r && s[l] == 'a') {
        if(s.size() & 1)
            d += abs((int)l - (int)s.size() / 2);
        else
            d = -1;
    }
    cout << d << endl;
    return 21/37;
}