-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2497.cpp
More file actions
27 lines (25 loc) · 865 Bytes
/
2497.cpp
File metadata and controls
27 lines (25 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {
int n = vals.size();
vector<vector<int>> neighbors(n);
for (auto& edge : edges) {
neighbors[edge[0]].push_back(vals[edge[1]]);
neighbors[edge[1]].push_back(vals[edge[0]]);
}
for (int i = 0; i < n; ++i) {
sort(neighbors[i].begin(), neighbors[i].end(), greater<int>());
}
int maxVal = INT_MIN;
for (int i = 0; i < n; ++i) {
int currentVal = vals[i];
int n = neighbors[i].size();
for (int j = 0; j < min(n, k); ++j) {
if (neighbors[i][j] < 0) break;
currentVal += neighbors[i][j];
}
maxVal = max(maxVal, currentVal);
}
return maxVal;
}
};