#pragma region library #ifdef __GNUC__ //#pragma GCC target("avx") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") #endif #define _CRT_SECURE_NO_WARNINGS #include "bits/stdc++.h" // #include // using boost::irange; #pragma region Macros #define itrall(x) std::begin(x), std::end(x) #define FOR(A,...) std::for_each(std::begin(A), std::end(A), [&]__VA_ARGS__ ) using i64 = long long; using cint = const int; using ci64 = const long long; using uint = unsigned int; using u64 = unsigned long long; using cuint = const unsigned int; using cu64 = const unsigned long long; #pragma endregion #ifndef CnM_library #define debug(x) constexpr int dx[] = { 1, 0, -1, 0 }; constexpr int dy[] = { 0, 1, 0, -1 }; template constexpr T INF_func() { return std::numeric_limits::max(); } template <> constexpr int INF_func() { return 1001001001; } template <> constexpr long long INF_func() { return 1501501501501501501ll; } template <> constexpr unsigned int INF_func() { return 1001001001; } template <> constexpr unsigned long long INF_func() { return 1501501501501501501ll; } template constexpr T INF = INF_func(); constexpr int mod = 1000000007; constexpr int mod2 = 998244353; #define elif else if #define len(T) (int)(T).size() template > constexpr inline bool chmin(T& xmin, const U& x, Comp comp = {}) noexcept { return comp(x, xmin) ? xmin = x, true : false; } template > constexpr inline bool chmax(T& xmax, const U& x, Comp comp = {}) noexcept { return comp(xmax, x) ? xmax = x, true : false; } namespace lib { template inline T Pow(T A, U B) { T res(1); while (B) { if (B & 1) res *= A; A *= A; B >>= 1; } return res; } inline long long gcd(long long A, long long B) { while (B) { const long long C = A; A = B; B = C % B; } return A; } inline long long lcm(const long long A, const long long B) { return A / gcd(A, B) * B; } inline long long extgcd(long long A, long long B, long long& X, long long& Y) { long long D = A; if (B != 0) { D = extgcd(B, A % B, Y, X); Y -= (A / B) * X; return D; } else { X = 1; Y = 0; return A; } } inline long long modpow(long long A, long long B, const long long MOD) { long long res(1); while (B) { if (B & 1) res *= A, res %= MOD; A *= A; A %= MOD; B >>= 1; } return res; } template inline T inverse(T A, const T M) { T B = M, U = 1, V = 0; while (B) { T t = A / B; A -= t * B; std::swap(A, B); U -= t * V; std::swap(U, V); } U %= M; return U < 0 ? U += M, U : U; } } template inline void read(T& Tar) { std::cin >> Tar; return; } template inline void read(T& Tar, Ts&... ts) { std::cin >> Tar; read(ts...); return; } template inline void print(T Tar) { std::cout << Tar; return; } template inline void print(T Tar, Ts... ts) { std::cout << Tar; print(ts...); return; } #endif #pragma endregion class ProCon_all_init { static constexpr bool float_fixed = false; static constexpr int ios_perc = 15; static constexpr bool ios_fast = false; static constexpr bool flush_auto = false; public: ProCon_all_init() { if (ios_fast) { std::cin.tie(nullptr); std::cout.tie(nullptr); std::ios::sync_with_stdio(false); } if (float_fixed) { std::cout << std::fixed << std::setprecision(ios_perc); } if (flush_auto) { std::cout << std::unitbuf; } } } PROCON_ALL_INIT_; template std::vector arg_sort(std::vector& v, bool isgreater = false) { std::vector res(v.size()); std::iota(res.begin(), res.end(), 0); std::sort(res.begin(), res.end(), [&](int i, int j) {return isgreater ? v[i] > v[j] : v[i] < v[j]; }); return res; } template class range { const T First, Last; public: class iter { friend range; T itr; constexpr iter(T x) noexcept : itr(x) {} public: void operator ++() noexcept { itr++; } constexpr T operator *() const noexcept { return itr; } constexpr bool operator != (const iter x) const noexcept { return itr != x.itr; } }; constexpr range(const T F, const T L) noexcept : First(F), Last(std::max(F, L)) {} constexpr range(const T L) noexcept :First(0), Last(std::max((T)0, L)) {} constexpr iter begin() const noexcept { return iter(First); } constexpr iter end() const noexcept { return iter(Last); } /* * range(l, r) -> [l, r) * l < r */ }; template class revge { const T First, Last; public: class reviter { friend revge; T itr; constexpr reviter(T x) noexcept : itr(x) {} public: void operator ++() noexcept { itr--; } constexpr T operator *() const noexcept { return itr; } constexpr bool operator != (const reviter x) const noexcept { return itr != x.itr; } }; constexpr revge(const T F, const T L) noexcept : First(std::max(L - 1, F)), Last(L) {} constexpr revge(const T F) noexcept : First(std::max((T)0, F)), Last(0) {} constexpr reviter begin() const noexcept { return reviter(First); } constexpr reviter end() const noexcept { return reviter(Last - 1); } /* * revge(r, l) -> [l, r] reverse * l >= r */ }; class Abs_sum { public: i64 N, K; std::vector A; Abs_sum() { read(N, K); A.resize(N); for (auto& e : A) read(e); } void solve() { i64 ans = INF; // right auto solve_sub = [&]() { i64 now = 0; for (int i : range(N)) { i64 C = abs(A[0] - A[i]); now += (i < K ? C : -C); } for (int i : range(N - 1)) { chmin(ans, now); i64 C1 = std::min(K, i + 1ll); i64 C2 = std::max(0ll, i + 1 - K); i64 S = abs(A[i + 1] - A[i]); now += C1 * S; now -= (K - C1) * S; now -= C2 * S; now += (N - K - C2) * S; } chmin(ans, now); }; solve_sub(); std::reverse(itrall(A)); solve_sub(); print(ans, '\n'); } }; int main(void) { Abs_sum solver; solver.solve(); return 0; }