#include <bits/stdc++.h>
#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair
using namespace std;
typedef long long ll;
const int inf = (int)1e9 + 7;
const int N = (int)1e5 + 2;
int n,kk;
int dp[2][N];
int a[N];
int t[21][N];
void buildtable() {
for (int i = 0; i <= log2(n); i ++) {
for (int j = 1; j <= n; j ++) {
if (i == 0) {
t[i][j] = a[j];
continue;
}
t[i][j] = max(t[i - 1][j],t[i - 1][j + (1 << (i - 1))]);
}
}
}
int get (int l,int r) {
int cur = log2(r - l + 1);
return max(t[cur][l],t[cur][r - (1 << cur) + 1]);
}
void compute (int k,int l,int r,int L,int R) {
if (l > r)
return;
int m = (r + l) / 2;
int opt = L;
dp[k][m] = inf;
int &d = dp[k][m];
for (int i = L; i <= min(m,R); i ++) {
int now = dp[k ^ 1][i - 1] + get(i,m);
if (now < d)
d = now,opt = i;
}
compute (k,l,m - 1,L,opt);
compute (k,m + 1,r,opt,R);
}
main () {
scanf ("%d%d", &n,&kk);
for (int i = 1; i <= n; i ++)
scanf ("%d", &a[i]);
buildtable();
for (int i = 1; i <= n; i ++)
dp[1][i] = get(1,i);
for (int i = 2; i <= kk; i ++)
compute (i & 1,i,n,i,n);
cout << dp[kk & 1][n] << endl;
}
Compilation message
blocks.cpp:54:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
blocks.cpp: In function 'int main()':
blocks.cpp:55:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d%d", &n,&kk);
^
blocks.cpp:58:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d", &a[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
11404 KB |
Output is correct |
2 |
Correct |
0 ms |
11404 KB |
Output is correct |
3 |
Correct |
0 ms |
11404 KB |
Output is correct |
4 |
Correct |
0 ms |
11404 KB |
Output is correct |
5 |
Correct |
0 ms |
11404 KB |
Output is correct |
6 |
Correct |
0 ms |
11404 KB |
Output is correct |
7 |
Correct |
0 ms |
11404 KB |
Output is correct |
8 |
Correct |
0 ms |
11404 KB |
Output is correct |
9 |
Correct |
0 ms |
11404 KB |
Output is correct |
10 |
Correct |
0 ms |
11404 KB |
Output is correct |
11 |
Incorrect |
0 ms |
11404 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
11404 KB |
Output is correct |
2 |
Correct |
0 ms |
11404 KB |
Output is correct |
3 |
Correct |
0 ms |
11404 KB |
Output is correct |
4 |
Correct |
0 ms |
11404 KB |
Output is correct |
5 |
Correct |
0 ms |
11404 KB |
Output is correct |
6 |
Correct |
0 ms |
11404 KB |
Output is correct |
7 |
Correct |
0 ms |
11404 KB |
Output is correct |
8 |
Correct |
0 ms |
11404 KB |
Output is correct |
9 |
Correct |
0 ms |
11404 KB |
Output is correct |
10 |
Correct |
0 ms |
11404 KB |
Output is correct |
11 |
Incorrect |
0 ms |
11404 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
11404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
11404 KB |
Output is correct |
2 |
Correct |
9 ms |
11404 KB |
Output is correct |
3 |
Correct |
153 ms |
11404 KB |
Output is correct |
4 |
Execution timed out |
1000 ms |
11404 KB |
Execution timed out |
5 |
Halted |
0 ms |
0 KB |
- |