Skip to content

Commit 739d919

Browse files
authored
Create 3068. Find the Maximum Sum of Node Values
1 parent 70b6e54 commit 739d919

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
private:
3+
long long dp[20001][2];
4+
long long f(int idx,int even,vector<int> &nums,int k) {
5+
// base cases
6+
if(idx >= nums.size()) return even? 0 : -1e9;
7+
if(dp[idx][even] != -1) return dp[idx][even];
8+
9+
long long take = (nums[idx]^k) + f(idx+1,even^1,nums,k);
10+
long long notTake = nums[idx] + f(idx+1,even,nums,k);
11+
12+
return dp[idx][even] = max(take,notTake);
13+
}
14+
public:
15+
long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {
16+
memset(dp,-1,sizeof(dp));
17+
return f(0,1,nums,k);
18+
}
19+
};

0 commit comments

Comments
 (0)