이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <stack>
#define ll long long
#define inf 1e18
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define fi first
#define se second
using namespace std;
const int N = 1e5 + 9;
int n, k;
ll a[N], dp[109][N];
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("nhap.inp", "r", stdin);
// freopen("xuat.out", "w", stdout);
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, k) FOR(j, 0, n) dp[i][j] = inf;
dp[1][0] = 0;
FOR(i, 1, n) dp[1][i] = max(dp[1][i-1], a[i]);
FOR(i, 2, k) {
stack < pair<ll, int>> st;
FOR(j, i, n){
ll minF = dp[i-1][j - 1];
while (!st.empty() && a[st.top().se] <= a[j]) {
minF = min(minF, st.top().fi);
st.pop();
}
dp[i][j] = min(dp[i][(!st.empty() ? st.top().se : 0)], minF + a[j]);
st.push({minF, j});
}
}
cout << dp[k][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... |