#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; for(int i = 0; i < x; i++) s -= a[i]; for(int i = x; i < k; i++) s += a[i]; for(int i = k; i < n; i++) s -= a[i]; ans = s + a[x] * (x * 2 + n - k * 2); for(int i = 0; x + i < n - 1 - x - 2; i += 2){ if(k + i / 2 >= n || x + i + 2 >= n) break; s += (a[i / 2] - a[x + i] - a[x + i + 1] + a[k + i / 2]) * 2; ans = min(ans, s + a[x + i + 2] * (x * 2 + n - k * 2 - (i / 2 + 1))); } } cout << ans << endl; }