Video Explanation (Both DP & Greedy approach with Intuition)
Explanation\n\nClick here for the video\n\n# Code\n\ntypedef long long int ll;\n\nconst int N = 2e4+1;\n\nll dp[2][N];\nint par[N];\n\nclass Solution {\n    \n 
# Explanation\n\n[Click here for the video](https://youtu.be/BYF3vGQ49Zw)\n\n# Code\n```\ntypedef long long int ll;\n\nconst int N = 2e4+1;\n\nll dp[2][N];\nint par[N];\n\nclass Solution {\n    \n    ll K;\n    vector<vector<int>> g;\n    vector<ll> val;\n    \n    void CalculateParent(int root, int p) {\n        par[root] = p;\n        \n        for (auto i : g[root]) \n            if (i != p) CalculateParent(i, root);\n    }\n    \n    ll dfs (int src, bool taken) {\n        ll &ans = dp[taken][src];\n        if (ans != -1) return ans;\n        \n        ans = 0;\n        ll min_diff = 1e18;\n        ll best = 0, taken_in_best = 0;\n        \n        min_diff = min (min_diff, abs(val[src] - (val[src]^K)));\n        best += max(val[src], val[src]^K);\n        if (val[src] < (val[src]^K)) taken_in_best ++;\n        \n        for (auto i: g[src]) {\n            if (i == par[src]) continue;\n            \n            ll with = dfs(i, true);\n            ll without = dfs(i, false);\n            \n            min_diff = min(min_diff, abs(with-without));\n    \n            best += max(with, without);\n            if (with > without) taken_in_best ++;\n        }\n        \n        taken_in_best %= 2;\n        \n        if (taken_in_best == taken) return (ans = best);\n        return (ans = best-min_diff);\n    }\n    \npublic:\n    long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n        memset(dp, -1, sizeof(dp));\n        memset(par, -1, sizeof(par));\n        g.clear(), g.resize(nums.size());\n        val.clear();\n        \n        for (auto i : nums) val.push_back(i);\n        K = k;\n        for (auto e : edges) {\n            g[e[0]].push_back(e[1]);\n            g[e[1]].push_back(e[0]);\n        }\n        \n        CalculateParent(0, -1);\n        return dfs (0, false);\n    }\n};\n```
['C++']