Submission #242060

#TimeUsernameProblemLanguageResultExecution timeMemory
242060przxctK blocks (IZhO14_blocks)C++17
0 / 100
12 ms2560 KiB
#include <bits/stdc++.h>
#define fto(i,j,h)      for (int i=j; i<=h; ++i)
#define fdto(i,j,h)     for (int i=j; i>=h; --i)
#define przxct "main"
#define maxn 100003
#define fi first
#define se second
#define ll long long
#define db double
#define pi 3.141592653589

using namespace std;

int n, k, a[maxn];
ll F[103][maxn];
stack<int> t;
stack<pair<int, ll> > t2;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //#ifndef ONLINE_JUDGE
    //freopen(przxct".inp", "r", stdin);
    //freopen(przxct".out", "w", stdout);
   // #endif // ONLINE_JUDGE
    cin >> n >> k;
    fto(i,1,n)      cin >> a[i];
    int Max = 0;
    fto(i,1,n){
       Max = max(Max, a[i]);
       F[1][i] = Max;
    }
    fto(i,2,k)
      fto(j,1,n){
         F[i][j] = 1e18;
      }
    fto(g,2,k){
      t2.push({g-1, F[g-1][g-1]});
      fto(i,g,n){
         while (t.empty() == false && a[t.top()] < a[i]){
             t.pop();
         }
         if (t.empty() == false)   F[g][i] = min(F[g][i], F[g][t.top()]);
         t.push(i);
         ll Min = 1e18;
         while (t2.empty() == false && a[t2.top().fi] <= a[i]){
            Min = min(Min, min(t2.top().se, F[g-1][t2.top().fi]));
            t2.pop();
         }
         if (t2.empty() == false)    Min = min(Min, F[g-1][t2.top().fi]);
         F[g][i] = min(F[g][i], Min + a[i]);
         t2.push({i, Min});
      }
    }
    cout << F[k][n];
    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...