이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
const int maxk = 110;
int n, k;
int dp[maxn][maxk];
int arr[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
int prefmax = 0;
for(int i=0;i<=n;i++) {
for(int j=0;j<=k;j++) {
dp[i][j] = INT_MAX;
}
}
for(int i=1;i<=n;i++) {
cin>>arr[i];
prefmax = max(prefmax, arr[i]);
dp[i][1] = prefmax;
}
for(int d=2;d<=k;d++) {
stack<int>dps, arrs;
for(int i=1;i<=n;i++) {
int curr = dp[i-1][d-1];
while(!arrs.empty() && arrs.top() < arr[i]) {
curr = min(curr, dps.top());
arrs.pop();
dps.pop();
}
if(arrs.empty() || arrs.top() + dps.top() >= curr + arr[i]) {
arrs.push(arr[i]);
dps.push(curr);
}
if(i >= d) dp[i][d] = arrs.top() + dps.top();
}
}
cout<<dp[n][k]<<"\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... |