Submission #285425

#TimeUsernameProblemLanguageResultExecution timeMemory
285425loideptrai1K blocks (IZhO14_blocks)C++14
100 / 100
246 ms42280 KiB
#include <bits/stdc++.h>

#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define name ""
#define inout freopen(name".inp","r", stdin); freopen(name".out","w",stdout);

using namespace std;

const int N=1e5+5;
const int MOD=1e9+7;
const int base=257;
const int M=1e6+1;
const int INFI=1e9;

int a[N], dp[105][N];
int n,k;
int main()
{
   cin >> n >> k;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    memset(dp, 0x3f, sizeof dp);
    dp[1][0] = 0;
    for (int i = 1; i <= n; ++i) dp[1][i] = max(dp[1][i - 1], a[i]);
    for (int i = 2; i <= k; ++i) {
        stack<pair<int, int> > S;
        for (int j = i; j <= n; ++j) {
            int minF = dp[i - 1][j - 1];
            while (!S.empty() && a[S.top().second] <= a[j]) {
                minF = min(minF, S.top().first);
                S.pop();
            }
            dp[i][j] = min(dp[i][S.empty() ? 0 : S.top().second], minF + a[j]);
            S.push(make_pair(minF, j));
        }
    }
    cout << dp[k][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...