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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include <bits/stdc++.h>
using namespace std;

// loops
#define FOR_3(i, a, b) for (int i = (a), bb##i = (b); i < bb##i; ++i)
#define FORR_3(i, a, b) for (int i = (b)-1, aa##i = (a); i >= aa##i; --i)

#define FOR_2(i, n) FOR_3(i, 0, n)
#define FORR_2(i, n) FORR_3(i, 0, n)

#define FOR_1(n) FOR_2(_i##__LINE__, n)
#define FOR_0() for (;;)

#define OVERLOAD(_0, _1, _2, _3, NAME, ...) NAME
#define FOR(...)                                                         \
	OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FOR_3, FOR_2, FOR_1, FOR_0) \
	(__VA_ARGS__)

#define FORR(...)                                                           \
	OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FORR_3, FORR_2, FORR_1, FOR_0) \
	(__VA_ARGS__)

// min / max

template <class T>
auto min(T &&t) { return move(t); };
template <class T, class... TS>
auto min(T &&t, TS &&...ts)
{
	auto rest = min(forward<TS>(ts)...);
	return t < rest ? t : rest;
}

template <class T>
auto max(T &&t) { return move(t); };
template <class T, class... TS>
auto max(T &&t, TS &&...ts)
{
	auto rest = max(forward<TS>(ts)...);
	return t > rest ? t : rest;
}

#define REMIN(a, b) ((a) = min(a, b))
#define REMAX(a, b) ((a) = max(a, b))

// containers / arrays
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) int((x).size())
#define ZERO(x) memset(x, 0, sizeof(x));

// other
#define UNUSED(...) (void)(__VA_ARGS__);

// types
#define umap unordered_map
#define vec vector
#define deq deque
using ll = long long;
using ld = long double;
using pii = pair<int, int>;

using di = deque<int>;
using dll = deque<ll>;
using dpii = deque<pii>;

// abbrevs
#define push push_back
#define pop pop_back
#define CMP bool operator<(const auto &o) const

// array operations
#define SORT(v) sort(ALL(v));
#define UNIQUE(c)                         \
	{                                     \
		SORT(c);                          \
		c.erase(unique(ALL(c)), c.end()); \
	}

auto PERM(int n)
{
	di v(n);
	FOR(i, n)
	v[i] = i;
	return v;
}

template <class V>
auto esort(const V &v)
{
	auto perm = PERM(SZ(v));
	sort(ALL(perm), [&](int a, int b)
		 { return v[a] < v[b]; });
	return perm;
}

template <class V>
auto eremap(V &&vv)
{
	auto v = move(vv);
	SORT(v);
	umap<typename V::value_type, int> r;
	int nxt = 0;
	for (auto &e : v)
		if (r.emplace(e, nxt).second)
			++nxt;
	return r;
}

template <class X, class FR, class TO>
bool range(const X &x, const FR &fr, const TO &to)
{
	return !(x < fr) && x < to;
}

#ifdef LOCAL
#include <sys/prctl.h>
#include <sys/wait.h>
#include <unistd.h>

void print_trace()
{
	char pid_buf[30];
	sprintf(pid_buf, "%d", getpid());
	char name_buf[512];
	name_buf[readlink("/proc/self/exe", name_buf, 511)] = 0;
	prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
	int child_pid = fork();
	if (!child_pid)
	{
		dup2(2, 1); // redirect output to stderr - edit: unnecessary?
		execl("/usr/bin/gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt",
			  name_buf, pid_buf, NULL);
		abort(); /* If gdb failed to start */
	}
	else
	{
		waitpid(child_pid, NULL, 0);
	}
}

extern const char *const sys_siglist[];

void handler(int sig)
{
	cerr << "SIGNAL " << sig << " (" << sys_siglist[sig] << ")" << endl;
	print_trace();
	exit(1);
}

#define INDENT_SIZE 2
string indent;

void _E() { cerr << endl; }
template <class ARG, class... ARGS>
void _E(ARG &&arg, ARGS &&...args)
{
	cerr << std::forward<ARG>(arg) << " ";
	_E(std::forward<ARGS>(args)...);
}

