Java Solution | Priority Queue | Heap | 100% 0ms solution | O(n)
Approach\nCreate the adjacency list first\nTraverse all the edges\nStore current edhe value in temp (because we need to add that value to the answer)\nget all t
# Approach\nCreate the adjacency list first\nTraverse all the edges\nStore current edhe value in temp (because we need to add that value to the answer)\nget all the adjacent values and add to the priority queue (you can also sort)\nadd the adjacent nodes to the temp sum and then remove them from the queue\nthen remove everything from the queue for the next iteration\nreturn the maximum result so far \n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution {\n    public int maxStarSum(int[] vals, int[][] edges, int k) {\n        int n = vals.length;\n        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();\n        for(int i=0;i<vals.length;i++){\n            adj.add(new ArrayList<>());\n        }\n        for(int i[]:edges){\n            adj.get(i[0]).add(i[1]);\n            adj.get(i[1]).add(i[0]);\n        }\n        int res = Integer.MIN_VALUE;\n        PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)->b-a);\n        for(int i=0;i<n;i++){\n            int temp = vals[i];\n            for(int j=0;j<adj.get(i).size();j++){\n                pq.add(vals[adj.get(i).get(j)]);\n            }\n            int sum = k;\n            while(sum-->0 && !pq.isEmpty() && pq.peek() > 0){\n                temp += pq.peek();\n                pq.remove();\n            }\n            while(!pq.isEmpty()) pq.remove();\n            res = Math.max(res,temp);\n        }\n        return res;\n    }\n}\n```
['Heap (Priority Queue)', 'Java']