C++ | Easy Simple TopDown DP | Memomization
\nclass Solution {\npublic:\n    \n    int solve(vector<vector<pair<int,int>>>& a, vector<int>& p,vector<vector<int>>& dp,int n,int t,int q){\n        if(t<0)\n
```\nclass Solution {\npublic:\n    \n    int solve(vector<vector<pair<int,int>>>& a, vector<int>& p,vector<vector<int>>& dp,int n,int t,int q){\n        if(t<0)\n            return INT_MAX;\n        if(q==n-1)\n            return p[q];\n        if(dp[q][t]!=-1)\n            return dp[q][t];\n        \n        int ans=INT_MAX;\n        for(auto& x:a[q]){\n            int res=solve(a,p,dp,n,t-x.second,x.first);\n            ans=min(ans,(res==INT_MAX?res:res+p[q]));\n        }\n        return dp[q][t]=ans;\n    }\n    \n    int minCost(int t, vector<vector<int>>& g, vector<int>& p) {\n        int n=p.size();\n        vector<vector<int>> dp(n,vector<int>(t+1,-1));\n        vector<vector<pair<int,int>>> a(n);\n        for(auto& x:g){\n            a[x[0]].push_back({x[1],x[2]});\n            a[x[1]].push_back({x[0],x[2]});\n        }\n        int ans=solve(a,p,dp,n,t,0);\n        return ans==INT_MAX?-1:ans;\n    }\n};\n```\n**Don\'t forget to upvote**
['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++']