#pragma comment(linker, "/STACK:66777216") #pragma warning(disable : 4996) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #pragma hdrstop static constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; #include #ifdef _MSC_VER #include #else #define popcount(a) __builtin_popcount(a) #define clz(a) __builtin_clz(a) #define ctz(a) __builtin_ctz(a) #endif template inline bool umin(T& a, const T& b) { return (b < a ? a = b, true : false); } #ifdef _MSC_VER #endif template struct MakeVector { }; template struct MakeVector { /// caide keep template> static R make_vector(std::size_t size, const T& value) { return R(size, value); } }; #ifdef _MSC_VER #else #define LLD "%lld" #define LLU "%llu" #endif #define ll long long namespace std { template std::istream& operator>>(std::istream& in, std::vector& vec) { for (auto& it : vec) { in >> it; } return in; } } // namespace std #include template class IntegerIterator : public std::iterator { public: using value_type = T; constexpr explicit IntegerIterator(const value_type value) : value_(value) {} IntegerIterator& operator++() { ++value_; return *this; } constexpr value_type operator*() const { return value_; } constexpr bool operator ==(const IntegerIterator rhs) const { return value_ == rhs.value_; } constexpr bool operator !=(const IntegerIterator rhs) const { return !(*this == rhs); } private: value_type value_; }; template class IntegerRange { public: using const_iterator = IntegerIterator; constexpr IntegerRange(const T begin, const T end) : begin_(begin), end_(end) {} constexpr const_iterator begin() const { return const_iterator(begin_); } constexpr const_iterator end() const { return const_iterator(end_); } private: T begin_; T end_; }; template constexpr IntegerRange inclusiveRange(const T from, const T to) { return IntegerRange(from, to + 1); } using namespace std; void solve(std::istream& in, std::ostream& out) { int n, k; in >> n >> k; vector a(n); in >> a; vector dp(n + 1, LINF); dp[0] = 0; for (int i : inclusiveRange(1, n)) { ll mn = LINF; for (int j : inclusiveRange(std::max(0, i - k - 1), std::min(n, i + k - 1))) { umin(mn, dp[j]); } for (int j : inclusiveRange(std::max(0, i - k), std::min(n, i + k))) { umin(dp[j], mn + a[i - 1]); } } out << dp[n] << endl; } #include int main() { srand(time(NULL)); ios_base::sync_with_stdio(0); cin.tie(0); istream& in = cin; ostream& out = cout; out << fixed << setprecision(16); solve(in, out); return 0; }