leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
3068.cpp (680B)
0 class Solution {
1 public:
2 long long maximumValueSum(const vector<int> &nums, int k, const vector<vector<int>> &edges) const {
3 int count = 0, mini = INT_MAX, maxi = INT_MIN;
4 long long total = 0;
6 for (const int val : nums) {
7 const int after = val ^ k;
8 const int change = after - val;
9 total += val;
11 if (change > 0) {
12 mini = min(mini, change);
13 total += change;
14 count++;
15 } else {
16 maxi = max(maxi, change);
17 }
18 }
20 if (count % 2 == 0) return total;
21 return max(total - mini, total + maxi);
22 }
23 };