#include #include using namespace std; typedef long long ll; typedef pair P; const ll INF = 100000000000; ll dp[200002][2]; int main() { int n, m; cin >> n >> m; P p[400002]; for(int i = 0; i < n; i++){ ll a; cin >> a; p[i] = P(a, 0); } for(int i = 0; i < m; i++){ ll b; cin >> b; p[i + n] = P(b, 1); } p[n + m] = P(-INF, 0); sort(p, p + n + m + 1); int c = 0; for(int i = 1; i <= n + m; i++){ if(p[i].second){ c++; dp[c][0] = dp[c - 1][0] + p[i].first - p[i - 1].first; dp[c][1] = min(dp[c - 1][1] + p[i].first - p[i - 1].first, dp[c - 1][0]); } else{ dp[c][0] = min(dp[c][0], dp[c][1] + p[i].first - p[i - 1].first); dp[c][1] = dp[c][0]; } } cout << dp[c][0] << endl; }