#define E(...)                                     \
	{                                              \
		cerr << indent << "[" << __LINE__ << "] "; \
		_E(__VA_ARGS__);                           \
	}

#define _V5(x) #x, "==", (x)
#define _V4(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V5(__VA_ARGS__))
#define _V3(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V4(__VA_ARGS__))
#define _V2(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V3(__VA_ARGS__))
#define _V1(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V2(__VA_ARGS__))
#define V(...) E(_V1(__VA_ARGS__))
#define INDENT auto _indenter = Indenter();

struct Indenter
{
	Indenter()
	{
		FOR(INDENT_SIZE)
		indent.push_back(' ');
	}
	~Indenter()
	{
		FOR(INDENT_SIZE)
		indent.pop_back();
	}
};

#define FAIL(str)                                                                 \
	{                                                                             \
		cerr << "ASSERT FAILED   " << str << "   " << __FILE__ << ':' << __LINE__ \
			 << endl;                                                             \
		abort();                                                                  \
	}

#define SLEEP this_thread::sleep_for(100ms);

#else
#define INDENT
#define SLEEP
#define FAIL(x) \
	{           \
	}
#define E(...) \
	{          \
	}
#define V(...) \
	{          \
	}
#endif

// asserts
#define CHECK(cond) \
	if (!(cond))    \
	FAIL(#cond)

#define CHECK_OP(op, a, b, res) \
	if (!(res))                 \
	FAIL(#a " " op " " #b "   (" + to_string(a) + " vs " + to_string(b) + ")")

#define CHECK_EQ(a, b) CHECK_OP("==", a, b, (a) == (b))
#define CHECK_NE(a, b) CHECK_OP("!=", a, b, (a) != (b))
#define CHECK_LE(a, b) CHECK_OP("<=", a, b, (a) <= (b))
#define CHECK_GE(a, b) CHECK_OP(">=", a, b, (a) >= (b))
#define CHECK_LT(a, b) CHECK_OP("<", a, b, (a) < (b))
#define CHECK_GT(a, b) CHECK_OP(">", a, b, (a) > (b))

#define CHECK_RANGE(x, a, b) \
	if (!(a <= x && x < b))  \
	FAIL(#x " in [" #a "," #b ")")

template <class A, class B>
auto divup(A a, B b)
{
	return (a + b - 1) / b;
}

// bit operations
int msb(int x)
{
	CHECK_GT(x, 0)
	return 31 - __builtin_clz(x);
}

int msb(ll x)
{
	CHECK_GT(x, 0)
	return 63 - __builtin_clzll(x);
}

int lsb(int x)
{
	CHECK_GT(x, 0)
	return __builtin_ctz(x);
}

int lsb(ll x)
{
	CHECK_GT(x, 0)
	return __builtin_ctzll(x);
}

int popcount(int x) { return __builtin_popcount(x); }
int popcount(ll x) { return __builtin_popcountll(x); }

#define FORB(b, mask) \
	for (auto m = mask, bit = lsb(m); m; m ^= (1 << b), bit = lsb(m))

#define FORS(s, mask) \
	for (auto s = mask; s; s = (s - 1) & mask) // iterate subsets

template <class T>
int log2up(T x)
{
	return x <= 1 ? 0 : msb(x - 1) + 1;
}

default_random_engine RNG(123);
#define SHUFFLE(c) shuffle(ALL(c), RNG);

// stdin
template <class T>
T read()
{
	T x;
	cin >> x;
	return x;
}
#define II read<int>()
#define ILL read<ll>()

void _I()
{
}
template <class ARG, class... ARGS>
void _I(ARG &&arg, ARGS &&...args)
{
	cin >> arg;
	_I(forward<ARGS>(args)...);
}
#define I(...) _I(__VA_ARGS__);

// stdout
template <class T>
void _print(T x)
{
	cout << x;
	bool space = true;
	if constexpr (is_same_v<T, char>)
	{
		if (x == '\n')
			space = false;
	}
	if (space)
		cout << " ";
}
template <class T>
void _print(deq<T> &d)
{
	for (auto &e : d)
		cout << e << " ";
}
void _O() {}
template <class ARG, class... ARGS>
void _O(ARG &&arg, ARGS &&...args)
{
	_print(arg);
	_O(forward<ARGS>(args)...);
}
#define O(...) _O(__VA_ARGS__ __VA_OPT__(, ) '\n');
#define OO(...) _O(__VA_ARGS__);

// stderr
template <class T>
auto &operator<<(ostream &os, deq<T> &v)
{
	os << "{";
	bool first = true;
	for (auto &e : v)
	{
		if (first)
			first = false;
		else
			os << " ";
		os << e;
	}
	return os << "}";
}

template <class A, class B>
auto &operator<<(ostream &os, pair<A, B> &p)
{
	return os << "{" << p.first << " " << p.second << "}";
}

#define LSB(S) (S & (-S)) // least significant bit

// 1-indexed
template <class T>
struct FW
{
	vector<T> v;

	FW(int n) : v(n + 1) {}

	// range [1..x]
	int sum(int x)
	{
		int sum = 0;
		for (; x; x -= LSB(x))
			sum += v[x];
		return sum;
	}

	void update(int x, int change)
	{
		for (; x <= SZ(v) - 1; x += LSB(x))
			v[x] += change;
	}
};

////////////////////////////////////////////////////////////////////////////////
//  _          _     _   _                                                    //
// | |        | |   | | | |                                                   //
// | |     ___| |_  | |_| |__   ___    __ _  __ _ _ __ ___   ___              //
// | |    / _ \ __| | __| '_ \ / _ \  / _` |/ _` | '_ ` _ \ / _ \             //
// | |___|  __/ |_  | |_| | | |  __/ | (_| | (_| | | | | | |  __/             //
// \_____/\___|\__|  \__|_| |_|\___|  \__, |\__,_|_| |_| |_|\___|             //
//                                     __/ |                                  //
//                                    |___/                                   //
//  ██████▓▓▒▒░░░░░░▒▒▒▒▒▒▒▒░░░░▒▒▒▒░░▒▒▒▒░░░░░░░░▒▒▒▒▒▒▒▒░░░░░░░░░░░░██████  //
//  ██████░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒░░░░░░░░░░▒▒░░▒▒░░░░░░░░░░░░  ░░▒▒████  //
//  ████░░░░░░░░░░▒▒░░░░░░░░░░░░░░░░▒▒▒▒▒▒░░░░▒▒░░░░▒▒    ░░░░░░░░░░  ░░████  //
//  ████░░░░░░▒▒▓▓████▓▓▓▓▓▓░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒░░░░▒▒▓▓▓▓████▓▓▒▒░░  ▓▓██  //
//  ████░░▒▒▓▓██████▓▓▓▓▓▓▓▓██▒▒░░░░░░░░▓▓▓▓░░░░░░░░██▓▓▓▓▒▒▒▒████▓▓▒▒░░████  //
//  ████▓▓▒▒▒▒▓▓████▓▓▓▓██▓▓████░░░░░░░░▒▒▒▒░░░░░░████▓▓▓▓▓▓░░▓▓██▓▓░░░░████  //
//  ████▒▒▒▒▓▓▓▓████▓▓▓▓▓▓▓▓████▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▓▓▓▓▓▓▒▒████▓▓░░░░▓▓██  //
//  ████▒▒▒▒▒▒▓▓▓▓██████▓▓██████░░▒▒▒▒▒▒▒▒▒▒░░░░░░████▓▓▓▓▓▓████▓▓▒▒░░░░▓▓██  //
//  ████▓▓▓▓▓▓▒▒▓▓▓▓▓▓██████▓▓▒▒▒▒▒▒▒▒░░▒▒░░░░░░░░▒▒████████▓▓▓▓▒▒░░▒▒▒▒░░██  //
//  ██▓▓░░░░▒▒▓▓██▒▒▒▒▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░▒▒▓▓▓▓▒▒▒▒▒▒▓▓▓▓░░▒▒██  //
//  ██░░▓▓▓▓▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░    ░░░░░░░░▒▒░░▒▒▓▓▒▒░░░░░░▓▓  //
//  ██░░░░▒▒▓▓▓▓▒▒▓▓▓▓▒▒▒▒▒▒░░▒▒░░░░░░░░░░░░      ░░░░░░░░▒▒▒▒▓▓▒▒▒▒▓▓▓▓▒▒▒▒  //
//  ▓▓▒▒▒▒▒▒░░▓▓▓▓▒▒▓▓▒▒▒▒▒▒░░░░░░░░░░░░░░░░      ░░░░░░░░▒▒▓▓▓▓▒▒▓▓░░        //
//  ▓▓▒▒▒▒▓▓▒▒▒▒▓▓▒▒▓▓▓▓▒▒▒▒░░░░░░░░░░░░░░░░      ░░░░░░▒▒▒▒██▒▒▓▓▓▓░░▒▒▒▒▒▒  //
//  ▓▓▒▒░░░░▓▓░░▓▓▓▓▒▒▓▓▒▒▒▒░░░░░░░░░░░░░░░░        ░░▒▒▒▒▓▓▓▓▒▒▓▓▒▒▒▒░░  ░░  //
//  ██▒▒▒▒░░▒▒▓▓▒▒▓▓▒▒▓▓▒▒▒▒░░░░░░░░░░░░░░░░        ░░░░▒▒▓▓▓▓▒▒▓▓░░▓▓  ▒▒    //
//  ██▒▒▒▒▒▒▒▒▓▓░░▓▓▒▒▓▓▒▒▒▒▒▒░░░░░░░░░░░░░░      ░░░░▒▒▒▒▒▒▓▓░░▓▓░░▓▓░░░░▓▓  //
//  ██▓▓▓▓▓▓▓▓▒▒▒▒▓▓▒▒▓▓▒▒▒▒▒▒▒▒░░░░░░░░░░░░    ░░▒▒▒▒▒▒▒▒▒▒▓▓▒▒▓▓░░▒▒▒▒░░██  //
//  ████▓▓▒▒▒▒▓▓▓▓▒▒▓▓▒▒▒▒▓▓▒▒▒▒░░░░░░░░░░░░      ░░▓▓▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▒▒████  //
//  ██████████▓▓▒▒▒▒▒▒▒▒██▓▓▓▓▒▒░░░░░░░░░░░░      ░░░░░░▒▒▒▒░░▒▒▒▒▓▓████████  //
//  ██████████████░░░░░░▒▒██▓▓▓▓██▓▓░░░░░░░░    ▓▓▒▒▒▒▓▓▓▓▒▒░░  ▒▒██████████  //
//  ████████████████░░░░░░▓▓▓▓▓▓▓▓▓▓▓▓▒▒░░░░▒▒▓▓▓▓▓▓▓▓▒▒▒▒      ████████████  //
//  ████████████████░░░░░░▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒░░      ░░████████████  //
//  ████████████████░░░░░░▒▒░░▒▒▒▒░░▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒        ██████████████  //
//  ████████████████▒▒░░░░▒▒░░░░░░░░░░░░▒▒▒▒░░░░░░░░▒▒░░    ▒▒██████████████  //
//  ██████████████████░░░░▒▒░░░░░░░░░░▒▒▒▒▒▒▒▒░░░░  ▓▓░░    ████████████████  //
//  ██████████████████░░░░▒▒░░░░░░░░░░▒▒▒▒▒▒▒▒░░░░  ▓▓░░  ▓▓████████████████  //
//                                                                            //
//           ;;;;;              ██████╗ ███████╗ ██████╗ ██╗███╗   ██╗        //
//           ;;;;;              ██╔══██╗██╔════╝██╔════╝ ██║████╗  ██║        //
//           ;;;;;              ██████╔╝█████╗  ██║  ███╗██║██╔██╗ ██║        //
//         ..;;;;;..            ██╔══██╗██╔══╝  ██║   ██║██║██║╚██╗██║        //
//          ':::::'             ██████╔╝███████╗╚██████╔╝██║██║ ╚████║        //
//            ':`               ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝╚═╝  ╚═══╝        //
//                                                                            //

const bool MULTIPLE_TEST_CASES = 0;

struct Test_Case
{
	struct Pt
	{
		array<int, 2> x;
		int c;
		bool operator<(const Pt &o) const { return x < o.x; }
	};
	deq<Pt> pts;

	ll n, k, q;
	dll in;

	string str;
	int a[2], b[2];
	vector<char> deleted;
	int fr, to;

	FW<int> fw = FW<int>(200'009);

	void update()
	{
		while (a[0] < SZ(str) && (deleted[a[0]] || str[a[0]] != 'a'))
			++a[0];

		while (b[0] < SZ(str) && (deleted[b[0]] || str[b[0]] != 'b'))
			++b[0];

		//

		while (a[1] >= 0 && (deleted[a[1]] || str[a[1]] != 'a'))
			--a[1];

		while (b[1] >= 0 && (deleted[b[1]] || str[b[1]] != 'b'))
			--b[1];

		//

		while (fr < SZ(str) && deleted[fr])
			++fr;

		while (to >= 0 && deleted[to])
			--to;
	}

	void setDeleted(int idx)
	{
		deleted[idx] = 1;
		fw.update(idx + 1, 1);
	}

	int sum(int aa, int bb)
	{
		int numDeleted = fw.sum(bb) - fw.sum(aa + 1);
		return bb - aa - numDeleted;
	}

	Test_Case()
	{
		I(str)
		deleted.resize(SZ(str));

		a[0] = 0;
		a[1] = SZ(str) - 1;

		b[0] = 0;
		b[1] = SZ(str) - 1;

		fr = 0;
		to = SZ(str) - 1;

		update();

		ll result = 0;

		for (;;)
		{
			if (str[fr] != str[to])
			{
				int cand0 = INT_MAX;
				if (str[fr] == 'a' && a[1] >= 0)
				{
					cand0 = sum(a[1], to);
				}

				if (str[fr] == 'b' && b[1] >= 0)
				{
					cand0 = sum(b[1], to);
				}

				//

				int cand1 = INT_MAX;
				if (str[to] == 'a' && a[0] < SZ(str))
				{
					cand1 = sum(fr, a[0]);
				}

				if (str[to] == 'b' && b[0] < SZ(str))
				{
					cand1 = sum(fr, b[0]);
				}

				//

				bool good = false;

				if (cand0 < INT_MAX && cand0 <= cand1)
				{
					int idx = str[fr] == 'a' ? a[1] : b[1];
					if (fr < idx)
					{
						setDeleted(fr);
						setDeleted(idx);
						result += sum(idx, to);
						good = true;
					}
				}

				if (cand1 < INT_MAX && cand1 < cand0)
				{
					int idx = str[to] == 'a' ? a[0] : b[0];
					if (idx < to)
					{
						setDeleted(to);
						setDeleted(idx);
						result += sum(fr, idx);
						good = true;
					}
				}

				if (!good)
				{
					result = -1;
					break;
				}
			}
			else
			{
				setDeleted(fr);
				setDeleted(to);
			}

			update();

			if (fr >= to)
				break;
		}

		O(result)
	}

	int yes = -1;
};

//                                                                            //
// ════════════ ('\../') ═════════════                                        //
// ════════════ (◕.◕) ═══════════════                                        //
// ════════════ (,,)(,,) ═════════════                                        //
// .▀█▀.█▄█.█▀█.█▄.█.█▄▀ █▄█.█▀█.█─█.                                        //
// ─.█.─█▀█.█▀█.█.▀█.█▀▄ ─█.─█▄█.█▄█- for reading my code!                   //
// ═══════════════════════════════════              --Adam                    //
////////////////////////////////////////////////////////////////////////////////

int main()
{
#ifdef LOCAL
	signal(SIGSEGV, handler);
	signal(SIGABRT, handler);
#endif
	ios_base::sync_with_stdio(0);
	cout.precision(20);
	cout << fixed;

	int num_cases = 1;
	if (MULTIPLE_TEST_CASES)
		cin >> num_cases;

	FOR(i, num_cases)
	{
		E()
		E("TEST CASE", i + 1)
		INDENT
		Test_Case tc;
		if (tc.yes != -1)
			O(tc.yes ? "YES" : "NO")
	}

	return 0;
}