1 solutions
-
0
#include<bits/stdc++.h> using namespace std; #define ll long long ll n,m,l,r,ans=0; const ll N=1e8+5; ll a[N],b[N]; ll find(ll g){ l=1,r=n; while(l<=r){ ll mid=(l+r)/2; if(a[mid]>=g){ r=mid-1; } else{ l=mid+1; } } return l; } int main(){ cin>>n>>m; for(ll i=1;i<=n;i++){ cin>>a[i]; } sort(a+1,a+n+1); for(ll i=1;i<=m;i++){ cin>>b[i]; if(b[i]<=a[1]){ ans+=a[1]-b[i]; } else{ ans+=min(abs(a[find(b[i])]-b[i]),abs(a[find(b[i])-1]-b[i])); } } cout<<ans; return 0; }
- 1
Information
- ID
- 1235
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 6
- Tags
- # Submissions
- 76
- Accepted
- 23
- Uploaded By