Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n;
- int niza[100005];
- int frog[100005];
- int rec(int frog_at) {
- if(frog_at == n - 1) {
- return 0;
- }
- if(frog[frog_at] != -1) {
- return frog[frog_at];
- }
- int result = 2e9;
- if(frog_at + 1 < n) {
- result = min(result, rec(frog_at + 1) + abs(niza[frog_at] - niza[frog_at + 1]));
- }
- if(frog_at + 2 < n) {
- result = min(result, rec(frog_at + 2) + abs(niza[frog_at] - niza[frog_at +
- 2]));
- }
- return frog[frog_at] = result;
- }
- int main()
- {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- }
- for(int i = 0; i <= n; i++) {
- frog[i] = -1;
- }
- cout<< rec(0) << endl;
- return 0;
- }
- /*
- rec(0) = MIN(rec(1) + 20 || rec(2) + 30) = MIN(30, 50) = 30
- rec(1) = MIN(rec(2) + 10 || rec(3) + 10) = Min(30, 10) = 10
- rec(2) = MIN(rec(3) + 20) = 20
- rec(3) = 0
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement