Tile Exchanging

Time limit1sMemory limit128 MB

Summary
For each of N tiles, pick a new side length (or keep the old one) so the total area equals M, minimizing the sum of squared side-length changes.
Level

Medium4 of 10

Topics
Dynamic programming, Math
Solved
No attempts yet

Problem

Farmer John wants to redo the floor of his barn using a collection of square tiles he bought from the local square mart (which, of course, only sells square objects). Unfortunately, he didn't measure the barn correctly before buying, so now he has to exchange some of his tiles for new square tiles of different sizes.

The NN square tiles Farmer John already owns have side lengths A1,A2,…,ANA_1, A_2, \dots, A_N. He wants to exchange some of them for new square tiles so that the total sum of the areas of his tiles becomes exactly MM. A tile of side length AiA_i can be exchanged for a new tile of side length BiB_i at a cost of ∣Ai−Bi∣×∣Ai−Bi∣|A_i - B_i| \times |A_i - B_i|. This offer applies only to the originally purchased tiles: a tile obtained through an exchange cannot itself be exchanged again (for example, a side-3 tile cannot be exchanged for a side-2 tile which is then exchanged for a side-1 tile).

Determine the minimum total cost needed to make the sum of the tile areas equal to MM. If it is impossible to reach a total area of MM, output −1-1.

Input

  • The first line contains two space-separated integers NN and MM (1≤N≤101 \le N \le 10, 1≤M≤100001 \le M \le 10000).
  • Each of the next NN lines contains one integer AiA_i, the side length of the ii-th square tile (1≤Ai≤1001 \le A_i \le 100).

Output

Print the minimum cost of exchanging tiles so that the total area becomes MM, or −1-1 if this is impossible.

Hint

In the first example there are 3 tiles: two squares of side length 3 and one square of side length 1, and the goal is a total area of 6. Exchange one side-3 square for a side-1 square at a cost of (3−1)2=4(3-1)^2 = 4, and the other side-3 square for a side-2 square at a cost of (3−2)2=1(3-2)^2 = 1. The total area then becomes 1+4+1=61 + 4 + 1 = 6 and the total cost is 4+1=54 + 1 = 5.

Examples3

  1. Example 1

    Input
    3 6
    3
    3
    1
    
    Expected output
    5
    
  2. Example 2

    Input
    1 1
    1
    
    Expected output
    0
    
  3. Example 3

    Input
    1 2
    1
    
    Expected output
    -1