#P11084. [ROI 2019] 灯串 (Day 2)

[ROI 2019] 灯串 (Day 2)

Background

Translated from ROI 2019 D2T1

For a sequence consisting of 0 and 1, the definition of “beautiful” is: if the number of 0 before the first 1 equals the number of 0 after the last 1, and the number of 0 between every two adjacent 1 is also equal to this value, then the sequence is called “beautiful”。

For example, 0101010 and 11111 are “beautiful”, but 10101 and 010100001000001010000 are not “beautiful”。

Problem Description

Given a sequence consisting of 0 and 1 (it is guaranteed that the number of 1 is not 00), you need to delete some elements at arbitrary positions so that the remaining sequence forms the longest possible beautiful string.

Input Format

The first line contains an integer nn, the length of the sequence。

The second line contains a string ss, consisting only of 0 and 1, representing the sequence。

Output Format

The first line outputs an integer mm, the length of the remaining sequence after deletions。

The second line outputs the “beautiful” sequence that remains after the deletions。

If there are multiple solutions, output any one。Note: a string consisting entirely of 0 is not “beautiful”。

10
0100100000
7
0001000
3
111
3
111
7
0100101
5
01010

Hint

Suppose the original string has kk 1s。

Subtask Score 1n1\le n\le 1k1\le k\le
11 2020 1515 nn
22 10001000 22
33 1515
44 1616 nn
55 1010 100000100000 5050
66 1414 500000500000 nn

Translated by ChatGPT 5