-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path131-devu-and-his-brother.cpp
More file actions
42 lines (41 loc) · 1.11 KB
/
Copy path131-devu-and-his-brother.cpp
File metadata and controls
42 lines (41 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Devu and his Brother [439D]
* Problem: https://codeforces.com/problemset/problem/439/D
* Verdict: ACCEPTED Solved: 2023-06-03
* Language: C++20 (GCC 11-64)
* Runtime: 61 ms Memory: 1600 KB
* Tags: binary search, sortings, ternary search, two pointers
* Author: BidoTeima
* Source: https://codeforces.com/contest/439/submission/208347079
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 3;
int n,m;
int a[N],b[N];
ll f(int x){
ll ret = 0;
for(int i = 0; i < n; i++)ret += max(0, x - a[i]);
for(int i = 0; i < m; i++)ret += max(0, b[i] - x);
return ret;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i = 0; i < n; ++i)cin>>a[i];
for(int i = 0; i < m; ++i)cin>>b[i];
int l = 0, r = 1e9;
ll ans = 1e18;
while(l <= r){
int mid1 = l + (r - l) / 3;
int mid2 = r - (r - l) / 3;
if(f(mid1) >= f(mid2)){
l = mid1 + 1;
ans=min(ans,f(mid2));
}else r = mid2 - 1, ans = min(ans,f(mid1));
}
cout<<ans;
return 0;
}