Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- int a[100000];
- int dp[100000];
- int rec(int c){
- if (c==n-1){
- return 0;
- }
- if (dp[c]!=-1){
- return dp[c];
- }
- else {
- int dis;
- int y=1e9;
- for (int j=1;j<3;j++){
- if (c+j<n){
- dis=abs(a[c]-a[c+j]);
- y=min(y,dis+rec(c+j));
- }
- }
- dp [c]=y;
- return y;
- }
- }
- int main(){
- cin>>n;
- for (int i=0;i<n;i++){
- cin>>a[i];
- dp[i]=-1;
- }
- cout<<rec(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment