#P7285. Maximize x - y in a Binary Array

    ID: 20484 Type: Default 1000ms 256MiB

Maximize x - y in a Binary Array

Maximize x - y in a Binary Array

You are given an array of length n consisting of binary elements (0 or 1). You are allowed to choose any number (possibly zero) of elements equal to 0 and change them to 1.

Define:

  • \( x \): the length of the longest contiguous subarray of 1's in the final array (by definition, if the array contains only 0's, then \( x=0 \)).
  • \( y \): the number of elements that were modified (changed from 0 to 1).

Your task is to find a way to perform the modifications such that \( x - y \) is maximized. Output one valid modified array that achieves the maximum value \( x - y \).

Note: If no modification improves the value, you may output the original array.

inputFormat

The input consists of two lines:

  1. The first line contains a single integer \( n \), the length of the array.
  2. The second line contains \( n \) integers separated by spaces, each either 0 or 1.

outputFormat

Output the modified array on a single line: \( n \) integers separated by spaces. The modified array should achieve the maximum possible value of \( x - y \), where \( x \) is the length of the longest contiguous subarray of 1's and \( y \) is the number of modifications (0 to 1 changes).

sample

5
1 0 1 0 1
1 1 1 1 1