#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node{
int s, e, m, val, lz;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
if(s != e)l = new node(s, m), r = new node(m+1, e);
val = lz = 0;
}
void prop(){
if(lz){
val += lz;
if(s != e)l->lz += lz, r->lz += lz;
lz = 0;
}
}
void upd(int a, int b, int c){
if(a == s && b == e)lz += c;
else{
if(b <= m)l->upd(a, b, c);
else if(a > m)r->upd(a, b, c);
else l->upd(a, m, c), r->upd(m+1, b, c);
l->prop(), r->prop();
val = min(l->val, r->val);
}
}
int qry(int a, int b){
prop();
if(a == s && b== e)return val;
else if(b <= m)return l->qry(a, b);
else if(a > m)return r->qry(a ,b);
else return min(l->qry(a, m), r->qry(m+1, b));
}
void clr(){
val = lz = 0;
if(s != e)l->clr(), r->clr();
}
}*root;
int A[100005], dp[105][100005];
void solve(){
int n, k; cin >> n >> k;
for(int i=1;i<=n;i++)cin >> A[i];
root = new node(1, n);
for(int i=1;i<=k;i++)dp[i][0] = 1e18;
for(int i=1;i<=n;i++)dp[0][i] = 1e18;
for(int i=1;i<=k;i++){
stack <int> s;
root->clr();
for(int j=1;j<=n;j++){
int prv = j - 1;
root->upd(j, j, dp[i - 1][j - 1] + A[j]);
while(!s.empty() && A[s.top()] < A[j]){
int x = s.top(); s.pop();
prv = x;
int nxt = (s.empty() ? 0 : s.top());
//cout << nxt + 1 << ' ' << x << ' ' << A[j] - A[x] << '\n';
root->upd(nxt + 1, x, A[j] - A[x]);
}
dp[i][j] = root->qry(1, j);
//cout << dp[i][j] << ' ';
s.push(j);
}
//cout << '\n';
}
cout << dp[k][n];
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
blocks.cpp: In function 'void solve()':
blocks.cpp:67:8: warning: variable 'prv' set but not used [-Wunused-but-set-variable]
67 | int prv = j - 1;
| ^~~
blocks.cpp: At global scope:
blocks.cpp:86:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
86 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6544 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
4556 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
8536 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
14680 KB |
Output is correct |
17 |
Correct |
2 ms |
14868 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6544 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
4556 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
2 ms |
8536 KB |
Output is correct |
31 |
Correct |
1 ms |
8540 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
14680 KB |
Output is correct |
35 |
Correct |
2 ms |
14868 KB |
Output is correct |
36 |
Correct |
2 ms |
10588 KB |
Output is correct |
37 |
Correct |
2 ms |
14684 KB |
Output is correct |
38 |
Correct |
3 ms |
18780 KB |
Output is correct |
39 |
Correct |
7 ms |
41304 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4440 KB |
Output is correct |
42 |
Correct |
1 ms |
4440 KB |
Output is correct |
43 |
Correct |
1 ms |
4440 KB |
Output is correct |
44 |
Correct |
1 ms |
4600 KB |
Output is correct |
45 |
Correct |
1 ms |
4440 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
6492 KB |
Output is correct |
49 |
Correct |
1 ms |
6492 KB |
Output is correct |
50 |
Correct |
1 ms |
6488 KB |
Output is correct |
51 |
Correct |
4 ms |
22876 KB |
Output is correct |
52 |
Correct |
4 ms |
22876 KB |
Output is correct |
53 |
Correct |
12 ms |
82524 KB |
Output is correct |
54 |
Correct |
11 ms |
82524 KB |
Output is correct |
55 |
Correct |
7 ms |
45404 KB |
Output is correct |
56 |
Correct |
12 ms |
82528 KB |
Output is correct |
57 |
Correct |
13 ms |
82524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6544 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
4556 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
2 ms |
8536 KB |
Output is correct |
31 |
Correct |
1 ms |
8540 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
14680 KB |
Output is correct |
35 |
Correct |
2 ms |
14868 KB |
Output is correct |
36 |
Correct |
2 ms |
10588 KB |
Output is correct |
37 |
Correct |
2 ms |
14684 KB |
Output is correct |
38 |
Correct |
3 ms |
18780 KB |
Output is correct |
39 |
Correct |
7 ms |
41304 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4440 KB |
Output is correct |
42 |
Correct |
1 ms |
4440 KB |
Output is correct |
43 |
Correct |
1 ms |
4440 KB |
Output is correct |
44 |
Correct |
1 ms |
4600 KB |
Output is correct |
45 |
Correct |
1 ms |
4440 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
6492 KB |
Output is correct |
49 |
Correct |
1 ms |
6492 KB |
Output is correct |
50 |
Correct |
1 ms |
6488 KB |
Output is correct |
51 |
Correct |
4 ms |
22876 KB |
Output is correct |
52 |
Correct |
4 ms |
22876 KB |
Output is correct |
53 |
Correct |
12 ms |
82524 KB |
Output is correct |
54 |
Correct |
11 ms |
82524 KB |
Output is correct |
55 |
Correct |
7 ms |
45404 KB |
Output is correct |
56 |
Correct |
12 ms |
82528 KB |
Output is correct |
57 |
Correct |
13 ms |
82524 KB |
Output is correct |
58 |
Correct |
68 ms |
24160 KB |
Output is correct |
59 |
Correct |
20 ms |
10840 KB |
Output is correct |
60 |
Correct |
93 ms |
14936 KB |
Output is correct |
61 |
Correct |
928 ms |
70384 KB |
Output is correct |
62 |
Execution timed out |
1050 ms |
96604 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |