File tree Expand file tree Collapse file tree 1 file changed +29
-1
lines changed
solution/3000-3099/3068.Find the Maximum Sum of Node Values Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -109,7 +109,35 @@ It can be shown that 9 is the maximum achievable sum of values.
109
109
#### C++
110
110
111
111
``` cpp
112
-
112
+ class Solution {
113
+ public:
114
+ long long maximumValueSum(vector<int >& nums, int k,
115
+ vector<vector<int >>& edges) {
116
+ long long totalSum = 0;
117
+ int count = 0;
118
+ int positiveMin = INT_MAX;
119
+ int negativeMax = INT_MIN;
120
+
121
+ for (int nodeValue : nums) {
122
+ int nodeValAfterOperation = nodeValue ^ k;
123
+ totalSum += nodeValue;
124
+ int netChange = nodeValAfterOperation - nodeValue;
125
+
126
+ if (netChange > 0) {
127
+ positiveMin = min(positiveMin, netChange);
128
+ totalSum += netChange;
129
+ count += 1;
130
+ } else {
131
+ negativeMax = max(negativeMax, netChange);
132
+ }
133
+ }
134
+
135
+ if (count % 2 == 0 ) {
136
+ return totalSum;
137
+ }
138
+ return max(totalSum - positiveMin, totalSum + negativeMax);
139
+ }
140
+ };
113
141
```
114
142
115
143
#### Go
You can’t perform that action at this time.
0 commit comments