cswork / OAOpen practice workspace

AppleOA #1

Ignore Sections

Estimated EasyJudge-ready

Topics (AI-labelled)SimulationIn-place Arrays

From left to right, delete each complete interval that starts with a 1 and ends at the first 0 after it, including both ends. Meeting another 1 inside an interval does not reset its start. A trailing segment with no closing 0 is kept in full, and all other elements are kept in their original order; also return the sum of the kept elements. The outputs of original examples 2 and 4 contradict the statement and explanation; this site corrects them according to the explicit statement.

Input

The first line contains n, and the second line contains n integers. 1≤n≤100000, −10⁹≤element≤10⁹.

Output

The first line is the number of kept elements m; the second line contains m integers (an empty line when m=0); the third line is the sum, which needs a 64-bit integer.

Example 1

Input:

9
6 1 7 0 2 5 6 1 3

Output:

6
6 2 5 6 1 3
23

Example 2

Input:

8
4 0 6 1 4 6 1 2

Output:

8
4 0 6 1 4 6 1 2
24

Example 3

Input:

9
0 6 1 1 9 1 3 0 2

Output:

3
0 6 2
8

Example notes

Example 1: delete 1, 7, 0 and keep 6, 2, 5, 6, 1, 3, sum 23. Example 2: neither of the two later 1s has a following 0, so the whole array is kept, sum 24; the original output dropped elements. Example 3: delete the interval from the first 1 to the following 0, keeping 0, 6, 2, sum 8; the original output omitted the 6.

Editorial

Loading solution…