(三分)Codeforces Round #643 (Div. 2) E - Restorer Distance

48 篇文章 0 订阅
1 篇文章 0 订阅

这e题确实比c好写多了,只要会三分就能做出来,可惜c花太多时间没时间写e了。。

思路:很明显是凹函数,高度只有取恰当值才能取到最小值,那就三分高度,对于某一高度h,计算出要去除的砖块x,填补的砖块y,那么mi=min(x,y),对于mi,先判断(a+r)和m的大小,前者大就用前者,后者大就用后者,对于x-mi,直接用r代价消去,因为你没地方转移了,y-mi同理,用a代价填

具体做法看代码

 You have to restore the wall. The wall consists of N pillars of bricks, the height of the i-th pillar is initially equal to hi, the height is measured in number of bricks. After the restoration all the N

pillars should have equal heights.

You are allowed the following operations:

  • put a brick on top of one pillar, the cost of this operation is A
  • ;
  • remove a brick from the top of one non-empty pillar, the cost of this operation is R
  • ;
  • move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is M
  • .

You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes 0

.

What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?

Input

The first line of input contains four integers N

, A, R, M (1≤N≤105, 0≤A,R,M≤104

) — the number of pillars and the costs of operations.

The second line contains N

integers hi (0≤hi≤109

) — initial heights of pillars.

Output

Print one integer — the minimal cost of restoration.

Examples

Input

Copy

3 1 100 100
1 3 8

Output

Copy

12

Input

Copy

3 100 1 100
1 3 8

Output

Copy

9

Input

Copy

3 100 100 1
1 3 8

Output

Copy

4

Input

Copy

5 1 2 4
5 5 3 6 5

Output

Copy

4

Input

Copy

5 1 2 2
5 5 3 6 5

Output

#include<bits/stdc++.h>
#define LOCAL
using namespace std;
typedef unsigned u;
#define ll long long
#define maxn 1350500
#define esp 1e-10
ll h[maxn];ll n,a,r,m;
ll js(ll mid)
{
    ll x=0,y=0;
    for(ll i=1;i<=n;i++)
    {
        if(mid<h[i]) x+=h[i]-mid;
        else y+=mid-h[i];
    }
    ll mi=min(x,y);
    ll ans=r*(x-mi)+a*(y-mi);
    if((a+r)<m) ans+=mi*(a+r);
    else ans+=mi*m;
    return ans;
}
int main()
{

    cin>>n>>a>>r>>m;
    ll l=0,r=1e9+10,p=1e18;
    for(ll i=1;i<=n;i++) cin>>h[i];
    while(l<=r)
    {
        ll lmid=l+(r-l)/3,rmid=r-(r-l)/3;
        p=min(p,min(js(lmid),js(rmid)));
        //printf("l=%lld lmid=%lld rmid=%lld r=%lld js(%lld)=%lld js(%lld)=%lld\n",l,lmid,rmid,r,lmid,js(lmid),rmid,js(rmid));
        if(js(lmid)>=js(rmid)) l=lmid+1;
        else r=rmid-1;
    }
    printf("%lld\n",p);
    /*double l,r,p=0.0;
    cin>>n>>l>>r;
    for(int i=n;i>=0;i--) cin>>a[i];
    while(dcmp(l-r)<=0)
    {
        double lmid=l+(r-l)/3.0,rmid=r-(r-l)/3.0;
        //printf("l=%.5f lmid=%.5f rmid=%.5f r=%.5f js(%.5f)=%.5f js(%.5f)=%.5f\n",l,lmid,rmid,r,lmid,js(lmid),rmid,js(rmid));
        if(dcmp(js(lmid)-js(rmid))>=0) l=lmid+esp;
        else r=rmid-esp;
    }
    printf("%.5f %.5f\n",l,js(l));*/

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值