#include <bits/stdc++.h>
#define up(i,a,b) for(int (i) = (a);(i)<=(b);++(i))
#define down(i,b,a) for(int (i) = (b);i>=(a);--i)
#define debug(x) cerr << (x) << '\n';
#define bits(x,i) ((x >> i) & 1)
using namespace std;
const int N = 100005;
const int K = 105;
const int INF = 2e9;
int n,k;
int dp[N][K];
int a[N];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("BLOCKS_IZHO.inp", "r", stdin);
// freopen("BLOCKS_IZHO.out", "w", stdout);
cin >> n >> k;
up(i,1,n) cin >> a[i];
int curMax = 0;
memset(dp, 0x3f, sizeof(dp));
dp[0][1] = 0;
up(i,1,n) dp[i][1] = max(dp[i-1][1], a[i]);
vector< pair<int,int> > sk;
for(int j = 2; j <= k ;++j){
for( int i = j;i <= n;++i){
int minF = dp[i-1][j-1];
// first = value
// second = id
while (!sk.empty() && a[sk.back().second] <= a[i]){
minF = min (minF, sk.back().first);
sk.pop_back();
}
dp[i][j] = min(dp[sk.empty() ? 0 : sk.back().second][j], minF + a[i]);
sk.push_back(pair<int,int> (minF, i));
}
}
cout << dp[n][k];
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:19:6: warning: unused variable 'curMax' [-Wunused-variable]
int curMax = 0;
^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
41464 KB |
Output is correct |
2 |
Correct |
41 ms |
41460 KB |
Output is correct |
3 |
Correct |
41 ms |
41400 KB |
Output is correct |
4 |
Correct |
41 ms |
41592 KB |
Output is correct |
5 |
Correct |
42 ms |
41372 KB |
Output is correct |
6 |
Correct |
37 ms |
41464 KB |
Output is correct |
7 |
Correct |
42 ms |
41464 KB |
Output is correct |
8 |
Correct |
41 ms |
41436 KB |
Output is correct |
9 |
Correct |
41 ms |
41464 KB |
Output is correct |
10 |
Incorrect |
36 ms |
41464 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
41464 KB |
Output is correct |
2 |
Correct |
41 ms |
41592 KB |
Output is correct |
3 |
Correct |
41 ms |
41464 KB |
Output is correct |
4 |
Correct |
36 ms |
41464 KB |
Output is correct |
5 |
Correct |
41 ms |
41464 KB |
Output is correct |
6 |
Correct |
42 ms |
41436 KB |
Output is correct |
7 |
Correct |
40 ms |
41404 KB |
Output is correct |
8 |
Correct |
42 ms |
41464 KB |
Output is correct |
9 |
Correct |
41 ms |
41464 KB |
Output is correct |
10 |
Incorrect |
36 ms |
41464 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
41464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
41600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |