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 pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
const int maxk = 110;
ll n, k;
ll dp[maxn][maxk];
ll arr[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
ll prefmax = 0LL;
for(int i=0;i<=n;i++) {
for(int j=0;j<=k;j++) {
dp[i][j] = (INT_MAX);
}
}
for(int i=1;i<=n;i++) {
cin>>arr[i];
prefmax = max(prefmax, arr[i]);
dp[i][1] = prefmax;
}
for(int d=2;d<=k;d++) {
stack<ll>dps, arrs;
for(int i=1;i<=n;i++) {
ll curr = dp[i-1][d-1];
while(!arrs.empty() && arrs.top() < arr[i]) {
curr = min(curr, dps.top());
arrs.pop();
dps.pop();
}
if(arrs.empty() || arrs.top() + dps.top() >= curr + arr[i]) {
arrs.push(arr[i]);
dps.push(curr);
}
if(i >= d) dp[i][d] = arrs.top() + dps.top();
}
}
cout<<dp[n][k]<<"\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... |