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