# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
166638 | abil | K blocks (IZhO14_blocks) | C++14 | 237 ms | 40828 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 fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
//#define int long long
using namespace std;
const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);
int a[N], dp[112][N], n;
main()
{
int k;
scanf("%d%d", &n, &k);
for(int i = 1;i <= n; i++){
scanf("%d", &a[i]);
}
for(int i = 1;i <= n; i++){
dp[1][i] = max(dp[1][i - 1], a[i]);
}
for(int i = 2;i <= k; i++){
stack<pair<int,int > > st;
dp[i][i] = dp[i - 1][i - 1] + a[i];
st.push({dp[i - 1][i - 1],a[i]});
for(int j = i + 1;j <= n; j++){
int c = dp[i - 1][j - 1];
while(!st.empty() && st.top().sc <= a[j]){
c = min(c, st.top().fr);
st.pop();
}
if(st.empty() || st.top().fr + st.top().sc > c + a[j]){
st.push({c, a[j]});
}
dp[i][j] = st.top().fr + st.top().sc;
}
}
cout << dp[k][n];
}
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... |