CodeForces - 589 B Layer Cake

B. Layer Cake

time limit per test

6 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n rectangular cake layers. The length and the width of the i-th cake layer were ai and bi respectively, while the height of each cake layer was equal to one.

From a cooking book Dasha learned that a cake must have a form of a rectangular parallelepiped constructed from cake layers of the same sizes.

扫描二维码关注公众号,回复: 2601146 查看本文章

Dasha decided to bake the biggest possible cake from the bought cake layers (possibly, using only some of them). It means that she wants the volume of the cake to be as big as possible. To reach this goal, Dasha can cut rectangular pieces out of the bought cake layers. She always cuts cake layers in such a way that cutting lines are parallel to the edges of that cake layer. Dasha isn't very good at geometry, so after cutting out a piece from the original cake layer, she throws away the remaining part of it. Also she can rotate a cake layer in the horizontal plane (swap its width and length).

Dasha wants her cake to be constructed as a stack of cake layers of the same sizes. Each layer of the resulting cake should be made out of only one cake layer (the original one or cut out from the original cake layer).

Help Dasha to calculate the maximum possible volume of the cake she can bake using given cake layers.

Input

The first line contains an integer n (1 ≤ n ≤ 4000) — the number of cake layers that Dasha can use.

Each of the following n lines contains two integer numbers ai and bi (1 ≤ ai, bi ≤ 106) — the length and the width of i-th cake layer respectively.

Output

The first line of the output should contain the maximum volume of cake that can be baked using given layers.

The second line of the output should contain the length and the width of the resulting cake. If there are many solutions with maximum possible volume, print any of them.

Examples

Input

Copy

5
5 12
1 1
4 6
6 4
4 6

Output

Copy

96
6 4

Input

Copy

2
100001 900000
900001 100000

Output

Copy

180000000000
900000 100000

Note

In the first example Dasha doesn't use the second cake layer. She cuts 4 × 6 rectangle from the first cake layer and she uses other cake layers as is.

In the second example Dasha cuts off slightly from the both cake layers.

题意:给你很多个矩形蛋糕,现在这些蛋糕堆在一起,你去切这个蛋糕,你可以舍弃部分蛋糕,因为你要保证每一层蛋糕的长宽都相等,同时你需要保证你的这个蛋糕是所有方案里体积最大的,输出你求的长和宽以及此时蛋糕的体积。

思路:把所有的蛋糕按从大到小的顺序堆在一起(排列)(优先长和宽都可以)(我优先的宽),然后从第一层(宽最大的那一层)开始,逐渐增加层数( i ),对于 i 层蛋糕,把他们的长都放到一个数组里面,然后从小到大排这些长,从最小的长开始( j ),以 i 层的宽为所有这( i-j+1 )层蛋糕的宽,第 j 层的长为所有这( i-j+1 )层蛋糕的长计算此时这( i-j+1 )层蛋糕的体积,如果这个蛋糕的体积会大于此前这样得到的体积就更新长,宽和体积,直到所有的 i 层都遍历完为止。

AC代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<deque>
#include<queue>
#include<list>
const int inf=0x3f3f3f3f;
const int MOD=1e9+7;
#define ll long long
#define ME0(x) memset(x,0,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
#define MEF(x) memset(x,-1,sizeof(x))
using namespace std;
//~~~~~~~~~~~~~~~~~~~~~~~~B
struct comp
{
    ll x,y;
    bool operator < (const comp & a) const
     {
        return a.x<x;
    }
}s[4005];
int main()
{
    ll n,a,b,u,v;
    cin>>n;
    for(int n1=1;n1<=n;n1++)
    {
        cin>>u>>v;
        a=min(u,v);
        b=max(u,v);
        s[n1].x=a;
        s[n1].y=b;
    }
    sort(s+1,s+1+n);
    //sort(s+1,s+1+n,cmp);
    ll len=0,al[4000],ans=0,sum;
    for(int n1=1;n1<=n;n1++)
    {
        al[++len]=s[n1].y;//这里好像直接用 n1 而不用 ++len 也可以,当时没发现,就不去改了
        sort(al+1,al+1+len);
        for(int len1=1;len1<=len;len1++)
        {
            sum=s[n1].x*al[len1]*(len+1-len1);
            if(sum>ans)
            {
                ans=sum;
                a=al[len1];
                b=s[n1].x;
            }
        }
    }
    cout<<ans<<endl;
    cout<<a<<' '<<b<<endl;
}

猜你喜欢

转载自blog.csdn.net/ecjtu_17_TY/article/details/81455392