#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 long long INF = 1e18;
int n,k;
long long dp[N][K];
int a[N];
int lf[N];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
up(i,1,n) cin >> a[i];
int curMax = 0;
up(i,0,n) up(j,0,k) dp[i][j] = INF;
up(i,1,n){
curMax = max(curMax, a[i]);
dp[i][1] = curMax;
}
int s = 0;
up(i,1,k){
s += a[i];
dp[i][i] = s;
}
vector<int> sk;
up(i,1,n){
while (!sk.empty()) {
if (a[sk.back()] < a[i]) sk.pop_back();
else break;
}
if (!sk.empty()) lf[i] = sk.back() + 1;
else lf[i] = 1;
sk.push_back(i);
}
for( int i = 2;i <= n;++i){
for(int j = 2; j < min(i,k+1) ;++j){
dp[i][j] = min(dp[max(lf[i], j-1)][j-1] + a[i], dp[lf[i]-1][j]);
}
}
cout << dp[n][k];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
0 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
8696 KB |
Output is correct |
2 |
Correct |
36 ms |
37496 KB |
Output is correct |
3 |
Correct |
38 ms |
37496 KB |
Output is correct |
4 |
Incorrect |
49 ms |
37728 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |