이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e2+5;
const int maxn=1e5+5;
int n,k,a[maxn];
ll dp[N][maxn];
stack<pair<int,ll>>s;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= n;i++) dp[1][i] = max(dp[1][i-1],1ll*a[i]);
for(int i = 2;i <= k;i++){
dp[i][i] = dp[i-1][i-1]+a[i];
s.push({i,dp[i][i]});
for(int j = i+1;j <= n;j++){
while(!s.empty() and a[s.top().first] <= a[j]) s.pop();
if(!s.empty()){
ll nw = a[j] + dp[i-1][s.top().first];
s.push({j,min(nw,s.top().second)});
}
else s.push({j,dp[i-1][i-1]+a[j]});
dp[i][j] = s.top().second;
}
}
cout<<dp[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... |