A. Boboniu Chats with Du
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.
In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.
Du will chat in the group for nn days. On the ii-th day:
- If Du can speak, he'll make fun of Boboniu with fun factor aiai. But after that, he may be muzzled depending on Boboniu's mood.
- Otherwise, Du won't do anything.
Boboniu's mood is a constant mm. On the ii-th day:
- If Du can speak and ai>mai>m, then Boboniu will be angry and muzzle him for dd days, which means that Du won't be able to speak on the i+1,i+2,⋯,min(i+d,n)i+1,i+2,⋯,min(i+d,n)-th days.
- Otherwise, Boboniu won't do anything.
The total fun factor is the sum of the fun factors on the days when Du can speak.
Du asked you to find the maximum total fun factor among all possible permutations of aa.
Input
The first line contains three integers nn, dd and mm (1≤d≤n≤105,0≤m≤1091≤d≤n≤105,0≤m≤109).
The next line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109).
Output
Print one integer: the maximum total fun factor among all permutations of aa.
Examples
input
Copy
5 2 11 8 10 15 23 5
output
Copy
48
input
Copy
20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7
output
Copy
195
Note
In the first example, you can set a′=[15,5,8,10,23]a′=[15,5,8,10,23]. Then Du's chatting record will be:
- Make fun of Boboniu with fun factor 1515.
- Be muzzled.
- Be muzzled.
- Make fun of Boboniu with fun factor 1010.
- Make fun of Boboniu with fun factor 2323.
Thus the total fun factor is 4848.
=========================================================================
把小于等于m这些不会带来禁言的放在一组,把剩下的大于m的放在一组。
然后暴力枚举一下该选几个禁言的,显然需要从大到小更优,而且其安排规则体现了贪心的思想
首先是0个,那么显然是全部小于等于m的累加即可
然后是1个,那么就是在小于等于的集合后面加一个最大的即可
然后是2个,也就是第一个后面放d个做填充,再放第二个。
显然优先用大于m的这些进行填充,然后不够了再用小于等于m的进行填充
# include<bits/stdc++.h>
using namespace std;
# define mod 1000000007
typedef long long int ll;
ll a[100000+10],b[100000+10],suma[100000+10],sumb[100000+10],bac[100000+10];
int lena,lenb;
bool cmp(ll x, ll y)
{
return x>y;
}
int main ()
{
int n,d,m;
cin>>n>>d>>m;
ll ans=0;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
if(x<=m)
{
lenb++;
b[lenb]=x;
ans+=x;
}
else
{
lena++;
a[lena]=x;
}
}
sort(a+1,a+1+lena,cmp);
sort(b+1,b+1+lenb,cmp);
for(int i=1;i<=lena;i++)
{
suma[i]=suma[i-1]+a[i];
//cout<<a[i]<<" ";
}
//cout<<endl;
for(int i=1;i<=lenb;i++)
{
sumb[i]=sumb[i-1]+b[i];
// cout<<b[i]<<" ";
}
//cout<<endl;
for(int i=lenb;i>=1;i--)
{
bac[i]=bac[i+1]+b[i];
}
for(int cnt=1;cnt<=lena;cnt++)
{
int nowneed=(cnt+(cnt-1)*d);
ll nowa=suma[cnt];
ll nowkong=(cnt-1)*d;
//需要这些来填充
ll cntbac=lena-cnt; //现在有这么多a
if(cntbac>=nowkong)
{
//a是够用的
ans=max(ans,suma[cnt]+sumb[lenb]);
// cout<<cnt<<" "<<suma[cnt]+sumb[lenb]<<" "<<suma[cnt]<<" "<<sumb[lenb]<<endl;
}
else
{
//a不够用,要拿b一点
int nowb=nowkong-cntbac;
if(nowb>lenb) //b也不够,不整了
break;
ans=max(ans,suma[cnt]+sumb[lenb]-bac[lenb-nowb+1]);
//cout<<cnt<<" "<<suma[cnt]+sumb[lenb]-bac[n-nowb+1]<<" "<<suma[cnt]<<" "<<sumb[lenb]<<" "<<bac[n-nowb+1]<<endl;
}
}
cout<<ans;
return 0;
}