This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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], maxi;
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];
maxi=max(maxi, a[i]);
dp[1][i]=maxi;
}
for(int kel=2; 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';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |