luogu#P4989. 二进制之谜

二进制之谜

Background

Although she has already passed FadedFaded, Xiaomai still did not solve the binary mystery.

Problem Description

At this moment, she sensed that there might be some special possible correspondence between 00 and 11. 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 00 with 11 so that, while maximizing the number of correspondences, the sum of inspiration coefficients is as large as possible.

The matching rules are as follows:

11. Each correspondence must start with 00 and end with 11; in other words, in every correspondence, 00 must be before (higher bit), and 11 must be after (lower bit).

22. 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.

e.g.e.g. Suppose one correspondence is between the 22-nd digit and the 44-th digit, and another correspondence is between the 33-rd digit and the 55-th digit. Then they cannot both be chosen, because they cross on the interval [3,4][3,4]. However, if the correspondences are between the 11-st and 55-th digits, and between the 22-nd and 44-th digits, then this is not considered crossing, because although they share the interval [2,4][2,4], there is a containment relationship, so they can both be chosen.

avatar

That is, crossing is not the same as intersection.

33. Each digit can appear in at most one correspondence.

Input Format

The first line contains an integer nn, the number of bits in the binary number.

The next line contains an nn-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 3030% of the data, 0<n<=200<n<=20.

For 100100% of the data, 0<n<=3000<n<=300.

Sample Explanation

For sample 1, since 11 is before 00, they cannot be matched.

For sample 2, the matching plan is 343-4, so the total sum is 11.

If you have already AKAK, you can try the Enhanced Data Version.

Translated by ChatGPT 5