Values not going into the array [HELP]

I’m solving atcoder educational dp contest. There’s a simple dp problem Frog-1(link: https://atcoder.jp/contests/dp/tasks/dp_a)

This is my code:

#include <bits/stdc++.h>
using namespace std;

int arr[100],n;
int dp[100];

int solve(int pos){
    if(pos==n-1)
        return 0;
    if(dp[pos]!=-1)
        return dp[pos];
    if(pos==n-2){
        dp[pos]=abs(arr[pos]-arr[pos+1]);
        return dp[pos];
    }
    if(pos<=n-3){
        dp[pos]=min(abs(arr[pos]-arr[pos+1])+solve(pos+1), abs(arr[pos]-arr[pos+2])+solve(pos+2));
        return dp[pos];
    }
}

int main(){
    cin>>n;
    memset(arr, 0, sizeof(arr));
    memset(dp, -1, sizeof(dp));
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    for(int i=0;i<n;i++)cout<<dp[i]<<" ";
    cout<<solve(0)<<endl;
    return 0;
}

The problem is, it prints the correct answer but when I ty to print the array dp[], it prints -1 -1 -1 …
So the values aren’t getting assigned in the array, but the answer is correct. Then how is the array still -1.

cout<<solve(0)<<endl;
for(int i=0;i<n;i++)cout<<dp[i]<<" ";

Oh my f#$%& godddd…

Bro you’re a lifesaver, I keep making these silly mistakes.
If it weren’t for you, it’d have taken forever to notice that mistake.

1 Like

in the beginning everyone does …

Ikr, but the problem is, I’ve been coding since almost 6 years now…