File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
solution/3000-3099/3068.Find the Maximum Sum of Node Values Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ long long maximumValueSum (vector<int >& nums, int k,
4
+ vector<vector<int >>& edges) {
5
+ long long totalSum = 0 ;
6
+ int count = 0 ;
7
+ int positiveMin = INT_MAX;
8
+ int negativeMax = INT_MIN;
9
+
10
+ for (int nodeValue : nums) {
11
+ int nodeValAfterOperation = nodeValue ^ k;
12
+ totalSum += nodeValue;
13
+ int netChange = nodeValAfterOperation - nodeValue;
14
+
15
+ if (netChange > 0 ) {
16
+ positiveMin = min (positiveMin, netChange);
17
+ totalSum += netChange;
18
+ count += 1 ;
19
+ } else {
20
+ negativeMax = max (negativeMax, netChange);
21
+ }
22
+ }
23
+
24
+ if (count % 2 == 0 ) {
25
+ return totalSum;
26
+ }
27
+ return max (totalSum - positiveMin, totalSum + negativeMax);
28
+ }
29
+ };
You can’t perform that action at this time.
0 commit comments