用线段树模拟一下就好了~
code:
#include <cstdio> #include <algorithm> #define lson ls[x] #define rson rs[x] #define N 10000006 #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; ll A,B; int n,k,tot; int ls[N],rs[N],sum[N]; void update(int &x,int l,int r,int p) { if(!x) x=++tot; ++sum[x]; if(l==r) return; int mid=(l+r)>>1; if(p<=mid) update(lson,l,mid,p); else update(rson,mid+1,r,p); } ll query(int x,int l,int r) { if(!x) return A; if(l==r) return B*sum[x]; int mid=(l+r)>>1; ll re=B*1ll*(r-l+1)*sum[x]; return min(re,query(lson,l,mid)+query(rson,mid+1,r)); } int main() { // setIO("input"); int i,j,bs=1,rt=0; scanf("%d%d%lld%lld",&n,&k,&A,&B); for(i=1;i<=n;++i) bs<<=1; for(i=1;i<=k;++i) { int po; scanf("%d",&po),update(rt,1,bs,po); } printf("%lld\n",query(rt,1,bs)); return 0; }