题意:
解法:
显然将快乐值最大的和痛苦值最小的匹配,
将a[]和b[]从小到大排序,令c[i]=b[i]-a[n-i+1],
则c[]为每次能获得的净快乐值,将c[]从大到小排序.
令d[i]为跑i次能获得的最大值,dp转移方程:
d[i]=d[i-1]+c[i]+(i%3==0?m:0),
每跑3次需要加上一个m.
最后ans=max(d[i]).
code:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e6+5;
int d[maxm];
int a[maxm];
int b[maxm];
int c[maxm];
int n,m;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
sort(a+1,a+1+n);
sort(b+1,b+1+n);
for(int i=1;i<=n;i++)c[i]=b[i]-a[n-i+1];
sort(c+1,c+1+n);
reverse(c+1,c+1+n);
for(int i=1;i<=n;i++){
d[i]=d[i-1]+c[i];
if(i%3==0)d[i]+=m;
}
int ans=0;
for(int i=1;i<=n;i++)ans=max(ans,d[i]);
cout<<ans<<endl;
return 0;
}