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