分析
既然每一刀都要切,那肯定代价越大的要越早切,
考虑按代价降序排序,如果切了一行,求切列的时候贡献的行数就多了1。
代码
#include <cstdio>
#include <cctype>
#include <algorithm>
#define rr register
using namespace std;
struct rec{int w,c;}a[20011];
int n,m,ans,cnt[2];
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
bool cmp(rec x,rec y){return x.w>y.w;}
signed main(){
n=iut()-1,m=iut()-1;
for (rr int i=1;i<=n;++i) a[i]=(rec){iut(),0};
for (rr int i=1;i<=m;++i) a[i+n]=(rec){iut(),1};
sort(a+1,a+1+n+m,cmp);
for (rr int i=1;i<=n+m;++i)
ans+=a[i].w*(cnt[a[i].c^1]+1),++cnt[a[i].c];
return !printf("%d",ans);
}