#include #include #include #include #include #include #include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; k = n - k; vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } ll r = 1LL << 60; for (int h = 0; h < 2; h++) { int x = a[min(k + n / 2, n - 1)]; ll t = 0; for (int i = 0; i < n; i++) { t += abs(x - a[i]) * (i < k ? -1 : 1); } r = min(r, t); reverse(a.begin(), a.end()); for (int i = 0; i < n; i++) { a[i] *= -1; } } cout << r << endl; return 0; }