#P8439. Key Collection for Constellations
Key Collection for Constellations
Key Collection for Constellations
A small robot is busy fishing for stars again. The stars in the sky form several constellations. Each constellation has a designated center — specifically, the star with the smallest number in that constellation. The constellation has a special property: it is connected and contains exactly as many contacts (edges) as stars. This means that if a constellation has n stars, it has n contacts, forming a unique cycle.
If any star loses its direct or indirect connection to the center, it will fall to the ground. The robot can use keys to cancel (remove) these contacts. However, note that if only one contact is removed in a cycle, the constellation turns into a tree and remains connected, so no star falls. Only after canceling a second contact can the robot disconnect a contiguous segment from the center, causing that entire segment (which can be chosen to be any size from 1 up to n-1) to drop.
Given several constellations, the robot wants to collect at least k fallen stars by using as few keys as possible. For each constellation with n stars, by using 2 keys the robot can drop any number of stars from 1 up to n-1 (by isolating the center as a single star). The robot may choose which constellations to operate on in order to reach his goal. Determine the minimum number of keys required to collect at least k fallen stars. If it is impossible, output -1.
Note: The robot can only use keys to cancel contacts, and canceling contacts in a constellation always costs keys in multiples of 2 (since canceling only one contact does not cause any stars to fall).
Input Overview: The input provides the number of constellations and the desired number of fallen stars followed by each constellation’s number of stars.
inputFormat
The first line contains two integers m and k (1 ≤ m ≤ 105, 0 ≤ k ≤ 109), where m is the number of constellations and k is the required number of fallen stars.
The second line contains m integers: a1, a2, ..., am (each ai ≥ 1), where ai denotes the total number of stars in the i-th constellation.
outputFormat
Output a single integer representing the minimum number of keys needed to obtain at least k fallen stars. If it is impossible, output -1.
sample
3 5
5 3 4
4