#include #include using namespace std; typedef long long ll; const ll INF = 100000000000000000; int main() { int n, k; cin >> n >> k; ll a[200005]; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = n - 1; i >= 0; i--) a[i] -= a[0]; ll ans = INF; int x = 0; if(k * 2 >= n) x = (k * 2 - n) / 2; if(k * 2 < n){ for(int c = 0; c < 2; c++){ ll s = 0; reverse(a, a + n); for(int i = 0; i < k; i++) s += abs(a[i] - a[x]); for(int i = k; i < n; i++) s -= abs(a[i] - a[x]); ans = min(ans, s); } } else{ ll s = 0; ll l = 0; for(int i = 0; i < n; i++){ if(i >= x && i < k){ s += a[i]; l--; } else{ s -= a[i]; l++; } } ans = s + a[x] * l; for(int i = 0; x + i <= n - 1 - x - 2; i += 2){ s += (a[i / 2] - a[x + i] - a[x + i + 1] + a[k + i / 2]) * 2; ans = min(ans, s + a[x + i + 2] * l); } } cout << ans << endl; }