-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3068.FindTheMaximumSumOfNodeValues.cpp
More file actions
38 lines (34 loc) · 1.16 KB
/
3068.FindTheMaximumSumOfNodeValues.cpp
File metadata and controls
38 lines (34 loc) · 1.16 KB
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
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Calculation variables.
long long total = 0;
int changedCount = 0, minimumChange = INT_MAX;
// Find values.
for (int i = 0; i < nums.size(); i++) {
const int value = nums[i];
const int changedValue = value ^ k;
if (changedValue > value) {
// Count as changed.
changedCount++;
// Update total.
total += changedValue;
// Update minimum change.
minimumChange = min(minimumChange, changedValue - value);
} else {
// Update total.
total += value;
// Update minimum change.
minimumChange = min(minimumChange, value - changedValue);
}
}
// Return total count (adjusted if odd).
return (changedCount % 2 == 0) ?
total :
(total - minimumChange);
}
};