#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;
int n,k;
int a[N];
int d[111][N];
void solve()
{
cin >> n >> k;
int mx = 0;
for( int i = 1; i <= n; i++ ){
cin >> a[i];
mx = max( mx , a[i] );
d[1][i] = mx;
}
for( int i = 2; i <= k; i++ ){
d[i][i] = d[i - 1][i - 1] + a[i];
stack < pair < int , int > > st;
st.push({d[i - 1][i - 1] , a[i]});
for( int j = i + 1; j <= n; j++ ){
int c = d[i - 1][j - 1];
while( !st.empty() && st.top().se <= a[j] ){
c = min( c , st.top().fi );
st.pop();
}
if( st.empty() || c + a[j] <= st.top().fi + st.top().se ){
st.push({c , 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 |
504 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 |
380 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 |
380 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 |
420 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 |
388 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 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 |
3 ms |
452 KB |
Output is correct |
9 |
Correct |
4 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 |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
760 KB |
Output is correct |
16 |
Correct |
2 ms |
760 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
2 ms |
760 KB |
Output is correct |
19 |
Correct |
3 ms |
864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1528 KB |
Output is correct |
2 |
Correct |
8 ms |
1016 KB |
Output is correct |
3 |
Correct |
11 ms |
2044 KB |
Output is correct |
4 |
Correct |
46 ms |
14328 KB |
Output is correct |
5 |
Correct |
129 ms |
40568 KB |
Output is correct |
6 |
Correct |
17 ms |
2552 KB |
Output is correct |
7 |
Correct |
66 ms |
19036 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
16 ms |
4856 KB |
Output is correct |
15 |
Correct |
17 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
1500 KB |
Output is correct |
17 |
Correct |
8 ms |
1016 KB |
Output is correct |
18 |
Correct |
12 ms |
2040 KB |
Output is correct |
19 |
Correct |
45 ms |
14328 KB |
Output is correct |
20 |
Correct |
119 ms |
40568 KB |
Output is correct |
21 |
Correct |
17 ms |
2552 KB |
Output is correct |
22 |
Correct |
62 ms |
19068 KB |
Output is correct |
23 |
Correct |
15 ms |
1784 KB |
Output is correct |
24 |
Correct |
24 ms |
5368 KB |
Output is correct |
25 |
Correct |
124 ms |
40544 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
504 KB |
Output is correct |
28 |
Correct |
19 ms |
4856 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
7 ms |
1528 KB |
Output is correct |
31 |
Correct |
8 ms |
1016 KB |
Output is correct |
32 |
Correct |
13 ms |
2164 KB |
Output is correct |
33 |
Correct |
88 ms |
14532 KB |
Output is correct |
34 |
Correct |
182 ms |
40948 KB |
Output is correct |
35 |
Correct |
18 ms |
2772 KB |
Output is correct |
36 |
Correct |
89 ms |
19444 KB |
Output is correct |
37 |
Correct |
4 ms |
504 KB |
Output is correct |
38 |
Correct |
15 ms |
4984 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 |