首先很显然的是,无论怎么选,这里肯定会有一条贯穿的横向和一条纵向的骨架
那么问题来了,剩下的呢
按照大小一个一个插,不要有环出现
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
int n,m;
int a[300005],b[300005];
int ans;
signed main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
}
for(int j=1;j<=m;++j){
scanf("%d",&b[j]);
}
sort(a+1,a+n+1);
sort(b+1,b+m+1);
ans+=(a[1]*(m-1)+b[1]*(n-1));
int sum=n+m-2;
int pa=2;
int pb=2;
int h=1;
int l=1;
while(pa<=n&&pb<=m){
if(a[pa]<=b[pb]){
ans+=(long long)a[pa++]*(m-h);
l++;
}else{
ans+=(long long)b[pb++]*(n-l);
h++;
}
} cout<<ans;
return 0;
}