#P3030. Barn Floor Remodeling
Barn Floor Remodeling
Barn Floor Remodeling
Farmer John wants to remodel the floor of his barn using a collection of square tiles. He originally purchased N square tiles with side lengths \(A_1, A_2, \ldots, A_N\). He would like to exchange some of these tiles for new square tiles so that the total sum of the tile areas becomes exactly \(M\). When exchanging a tile, a tile with side length \(A_i\) can be exchanged for a new tile with side length \(B_i\) at a cost of \(|A_i - B_i|^2\) units. Note that each tile can only be exchanged once (i.e. you cannot exchange an already exchanged tile).
Determine the minimum total cost required so that after the exchanges the sum of the areas of the tiles is exactly \(M\). If it is impossible, output \(-1\).
inputFormat
The input consists of two lines. The first line contains two integers \(N\) and \(M\) where \(N\) is the number of tiles and \(M\) is the target total area. The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N\) representing the side lengths of the original tiles.
outputFormat
Output a single integer: the minimum cost required to exchange the tiles such that the sum of the areas equals \(M\). If it is impossible to obtain exactly \(M\), output \(-1\).
sample
3 14
1 2 3
0