#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
ll dp[2][100005];
ll a[100005];
ll pre[100005];
const ll INF = 1000000000000000LL;
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
ll n, k;
cin >> n >> k;
for(ll i=1; i<=n; i++) cin >> a[i];
for(ll i=1; i<=n; i++){
dp[0][i] = max(dp[0][i-1], a[i]);
pre[i] = pre[i-1] + a[i];
}
dp[0][0] = INF;
for(ll g=2; g<=k; g++){
deque <pair <pair <ll, ll>, pair <ll, ll>>> q;
for(ll i=0; i<g; i++){
dp[1][i] = INF;
}
//dp[1][g] = pre[g];
for(ll i=g; i<=n; i++){
ll mnl = i-1;
while(!q.empty() && q.back().second.first <= a[i]){
mnl = min(mnl, q.back().first.first);
q.pop_back();
}
//cout << i << " " << mnl << endl;
dp[1][i] = dp[0][mnl] + a[i];
if(!q.empty()) dp[1][i] = min(dp[1][i], q.back().second.second);
q.push_back({{mnl, i-1}, {a[i], dp[1][i]}});
}
for(ll i=0; i<=n; i++) dp[0][i] = min(INF, dp[1][i]);
}
cout << dp[0][n] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |