이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int MAXN = 1e5 + 5;
int n, k;
int dp[MAXN][105], a[MAXN];
int main() {
// freopen("blocks.inp", "r", stdin);
// freopen("blocks.out", "w", stdout);
cin >> n >> k;
memset(dp, 0x3f, sizeof(dp));
int mx = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mx = max(mx, a[i]);
dp[i][1] = mx;
}
for (int j = 2; j <= k; j++) {
stack<pii> s;
for (int i = 1; i <= n; i++) {
int tmp = dp[i - 1][j - 1];
while (!s.empty() && s.top().first < a[i]) {
tmp = min(tmp, s.top().second);
s.pop();
}
if (s.empty() || a[i] + tmp < s.top().first + s.top().second)
s.push({a[i], tmp});
if (i >= j) dp[i][j] = s.top().first + s.top().second;
}
}
cout << dp[n][k];
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... |