#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define fi first
#define se second
#define ll long long
using namespace std;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
ll n,k;
ll a[N];
ll d[111][N];
void solve()
{
cin >> n >> k;
for( int i = 1; i <= n; i++ ){
cin >> a[i];
d[1][i] = max( d[1][i - 1] , a[i] );
}
for( int i = 2; i <= k; i++ ){
d[i][i] = d[i - 1][i - 1] + a[i];
stack < pair < ll , ll > > st;
st.push({d[i - 1][i - 1] , a[i]});
for( int j = i + 1; j <= n; j++ ){
ll cnt = d[i - 1][j - 1];
while( !st.empty() && st.top().se <= a[j] ){
cnt = min( cnt , st.top().fi );
st.pop();
}
if( st.empty() || st.top().fi + st.top().se > cnt + a[j] ){
st.push({cnt , a[j]});
}
d[i][j] = st.top().fi + st.top().se;
}
}
cout << d[k][n];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int t = 1;//cin >> t;
while( t-- ){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 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 |
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 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
888 KB |
Output is correct |
16 |
Correct |
2 ms |
888 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
2 ms |
888 KB |
Output is correct |
19 |
Correct |
2 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2552 KB |
Output is correct |
2 |
Correct |
7 ms |
1272 KB |
Output is correct |
3 |
Correct |
11 ms |
3448 KB |
Output is correct |
4 |
Correct |
55 ms |
27744 KB |
Output is correct |
5 |
Correct |
154 ms |
80060 KB |
Output is correct |
6 |
Correct |
17 ms |
4176 KB |
Output is correct |
7 |
Correct |
77 ms |
37160 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
18 ms |
8824 KB |
Output is correct |
15 |
Correct |
4 ms |
676 KB |
Output is correct |
16 |
Correct |
6 ms |
2552 KB |
Output is correct |
17 |
Correct |
7 ms |
1400 KB |
Output is correct |
18 |
Correct |
11 ms |
3392 KB |
Output is correct |
19 |
Correct |
52 ms |
27768 KB |
Output is correct |
20 |
Correct |
139 ms |
80092 KB |
Output is correct |
21 |
Correct |
17 ms |
4216 KB |
Output is correct |
22 |
Correct |
71 ms |
36984 KB |
Output is correct |
23 |
Correct |
14 ms |
2552 KB |
Output is correct |
24 |
Correct |
26 ms |
9592 KB |
Output is correct |
25 |
Correct |
144 ms |
80164 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
504 KB |
Output is correct |
28 |
Correct |
23 ms |
8824 KB |
Output is correct |
29 |
Correct |
4 ms |
760 KB |
Output is correct |
30 |
Correct |
8 ms |
2552 KB |
Output is correct |
31 |
Correct |
7 ms |
1272 KB |
Output is correct |
32 |
Correct |
13 ms |
3696 KB |
Output is correct |
33 |
Correct |
79 ms |
27992 KB |
Output is correct |
34 |
Correct |
232 ms |
80644 KB |
Output is correct |
35 |
Correct |
19 ms |
4532 KB |
Output is correct |
36 |
Correct |
112 ms |
37544 KB |
Output is correct |
37 |
Correct |
3 ms |
632 KB |
Output is correct |
38 |
Correct |
17 ms |
8824 KB |
Output is correct |
39 |
Correct |
2 ms |
504 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |