#P10382. Replace Zeros to Form Acyclic Forest

    ID: 12389 Type: Default 1000ms 256MiB

Replace Zeros to Form Acyclic Forest

Replace Zeros to Form Acyclic Forest

You are given an integer x and a sequence a of length n. Each element of a is an integer in the range \([-1,n]\) (note that some elements may be 0). A sequence \(a\) is called legal if it satisfies the following conditions:

  • For each \(i\) (\(1 \le i \le n\)), either \(a_i = -1\) or \(a_i \in [1,n]\).
  • For every \(i\) with \(a_i \neq -1\), if you add a directed edge from \(a_i\) to \(i\) (i.e. \(a_i \to i\)), the resulting directed graph contains no cycle.

Your task is to replace every occurrence of 0 in \(a\) with some integer from \([-1,n]\) (i.e. either \(-1\) or a number in \([1,n]\)) so that:

  1. The sum satisfies \(\sum_{i=1}^{n} a_i = x\).
  2. The modified sequence is legal (i.e. when considering all \(i\) with \(a_i \neq -1\) and adding the edge \(a_i \to i\), the graph does not have any cycles). Note that an edge from a vertex to itself (i.e. \(a_i = i\)) is considered a cycle.

If there is no way to obtain such a sequence, report -1.

Note: It is guaranteed that the input sequence has all elements in \([-1,n]\), and only those positions where \(a_i=0\) can be changed.

inputFormat

The first line contains two integers n and x (n is the length of the sequence, and x is the required sum).

The second line contains n space‐separated integers \(a_1, a_2, \ldots, a_n\). Elements with value 0 are the ones you are allowed to replace.

outputFormat

If a solution exists, output the modified sequence as n space‐separated integers. Otherwise, output -1.

sample

3 2
0 0 0
-1 1 2

</p>