이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pi pair<ll,ll>
#define s second
#define f first
#define left (h<<1),l,((l+r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
using namespace std;
const int N = 200005;
ll a[N],dp[N][105];
stack <pi> st;
signed main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,k;
cin>>n>>k;
for (int i = 1; i <= n; i++){
cin>>a[i];
dp[i][1] = max(dp[i - 1][1], a[i]);
}
for (int K = 2; K <= k; K++){
while(st.size())
st.pop();
for (int j = K; j <= n; j++){
ll d = dp[j - 1][K - 1];
while(st.size() && a[j] > st.top().f){
d = min(d, st.top().s);
st.pop();
}
if (!st.size() || st.top().f + st.top().s > a[j] + d)
st.push({a[j],d});
dp[j][K] = st.top().f + st.top().s;
}
}
cout<<dp[n][k];
}
| # | 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... |