Binary String Sorting
Contest: Educational Codeforces Round 145 (Rated for Div. 2)
Difficulty rating: 1800
Tags: constructive algorithms, greedy

You are given a binary string $$$s$$$ consisting of only characters 0 and/or 1.

You can perform several operations on this string (possibly zero). There are two types of operations:

- choose two consecutive elements and swap them. In order to perform this operation, you pay $$$10^{12}$$$ coins;
- choose any element from the string and remove it. In order to perform this operation, you pay $$$10^{12}+1$$$ coins.

Your task is to calculate the minimum number of coins required to sort the string $$$s$$$ in non-decreasing order (i. e. transform $$$s$$$ so that $$$s_1 \le s_2 \le \dots \le s_m$$$, where $$$m$$$ is the length of the string after applying all operations). An empty string is also considered sorted in non-decreasing order.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The only line of each test case contains the string $$$s$$$ ($$$1 \le |s| \le 3 \cdot 10^5$$$), consisting of only characters 0 and/or 1.

The sum of lengths of all given strings doesn't exceed $$$3 \cdot 10^5$$$.

Output format:
For each test case, print a single integer — the minimum number of coins required to sort the string $$$s$$$ in non-decreasing order.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example, you have to remove the $$$1$$$-st element, so the string becomes equal to 00.

In the second example, the string is already sorted.

In the third example, you have to swap the $$$2$$$-nd and the $$$3$$$-rd elements, so the string becomes equal to 0011.

In the fourth example, you have to swap the $$$3$$$-rd and the $$$4$$$-th elements, so the string becomes equal to 00011101, and then remove the $$$7$$$-th element, so the string becomes equal to 0001111.

In the fifth example, you have to remove the $$$1$$$-st element, so the string becomes equal to 001101, and then remove the $$$5$$$-th element, so the string becomes equal to 00111.

In the sixth example, the string is already sorted.

Example input:
6
100
0
0101
00101101
1001101
11111

Example output:
1000000000001
0
1000000000000
2000000000001
2000000000002
0

Editorial:
Note that the price of operations is much greater than the difference between them. Therefore, first of all, we have to minimize the number of operations, and then maximize the number of operations of the first type.
Swapping two elements if at least one of them will be deleted later is not optimal. Therefore, first let's delete some elements of the string, and then sort the remaining elements using swaps. The number of swaps for sorting is equal to the number of inversions (i. e. the number of pairs such that $$$s_j > s_i$$$ and $$$j < i$$$). From here we can notice that if the number of inversions is greater than $$$1$$$, then there is an element that produces at least $$$2$$$ inversions. So it is more profitable for us to remove it, to minimize the number of operations.
From the above we get that the number of operations of the first type is at most $$$1$$$. If all operations are only of the second type, then we need to find a subsequence of the maximum length of the form 0000111111. To do this, we can iterate over the number of zeros that we include in the final string, and then add the number of ones from the remaining suffix of the string (that goes after the fixed number of zeros). If there is an operation of the first type, then it is enough to iterate over the pair that creates the inversion, to the left of it take all zeros, and to the right of it take all ones (you can notice that in fact it is enough to iterate over only a pair of neighboring elements of the string).