Submission #49860

#TimeUsernameProblemLanguageResultExecution timeMemory
49860mra2322001K blocks (IZhO14_blocks)C++14
0 / 100
18 ms4556 KiB
#include <bits/stdc++.h>
#define f0(i, n) for(int i(0); i<(n); i++)
#define f1(i, n) for(int i(1); i<=(n); i++)

using namespace std;
typedef long long ll;
const int N = 1e5 + 3;

int n, k, a[N], f[N][102];

int main(){
    ios_base::sync_with_stdio(0);

    cin >> n >> k;
    f1(i, n) cin >> a[i];
    stack <int> s;
    f1(j, k){
        while(s.size()) s.pop();
        s.push(j - 1);
        f1(i, n){
            f[i][j] = 1e9;
            int &res = f[i][j];
            while(s.size() && a[s.top()] <= a[i]){
                if(s.top() >= j) res = min(res, f[s.top()][j] - a[s.top()] + a[i]);
                if(s.top() >= j - 1) res = min(res, f[s.top()][j - 1] + a[i]);
                s.pop();
            }
            if(s.size()){
                if(s.top() >= j) res = min(res, f[s.top()][j] - a[s.top()] + a[i]);
                if(s.top() >= j - 1) res = min(res, f[s.top()][j - 1] + a[i]);
            }
            else res = min(res, f[j - 1][j - 1] + a[i]);
            s.push(i);
        }
    }
    int best = f[n][k], ma = a[n];
    for(int i = n - 1; i >= k - 1; i--){
        /// i + 1, n
        best = min(best, f[i][k - 1] + ma);
        ma = max(ma, a[i]);
    }
    cout << best;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...