이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define maxn 100100
using namespace std;
int n, k;
int a[maxn], dp[maxn][110];
int main(){
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
dp[0][0] = 0;
for(int i = 0; i < n; ++i){
dp[i+1][1] = max(dp[i][1], a[i+1]);
}
for(int j = 2; j <= k; ++j){
vector<pair<int, int> > dpStk;
for(int i = j; i <= n; ++i){
int tmpM = dp[i-1][j-1];
while(!dpStk.empty() and dpStk.back().first <= a[i]){
tmpM = min(tmpM, dpStk.back().second);
dpStk.pop_back();
}
if(dpStk.empty() or dpStk.back().first+dpStk.back().second >= tmpM+a[i]){
dpStk.push_back({a[i], tmpM});
//cerr << tmpM << '\n';
}
dp[i][j] = tmpM+a[i];
}
}
cout << dp[n][k];
}
# | 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... |