#include #include #include #include #include #include #include #include using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 1'000'000'007; struct fast_io { fast_io(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); }; } fio; int f(const vector &A, const vector &B, int x){ int sum = 0; int N = A.size(); for(int i = 0; i < N; i++){ sum += B[i] * abs(A[i] - x); } return sum; } int minf(const vector &A, const vector &B, int K){ int sum = 0, res = INF; int N = A.size(); int a = 0, b = 0; int ca = 0, cb = 0; int sa = 0, sb = 0; for(int i = 0; i < N; i++){ if(B[i] > 0) { sa += A[i]; } else { sb += A[i]; } } for(int i = 0; i < N; i++){ sum = 0; sum += (sa - a) - A[i] * (K - ca); sum -= (sb - b) - A[i] * (N - K - cb); sum += A[i] * ca - a; sum -= A[i] * cb - b; if(B[i] > 0) { ca++; a += A[i]; } else { cb++; b += A[i]; } // assert(f(A, B, A[i]) == sum); res = min(res, sum); } return res; } void test(){ int N, K; int res = INF; vector A, B; cin>>N>>K; A.resize(N); B.resize(K, 1); for(int i = K; i < N; i++){ B.push_back(-1); } for(int i = 0; i < N; i++){ cin>>A[i]; } // res = minf(A, B, K); if((A[N-1] - A[0]) * N <= 100000000) { for(int i = A[0]; i <= A[N-1]; i++){ res = min(res, f(A, B, i)); } } cout<