-
Notifications
You must be signed in to change notification settings - Fork 0
/
439D.cpp
49 lines (47 loc) · 999 Bytes
/
439D.cpp
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
43
44
45
46
47
48
49
#include <bits/stdc++.h>
#define int long long
using namespace std;
//Solution + fenwick tree
int f(int x, vector<int>&a, vector<int>&b)
{
int d = 0;
for(int i = 0; i < a.size(); i++)
{
if(a[i] < x) d += (x-a[i]);
}
for(int i = 0; i < b.size(); i++)
{
if(b[i]>x)d+=(b[i]-x);
}
return d;
}
main()
{
int n,m;
cin >> n >> m;
vector<int> a(n);
vector<int> b(m);
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < m; i++) cin >> b[i];
int l = 1;
int r = 1e10;
while (r - l > 1)
{
int m1 = l + (r - l) / 3,
m2 = r - (r - l) / 3;
// cout <<l << " "<<r << " " << m1 << " " << m2 << endl;;
if (f(m1,a,b) > f(m2,a,b)){
if(l==m1)break;
l = m1;
}
else{
if(r==m2)break;
r = m2;
}
}
int minx = 1e18;
for(int i = l-10; i <= r+10; i++) {
minx=min(minx,f(i,a,b));
}
cout << minx;
}