fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. #define ll long long
  5.  
  6. int main() {
  7. ll n, i, j, minjumps, maxjumps, minenergy = 1000000000;
  8. cin >> n;
  9. ll p[n];
  10. for (i = 0; i < n; i++) {
  11. cin >> p[i];
  12. }
  13. cin >> minjumps >> maxjumps;
  14. ll a[2][n + 1];
  15. a[0][0] = 0;
  16. for (i = 1; i < n; i++)a[0][i] = a[0][i - 1] + abs(p[i] - p[i - 1]);
  17. if (minjumps == 0)minenergy = a[0][n - 1];
  18. for (j = 1; j <= maxjumps; j++) {
  19. a[j % 2][j * 2] = a[(j - 1) % 2][j * 2 - 2] + 3 * abs(p[j * 2] - p[j * 2 - 2]);
  20. for (i = j * 2 + 1; i < n; i++) {
  21. a[j % 2][i] = min(a[j % 2][i - 1] + abs(p[i - 1] - p[i]), a[(j - 1) % 2][i - 2] + 3 * abs(p[i - 2] - p[i]));
  22. }
  23. if ((j >= minjumps) and (minenergy > a[j % 2][n - 1])) { minenergy = a[j % 2][n - 1]; }
  24. }
  25. cout << minenergy << endl;
  26. return 0;
  27. }
Runtime error #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Standard output is empty