accessing index outside the bounds of array in c++

I recently encountered a problem - code snippet is

int n,k,o[30110],dp[30110][600],x;
int dfs(int s,int l){
    if (dp[s][l]!=-1) return dp[s][l];
    int res=0,i,v;
    for (i=-1;i<=1; ++i){
        v=s+i+l;
        if(v>30001 || v<=s ) continue;
        res=max(res,dfs(v,l+i));
    }
    return dp[s][l]=res+o[s];
}
int main(){
    memset(dp,-1,sizeof dp);
    cin>>n>>k;
    while(n--){
        cin>>x;
        o[x]++;
    }
    cout<<dfs(k,k)<<endl;
    return 0;
}

This code is running on test case test case:

**1 30000
30000**

though my array is not of this much size.

It has been successfully submitted on the online judge codeforces. I researched and found out that out of bound is not taken care in C++ but then reducing the array to dp[30110][10] is giving a wrong answer. kindly help. Also there are less than 500 states in dp where I can go. problem link - https://codeforces.com/contest/505/problem/C