This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, k;
int a[N];
ll dp[105][N];
void solve()
{
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 0; i <= k; i++)
for(int j = 0; j <= n; j++)
dp[i][j] = 1e17;
dp[1][0] = 0;
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++)
{
stack<int> st;
for(int j = i; j <= n; j++)
{
while(st.size() && a[j] >= a[st.top()])
{
dp[i][j] = min(dp[i][j], dp[i - 1][st.top()] + a[j]);
st.pop();
}
int k = st.size() ? st.top() : i - 1;
dp[i][j] = min({dp[i][j], dp[i][k], dp[i - 1][k] + a[j]});
st.push(j);
}
}
cout << dp[k][n];
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while(t--) solve();
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... |