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;
const int maxn = 1e5 + 5;
int dp[maxn][105], v[maxn];
int main() {
// freopen("BLOCKS.inp","r",stdin);
// freopen("BLOCKS.out","w",stdout);
int n, k;
cin >> n >> k;
for(int i=0; i<=n; i++)
for(int j=0; j<=k; j++) dp[i][j] = 1e9;
for(int i=1; i<=n; i++) cin >> v[i];
dp[0][0] = 0;
int mx = 0;
for(int i=1; i<=n; i++) {
mx = max(mx, v[i]);
dp[i][1] = mx;
}
for(int j=2; j<=k; j++) {
stack<int> st1, st2;
for(int i=1; i<=n; i++) {
int mn = dp[i-1][j-1];
while(!st1.empty() && st1.top() <= v[i]) {
mn = min(mn, st2.top());
st1.pop(); st2.pop();
}
if(st1.empty() || mn + v[i] < st1.top() + st2.top()) {
st1.push(v[i]);
st2.push(mn);
}
dp[i][j] = st1.top() + st2.top();
}
}
cout << dp[n][k] << '\n';
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... |