Submission #1033391

#TimeUsernameProblemLanguageResultExecution timeMemory
1033391epicci23K개의 묶음 (IZhO14_blocks)C++17
100 / 100
153 ms84676 KiB
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n" 
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 * DON'T GET STUCK ON ONE APPROACH
 * SIMPLIFY THE PROBLEM
 * READ THE STATEMENT CAREFULLY
  !!! if there is an specified/interesting smth(i.e. constraint) in the statement,
  then you must be careful about that   
*/

const int INF = 1e18;

void solve(){
  int n,k;cin >> n >> k;
  int ar[n+5];
  for(int i=1;i<=n;i++) cin >> ar[i];
  int dp[n+5][k+5];
  for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) dp[i][j]=INF;
  for(int i=1;i<=n;i++) dp[i][1]=max((i>1 ? dp[i-1][1] : 0),ar[i]);
  for(int j=2;j<=k;j++){
  	stack<array<int,2>> s;
  	for(int i=1;i<=n;i++){
      int cur=dp[i-1][j-1];
      while(!s.empty() && s.top()[0]<=ar[i]){
      	cur=min(cur,s.top()[1]);
      	s.pop();
      }
      if(s.empty() || ar[i]+cur<s.top()[0]+s.top()[1]) s.push({ar[i],cur});
      dp[i][j]=s.top()[0]+s.top()[1];
  	}
  }
  cout << dp[n][k] << endl;
}

int32_t main(){

  cin.tie(0); ios::sync_with_stdio(0);
  
  int t=1;//cin >> t;
  while(t--) solve();

  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...