codeforces 1111C. Creative Snap

本文介绍了一种解决Thanos如何使用最小能量摧毁复仇者基地的问题的算法。基地长度为2的幂,Thanos可以整体摧毁或分成两半分别摧毁,能量消耗依赖于基地中复仇者数量及基地长度。文章详细解析了算法实现,包括输入参数、输出结果、示例和代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Creative Snap

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base.

Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of 22. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following:

  • if the current length is at least 22, divide the base into 22 equal halves and destroy them separately, or
  • burn the current base. If it contains no avenger in it, it takes AA amount of power, otherwise it takes his B⋅na⋅lB⋅na⋅l amount of power, where nana is the number of avengers and ll is the length of the current base.

Output the minimum power needed by Thanos to destroy the avengers' base.

Input

The first line contains four integers nn, kk, AA and BB (1≤n≤301≤n≤30, 1≤k≤1051≤k≤105, 1≤A,B≤1041≤A,B≤104), where 2n2n is the length of the base, kkis the number of avengers and AA and BB are the constants explained in the question.

The second line contains kk integers a1,a2,a3,…,aka1,a2,a3,…,ak (1≤ai≤2n1≤ai≤2n), where aiai represents the position of avenger in the base.

Output

Output one integer — the minimum power needed to destroy the avengers base.

Examples

input

Copy

2 2 1 2
1 3

output

Copy

6

input

Copy

3 2 1 2
1 7

output

Copy

8

Note

Consider the first example.

One option for Thanos is to burn the whole base 1−41−4 with power 2⋅2⋅4=162⋅2⋅4=16.

Otherwise he can divide the base into two parts 1−21−2 and 3−43−4.

For base 1−21−2, he can either burn it with power 2⋅1⋅2=42⋅1⋅2=4 or divide it into 22 parts 1−11−1 and 2−22−2.

For base 1−11−1, he can burn it with power 2⋅1⋅1=22⋅1⋅1=2. For 2−22−2, he can destroy it with power 11, as there are no avengers. So, the total power for destroying 1−21−2 is 2+1=32+1=3, which is less than 44.

Similarly, he needs 33 power to destroy 3−43−4. The total minimum power needed is 66.

 

放假太划水  回来水题练手先

这题主要是看k范围,虽然区间为1-1e9,但是由于k只有5次方,很多区间都是没有avengers的,所以直接暴力就行。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll num,sum,t,n,m,x,y,k;
ll a[100005];
ll dfs(int l,int r){
    ll ans;
    ll len=upper_bound(a+1,a+k+1,r)-lower_bound(a+1,a+k+1,l);
    if(!len)
        return ans=x;
    else
        ans=(r-l+1)*len*y;
    if(r-l<1)
        return ans;
    ll mid=(l+r)/2;
    ans=min(ans,dfs(l,mid)+dfs(mid+1,r));
    return ans;
}
int main(){
    ll i,j,l,r;
    while(~scanf("%lld%lld%lld%lld",&n,&k,&x,&y)){
        for(i=1;i<=k;i++)
            scanf("%lld",&a[i]);
        sort(a+1,a+k+1);
        num=1;
        while(n--)
            num*=2;
        ll ans=dfs(1,num);
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值