C++ recursive DP with memoization
define ll long long\nclass Solution {\npublic:\n    ll dp[5005][7][18],nr,ar[7],mod=1e9+7;\n \n    ll f(ll idx,ll lst,ll cnt)\n    {\n        if(idx==nr)\n     
#define ll long long\nclass Solution {\npublic:\n    ll dp[5005][7][18],nr,ar[7],mod=1e9+7;\n \n    ll f(ll idx,ll lst,ll cnt)\n    {\n        if(idx==nr)\n            return 1;\n        ll &res=dp[idx][lst][cnt];\n        if(res!=-1)\n            return res%mod;\n        res=0;\n        for(int i=1;i<=6;i++)\n        {\n            if(lst==i)\n            {\n                if(cnt==ar[i])\n                    continue;\n                res =(res%mod + f(idx+1,lst,cnt+1)%mod)%mod;\n \n            }\n            else\n            {\n                res=(res%mod+f(idx+1,i,1)%mod)%mod;\n            }\n        }\n \n        return res%mod;\n    }\n    int dieSimulator(int n, vector<int>& rollMax) {\n \n        nr=n;\n        for(int i=1;i<7;i++)\n            ar[i]=rollMax[i-1];\n        memset(dp,-1,sizeof(dp));\n        return f(0,0,0);\n \n    }\n};
[]