// #define _NDEBUG #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include "bits/stdc++.h" #include #include #ifdef _MSC_VER #include #include #include //#include //#include //#include #include #include #include //#else //#include #endif /* type */ using uint = unsigned; using ll = long long; using ull = unsigned long long; using pii = std::pair; using pll = std::pair; /* io */ template std::ostream& operator << (std::ostream& o, const std::pair<_KTy, _Ty>& m) { o << "[" << m.first << ", " << m.second << "]"; return o; } template std::ostream& operator << (std::ostream& o, const std::map<_KTy, _Ty>& m) { if (m.empty()) { o << "[]"; return o; } o << "[" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { o << ", " << *itr; } o << "]"; return o; } template std::ostream& operator << (std::ostream& o, const std::unordered_map<_KTy, _Ty>& m) { if (m.empty()) { o << "[]"; return o; } o << "[" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { o << ", " << *itr; } o << "]"; return o; } template std::ostream& operator << (std::ostream& o, const std::vector<_Ty>& v) { if (v.empty()) { o << "[]"; return o; } o << "[" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { o << ", " << *itr; } o << "]"; return o; } template std::ostream& operator << (std::ostream& o, const std::deque<_Ty>& v) { if (v.empty()) { o << "[]"; return o; } o << "[" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { o << ", " << *itr; } o << "]"; return o; } template std::ostream& operator << (std::ostream& o, const std::set<_Ty>& s) { if (s.empty()) { o << "[]"; return o; } o << "[" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { o << ", " << *itr; } o << "]"; return o; } template std::ostream& operator << (std::ostream& o, const std::unordered_set<_Ty>& s) { if (s.empty()) { o << "[]"; return o; } o << "[" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { o << ", " << *itr; } o << "]"; return o; } template std::ostream& operator << (std::ostream& o, const std::stack<_Ty>& s) { if (s.empty()) { o << "[]"; return o; } std::stack<_Ty> t(s); o << "[" << t.top(); t.pop(); while (!t.empty()) { o << ", " << t.top(); t.pop(); } o << "]"; return o; } template std::ostream& operator << (std::ostream& o, const std::list<_Ty>& l) { if (l.empty()) { o << "[]"; return o; } o << "[" << l.front(); for (auto itr = ++l.begin(); itr != l.end(); ++itr) { o << ", " << *itr; } o << "]"; return o; } template std::istream& operator >> (std::istream& is, std::pair<_KTy, _Ty>& m) { is >> m.first >> m.second; return is; } template std::istream& operator >> (std::istream& is, std::vector<_Ty>& v) { for (size_t t = 0; t < v.size(); t++) is >> v[t]; return is; } template std::istream& operator >> (std::istream& is, std::deque<_Ty>& v) { for (size_t t = 0; t < v.size(); t++) is >> v[t]; return is; } namespace aux { // print tuple template struct tp { static void print(std::ostream& os, const Ty& v) { os << std::get(v) << ", "; tp::print(os, v); } }; template struct tp { static void print(std::ostream& os, const Ty& v) { os << std::get(v); } }; } template std::ostream& operator<<(std::ostream& os, const std::tuple& t) { os << "["; aux::tp, 0, sizeof...(Tys) - 1>::print(os, t); os << "]"; return os; } /* fill */ template void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } /* format */ template std::string format(const std::string& fmt, Args ... args) { size_t len = std::snprintf(nullptr, 0, fmt.c_str(), args ...); std::vector buf(len + 1); std::snprintf(&buf[0], len + 1, fmt.c_str(), args ...); return std::string(&buf[0], &buf[0] + len); } /* dump */ #define DUMPOUT std::cerr #define dump(...) do{DUMPOUT<<" ";DUMPOUT<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<<__FUNCTION__<<"]"< void dump_func(Head&& head, Tail&&... tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...); } /* timer */ class Timer { public: double t = 0; Timer() { reset(); } static double time() { #ifdef _MSC_VER return __rdtsc() / 2.8e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 2.8e9; #endif } //void measure() { t = time() - t; } void reset() { t = time(); } double elapsedMs() { return (time() - t) * 1000.0; } } timer; /* rand */ struct Xorshift { uint64_t x = 88172645463325252LL; unsigned next_int() { x = x ^ (x << 7); return x = x ^ (x >> 9); } unsigned next_int(unsigned mod) { x = x ^ (x << 7); x = x ^ (x >> 9); return x % mod; } unsigned next_int(unsigned l, unsigned r) { x = x ^ (x << 7); x = x ^ (x >> 9); return x % (r - l + 1) + l; } double next_double() { return double(next_int()) / UINT_MAX; } } rnd; /* shuffle */ template void shuffle_vector(std::vector& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } } using namespace std; ll f_(ll x, const vector& A, ll mask) { ll res = 0; for (ll i = 0; i < A.size(); i++) { res += ((mask >> i) & 1LL ? 1 : -1) * abs(x - A[i]); } return res; } ll f(ll x, const vector& A, const vector& B) { ll res = 0; for (ll i = 0; i < A.size(); i++) { res += B[i] * abs(x - A[i]); } return res; } unsigned long long popcountll(unsigned long long x) { x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1) + (x & 0x5555555555555555UL); x = ((x & 0xccccccccccccccccUL) >> 2) + (x & 0x3333333333333333UL); x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4) + (x & 0x0f0f0f0f0f0f0f0fUL); x = ((x & 0xff00ff00ff00ff00UL) >> 8) + (x & 0x00ff00ff00ff00ffUL); x = ((x & 0xffff0000ffff0000UL) >> 16) + (x & 0x0000ffff0000ffffUL); x = ((x & 0xffffffff00000000UL) >> 32) + (x & 0x00000000ffffffffUL); return x; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll N, K; cin >> N >> K; vector A(N); cin >> A; vector B(N, -1); for (ll i = 0; i < K; i++) B[i] = 1; ll ans = INT64_MAX; ans = min({ ans, f(A.front(), A, B), f(A.back(), A, B) }); reverse(B.begin(), B.end()); ans = min({ ans, f(A.front(), A, B), f(A.back(), A, B) }); cout << ans << endl; return 0; } int _main() { ios::sync_with_stdio(false); cin.tie(0); ll N, K; cin >> N >> K; vector A(N); cin >> A; //vector A({ -7, -2, 2, 4 }); for(ll mask = 0; mask < (1LL << A.size()); mask++) { if (popcountll(mask) != K) continue; cerr << bitset<15>(mask) << ": "; for (ll i = 0; i < A.size(); i++) { cerr << f_(A[i], A, mask) << " "; } cerr << endl; } return 0; }