8296: Tile Exchanging

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:7 通过:7

题目描述

农夫约翰想用他最近从当地的广场集市上购买的一批正方形瓷砖来改造牛棚的地板。

不幸的是,在购买瓷砖之前,他没有准确测量牛棚的大小,所以现在他需要把他的一部分瓷砖换成不同尺寸的新正方形瓷砖。

约翰之前购买的 N 块正方形瓷砖的边长为 A1,…,AN。

他想换取一些新的瓷砖,使得他拥有的瓷砖的面积之和等于 M。

集市目前提供一项特殊优惠:花费 |Ai−Bi|×|Ai−Bi|元钱,就可以将边长为 Ai 的瓷砖换成边长为 Bi 的新瓷砖。

但是此项交易只适用于先前购买的瓷砖,不能用交换得到的瓷砖进行再次交换(也就是不能先将边长为 3 的瓷砖换成边长为 2 的瓷砖,再用此边长为 2 的瓷砖换成边长为 1 的瓷砖)。

请确定通过交换瓷砖使得瓷砖总面积等于 M 所需花费的最小金额。

注意,集市只提供整数边长的瓷砖。

输入格式

第一行包含两个整数 N 和 M。

接下来 N 行,每行包含一个整数表示 Ai

输出格式

输出交换瓷砖的最小花费。

如果无法通过交换瓷砖使得瓷砖总面积等于 M,则输出 1

数据范围

1≤N≤10,
1≤M≤10000,
1≤Ai≤100

输入样例:

3 6
3
3
1

输出样例:

5

样例解释

将一个边长为 3 的瓷砖换成边长为 1 的瓷砖,将另一个边长为 3 的瓷砖换成边长为 2 的瓷砖。

即可获得总面积为 6 的瓷砖,花费为 4+1=5。


Problem 3: Tile Exchanging [Ray Li]


Farmer John wants to remodel the floor of his barn using a collection of
square tiles he recently purchased from the local square mart store (which
of course, only sells square objects).  Unfortunately, he didn't measure
the size of the barn properly before making his purchase, so now he needs
to exchange some of his tiles for new square tiles of different sizes.


The N square tiles previously purchased by FJ have side lengths A_1...A_N.
He would like to exchange some of these with new square tiles so that the
total sum of the areas of the his tiles is exactly M.  Square mart is
currently offering a special deal: a tile of side length A_i can be
exchanged for a new tile of side length B_i for a cost of
|A_i-B_i|*|A_i-B_i| units. However, this deal only applies to
previously-purchased tiles -- FJ is not allowed to exchange a tile that he
has already obtained via exchanging some other tile (i.e., a size-3 tile
cannot be exchanged for a size-2 tile, which is then exchanged for a size-1
tile).


Please determine the minimum amount of money required to exchange tiles so
that the sum of the areas of the tiles becomes M.  Output -1 if it is
impossible to obtain an area of M.


PROBLEM NAME: tilechng


INPUT FORMAT:


* Line 1: Two space-separated integers, N (1<=N<=10) and M
       (1<=M<=10,000).


* Lines 2..1+N: Each line contains one of the integers A_1 through
       A_N, describing the side length of an input square
       (1<=A_i<=100).


SAMPLE INPUT (file tilechng.in):


3 6
3
3
1


INPUT DETAILS:


There are 3 tiles.  Two are squares of side length 3, and one is a square
with side length 1.  We would like to exchange these to make a total area of 6.


OUTPUT FORMAT:


* Line 1: The minimum cost of exchanging tiles to obtain M units of
       total area, or -1 if this is impossible.


SAMPLE OUTPUT (file tilechng.out):


5


OUTPUT DETAILS:


Exchange one of the side-3 squares for a side-2 square, and another side-3
square for a side-1 square.  This gives the desired area of 4+1+1=6 and
costs 4+1=5 units.

输入格式

INPUT FORMAT:


* Line 1: Two space-separated integers, N (1<=N<=10) and M
       (1<=M<=10,000).


* Lines 2..1+N: Each line contains one of the integers A_1 through
       A_N, describing the side length of an input square
       (1<=A_i<=100).

输出格式

OUTPUT FORMAT:


* Line 1: The minimum cost of exchanging tiles to obtain M units of
       total area, or -1 if this is impossible.

输入样例 复制

3 6
3
3
1

输出样例 复制

5