Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include<iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <set>
  5. using namespace std;
  6. int a[30001];
  7. int dp[30001];
  8. int main()
  9. {
  10. int n;
  11. cin >> n;
  12. for (int i =0; i < n; i++)
  13. {
  14. cin >> a[i];
  15. }
  16. if(n == 1)
  17. {
  18. cout << a[0];
  19. return 0;
  20. }
  21. if(n == 2)
  22. {
  23. int kol1 = 0;
  24. kol1 = abs(a[0] - a[1]);
  25. cout << kol1;
  26. return 0;
  27. }
  28. for(int i = 2; i < n; i++)
  29. {
  30. int kol = 0, kol1 = 0;
  31. kol = abs(a[i - 2] - a[i - 1]) + abs(a[i - 1] - a[i]) + dp[i - 1];
  32. kol1 = abs(a[i] - a[i - 2]) * 3 + dp[i - 1];
  33. dp[i] = min(kol, kol1) ;
  34. }
  35. cout << dp[n -1];
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement