luogu#P4989. 二进制之谜
二进制之谜
Background
Although she has already passed , Xiaomai still did not solve the binary mystery.
Problem Description
At this moment, she sensed that there might be some special possible correspondence between and . So, she defined the “inspiration coefficient” as the absolute value of the difference between the digit positions (counted from high to low) of a corresponding pair. She now wants to match with so that, while maximizing the number of correspondences, the sum of inspiration coefficients is as large as possible.
The matching rules are as follows:
. Each correspondence must start with and end with ; in other words, in every correspondence, must be before (higher bit), and must be after (lower bit).
. You may choose several correspondences, but correspondences must not cross. The meaning of crossing is: they share some interval and it is not a containment relationship.
Suppose one correspondence is between the -nd digit and the -th digit, and another correspondence is between the -rd digit and the -th digit. Then they cannot both be chosen, because they cross on the interval . However, if the correspondences are between the -st and -th digits, and between the -nd and -th digits, then this is not considered crossing, because although they share the interval , there is a containment relationship, so they can both be chosen.

That is, crossing is not the same as intersection.
. Each digit can appear in at most one correspondence.
Input Format
The first line contains an integer , the number of bits in the binary number.
The next line contains an -bit binary number.
Output Format
One line, representing the maximum possible sum of inspiration coefficients under the condition that the number of correspondences is maximized.
2
10
0
6
110100
1
Hint
For % of the data, .
For % of the data, .
Sample Explanation
For sample 1, since is before , they cannot be matched.
For sample 2, the matching plan is , so the total sum is .
If you have already , you can try the Enhanced Data Version.
Translated by ChatGPT 5