/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 +5;
const int MAXK = 105;
int arr[MAXN] ,pos[MAXN],n,k;
long long dp[MAXN][MAXK], pref[MAXN];
stack <int> gay;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
//freopen("blocks.in", "r", stdin);
//freopen("blocks.out", "w", stdout);
cin>>n>>k;
for(int i=1; i<=n;i++) {cin>>arr[i]; pref[i]=pref[i-1]+arr[i];}
for(int i=n; i>=1; i--){
while(!gay.empty()){
if(arr[i]>arr[gay.top()]) {pos[gay.top()]= i;gay.pop();}
else break;
}
gay.push(i);
}
int maxt=-1;
for(int i=1 ;i<=n;i++){
maxt=max(maxt, arr[i]);
dp[i][1]=maxt;
for(int j=1;j<=k;j++) if(j>i) dp[i][j]=1e18;
}
for (int i=1; i<=n;i++){
for(int j=2; j<=k;j++){
if(i<j) break;
if(pos[i]<j-1) dp[i][j]= pref[j-1]+arr[i];
else dp[i][j]= min(dp[pos[i]][j-1] + arr[i],dp[pos[i]][j]);
}
}
//cout<<pos[6]<<endl;
cout<<dp[n][k];
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |