#include "bits/stdc++.h"
#define MIN_DP first
#define MAX_A second
using lint = long long;
const lint INF = 0x3f3f3f3f;
using namespace std;
//21+18+14 pts
lint dp[102][100002];
lint a[100002];
int n, k;
using str = pair<lint, lint>; // mindp_suf, max_a_suf
void init(){
for(int qtd = 0; qtd <= k; qtd++)
for(int i=0; i<=n; i++)
dp[qtd][i] = INF;
dp[1][1] = a[1];
for(int i=2; i <=n; i++)
dp[1][i] = max(dp[1][i-1], a[i]);
}
void assertMono(){
for(int q = 1; q < k; q++)
for(int q2 = q+1; q2 < k; q2++)
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
assert(dp[q][i] + dp[q2][j] <= dp[q][j] + dp[q2][i]);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>k;
for(int i=1; i<= n; i++)
cin>>a[i];
init();
for(int qtd=2; qtd <=k; qtd++){
vector<str> stack;
for(int i=1; i<=n; i++){
lint min_dp = dp[qtd-1][i-1];
//se eu não der pop encontrei um a[j] > a[i] então tudo bem :D
while(!stack.empty() && stack.back().MAX_A < a[i]){
min_dp = min(min_dp, stack.back().MIN_DP);
stack.pop_back();
}
// como min_dp[j] < min_dp_atual (já que a[j] > a[i]) se eu não melhoro o min_dp + a[i] posso jogar fora porque o próximo min_dp[j] é menor com certeza!!
if(stack.empty() || min_dp + a[i] < stack.back().MAX_A + stack.back().MIN_DP){
stack.push_back({min_dp, a[i]});
}
if(i >= qtd)
dp[qtd][i] = stack.back().MIN_DP + stack.back().MAX_A;
}
}
assertMono();
cout<<dp[k][n]<<"\n";
return 0;
}
/*
[ ]Leu o problema certo???
[ ]Ver se precisa de long long
[ ]Viu o limite dos fors (é n? é m?)
[ ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[ ]Testar sample
[ ]Testar casos de borda
[ ]1LL no 1LL << i
[ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 6 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
288 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 6 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 6 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 6 |
10 |
Halted |
0 ms |
0 KB |
- |