#include <iostream>
#include <stdio.h>
#include <deque>
using namespace std;
typedef long long ll;
const int N = 1e5 + 11;
const ll oo = 1e18;
int n,k,lastGreater[N];
ll f[N][101],a[N],minf[N],tmp[N];
deque <int> Q;
void init(){
ll cur = -1;
for(int i = 1; i <= n; ++i){
cur = max(cur,a[i]);
f[i][1] = cur;
}
for(int i = 1; i <= n; ++i){
lastGreater[i] = 0;
while(Q.size() && a[Q.back()] <= a[i]) Q.pop_back();
if(Q.size()) lastGreater[i] = Q.back();
Q.push_back(i);
}
//for(int i = 1; i <= n; ++i) cout << i << " ~> " << lastGreater[i] << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
cin >> n >> k;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 0; i <= n; ++i)
for(int j = 1; j <= k; ++j) f[i][j] = oo;
init();
minf[0] = oo;
//for(int i = 1; i <= n; ++i) cout << "lastGreater["<<i<<"] = " << lastGreater[i] << endl;
/*for(int i = 1; i <= n; ++i){
cout << "f["<<i<<"][1] = " << f[i][1] << endl;
}*/
for(int j = 2; j <= k; ++j){
Q.clear();
for(int i = 1; i <= n; ++i){
minf[i] = f[i - 1][j - 1]; // minf[i] : from lastGreater[i] -> i - 1
while(Q.size() && a[Q.back()] <= a[i]){
minf[i] = min(minf[i],min(minf[Q.back()],f[Q.back()][j - 1]));
Q.pop_back();
}
//cout << i << " => ["<< (Q.size() ? Q.back() : 0) << " -> " << i - 1<<"] = " << minf[i] << endl;
Q.push_back(i);
}
for(int i = 1; i <= n; ++i) minf[i] += a[i];
for(int i = 1; i <= n; ++i){
f[i][j] = minf[i] = min(minf[i],minf[lastGreater[i]]);
}
/*cout << "j = " << j << " : " << endl;
for(int i = 1; i <= n; ++i) cout << f[i][j] << " / ";
cout << endl;
for(int i = 1; i <= n; ++i) tmp[i] = f[i][j],f[i][j] = oo;
for(int i = 1; i <= n; ++i){
ll cc = 0;
for(int v = i; v > 0; --v){
cc = max(cc,a[v]);
f[i][j] = min(f[i][j],f[v - 1][j - 1] + cc);
}
if(f[i][j] != tmp[i]){
cout << "ok at f["<<i<<","<<j<<"] = " << f[i][j] << endl;
}
}
for(int i = 1; i <= n; ++i) cout << f[i][j] << " / ";
cout << endl << endl;*/
}
cout << f[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
504 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
380 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
380 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
4 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
8440 KB |
Output is correct |
2 |
Correct |
29 ms |
35960 KB |
Output is correct |
3 |
Correct |
35 ms |
36344 KB |
Output is correct |
4 |
Correct |
112 ms |
36344 KB |
Output is correct |
5 |
Correct |
429 ms |
81408 KB |
Output is correct |
6 |
Correct |
65 ms |
81400 KB |
Output is correct |
7 |
Correct |
219 ms |
81400 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
1144 KB |
Output is correct |
13 |
Correct |
5 ms |
1144 KB |
Output is correct |
14 |
Correct |
40 ms |
8444 KB |
Output is correct |
15 |
Correct |
10 ms |
8440 KB |
Output is correct |
16 |
Correct |
17 ms |
8440 KB |
Output is correct |
17 |
Correct |
28 ms |
35960 KB |
Output is correct |
18 |
Correct |
37 ms |
36412 KB |
Output is correct |
19 |
Correct |
136 ms |
36344 KB |
Output is correct |
20 |
Correct |
471 ms |
81508 KB |
Output is correct |
21 |
Correct |
66 ms |
81400 KB |
Output is correct |
22 |
Correct |
243 ms |
81400 KB |
Output is correct |
23 |
Correct |
54 ms |
80632 KB |
Output is correct |
24 |
Correct |
96 ms |
81400 KB |
Output is correct |
25 |
Correct |
478 ms |
81420 KB |
Output is correct |
26 |
Correct |
5 ms |
1144 KB |
Output is correct |
27 |
Correct |
5 ms |
1144 KB |
Output is correct |
28 |
Correct |
32 ms |
8440 KB |
Output is correct |
29 |
Correct |
10 ms |
8440 KB |
Output is correct |
30 |
Correct |
15 ms |
8440 KB |
Output is correct |
31 |
Correct |
29 ms |
36088 KB |
Output is correct |
32 |
Correct |
37 ms |
36472 KB |
Output is correct |
33 |
Correct |
116 ms |
36472 KB |
Output is correct |
34 |
Correct |
423 ms |
81528 KB |
Output is correct |
35 |
Correct |
68 ms |
81528 KB |
Output is correct |
36 |
Correct |
215 ms |
81528 KB |
Output is correct |
37 |
Correct |
10 ms |
8440 KB |
Output is correct |
38 |
Correct |
33 ms |
8440 KB |
Output is correct |
39 |
Correct |
5 ms |
1144 KB |
Output is correct |
40 |
Correct |
5 ms |
1144 KB |
Output is correct |
41 |
Correct |
5 ms |
376 KB |
Output is correct |
42 |
Correct |
5 ms |
376 KB |
Output is correct |