Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * code generated by JHelper
- * More info: https://github.com/AlexeyDmitriev/JHelper
- * @author JustLive
- */
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize("unroll-loops")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #define _CRT_SECURE_NO_WARNINGS
- #include <bits/stdc++.h>
- #define mp make_pair
- #define mt make_tuple
- #define fi first
- #define se second
- #define pb push_back
- #define all(x) (x).begin(), (x).end()
- #define rall(x) (x).rbegin(), (x).rend()
- #define forn(i, n) for (int i = 0; i < (int)(n); ++i)
- #define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
- #define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
- #define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
- #define forr(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
- #ifdef LOCAL_SYSTEM
- #define DEBUG(x) cerr << #x << ": " << (x) << endl;
- #define DEBUGARR(a, n) cerr << #a << ": "; print_array(cerr, a, n); cerr << endl;
- #else
- #define DEBUG(x)
- #define DEBUGARR(a, n)
- #endif
- using namespace std;
- template<typename T> void print_sequential_container(ostream& stream, T& c) {
- stream << "[";
- for (auto it = c.begin(); it != c.end(); ++it) {
- stream << *it;
- if (it != --c.end()) {
- stream << ", ";
- }
- }
- stream << "]";
- }
- template<typename T> void print_array(ostream& stream, T a[], int n) {
- stream << "[";
- for (int i = 0; i < n; i++) {
- stream << a[i];
- if (i != n - 1) {
- stream << ", ";
- }
- }
- stream << "]";
- }
- template<typename T> void print_set(ostream& stream, T& c) {
- int cnt = 0;
- stream << "{";
- for (auto it = c.begin(); it != c.end(); ++it, ++cnt) {
- stream << *it;
- if (cnt + 1 < c.size()) {
- stream << ", ";
- }
- }
- stream << "}";
- }
- template<typename T> void print_map(ostream& stream, T& c) {
- int cnt = 0;
- stream << "{";
- for (auto it = c.begin(); it != c.end(); ++it, ++cnt) {
- stream << it->fi << ": " << it->se;
- if (cnt + 1 < c.size()) {
- stream << ", ";
- }
- }
- stream << "}";
- }
- template<typename T> ostream& operator << (ostream& stream, vector<T>& c) {
- print_sequential_container(stream, c);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, list<T>& c) {
- print_sequential_container(stream, c);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, forward_list<T>& c) {
- print_sequential_container(stream, c);
- return stream;
- }
- template<typename T, size_t N> ostream& operator << (ostream& stream, array<T, N>& c) {
- print_sequential_container(stream, c);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, deque<T>& c) {
- print_sequential_container(stream, c);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, stack<T>& c) {
- vector<T> v;
- while (not c.empty()) {
- v.pb(c.top());
- c.pop();
- }
- for (int i = ((int)v.size()) - 1; i >= 0; i--) {
- c.push(v[i]);
- }
- print_sequential_container(stream, v);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, queue<T>& c) {
- vector<T> v;
- while (not c.empty()) {
- v.pb(c.front());
- c.pop();
- }
- for (T e: v) {
- c.push(e);
- }
- print_sequential_container(stream, v);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, priority_queue<T>& c) {
- vector<T> v;
- while (not c.empty()) {
- v.pb(c.top());
- c.pop();
- }
- for (T e: v) {
- c.push(e);
- }
- print_sequential_container(stream, v);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, set<T>& c) {
- print_set(stream, c);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, unordered_set<T>& c) {
- print_set(stream, c);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, multiset<T>& c) {
- print_set(stream, c);
- return stream;
- }
- template<typename T> ostream& operator << (ostream& stream, unordered_multiset<T>& c) {
- print_set(stream, c);
- return stream;
- }
- template<typename T1, typename T2> ostream& operator << (ostream& stream, map<T1, T2>& c) {
- print_map(stream, c);
- return stream;
- }
- template<typename T1, typename T2> ostream& operator << (ostream& stream, unordered_map<T1, T2>& c) {
- print_map(stream, c);
- return stream;
- }
- template<typename T1, typename T2> ostream& operator << (ostream& stream, multimap<T1, T2>& c) {
- print_map(stream, c);
- return stream;
- }
- template<typename T1, typename T2> ostream& operator << (ostream& stream, unordered_multimap<T1, T2>& c) {
- print_map(stream, c);
- return stream;
- }
- template<typename T1, typename T2> ostream& operator << (ostream& stream, pair<T1, T2>& p) {
- stream << "(" << p.fi << ", " << p.se << ")";
- return stream;
- }
- template<typename T>
- size_t make_hash(const T& v) {
- return std::hash<T>()(v);
- }
- size_t hash_combine(const size_t& h, const size_t& v) {
- return h ^ (v + 0x9e3779b9 + (h << 6) + (h >> 2));
- }
- namespace std {
- template <typename T1, typename T2>
- struct hash<pair<T1, T2>> {
- public:
- size_t operator()(const pair<T1, T2>& p) const {
- return hash_combine(make_hash(p.fi), make_hash(p.se));
- }
- };
- }
- template<typename T>
- struct hash_container
- {
- size_t operator()(const T& c) const {
- size_t h = 0;
- for(const auto& e: c) {
- h = hash_combine(h, make_hash(e));
- }
- return h;
- }
- };
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- typedef vector<pii> vpii;
- typedef vector<vi> vvi;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef unsigned int uint;
- typedef vector<ll> vll;
- typedef vector<vll> vvll;
- typedef pair<ll, ll> pll;
- typedef long double ld;
- template<typename T> bool amin(T &a, T b) { return a > b ? (a = b, true) : false; }
- template<typename T> bool amax(T &a, T b) { return a < b ? (a = b, true) : false; }
- auto seed() {
- return chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
- }
- const vi some_primes = {1000000007, 1000000411, 1000001501, 1000001927, 1000002637,
- 1000003163, 1000003651, 1000003769, 1000003931, 1000004099};
- const int mod = 1000000007;
- const int inf = 1000000000;
- const double eps = 1e-9;
- const double pi = atan2(-1, 0);
- const ll infll = (ll)inf * (ll)inf;
- const int maxn = 100000;
- const int L = 20;
- int n, c;
- vi a;
- vvi r;
- void build() {
- forn(j, L) {
- for (int i = 0; i + (1 << j) - 1 < n; i++) {
- if (j == 0) r[i][j] = a[i];
- else {
- r[i][j] = min(r[i][j - 1], r[i + (1 << (j - 1))][j - 1]);
- }
- }
- }
- }
- int get(int i, int j) {
- int d = (int)log2(j - i + 1);
- return min(r[i][d], r[j - (1 << d) + 1][d]);
- }
- class TaskE {
- public:
- void solve(istream& in, ostream& out) {
- in >> n >> c;
- a.clear();
- r.clear();
- a.resize(n);
- r.resize(n, vi(L));
- ll total = 0;
- vll psum(n + 1);
- psum[0] = 0;
- forn(i, n) {
- in >> a[i];
- total += a[i];
- psum[i + 1] = total;
- }
- if (c > n) {
- out << total;
- return;
- }
- build();
- ll res = total;
- vll dp(n + 1, infll);
- dp[0] = 0;
- forn(i, c) {
- dp[i + 1] = psum[i + 1];
- }
- fore(i, c, n) {
- dp[i] = min(
- dp[i - 1] + a[i - 1],
- dp[i - c] + (psum[i] - psum[i - c]) - get(i - c, i - 1)
- );
- }
- out << dp[n];
- }
- };
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- TaskE solver;
- std::istream& in(std::cin);
- std::ostream& out(std::cout);
- solver.solve(in, out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement