이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 1;
int big = 1e9;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
int dp[n + 1][k + 1], a[n], greater[n];
for (int &x : a)
cin >> x;
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.top()] < a[i])
greater[st.top()] = i, st.pop();
st.push(i);
}
while (!st.empty())
greater[st.top()] = n, st.pop();
for (int i = 0; i < k; i++)
dp[n][i] = big;
for (int i = 0; i < n; i++)
dp[i][k] = big;
dp[n][k] = 0;
for (int i = n - 1; i >= 0; i--)
for (int j = k - 1; j >= 0; j--)
dp[i][j] = min(dp[greater[i]][j], a[i] + dp[i + 1][j + 1]);
cout << dp[0][0] << '\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... |