98% Beats || Very Easy C++ Code ✅☑
\n\n# Intuition\nCreate adjacency list with priority queue and take top k elements by value for each node.\n Describe your first thoughts on how to solve this p
![leet8.PNG](https://assets.leetcode.com/users/images/aafb94b7-0915-4bbd-92dd-f32d91abd086_1706900806.7531796.png)\n\n# Intuition\nCreate adjacency list with priority queue and take top k elements by value for each node.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. create a adjacency list using priority queue to insert adjcent nodes in sorted order.\n2. Find max value among all nodes. (in case of disconnected graph).\n3. push Given edges into adjacency list as {val, key} pair.\n4. interate through graph and find max_sum at each node.\n5. return mx.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n*k)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n*m)$$ // (n = no. of nodes, m= no. of edges)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\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        priority_queue<pair<int, int>> adj[n]; // {val, key}\n        int m = edges.size();\n        int mx = INT_MIN;\n\n        for (int i = 0; i < n; i++) {\n            mx = max(mx, vals[i]);\n        }\n        if (m == 0) {\n            return mx;\n        }\n        for (int i = 0; i < m; i++) {\n            adj[edges[i][0]].push({vals[edges[i][1]], edges[i][1]});\n            adj[edges[i][1]].push({vals[edges[i][0]], edges[i][0]});\n        }\n\n        for (int i = 0; i < n; i++) {\n            int sum = vals[i]; // Include the central node\'s value\n            int j = 1;\n\n            while (!adj[i].empty() && j <= k) {\n                int add = adj[i].top().first;\n                sum += add;\n                adj[i].pop();\n                j++;\n                mx = max(mx, max(sum, vals[i]));\n            }\n        }\n\n        return mx;\n    }\n};\n\n```
['C++']