Optimized nth_element
For each node, collect values of all its neighbors. \n\n> Note that we only collect positive values.\n\nThen, for each node, we pick up to k largest values. For
For each node, collect values of all its neighbors. \n\n> Note that we only collect positive values.\n\nThen, for each node, we pick up to `k` largest values. For that, we need to sort values first.\n\nOptimization 1: use `nth_element` to have `k` largest values together. We do not those values to be perfectly sorted.\n\nOptimization 2: only do the sorting when we have more than `k` positive values.\n\n**C++**\n```cpp\nint maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {\n    vector<vector<int>> av(vals.size());\n    for (auto &e : edges) {\n        if (vals[e[1]] > 0)\n            av[e[0]].push_back(vals[e[1]]);\n        if (vals[e[0]] > 0)        \n            av[e[1]].push_back(vals[e[0]]);\n    }\n    for (int i = 0; i < vals.size(); ++i) {\n        if (av[i].size() > k)\n            nth_element(begin(av[i]), begin(av[i]) + k, end(av[i]), greater<int>());\n        vals[i] += accumulate(begin(av[i]), k < av[i].size() ? begin(av[i]) + k : end(av[i]), 0);\n    }\n    return *max_element(begin(vals), end(vals));\n} \n```
[]