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>
#define ll long long
#define lld long double
#define ff first
#define ss second
#define pb push_back
#define vr(v) v.begin(),v.end()
#define rv(v) v.rbegin(),v.rend()
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Davit cout.tie(NULL);
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> v(n+5);
for (int i = 1; i <= n; i++)cin >> v[i];
vector<vector<int>> dp(k+5, vector<int>(n+5,1e9));
int mx = v[1];
for (int i = 1; i <= n; i++) {
mx = max(mx, v[i]);
dp[1][i] = mx;
}
for (int i = 2; i <= k; i++) {
stack<int> s1, s2;
for (int j = 1; j <= n; j++) {
int my_dp = dp[i - 1][j - 1];
while (!s1.empty() && s1.top() < v[j]) {
my_dp = min(my_dp, s2.top());
s1.pop();
s2.pop();
}
if (s1.empty() || my_dp + v[j] < s1.top() + s2.top()) {
s1.push(v[j]);
s2.push(my_dp);
}
if(j>=i)dp[i][j] = s1.top() + s2.top();
}
}
cout << dp[k][n] << endl;
}
# | 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... |