# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1039070 | ttviet2805 | K blocks (IZhO14_blocks) | C++14 | 1 ms | 604 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>
using namespace std;
#define int long long
#define II pair <int, int>
#define fi first
#define se second
#define all(S) S.begin(), S.end()
const int INF = 1e15;
const int N = 1e5 + 5;
int n, a[N], K;
void in() {
cin >> n >> K;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
vector <vector <int>> f(K + 1, vector <int> (n + 5, INF));
stack <II> st;
f[0][0] = 0;
a[0] = INF;
for(int X = 1; X <= K; X++) {
st.push({X - 1, f[X - 1][X - 1]});
for(int i = X; i <= n; i++) {
int curMin = INF;
while(!st.empty() && a[i] >= a[st.top().fi]) {
curMin = min(curMin, st.top().se);
st.pop();
}
int res = min(curMin, f[X - 1][st.top().fi]);
f[X][i] = f[X][st.top().fi];
f[X][i] = min(f[X][i], res + a[i]);
curMin = min(curMin, f[X - 1][i]);
st.push({i, curMin});
}
}
// for(int X = 1; X <= K; X++) {
// for(int i = 1; i <= n; i++) {
// cout << "#: " << X << ' ' << i << "\t" << f[X][i] << endl;
// }
// }
cout << f[K][n] << '\n';
}
void process() {
}
int32_t main() {
if(fopen("trash.inp", "r"))
freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
while(T--) {
in();
process();
}
}
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... |