只得了42分,k=1 的情况全过了,但是 k=2 的情况有几个点WA,求查错
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;
#define impossible {puts("-1");return 0;}
#define ll long long
const int N=3e5+5;
int n,m,k;
ll a[N];
ll f(int l,int r){
ll kase=m-1;
ll ans=0;int i;
for(i=1;i<=m;++i){
if(kase>0){
ans+=kase*a[l++];
}
else if(kase<0){
break;
}
kase-=2;
}
kase=-m+1;
while(kase<0){
ans+=kase*a[r--];
kase+=2;
}
return ans;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;++i){
scanf("%lld",&a[i]);
}
if(k==2&&n==m) impossible;
sort(a+1,a+n+1,greater<ll>());
ll bans=f(1,n);
if(k==1){
printf("%lld\n",bans);
}
else{
ll ans1=f(2,n),ans2=f(1,n-1),ans=0;
if(ans1<bans) ans=max(ans,ans1);
if(ans2<bans) ans=max(ans,ans2);
printf("%lld\n",ans);
}
return 0;
}