#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
const int mod=998244353, maxn=1e5+5;
int n, k, a[maxn], mindp, val, dp[105][maxn];
stack<pii> s;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> k;
for(int i=1; i<=n; i++) cin >> a[i];
for(int kel=1; kel<=k; kel++) {
for(int i=kel; i<=n; i++) {
mindp=dp[kel-1][i-1];
while(!s.empty()&&a[s.top().fi]<=a[i]) {
mindp=min(mindp, s.top().se);
s.pop();
}
val=1e9;
if(!s.empty()) val=dp[kel][s.top().fi];
dp[kel][i]=min(val, mindp+a[i]);
s.push({i, mindp});
}
while(!s.empty()) s.pop();
}
cout << dp[k][n] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |