/* -*- coding: utf-8 -*- * * 270.cc: No.270 next_permutation (1) - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_K = 100000; /* typedef */ typedef long long ll; /* global variables */ int ps[MAX_N], qs[MAX_N], bs[MAX_N]; /* subroutines */ ll dist(int n, int as[], int bs[]) { ll sum = 0; for (int i = 0; i < n; i++) sum += abs(as[i] - bs[i]); return sum; } /* main */ int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> ps[i]; for (int i = 0; i < n; i++) cin >> bs[i]; memcpy(qs, ps, sizeof(ps)); int fn = 1; for (int i = 1; i <= n; i++) { fn *= i; if (fn > k) { fn = k; break; } } ll sum = 0; for (int i = 0; i < fn; i++) { sum += dist(n, ps, bs); next_permutation(ps, ps + n); } if (fn < k) { sum *= k / fn; k %= fn; while (k--) { sum += dist(n, ps, bs); next_permutation(ps, ps + n); } } printf("%lld\n", sum); return 0; }