제출 #14805

#제출 시각아이디문제언어결과실행 시간메모리
14805erdemkirazK개의 묶음 (IZhO14_blocks)C++98
100 / 100
182 ms3672 KiB
#include <iostream>

using namespace std;

const int inf = 1e9 + 5;
const int N = 1e5 + 5;

int n, k, a[N], dp[N], bef[N];
pair < int, int > p[N];

int main () {
    
    cin >> n >> k;
    
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        bef[i] = max(bef[i - 1], a[i]);
    }
    
    for(int j = 2; j <= k; j++) {
        int sz = 0;
        for(int i = 1; i <= n; i++) {
            int ans = j <= i ? bef[i - 1] : inf;
            bef[i - 1] = dp[i - 1];
            while(sz and a[i] >= p[sz - 1].first)
                ans = min(ans, p[--sz].second);
            if(!sz or a[i] + ans < p[sz - 1].first + p[sz - 1].second)
                p[sz++] = make_pair(a[i], ans);
            dp[i] = p[sz - 1].first + p[sz - 1].second;
        }
        bef[n] = dp[n];
    }
    
    cout << bef[n] << endl;
    
    return 0;
    
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...