VERY EASY CPP SOLUTION WITH EXPLANATION - PRIORITY QUEUE
\n# Time Complextity \n@deepeshdkpathak thanks for that\n- creating an adjacency list = O(E) &\n- traversing through all the nodes * [going through all the adja
\n# Time Complextity \n@deepeshdkpathak thanks for that\n- creating an adjacency list = O(E) &\n- traversing through all the nodes * [going through all the adjacent nodes of a specific node * (adding them to a priority queue) + emptying the priority queue which will have a size of K] = O(N * [No. of adj Nodes of a given node * (log(No. of adj nodes)) + K])\n- combining these two we get\n    -- O(E) + O(N * (ADJ * log(ADJ)+ K))\n- where E is the number of edges and ADJ is the number of adjacent nodes of any specific node and K is already provided.\n\n# Space Complexity\n-   O(N * ADJ)\n\n# Code\n```\nclass Solution {\npublic:\n    int maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {\n        int n = vals.size();\n        \n        // create graph\n        vector<vector<int>> g(n);\n        for(auto e : edges) {\n            g[e[0]].push_back(e[1]);\n            g[e[1]].push_back(e[0]);\n        }\n        \n        \n        int ans = INT_MIN;\n        \n        // we going to each node and calculate max sum of at most k edges connected centered node\n        // given question says maximum sum of node vals with at most k edges so we neglacting negaticve vals to connected centerd node \n        for(int i = 0; i < n; i++) {\n            priority_queue<int, vector<int>, greater<int>> pq;\n            \n            int tt = vals[i];  // adding centered node value\n            \n            for(int u : g[i]) {\n                pq.push(vals[u]);              \n                if(pq.size() > k) pq.pop();  // calculate max values of k nodes connected to centered node\n            }\n  \n            while(!pq.empty()){ \n                if(pq.top() > 0) tt += pq.top();  // we only add positive values. suppose centered node connected all -ve vals so in that case only centered node vals is max sum with zero edges\n                pq.pop(); \n            }\n            \n            ans = max(tt, ans);\n        }\n        \n        return  ans;\n    }\n};\n```\n\nIf you helped please like...\nIf any suggestions please leave comment.\n
['C++']