/* -*- coding: utf-8 -*- * * 1767.cc: No.1767 BLUE to RED - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int INF = (1U << 31) - 1; const int MINF = -(1 << 30); /* typedef */ /* global variables */ int as[MAX_N + 2], bs[MAX_N]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", as + i); as[0] = MINF, as[n + 1] = INF; n += 2; for (int i = 0; i < m; i++) scanf("%d", bs + i); int sum = 0; for (int i = 0, k = 0; i < m;) { while (as[k + 1] < bs[i]) k++; int j = i + 1; while (j < m && bs[j] < as[k + 1]) j++; int mind = INF; for (int l = i; l <= j; l++) { int d = 0; if (l > i) d += bs[l - 1] - as[k]; if (l < j) d += as[k + 1] - bs[l]; mind = min(mind, d); } sum += mind; i = j; } printf("%d\n", sum); return 0; }