#include <bits/stdc++.h>
#define fi first
#define se second
#define NAME "wiring"
using namespace std;
typedef long long LL;
typedef pair <int, int> pii;
typedef double DB;
typedef pair <LL, LL> pLL;
const int NMAX = 1e6+7;
int n, m, x[NMAX], y[NMAX], f[NMAX];
pii a[NMAX];
int64_t min_total_length(int r[], int b[]) {
int64_t res = 0;
if(r[n-1] < b[0]) {
for(int i = 0; i < n; ++i)
res += abs(b[0] - r[i]);
for(int i = 0; i < m; ++i)
res += abs(r[n-1] - b[i]);
return res;
}
for(int i = 0; i < n; ++i) {
a[i].fi = r[i];
a[i].se = 0;
}
for(int i = 0; i < m; ++i) {
a[i+n].fi = b[i];
a[i+n].se = 1;
}
int nm = n + m;
sort(a, a + nm);
int i = 0, j = 0, ck = 0;
while(i < nm) {
while(j+1 < nm && a[j+1].se == a[i].se) j++;
int pos1, pos2;
if(i > 0) pos1 = a[i-1].fi;
else pos1 = INT_MAX;
if(j < nm-1) pos2 = a[j+1].fi;
else pos2 = INT_MAX;
for(int t = i; t <= j; ++t)
res += min(abs(pos1 - a[t].fi), abs(pos2 - a[t].fi));
if(i > 0 && ck == 1)
res -= abs(pos1 - a[i].fi);
if(abs(pos1 - a[j].fi) > abs(pos2 - a[j].fi)) ck = 1;
else ck = 0;
j++; i = j;
}
return res;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(NAME".inp", "r", stdin);
//freopen(NAME".out", "w", stdout);
cin >> n >> m;
for(int i = 0; i < n; ++i)
cin >> x[i];
for(int i = 0; i < m; ++i)
cin >> y[i];
cout << min_total_length(x, y);
}
Compilation message
/tmp/cckW3pyH.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccAeZy25.o:wiring.cpp:(.text.startup+0x0): first defined here
/tmp/cckW3pyH.o: In function `main':
grader.cpp:(.text.startup+0x23b): undefined reference to `min_total_length(std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status