Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int n;
- int arr[100005];
- int memo[100005];
- int frog(int i) {
- if(i == n - 1) {
- return 0;
- }
- if(memo[i] != -1) {
- return memo[i];
- }
- int result = 2e9;
- if(i + 1 < n) {
- result = min(result, frog(i + 1) + abs(arr[i] - arr[i + 1]));
- }
- if(i + 2 < n) {
- result = min(result, frog(i + 2) + abs(arr[i] - arr[i + 2]));
- }
- return memo[i] = result;
- }
- int main() {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> arr[i];
- memo[i] = -1;
- }
- cout << frog(0) << endl;
- return 0;
- }
- // fib(7) = fib(6) + fib(5)
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement