This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define read() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define day() time_t now = time(0);char* x = ctime(&now);cerr<<"Right Now Is : "<<x<<"\n"
#define ii pair<int,int>
#define X first
#define Y second
const long long MAX = (int)1e5 + 5;
const long long INF = (int)1e9;
const long long MOD = (int)1e9 + 7;
int n,k;
int a[MAX];
int dp[2][MAX];
signed main(){
read();
cin >> n >> k;
int bit = 0;
for(int i = 1;i <= n;i++){
cin >> a[i];
dp[bit][i] = INF;
}
for(int j = 1;j <= k;j++){
bit = 1 - bit;
stack<ii> st;
for(int i = 1;i <= n;i++){
if(j > i)continue;
int summ = dp[1 - bit][i - 1];
//cout << summ << " :\n";
while(!st.empty() && a[st.top().X] <= a[i]){
//cout << st.top().Y << " " << st.top().X << "A\n";
summ = min(summ,st.top().Y);
st.pop();
}
//cout << summ << "\n";
if(st.empty() || a[st.top().X] + st.top().Y > a[i] + summ){
st.push({i,summ});
}
if(i >= j){
//cout << st.top().X << " " << st.top().Y << '\n';
dp[bit][i] = a[st.top().X] + st.top().Y;
}
//cout << dp[bit][i] << " ";
}
for(int i = 0;i <= n;i++){
dp[1 - bit][i] = INF;
}
//cout << "\n";
}
cout << dp[bit][n];
}
# | 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... |