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