#include <bits/stdc++.h>
#define fto(i,j,h) for (int i=j; i<=h; ++i)
#define fdto(i,j,h) for (int i=j; i>=h; --i)
#define przxct "main"
#define maxn 100003
#define fi first
#define se second
#define ll long long
#define db double
#define pi 3.141592653589
using namespace std;
int n, k, a[maxn];
ll F[103][maxn];
stack<int> t;
stack<pair<int, ll> > t2;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//#ifndef ONLINE_JUDGE
//freopen(przxct".inp", "r", stdin);
//freopen(przxct".out", "w", stdout);
// #endif // ONLINE_JUDGE
cin >> n >> k;
fto(i,1,n) cin >> a[i];
int Max = 0;
fto(i,1,n){
Max = max(Max, a[i]);
F[1][i] = Max;
}
fto(i,2,k)
fto(j,1,n){
F[i][j] = 1e18;
}
fto(g,2,k){
t2.push({g-1, F[g-1][g-1]});
fto(i,g,n){
while (t.empty() == false && a[t.top()] < a[i]){
t.pop();
}
if (t.empty() == false) F[g][i] = min(F[g][i], F[g][t.top()]);
t.push(i);
ll Min = 1e18;
while (t2.empty() == false && a[t2.top().fi] <= a[i]){
Min = min(Min, min(t2.top().se, F[g-1][t2.top().fi]));
t2.pop();
}
if (t2.empty() == false) Min = min(Min, F[g-1][t2.top().fi]);
F[g][i] = min(F[g][i], Min + a[i]);
t2.push({i, Min});
}
}
cout << F[k][n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
2560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |