-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3068.cpp
More file actions
24 lines (21 loc) · 680 Bytes
/
Copy path3068.cpp
File metadata and controls
24 lines (21 loc) · 680 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
class Solution {
public:
long long maximumValueSum(const vector<int> &nums, int k, const vector<vector<int>> &edges) const {
int count = 0, mini = INT_MAX, maxi = INT_MIN;
long long total = 0;
for (const int val : nums) {
const int after = val ^ k;
const int change = after - val;
total += val;
if (change > 0) {
mini = min(mini, change);
total += change;
count++;
} else {
maxi = max(maxi, change);
}
}
if (count % 2 == 0) return total;
return max(total - mini, total + maxi);
}
};