#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define MAX 100002 int n; int k; long long int p[MAX]; long long int b[MAX]; bool visited[MAX]; long long int dist = 0; long long int summ = 0; bool flag[MAX]; set s; set::iterator ite; inline void dfs(int a){ if (a == n){ k--; summ += dist; return; } int be = 1; ite = s.begin(); if (!visited[a]){ be = p[a]; ite = s.lower_bound(be); visited[a] = true; } for (; ite != s.end(); ite++){ int i = (*ite); s.erase(ite); dist += (long long int)(abs(i - b[a])); dfs(a + 1); dist -= (long long int)(abs(i - b[a])); s.insert(i); ite = s.lower_bound(i); if (k == 0){ return; } } } int main(){ scanf("%d%d", &n, &k); for (int i = 0; i < n; i++){ scanf("%d", &p[i]); } for (int i = 0; i < n; i++){ scanf("%d", &b[i]); } for (int i = 1; i <= n; i++){ s.insert(i); } int countt3 = 0; while (k){ dfs(0); } printf("%lld\n", summ); return 0; }