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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {

	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);

	char jaka;
	string ciag;
	int a = 0;
	int b = 0;
	int l = 0;
	int p = 0;
	unsigned long long swapy = 0;
	vector <int> pozycje;

	cin >> ciag;

	const int R = ciag.size();

	for (int i = 0; i < R; i++)
	{
		if (ciag[i] == 'a') {
			a = a + 1;
		}
		else {
			b = b + 1;
		}
	}

	if (a % 2 != 0 && b % 2 != 0) {
		cout << -1;
		return 0;
	}
	else if (a % 2 != 0) {
		jaka = 'b';
	}
	else if (b % 2 != 0) {
		jaka = 'a';
	}
	else {
		if (a > b) {
			jaka = 'b';
		}
		else {
			jaka = 'a';
		}
	}

	for (int i = 0; i < R; i++)
	{
		if (ciag[i] == jaka) {
			pozycje.push_back(i);
		}
	}

	const int K =  pozycje.size();
	for (int i = 0; i < K/2; i++)
	{
		l = pozycje[i];
		p = pozycje[K - (1 + i)];
		p = R - p - 1;
		swapy = swapy + abs(l - p);
	}
	cout << swapy;
}