제출 #488132

#제출 시각아이디문제언어결과실행 시간메모리
488132SirCovidThe19thK blocks (IZhO14_blocks)C++17
100 / 100
385 ms81164 KiB
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, x, y) for (int i = x; i < y; i++)
#define pii pair<int, int>
#define f first
#define s second

int main(){
    int n, k; cin >> n >> k; int A[n + 1]; 
    FOR(i, 1, n + 1) cin >> A[i];

    pii dp[n + 1][k + 1]; memset(dp, 0x3f, sizeof(dp)); dp[0][0] = {0, 0};

    FOR(j, 1, k + 1){
        stack<pii> stk;
        FOR(i, j, n + 1){
            dp[i][j] = {dp[i - 1][j - 1].f + A[i], A[i]}; //create new

            while (!stk.empty() and stk.top().s < A[i]){
                pii tmp = {stk.top().f - stk.top().s + A[i], A[i]};
                if (tmp.f <= dp[i][j].f) dp[i][j] = tmp;
                stk.pop();
            }
            if (!stk.empty())
                if (stk.top().f <= dp[i][j].f) dp[i][j] = stk.top();
            
            stk.push(dp[i][j]);
        }
    }
    cout<<dp[n][k].f<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...