# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1078684 | Tony_Tung | K blocks (IZhO14_blocks) | C++14 | 905 ms | 9084 KiB |
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>
#define task "blocks"
#define int long long
using namespace std;
const int N = 1e5 + 5, K = 1e2 + 5, INF = 1e18;
int a[N], dp[N][K], n, k;
stack<int> st;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (fopen(task".in", "r"))
{
freopen(task".in", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> k;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= k; j++) dp[i][j] = INF;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
dp[i][1] = i == 1 ? a[i] : max(a[i], dp[i-1][1]);
}
for (int j = 2; j <= n; j++)
{
while (st.size()) st.pop();
for (int i = 1; i <= n; i++)
{
while (st.size() && a[i] >= a[st.top()])
{
dp[i][j] = min(dp[i][j], min(dp[st.top()][j-1] + a[i], dp[st.top()][j] - a[st.top()] + a[i]));
st.pop();
}
if (st.size())
dp[i][j] = min(dp[i][j], min(dp[st.top()][j-1] + a[i], dp[st.top()][j]));
st.push(i);
}
}
cout << dp[n][k];
return 0;
}
Compilation message (stderr)
# | 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... |