#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]; ll ans = INF; int x = 0; if(k * 2 >= n) x = (k * 2 - n) / 2; 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); } cout << ans << endl; }