➤ Problem Link : 439D. Devu and his Brother
✅ C++ Solution :
#include<bits/stdc++.h> using namespace std; #define ll long long int int n,m; ll a[100001]; ll b[100001]; ll fun(int v) { ll curr=0; for(int i=0;i<n;i++) { if(a[i]<v) curr+=v-a[i]; } for(int i=0;i<m;i++) { if(b[i]>v) curr+=b[i]-v; } return curr; } int main() { cin>>n>>m; for(int i=0;i<n;i++) cin>>a[i]; for(int j=0;j<m;j++) cin>>b[j]; ll low=0,high=pow(10,9)+1,mid; while(low<high) { mid=low+(high-low+1)/2; if(fun(mid) < fun(mid-1)) low=mid; else high=mid-1; } cout<<fun(high); }
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!