I’m solving atcoder educational dp contest. There’s a simple dp problem Frog-1(link: A - Frog 1)
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.