이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
vector<vector<long long>> dp(k, vector<long long> (n));
for (int i = 0; i < n; ++i) {
cin >> a[i];
dp[0][i] = a[i];
if (i) {
dp[0][i] = max(dp[0][i], dp[0][i - 1]);
}
}
for (int j = 1; j < k; ++j) {
stack<pair<int, long long>> stk;
for (int i = j; i < n; ++i) {
long long x = dp[j - 1][i - 1];
while (!stk.empty() && stk.top().first <= a[i]) {
x = min(x, stk.top().second);
stk.pop();
}
if (stk.empty() || (stk.top().first + stk.top().second > a[i] + x)) {
stk.push({a[i], x});
}
dp[j][i] = stk.top().first + stk.top().second;
//deb(stk.top());deb(dp[j][i]); cout << '\n';
}
}
cout << dp[k - 1][n - 1] << '\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... |