leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

3068.cpp (680B)


      1 class Solution {
      2   public:
      3     long long maximumValueSum(const vector<int> &nums, int k, const vector<vector<int>> &edges) const {
      4         int count = 0, mini = INT_MAX, maxi = INT_MIN;
      5         long long total = 0;
      6 
      7         for (const int val : nums) {
      8             const int after = val ^ k;
      9             const int change = after - val;
     10             total += val;
     11 
     12             if (change > 0) {
     13                 mini = min(mini, change);
     14                 total += change;
     15                 count++;
     16             } else {
     17                 maxi = max(maxi, change);
     18             }
     19         }
     20 
     21         if (count % 2 == 0) return total;
     22         return max(total - mini, total + maxi);
     23     }
     24 };