fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4.  
  5. // freopen("snakes.in", "r", stdin);
  6. // freopen("snakes.out", "w", stdout);
  7.  
  8. int n, k;
  9.  
  10. cin >> n >> k;
  11.  
  12. int snakes[n + 1];
  13.  
  14. for(int i = 1;i <= n;i++) cin >> snakes[i];
  15.  
  16. int dp[n + 1][k + 1];
  17.  
  18. int max1 = -1;
  19.  
  20. int tot = 0;
  21.  
  22. for(int i = 1;i <= n;i++){
  23.  
  24. max1 = max(max1, snakes[i]);
  25.  
  26. dp[i][0] = max1 * i; // bat i con ma khong doi size lan nao
  27.  
  28. for(int j = 1;j <= k;j++){
  29. dp[i][j] = INT_MAX;
  30. int max2 = snakes[i];
  31.  
  32. // doi size khi bat den con thu l
  33. for(int l = i - 1;l >= 0;l--){
  34.  
  35. dp[i][j] = min(dp[i][j], dp[l][j - 1] + (i - l) * max2);
  36.  
  37. max2 = max(max2, snakes[l]);
  38.  
  39. }
  40. }
  41. tot += snakes[i];
  42. }
  43. cout << dp[n][k] - tot << endl;
  44. }
Success #stdin #stdout 0.01s 5272KB
stdin
6 2
7 9 8 2 3 2
stdout
-2047566147