Using map and sorting
\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        int ans
\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        int ans=INT_MIN;\n        vector<int> adj[n+1];\n        //Make adjacency list of given graph\n        for(int i=0;i<edges.size();i++){\n            adj[edges[i][0]].push_back(vals[edges[i][1]]);\n            adj[edges[i][1]].push_back(vals[edges[i][0]]);\n        }\n        map<int,vector<int>> mp;\n        //Store the value of all connected nodes to each node in a vector, sort it and store it in a map.\n        for(int i=0;i<n;i++){\n            vector<int> temp;\n            for(auto it:adj[i]){\n                temp.push_back(it);\n            }\n            sort(rbegin(temp),rend(temp));\n            mp[i]=temp;\n        }\n        //Traverse each vector upto K elements and find max sum.\n        for(auto i:mp){\n            vector<int> temp = i.second;\n            int sum=vals[i.first];\n            ans=max(ans,sum);\n            for(int it=0;it<k and it<temp.size();it++){\n                ans=max(ans,sum);\n                sum+=temp[it];\n            } \n            ans=max(ans,sum);\n        }    \n            \n        return ans;\n    }\n};\n```
['Sorting', 'C++']