C++ | Sorting | Easy Explanation ✅
\nstatic bool comp(pair<int, int> &a, pair<int, int> &b) {\n\treturn a.second > b.second;\n}\nint maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int 
```\nstatic bool comp(pair<int, int> &a, pair<int, int> &b) {\n\treturn a.second > b.second;\n}\nint maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {\n\tint n = vals.size();\n\t\n\t// Create adjacency list\n\tvector<vector<pair<int, int>>> adj(n);\n\tfor(int i=0; i<edges.size(); i++) {\n\t\tadj[edges[i][0]].push_back({edges[i][1], vals[edges[i][1]]});\n\t\tadj[edges[i][1]].push_back({edges[i][0], vals[edges[i][0]]});\n\t}\n\t\n\t// Sort the adjacency list for each node\n\tint maxi = -1e9;\n\tfor(int i=0; i<n; i++) {\n\t\tmaxi = max(maxi, vals[i]);\n\t\tsort(adj[i].begin(), adj[i].end(), comp);\n\t}\n\t\n\t// get maximum star sum\n\tfor(int i=0; i<n; i++) {\n\t\tint sum = vals[i];\n\t\tint size = adj[i].size();\n\t\tfor(int j = 0; j<min(k, size); j++) {\n\t\t\tsum += adj[i][j].second;\n\t\t\tif(sum > maxi) {\n\t\t\t\tmaxi = sum;\n\t\t\t}\n\t\t\tif(sum < 0) {\n\t\t\t\tsum = 0;\n\t\t\t}\n\t\t}\n\t}\n\treturn maxi;\n}\n```
['Graph', 'Sorting']