#include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) typedef long long ll; using namespace std; int main() { int n, k; cin >> n >> k; vector a(n); repeat (i,n) cin >> a[i]; vector b(n); repeat (i,n) cin >> b[i]; ll ans = 0; int l = n; // TLE ll base = 0; repeat (i,l) base += abs(a[i] - b[i]); while (k --) { ans += base; repeat_from (i,l,n) ans += abs(a[i] - b[i]); bool is_not_overflow = next_permutation(a.begin() + l, a.end()); if (not is_not_overflow) { prev_permutation(a.begin() + l, a.end()); next_permutation(a.begin(), a.end()); base = 0; repeat (i,l) base += abs(a[i] - b[i]); } } cout << ans << endl; return 0; }