fork download
  1. #include <stdio.h>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. class FrogJump1 {
  7. public:
  8. int totalCost(vector<int>& heights, int n) {
  9.  
  10. vector<int> cost(n);
  11. cost[0] = 0;
  12. cost[1] = abs(heights[0] - heights[1]);
  13.  
  14. for(int i = 2; i < n; i++) {
  15. cost[i] = min(cost[i-1] + abs(heights[i] - heights[i-1]), cost[i-2] + abs(heights[i] - heights[i-2]));
  16. }
  17. return cost[n-1];
  18. }
  19. };
  20.  
  21. int main() {
  22. int n;
  23. vector<int> heights(n);
  24. cin >> n;
  25. for(int i = 0; i < n; ++i) {
  26. cin >> heights[i];
  27. }
  28. FrogJump1 *obj = new FrogJump1();
  29. cout << obj->totalCost(heights, n) << endl;
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5320KB
stdin
4
10 30 40 20
stdout
30