C++ | Beats 100% | Simple Intuition | Vector of priority queue
IntuitionWe need "maximum upto k", most suitable datastructure would be priority_queue as it maintains the min/max elements at topApproach
create the adjacency
# Intuition\nWe need "maximum upto k", most suitable datastructure would be priority_queue as it maintains the min/max elements at top\n\n# Approach\n- create the adjacency list as vector of priority queue\n- add each weight connected to that node. No need to add connected node number as we only need to find the weight. \n# Complexity\n- Time complexity:\n```\nO(Elogn+n\u22C5k\u22C5logn)\n```\n\n- Space complexity:\n```\nO(E)\n```\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {\n        // Initialize adjacency list of priority queues\n        int n = vals.size();\n        vector<priority_queue<int>> adj(n); // Max-heaps to store neighbor values\n\n        // Populate adjacency list\n        for (auto& edge : edges) {\n            int u = edge[0], v = edge[1];\n            adj[u].push(vals[v]);\n            adj[v].push(vals[u]);\n        }\n\n        int ans = INT_MIN;\n\n        // Iterate over all nodes\n        for (int i = 0; i < n; i++) {\n            int temp = vals[i]; // Start with the node\'s value\n            int count = k;\n\n            // Add values of up to k neighbors\n            while (count-- > 0 && !adj[i].empty()) {\n                if(adj[i].top()<0)\n                break;\n                temp += adj[i].top();\n                \n                adj[i].pop();\n            }\n\n            // Update the maximum star sum\n            ans = max(ans, temp);\n        }\n\n        return ans;\n    }\n};\n\n```
['Heap (Priority Queue)', 'C++']