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