id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) \n {\n if(s.length()!=t.length()) return false;\n int n=s.length();\n int m1[256]={0},m2[256]={0};\n for(int i=0;i<n;i++)\n {\n if(m1[s[i]]!=m2[t[i]]){\n return false;\n }\n m1[s[i]]=i+1;\n m2[t[i]]=i+1;\n }\n return true;\n }\n};", "memory": "8600" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n map<char, char> mpp;\n vector<int> si(200, -1);\n vector<int> ti(200, -1);\n\n int n = s.size();\n int m = t.size();\n\n if(n != m) return false;\n\n for (int i=0; i<n; i++) {\n if (si[s[i]] != ti[t[i]]) return false;\n\n si[s[i]] = i;\n ti[t[i]] = i;\n }\n\n return true;\n }\n};", "memory": "8600" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int map_s_to_t[256] = {0};\n int map_t_to_s[256] = {0};\n\n for (int i = 0; i < s.length(); i++) {\n char char_s = s[i];\n char char_t = t[i];\n\n if (map_s_to_t[char_s] == 0 && map_t_to_s[char_t] == 0) {\n map_s_to_t[char_s] = char_t;\n map_t_to_s[char_t] = char_s;\n } else if (map_s_to_t[char_s] != char_t || map_t_to_s[char_t] != char_s) {\n return false;\n }\n }\n\n return true;\n }\n};", "memory": "8700" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n if (s.size() != t.size()) return false; // Check if strings are of equal length\n\n map<char, char> translate;\n map<char, char> secondWay;\n\n for (int i = 0; i < s.size(); i++) {\n char charS = s[i];\n char charT = t[i];\n\n // Check for the mapping from s to t\n auto itS = translate.find(charS);\n if (itS == translate.end()) {\n translate[charS] = charT;\n } else {\n if (itS->second != charT) return false;\n }\n\n // Check for the reverse mapping from t to s\n auto itT = secondWay.find(charT);\n if (itT == secondWay.end()) {\n secondWay[charT] = charS;\n } else {\n if (itT->second != charS) return false;\n }\n }\n\n return true;\n}\n};", "memory": "8700" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\nprivate:\n bool ismapped(string s,string q){\n vector<pair<char,char>> mappin;\n for(int i=0;i<s.length();i++){\n int cnt =0;\n\n for(int j=0;j<mappin.size();j++){\n if(mappin[j].first == s[i]){\n cnt = 1;\n \n if(mappin[j].second != q[i]){\n return false;\n }\n }\n }\n\n if(cnt == 0){\n pair<char,char> p=make_pair(s[i],q[i]);\n mappin.push_back(p);\n }\n }\n\n return true;\n }\npublic:\n bool isIsomorphic(string s, string t) {\n if(ismapped(s,t) && ismapped(t,s)){\n return true;\n }\n\n return false;\n }\n};", "memory": "8800" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n vector<int> spos(1000, -1),tpos(1000, -1);\n for(int i=0;i<s.size();i++){\n if(spos[(int)s[i]] != tpos[(int)t[i]]){\n return false;\n }\n spos[(int)s[i]] = i;\n tpos[(int)t[i]] = i;\n }\n return true;\n }\n};", "memory": "8900" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n\nvoid mapping(string &s){\n vector<char>mp(300,'0');\n char start='a';\n for(int i=0;i<s.size();i++){\n int idx=(int)s[i];\n if(mp[idx]=='0'){\n mp[idx]=start;\n start++;\n }\n }\n string ans=\"\";\n for(int i=0;i<s.size();i++){\n int idx=(int)s[i];\n char c=mp[idx];\n ans+=c;\n }\n\n s=ans;\n return;\n}\n bool isIsomorphic(string s, string t) {\n mapping(s);\n mapping(t);\n if(s==t){\n return true;\n }\n return false;\n }\n};", "memory": "9000" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string mapping(string str, char* mapping,char& start){\n // mappedString to store \n string mappedString;\n\n for(auto ch : str){\n if(mapping[ch] == 0){\n mapping[ch] = start;\n start++;\n }\n\n // use mapping to create a new string and store in mappedString\n mappedString.push_back(mapping[ch]);\n }\n return mappedString;\n }\n\n bool isIsomorphic(string s, string t) {\n if(s.length() != t.length()) return false;\n\n // convert (map) both strings 's' and 't' \n // to this mapping and then compare if \n // same then return true thus they are\n // isomorphic else return false \n\n // Create two mapping arrays for both directions\n char mapS[256] = {0}; // Mapping from 's' to 't'\n char mapT[256] = {0}; // Mapping from 't' to 's'\n char startS = 'a', startT = 'a';\n\n // Map string 's' and 't' using separate mappings\n string mappedS = mapping(s, mapS, startS);\n string mappedT = mapping(t, mapT, startT);\n\n if(mappedS.compare(mappedT) == 0){\n // matches \n return true;\n }\n else {\n // not matches\n return false;\n }\n }\n};", "memory": "9100" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string mapping(string str){\n // mappedString to store \n string mappedString;\n\n // create mapping\n char mapping[256] = {0};\n\n // start mapping from 'a'\n char start = 'a';\n\n for(auto ch : str){\n if(mapping[ch] == 0){\n mapping[ch] = start;\n start++;\n }\n\n // use mapping to create a new string and store in mappedString\n mappedString.push_back(mapping[ch]);\n }\n return mappedString;\n }\n\n bool isIsomorphic(string s, string t) {\n // convert (map) both strings 's' and 't' \n // to this mapping and then compare if \n // same then return true thus they are\n // isomorphic else return false \n\n string mappedS;\n string mappedT;\n\n // map string 's'\n mappedS = mapping(s);\n\n // map string 't'\n mappedT = mapping(t);\n\n if(mappedS.compare(mappedT) == 0){\n // matches \n return true;\n }\n else {\n // not matches\n return false;\n }\n }\n};", "memory": "9100" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int arr1[1230] = {0};\n int arr2[1230] = {0};\n \n if(s.size() != t.size())\n return 0;\n\n for(int i=0;i<s.size();i++)\n {\n if(arr1[s[i]] != arr2[t[i]])\n return 0;\n\n arr1[s[i]] = i+5;\n arr2[t[i]] = i+5;\n }\n \n return 1;\n }\n};", "memory": "9200" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool present(unordered_map<char,char>m,char ch){\n for(auto x:m){\n if(x.second==ch){\n return true;\n }\n }\n return false;\n }\n bool isIsomorphic(string s, string t) {\n unordered_map<char,char>m;\n for(int i=0;i<s.length();i++){\n if(m.empty()){\n m.insert({s[i],t[i]});\n }\n else{\n if(m.find(s[i])!=m.end()){\n auto it=m.find(s[i]);\n if(it->second!=t[i]){\n return false;\n }\n }\n else {\n if(present(m,t[i])){\n return false;\n }\n else{\n m.insert({s[i],t[i]});\n }\n }\n }\n }\n return true;\n }\n};", "memory": "9300" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool present(unordered_map<char,char>m,char ch){\n for(auto x:m){\n if(x.second==ch){\n return true;\n }\n }\n return false;\n }\n bool isIsomorphic(string s, string t) {\n unordered_map<char,char>m;\n for(int i=0;i<s.length();i++){\n if(m.empty()){\n m.insert({s[i],t[i]});\n }\n else{\n if(m.find(s[i])!=m.end()){\n auto it=m.find(s[i]);\n if(it->second!=t[i]){\n return false;\n }\n }\n else {\n if(present(m,t[i])){\n return false;\n }\n else{\n m.insert({s[i],t[i]});\n }\n }\n }\n }\n return true;\n }\n};", "memory": "9300" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int n1= s.length();\n // int n2= t.length();\n\n map<char, char> m1, m2;\n for(int i=0;i<n1;i++){\n if(m1[s[i]] == 0 && m2[t[i]] == 0){\n m1[s[i]] = t[i];\n m2[t[i]] = s[i];\n }\n if(m1[s[i]] != t[i] && m2[t[i]] != s[i]){\n return 0;\n }\n }\n return 1;\n }\n};", "memory": "9700" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int n1= s.length();\n // int n2= t.length();\n\n map<char, char> m1, m2;\n for(int i=0;i<n1;i++){\n if(m1[s[i]] == 0 && m2[t[i]] == 0){\n m1[s[i]] = t[i];\n m2[t[i]] = s[i];\n }\n else if(m1[s[i]] != t[i] || m2[t[i]] != s[i]){\n return 0;\n }\n }\n return 1;\n }\n};", "memory": "9700" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n map <char,int>m1,m2;\n if (s.size()!= t.size()) {\n return false;\n }\n for (int i = 0; i< s.size(); i++) {\n if (m1.find(s[i])!= m1.end())\n {\n if (m1[s[i]] != t[i])\n {\n return false;\n }\n }\n if (m2.find(t[i])!= m2.end()) \n {\n if (m2[t[i]] != s[i])\n {\n return false;\n }\n }\n \n else {\n m1[s[i]] = t[i];\n m2[t[i]] = s[i];\n }\n}\n return true;\n }\n};", "memory": "9800" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n map<char,char>mp1,mp2;\n if(s.length()!=t.length())return false;\n for(int i=0;i<s.size();i++){\n char original=s[i];\n char replacement=t[i];\n if(mp1.find(original)!=mp1.end() && mp1[original]!=replacement || mp2.find(replacement)!=mp2.end() && mp2[replacement]!=original)return false;\n else{\n mp1[original]=replacement;\n mp2[replacement]=original;\n }\n }\n return true;\n }\n};", "memory": "9800" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool bCheck(std::string s, std::string t)\n {\n std::unordered_map<char, char> umap;\n\n for (int i = 0; i < s.size(); i++)\n {\n if (umap.find(s[i]) == umap.end())\n umap[s[i]] = t[i];\n else\n {\n if (umap[s[i]] != t[i])\n return false;\n }\n }\n\n return true;\n }\n\n bool isIsomorphic(string s, string t) {\n\n return bCheck(s,t) && bCheck(t,s);\n }\n};", "memory": "9900" }
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;egg&quot;, t = &quot;add&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can be made identical by:</p> <ul> <li>Mapping <code>&#39;e&#39;</code> to <code>&#39;a&#39;</code>.</li> <li>Mapping <code>&#39;g&#39;</code> to <code>&#39;d&#39;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;foo&quot;, t = &quot;bar&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>&#39;o&#39;</code> needs to be mapped to both <code>&#39;a&#39;</code> and <code>&#39;r&#39;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;paper&quot;, t = &quot;title&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>t.length == s.length</code></li> <li><code>s</code> and <code>t</code> consist of any valid ascii character.</li> </ul>
3
{ "code": "#include <unordered_map>\nclass Solution {\npublic:\n bool check(string str1, string str2){\n // Unordered map to store the\n // Hash value for each string\n unordered_map<char, int> fre;\n \n int size1 = str1.size();\n int size2 = str2.size();\n \n // Check whether size equals or not,\n // if not then isomorphism\n // can't be achieved\n if (size1 != size2)\n return false;\n \n for (int i = 0; i < size1; i++) {\n \n // Check whether current character\n // already hashed or not\n if (fre.find(str1[i]) == fre.end()) {\n \n // If not then assign\n // hash value to it\n fre[str1[i]] = str1[i] - str2[i];\n }\n \n // If already hashed then compare\n // with its current hashed value\n else if (fre[str1[i]]\n != (str1[i] - str2[i])) {\n return false;\n }\n }\n return true;\n }\n bool isIsomorphic(string s, string t) {\n \n // int n = s.size();\n // int m = t.size();\n\n // if(n!=m)\n // return false;\n\n // unordered_map<char, char> mpp;\n\n // for(int i=0; i<n; i++){\n // if(mpp.find(s[i]) == mpp.end()){\n // mpp[s[i]] = t[i];\n // }\n // if(mpp.find(s[i]) == mpp.end()){\n // return false;\n // }\n // else if(mpp[s[i]] != t[i])\n // return false;\n // }\n // return true;\n\n // Method 2 \n return check(s,t) && check(t,s);\n }\n};", "memory": "9900" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
0
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, str_k, str3; getline(cin, str) && getline(cin, str_k) && getline(cin, str3); cout << '\\n') {\n stringstream ss(str); ss.ignore();\n long long k = stoll(str_k);\n long long cnt = 0ll, res = 0ll, min = LLONG_MAX;\n for (int n; ss >> n; ss.ignore()) {\n long long x = n;\n long long y = n ^ k;\n if (x > y) {\n res += x;\n } else {\n res += y;\n ++cnt;\n }\n min = std::min(min, abs(x - y));\n }\n if (cnt & 1) res -= min;\n cout << res;\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n return 0ll;\n }\n};", "memory": "11437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
0
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, str_k, str3; getline(cin, str) && getline(cin, str_k) && getline(cin, str3); cout << '\\n') {\n stringstream ss(str); ss.ignore();\n long long k = stoll(str_k);\n long long cnt = 0ll, res = 0ll, min = LLONG_MAX;\n //long long max = LLONG_MIN;\n for (int n; ss >> n; ss.ignore()) {\n long long x = n;\n long long y = n ^ k;\n if (x > y) {\n res += x;\n //max = std::min(max, x);\n } else {\n res += y;\n ++cnt;\n }\n // get minimum value difference \n min = std::min(min, abs(x - y));\n }\n if (cnt & 1) res -= min; // either lose the smallest increase\n // or add smallest decrease\n cout << res;\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n return 0ll;\n }\n};", "memory": "11437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
0
{ "code": "static auto speedup = []() { std::ios_base::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return NULL; }();\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long total = accumulate(nums.begin(),nums.end(),0ll);\n long long min_diff = INT_MAX;\n int positive_count=0;\n long long total_diff=0, diff=0;\n\n for(auto v:nums)\n {\n diff=(v^k)-v;\n cout << v << \" \" << diff << \" v^k=\" <<(v^k) << endl;\n if(diff>0)\n {\n positive_count++;\n total_diff+=diff;\n }\n cout << \"tdiff\" << total_diff << endl;\n min_diff=min(min_diff,abs(diff));\n }\n cout << \"minDiff\" << min_diff << endl;\n if(positive_count%2==1)\n {\n total_diff-=min_diff;\n }\n cout << \"tdiff2\" << total_diff << endl;\n return total+total_diff;\n }\n};", "memory": "13312" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum = 0, cnt = 0;\n int sacrifice = INT_MAX;\n \n for (int n : nums) {\n int xorValue = n ^ k;\n if (xorValue > n) {\n sum += xorValue;\n cnt++;\n sacrifice = min(sacrifice, xorValue - n);\n } else {\n sum += n;\n sacrifice = min(sacrifice, n - xorValue);\n }\n }\n \n if (cnt % 2 != 0) {\n sum -= sacrifice;\n }\n \n return sum;\n }\n};", "memory": "15187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n \n long long int cg = 0;\n long long int mini = 1e18;\n long long int sum = 0;\n for(int i = 0;i<nums.size();i++)\n {\n sum+=max(nums[i],(nums[i]^k));\n cg += (nums[i] < (nums[i]^k))?1:0;\n mini = min(mini,(long long int)abs(nums[i] - (nums[i]^k)));\n }\n return (cg%2 == 0) ? sum : sum - mini;\n }\n};", "memory": "15187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
0
{ "code": "//greedy approach\n\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum=0, cnt=0;\n int sacrifice = INT_MAX;\n\n for(int n:nums){\n int xorValue = n^k;\n if(xorValue>n){\n sum += xorValue;\n cnt++;\n sacrifice = min(sacrifice, xorValue-n);\n }else{\n sum += n;\n sacrifice = min(sacrifice, n - xorValue);\n }\n }\n if(cnt%2 != 0){\n sum -= sacrifice;\n }\n return sum;\n }\n};", "memory": "17062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum = 0, cnt = 0;\n int sacrifice = INT_MAX;\n \n for (int n : nums) {\n int xorValue = n ^ k;\n if (xorValue > n) {\n sum += xorValue;\n cnt++;\n sacrifice = min(sacrifice, xorValue - n);\n } else {\n sum += n;\n sacrifice = min(sacrifice, n - xorValue);\n }\n }\n \n if (cnt % 2 != 0) {\n sum -= sacrifice;\n }\n \n return sum;\n }\n};", "memory": "17062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum = 0, cnt = 0, sacrifice = INT_MAX;\n for (long long n : nums) {\n sum += max(n ^ k, n);\n cnt += (n ^ k) > n;\n sacrifice = min(sacrifice, abs(n - (n ^ k)));\n }\n return sum - (cnt % 2 ? sacrifice : 0);\n }\n};", "memory": "18937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum = 0;\n int cnt=0,minDiff = INT_MAX;\n for(const auto & num : nums){\n int X = (num^k);\n if(X > num)\n {\n sum += X;\n cnt++;\n }\n else\n {\n sum += num;\n }\n minDiff = min(minDiff,abs(X-num));\n }\n if(cnt&1){\n return (sum - minDiff);\n }\n return sum;\n }\n};", "memory": "20812" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<int> diff(n);\n long long sum = 0;\n for(int i=0; i<nums.size(); i++){\n diff[i] = (nums[i]^k) - nums[i];\n sum += nums[i];\n }\n sort(diff.begin(), diff.end(), greater<int>());\n for(auto it:diff){cout << it << endl;}\n for(int i=0; i<nums.size(); i+=2){\n if(i+1 == nums.size()) break;\n int xor_sum = diff[i] + diff[i+1];\n if(xor_sum <= 0) break;\n sum += xor_sum;\n }\n return sum;\n \n }\n};", "memory": "33937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<int> diff(n);\n long long sum = 0;\n for(int i=0; i<nums.size(); i++){\n diff[i] = (nums[i]^k) - nums[i];\n sum += nums[i];\n }\n sort(diff.begin(), diff.end(), greater<int>());\n for(int i=0; i<nums.size(); i+=2){\n if(i+1 == nums.size()) break;\n int xor_sum = diff[i] + diff[i+1];\n if(xor_sum <= 0) break;\n sum += xor_sum;\n }\n return sum;\n \n }\n};", "memory": "33937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n=nums.size();\n vector<int> ans(n,0);\n for(int i=0;i<n;i++) ans[i]=nums[i]^k;\n int count=0;\n long long mini=INT_MAX;\n long long sum=0;\n for(int i=0;i<n;i++){\n if(ans[i]>nums[i]) count++;\n mini=min(mini,(long long)(abs(ans[i]-nums[i])));\n sum+=max(ans[i],nums[i]);\n }\n if(count%2==0) return sum;\n return sum-mini;\n \n \n }\n};", "memory": "35812" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\tlong long maximumValueSum(std::vector<int>& nums, int k, std::vector<std::vector<int>>& edges) {\n\t\tstd::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n int n = nums.size();\n\t\tstd::vector<int> delta(n);\n\t\tlong long sum = 0;\n\t\tfor (auto num: nums)\n\t\t{\n\t\t\tsum += num;\n\t\t}\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tdelta[i] = (nums[i] ^ k) - nums[i];\n\t\t}\n\t\tstd::stable_sort(delta.begin(), delta.end(), std::greater());\n\t\tfor (int i = 1; i < n; i += 2)\n\t\t{\n\t\t\tif(delta[i] + delta[i -1] < 0) break;\n\t\t\tsum += delta[i] + delta[i - 1];\n\t\t}\n\t\treturn sum;\n\t}\n};", "memory": "37687" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\nprivate:\n long long dp[20001][2];\n long long f(int idx,int even,vector<int> &nums,int k) {\n // base cases\n if(idx >= nums.size()) return even? 0 : -1e9;\n if(dp[idx][even] != -1) return dp[idx][even];\n\n long long take = (nums[idx]^k) + f(idx+1,even^1,nums,k); \n long long notTake = nums[idx] + f(idx+1,even,nums,k);\n\n return dp[idx][even] = max(take,notTake);\n }\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n memset(dp,-1,sizeof(dp));\n return f(0,1,nums,k);\n }\n};", "memory": "39562" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long dp[20005][2];\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n memset(dp,-1,sizeof(dp));\n return fun(0,0,nums,k);\n }\n long long fun(int idx,int ev,vector<int>& nums,int k){\n if(idx==nums.size()){\n if(ev){\n return INT_MIN;\n }\n else{\n return 0;\n }\n }\n if(dp[idx][ev]!=-1)return dp[idx][ev];\n long long np=nums[idx]+fun(idx+1,ev,nums,k);\n long long pp=(nums[idx]^k)+fun(idx+1,1^ev,nums,k);\n return dp[idx][ev]=max(np,pp);\n }\n};", "memory": "41437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long dp[20001][2];\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n if (n == 0) return 0; // Edge case: empty nums array\n\n // Initialize dp array with -1 (indicating uncomputed states)\n memset(dp, -1, sizeof(dp));\n\n // Start recursion with flag as true (indicating even), index 0\n return recurse(nums, k, 1, 0);\n }\n\n long long recurse(vector<int> &nums, int k, int flag, int index) {\n if (index >= nums.size()) return flag == 1 ? 0 : -1e9;\n\n // Return precomputed result if available\n if (dp[index][flag] != -1) return dp[index][flag];\n\n // Compute the maximum value by considering the current element with and without XOR\n long long withXor = recurse(nums, k, 1 - flag, index + 1) + (nums[index] ^ k);\n long long withoutXor = recurse(nums, k, flag, index + 1) + nums[index];\n\n // Store the result in dp table and return\n dp[index][flag] = max(withXor, withoutXor);\n return dp[index][flag];\n }\n};\n", "memory": "43312" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<int> changed;\n for (int i = 0; i < nums.size(); ++i) {\n if ((nums[i] ^ k) < nums[i]) {\n continue;\n }\n\n nums[i] ^= k;\n changed.push_back(nums[i]);\n }\n\n long long sum = accumulate(nums.begin(), nums.end(), 0LL);\n if (changed.size() % 2 == 0) {\n return sum;\n }\n\n int minUpdate = 1e9 + 7;\n for (const int& changedVal : nums) {\n int diff = changedVal - (changedVal ^ k);\n minUpdate = min(minUpdate, diff);\n }\n\n return sum - minUpdate;\n }\n};", "memory": "45187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<long long>delta(n);\n for(int i =0; i<n; i++){\n delta[i]=(nums[i]^k)-nums[i];\n }\n sort(delta.begin(), delta.end(), greater<long long>());\n if(n%2!=0)n--;\n long long sum = accumulate(nums.begin(), nums.end(), 0LL);\n for(int i =0; i<n; i+=2){\n if(delta[i]+delta[i+1]>0) sum+=delta[i]+delta[i+1];\n }\n return sum;\n }\n};", "memory": "47062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& a, int k, vector<vector<int>>& edges) {\n int n=a.size();\n\n vector<int> num;\n long long mn=INT_MIN+5;\n int zero=0;\n long long sum=0;\n\n for(int i=0;i<n;i++){\n sum+=1LL*a[i];\n int temp=(a[i]^k)-a[i];\n if(temp<=0) mn=max(mn,temp*1LL);\n else if(temp>0) num.push_back(temp);\n \n \n \n }\n sort(num.begin(),num.end());\n reverse(num.begin(),num.end());\n int size=num.size();\n\n if(size%2==1){\n int temp=num[size-1];\n num.pop_back();\n size--;\n if(temp>=abs(mn)){\n sum+=1LL*temp+mn;\n }\n\n }\n for(int i=0;i<size;i++)\n sum+=1LL*num[i];\n\n return sum;\n \n }\n};", "memory": "48937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size(), mxmn = -1e9;\n vector<int> pos;\n long long sum = 0, extra = 0; \n for(int i=0; i<n; i++) {\n sum += nums[i];\n int xk = (nums[i]^k);\n if(xk>nums[i]) {pos.push_back(xk-nums[i]); extra+=(xk-nums[i]);}\n else mxmn = max(mxmn, xk-nums[i]);\n }\n sort(pos.begin(), pos.end());\n int p = pos.size();\n if(p%2==0) return (sum+extra);\n if(pos[0]+mxmn>0) return (sum+extra+mxmn);\n return (sum+extra-pos[0]);\n }\n};", "memory": "50812" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n // void dfs(int node, int par, vector<int>& nums, int k, vector<vector<int>>& graph, vector<vector<long long>>& dp){\n // for(auto it:graph[node]){\n\n // }\n // }\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans=0;\n int n=nums.size();\n vector<long long> arr(n);\n for(int i=0;i<n;i++){\n ans+=nums[i];\n arr[i]=(nums[i]^k)-nums[i];\n }\n sort(arr.begin(),arr.end());\n int i=n-1;\n while(i>=0){\n if(i>0 && arr[i]+arr[i-1]>0){\n ans+=(arr[i]+arr[i-1]);\n i-=2;\n }\n else{\n break;\n }\n }\n return ans;\n }\n};", "memory": "52687" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<ll> netDiff(n, 0);\n ll sum = 0;\n for(int i = 0; i < n; i++){\n sum += nums[i] * 1ll;\n netDiff[i] = (nums[i] ^ k) - nums[i];\n } \n sort(netDiff.begin(), netDiff.end());\n reverse(netDiff.begin(), netDiff.end());\n for(int i = 0; i + 1 < n; i += 2){\n if(netDiff[i] + netDiff[i + 1] > 0) sum += netDiff[i] + netDiff[i + 1];\n }\n return sum;\n }\n};", "memory": "54562" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<int> delta(nums.size());\n \n for (int i = 0; i < nums.size(); ++i) {\n delta[i] = (nums[i] ^ k) - nums[i];\n }\n \n std::sort(delta.rbegin(), delta.rend());\n long long ans = 0 ;\n for(int i=0; i<nums.size(); i++) ans = ans+nums[i];\n \n for (int i = 0; i < nums.size() - 1; i += 2) {\n long long path = delta[i] + delta[i + 1];\n if (path <= 0) {\n break;\n }\n ans += path;\n }\n \n return ans;\n }\n \n};", "memory": "56437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n#define ll long long \n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n=nums.size();\n vector<int>v(n);\n for(int i=0;i<n;i++){\n v[i]=(nums[i]^k)-nums[i];\n }\n sort(v.rbegin(),v.rend());\n ll ans=accumulate(nums.begin(),nums.end(),0LL);\n for(int i=0;i<n;i+=2){\n int ct=0;\n int sum=v[i];\n if(i+1<n){sum+=v[i+1];ct=1;}\n if(sum>=0 and ct==1){\n ans+=sum;\n }else return ans;\n }\n return ans;\n \n }\n};", "memory": "58312" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n long long res = accumulate(nums.begin(), nums.end(), 0LL);\n vector<int> temp(n);\n for(int i=0;i<n;i++){\n temp[i] = (nums[i]^k)-nums[i];\n }\n sort(temp.rbegin(), temp.rend());\n for(int i=0;i<n;i+=2){\n if(i==n-1) break;\n int cursum = temp[i] + temp[i+1];\n if(cursum<=0) break;\n res += cursum;\n }\n return res;\n }\n};", "memory": "60187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector <int > d(nums.size());\n long long sum=0;\n for (int i=0;i<nums.size();i++){\n d[i]=(nums[i]^k)-nums[i];\n sum+=nums[i];\n }\n sort (d.rbegin(),d.rend());\n for (int i=0;i<nums.size();i++){\n cout<<d[i]<<\" \";\n }\n for (int i=0;i<nums.size();i+=2){\n if (i+1<nums.size()){\n if (d[i]+d[i+1]>=0)sum+=d[i]+d[i+1];\n }\n }\n return sum;\n }\n};", "memory": "62062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans = 0;\n vector<int> v;\n for(int a: nums) {\n ans += a;\n v.push_back((a ^ k) - a);\n }\n sort(v.begin(), v.end(), greater<int>());\n for(int i = 0; i + 1 < v.size(); i += 2) {\n if (v[i] + v[i + 1] <= 0) break;\n ans += (v[i] + v[i + 1]);\n }\n return ans;\n }\n};", "memory": "63937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "using LL=long long;\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n=nums.size();\n LL originSum=accumulate(nums.begin(),nums.end(),0LL);\n vector<LL> diff(n);\n for(int i=0;i<n;i++){\n diff[i]=(nums[i]^k)-nums[i];\n //cout<<diff[i]<<\" \";\n }\n sort(diff.rbegin(),diff.rend());\n for(int i=0;i<n-1;i+=2){\n LL x=diff[i]+diff[i+1];\n originSum+=max(x,0LL);\n }\n return originSum;\n }\n};", "memory": "75187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "const int maxn = 2e5 + 5;\nlong long dp[maxn][2];\nclass Solution\n{\npublic:\n long long rec_sol(int idx, vector<int> &nums, int k, int even)\n {\n int n = nums.size();\n if (idx == n)\n {\n // return even == 1 ? 0 : INT_MIN;\n return (dp[idx][even] = (even == 1 ? 0 : LONG_MIN));\n }\n\n if (dp[idx][even] != -1)\n return dp[idx][even];\n // 2 choice xor current element or not\n long long choice1 = 0, choice2 = 0;\n choice1 = (nums[idx] ^ k) + rec_sol(idx + 1, nums, k, even ^ 1);\n choice2 = nums[idx] + rec_sol(idx + 1, nums, k, even);\n\n return dp[idx][even] = max(choice1, choice2);\n }\n long long maximumValueSum(vector<int> &nums, int k, vector<vector<int>> &edges)\n {\n memset(dp, -1, sizeof(dp));\n return rec_sol(0, nums, k, 1);\n }\n};", "memory": "77062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n // MISTAKES -->\n // 1. Use sgrtl() instead of sqrt() for long long\n // 2. GCD of |x-y|?\n // 3. Huffman Coding? Merging 2 smallest elements?\n // 4. Coordinate Compression?\n // 5. Min/Max Spanning Tree.\n // 6. Topo sort ? if there is something related to relative ordering.\n // 7. Try calculating it in reverse order?\n // 8. To check if a number is a power of 2, check if n & (n-1) == 0\n // 9. Use (num & (1LL << K)) instead of (num & (1 << K)).\n // 10. Using \"LONG LONG INT\" instead of \"INT\" sometimes gives TLE.\n #define all_(v) v.begin(), v.end()\n #define sort_(a) sort(a.begin(), a.end());\n #define sort_rev(a) { sort(a.begin(), a.end()); reverse(a.begin(), a.end()); }\n #define printMatrix(v) { for(int i=0 ; i<v.size() ; i++){ for(int j=0 ; j<v[0].size() ; j++){ cout << v[i][j] << \" \"; }cout << endl; }}\n #define debug(x) cout << #x <<\" = \"; _print(x); cout << endl;\n void _print(int t) {cout << t;}\n void _print(unsigned long long x) {cout << x;}\n void _print(float x) {cout << x;}\n void _print(double t) {cout << t;}\n void _print(long double t) {cout << t;}\n void _print(char x) {cout << '\\'' << x << '\\'';}\n void _print(const char *x) {cout << '\\\"' << x << '\\\"';}\n void _print(const string &x) {cout << '\\\"' << x << '\\\"';}\n void _print(bool x) {cout << (x ? \"true\" : \"false\");}\n template <class T, class V> void _print(pair <T, V> p) {cout << \"{\"; _print(p.first); cout << \",\"; _print(p.second); cout << \"}\";}\n template <class T> void _print(vector <T> v) {cout << \"[ \"; for (T i : v) {_print(i); cout << \" \";} cout << \"]\";}\n template <class T> void _print(deque <T> v) {cout << \"[ \"; for (T i : v) {_print(i); cout << \" \";} cout << \"]\";}\n template <class T> void _print(set <T> v) {cout << \"[ \"; for (T i : v) {_print(i); cout << \" \";} cout << \"]\";}\n template <class T> void _print(multiset <T> v) {cout << \"[ \"; for (T i : v) {_print(i); cout << \" \";} cout << \"]\";}\n template <class T, class V> void _print(map <T, V> v) {cout << \"[ \"; for (auto i : v) {_print(i); cout << \" \";} cout << \"]\";}\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n // 1. XOR twice gives same results, so XOR only once if needed\n // 2. We can XOR any 2 nodes in the graph.\n // 3. We can pick 2 nodes at a time, so no ODD number of changes.\n long long sum = 0;\n vector<int> delta;\n for(auto i : nums){\n sum += i;\n delta.push_back((i^k) - i);\n }\n sort(delta.begin(), delta.end(), greater<int>());\n debug(delta);\n for(int i=0 ; i<delta.size()-1 ; i+=2){\n if(delta[i] + delta[i+1] > 0) sum += delta[i] + delta[i+1];\n else break;\n }\n return sum;\n }\n};", "memory": "78937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<int> v;\n long long sum = 0;\n for(int i = 0; i < nums.size(); i++) {\n v.push_back((nums[i] ^ k) - nums[i]);\n sum += nums[i];\n }\n sort(v.rbegin(), v.rend());\n for(int i = 0; i < v.size(); i += 2) {\n if(i == v.size() - 1) break;\n if(v[i] + v[i + 1] <= 0) break;\n sum += v[i] + v[i + 1];\n }\n return sum;\n }\n};", "memory": "80812" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& a, int k, vector<vector<int>>& g) {\n vector<int> pos, neg;\n for (int i = 0; i < a.size(); i++) {\n int x = a[i] ^ k;\n if (x > a[i]) pos.push_back(x - a[i]);\n else neg.push_back(x - a[i]);\n }\n\n sort(pos.begin(), pos.end());\n sort(neg.rbegin(), neg.rend());\n\n long long ans = accumulate(a.begin(), a.end(), 0ll);\n if (pos.size() == 0) return ans;\n if (pos.size() % 2 == 0) return ans + accumulate(pos.begin(), pos.end(), 0ll);\n if (neg.size() == 0) return ans + accumulate(pos.begin() + 1, pos.end(), 0ll);\n long long ans2 = accumulate(pos.begin() + 1, pos.end(), 0ll);\n return max(ans + ans2, ans + ans2 + pos.front() + neg.front());\n }\n};", "memory": "82687" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) \n {\n vector<int>fayda;\n\n long long sum=0;\n\n for(int i=0;i<nums.size();i++)\n {\n sum+=nums[i];\n\n fayda.push_back((nums[i]^k)-nums[i]);\n }\n \n sort(fayda.rbegin(),fayda.rend());\n \n int n=fayda.size();\n\n for(int i=0;i<n-1;i+=2)\n {\n long long pairsum=fayda[i]+fayda[i+1];\n \n if(pairsum>0)\n sum+=pairsum;\n\n }\n \n return sum;\n\n \n }\n};", "memory": "84562" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\ntypedef long long ll;\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<int>fayda;\n ll normalSum=0;\n for(int i=0;i<nums.size();i++){\n normalSum+=nums[i];\n fayda.push_back((nums[i]^k)-nums[i]);\n }\n sort(fayda.rbegin(),fayda.rend());\nint n=fayda.size();\n for(int i=0;i<n-1;i+=2\n ){\n ll pairSum=fayda[i]+fayda[i+1];\n if(pairSum>0){\n normalSum+=pairSum;\n }\n }\nreturn normalSum;\n }\n};", "memory": "86437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans=0;\n\n long long cnt=0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n if((nums[i]^k)>=nums[i]){\n cnt++;\n }\n }\n\n if(cnt%2==0){\n for(int i=0;i<n;i++){\n if((nums[i]^k)>=nums[i]){\n ans+=(nums[i]^k);\n }\n else{\n ans+=nums[i];\n }\n }\n return ans;\n }\n else{\n vector<pair<long long,int>>profits;\n vector<int>mark(n,0);\n long long minn=LONG_MAX;\n for(int i=0;i<n;i++){\n if((nums[i]^k)>=nums[i]){\n profits.push_back({(nums[i]^k)-nums[i],i});\n ans+=(nums[i]^k);\n minn=min(minn,(long long)(nums[i]^k)-nums[i]);\n }\n else{\n ans+=nums[i];\n minn=min(minn,(long long)nums[i]-(nums[i]^k));\n }\n }\n ans-=minn;\n\n return ans;\n }\n\n }\n};", "memory": "88312" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans = 0;\n int n = nums.size();\n vector<long long> v;\n for(int i=0; i<n; i++){\n cout<<(nums[i]^k)<<\" \";\n v.push_back((nums[i]^k)-nums[i]);\n ans += nums[i];\n }\n sort(v.begin(), v.end(), greater<int>());\n for(int i=0; (i+1)<v.size(); i+=2){\n if(v[i] + v[i+1] < 0) break;\n ans += v[i] + v[i+1];\n }\n return ans;\n }\n};", "memory": "90187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n=nums.size();\n vector<long long> delta;\n long long sum=0;\n for(int x:nums) sum+=x;\n for(int i=0; i<n; i++){\n delta.push_back((nums[i]^k)-nums[i]);\n }\n sort(delta.begin(), delta.end(), greater<int>());\n n=delta.size();\n for(int i=0; i<n-1; i+=2){\n if(delta[i]+delta[i+1]<=0) break;\n sum+=delta[i]+delta[i+1];\n }\n return sum;\n }\n};", "memory": "92062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<long long> delta;\n for(auto& num: nums) {\n delta.push_back((num^k) - num);\n }\n sort(delta.begin(), delta.end(), greater<int>());\n\n long long res = accumulate(nums.begin(), nums.end(), 0l);\n for(int i=0; i<n; i+=2) {\n if(i == n-1) break;\n long long path_delta = delta[i]+delta[i+1];\n if(path_delta <= 0) break;\n res+=path_delta;\n }\n return res;\n }\n};", "memory": "93937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n long long ans = accumulate(nums.begin(),nums.end(),0LL);// case: we don't change anything\n vector<long long> delta;\n for(int& num : nums) delta.push_back((num^k) - num);\n sort(delta.begin(),delta.end(),greater<long long>());\n for(long long i=0;i<n;i+=2){\n if(i==n-1) break;\n long long netDelta = delta[i] + delta[i+1];\n if(netDelta < 0) break; // already sorted, no chance in future\n ans+=netDelta;\n }\n return ans;\n }\n};", "memory": "95812" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n priority_queue<long long, vector<long long>> pq;\n long long sum = 0;\n for(int i = 0; i < n; i++){\n //cout << ( (nums[i]^k) - nums[i] ) << endl;\n pq.push((nums[i]^k) - nums[i]);\n sum += nums[i];\n }\n while(pq.size() >= 2){\n long long t1 = pq.top();\n pq.pop();\n long long t2 = pq.top();\n pq.pop();\n if(t1+t2 >=0 ){\n sum += (t1+t2);\n }\n }\n return sum;\n }\n};", "memory": "97687" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<long long> diffs;\n long long result = 0;\n for (int num: nums) {\n result += num;\n diffs.push_back((num ^ k) - num);\n }\n sort(diffs.rbegin(), diffs.rend());\n for (int i = 1; i < diffs.size(); i += 2) {\n result = max(result, result + diffs[i] + diffs[i-1]);\n }\n return result;\n }\n};", "memory": "99562" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& a, int k, vector<vector<int>>& edges) {\n int n=a.size();\n vector<pair<int,int> > diff;\n long long ans=0;\n for(int i=0;i<n;i++){\n int temp = (a[i]^k)-a[i];\n // cout << a[i] << \" \" << (a[i]^k) << \" \" << temp << \"\\n\";\n diff.push_back({temp,i});\n ans += a[i];\n }\n sort(diff.rbegin(),diff.rend());\n // for(auto it:diff){\n // cout << it.first << \" \" << it.second << \"\\n\";\n // }\n for(int i=1;i<n;i+=2){\n int idx = diff[i].second;\n int idx2 = diff[i-1].second;\n // cout << idx << \" \" << idx2 << \"\\n\";\n // cout << diff[i].first << \" \" << diff[i-1].first << '\\n';\n // cout << a[idx] << \" \" << a[idx2] << '\\n'\n \n ans += max(diff[i].first+diff[i-1].first,0);\n }\n return ans;\n }\n};\n", "memory": "101437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<long long> v;\n long long ans = 0;\n for (auto i : nums){\n v.push_back((i ^ k) - i);\n ans += i;\n }\n long long p = 0, s = ans;\n sort(v.rbegin(), v.rend());\n for (int i = 0; i < v.size()-1; i += 2){\n p += (v[i]+v[i+1]);\n ans = max(ans, s + p);\n }\n return ans;\n }\n};", "memory": "103312" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans = 0LL;\n\n vector<pair<long long, long long>> ns;\n\n int n = nums.size();\n long long diff = LLONG_MAX, before = -1LL, after;\n \n for (int i=0; i<n; i++) {\n cout << nums[i] << \" \" << (nums[i] ^ k) << endl;\n if ((nums[i] ^ k) > nums[i]) {\n ns.push_back({nums[i] ^ k, (nums[i] ^ k) - nums[i]});\n }\n else {\n ans += (long long)nums[i];\n\n if ((long long)(nums[i] - (nums[i]^k)) < diff) {\n diff = (long long)(nums[i] - (nums[i]^k));\n before = (long long)nums[i];\n after = (long long)(nums[i]^k);\n }\n }\n }\n\n sort(ns.begin(), ns.end(), [](const pair<long long, long long>& a, const pair<long long, long long>& b) {\n return a.second > b.second;\n });\n\n int i=0; \n while (i < ns.size()) {\n if (i+1 >= ns.size()) break;\n\n ans += (long long)ns[i].first + (long long)ns[i+1].first;\n \n i += 2;\n }\n int nsSize = ns.size();\n\n if (nsSize&1) {\n if (before == -1LL) {\n ans += (ns[nsSize-1].first^k);\n }\n else {\n if (diff <= (ns[nsSize-1].first - (ns[nsSize-1].first^k))) {\n ans -= before;\n ans += after;\n ans += ns[nsSize-1].first;\n }\n else {\n ans += (ns[nsSize-1].first^k);\n }\n }\n }\n for (int i=0; i<nsSize; i++) {\n cout << ns[i].first << \" \" << ns[i].second << endl;\n }\n return ans;\n }\n};\n\n// 67 68\n// 75 76\n// 11 12\n// 0 7\n// 41 46\n\n// 13 10\n// 79 72\n// 13 10\n// 94 89", "memory": "105187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "#define pii pair<int,int>\n\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& v2, int K, vector<vector<int>>& edges) \n {\n\n vector<int> v1; for(auto it:v2) v1.push_back(it^K);\n vector< pii > d; for(int i=0;i<v1.size();i++) d.push_back({ v1[i], v2[i]}); \n sort(d.begin(), d.end(),[&](pii p,pii q){\n int a = (p.first - p.second);\n int b = (q.first - q.second);\n return a > b;\n });\n\n int n = v1.size();\n long long suf = 0, pre = 0;\n for(int i=0;i<n;i++) suf += v2[i];\n long long ans = suf;\n\n for(int i=0;i<n;i++)\n {\n if(i%2 == 0)\n {\n ans = max(ans, pre + suf);\n }\n pre += d[i].first;\n suf -= d[i].second;\n }\n\n if(n%2 == 0) ans = max(ans, pre + suf);\n\n return ans;\n }\n};", "memory": "107062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\ntypedef long long int ll;\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n ll n=nums.size();\n multimap<ll,ll> mp;\n ll flag=0;\n vector<ll> vis(n,0);\n for(ll i=0;i<n;i++){\n if((nums[i]^k)>nums[i]){\n mp.insert({(nums[i]^k)-nums[i],nums[i]^k});\n vis[i]=1;\n }\n if((nums[i]^k)==nums[i]) flag=1;\n }\n ll sum=0;\n if(mp.size()%2==0||flag==1){\n for(auto it : mp) sum+=it.second;\n for(ll i=0;i<n;i++){\n if(!vis[i]) sum+=nums[i];\n }\n return sum;\n }\n else{\n ll ans=0;\n for(auto it= mp.begin();it!=mp.end();it++){\n if(it!=mp.begin()){\n ans+=(it)->second;\n }\n else{\n ans+=(it->second)^k;\n cout<<it->second<<endl;\n }\n }\n for(ll i=0;i<n;i++){\n if(!vis[i])\n ans+=nums[i];\n }\n sum=max(ans,sum);\n ans=0;\n ll mn=INT_MIN;\n ll node=-1;\n for(ll i=0;i<n;i++){\n if(!vis[i]){\n if(mn<=((nums[i]^k)-nums[i])){\n mn=max(mn,ll((nums[i]^k)-nums[i]));\n node=i;\n }\n }\n }\n for(auto it=mp.begin();it!=mp.end();it++){\n if(it!=mp.begin())\n ans+=it->second;\n }\n for(ll i=0;i<n;i++){\n if(!vis[i]&&i!=node){\n ans+=nums[i];\n }\n }\n if(node!=-1){\n ans+=nums[node]^k;\n ans+=mp.begin()->second;\n }\n else{\n ans+=mp.begin()->second^k;\n }\n return max(ans,sum);\n }\n }\n};", "memory": "120187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n\n long long maximumValueSum(vector<int>& a, int k, vector<vector<int>>& edges) {\n int n = a.size();\n vector<vector<ll>> dp(n, vector<ll> (2, -1e9));\n dp[0][0] = a[0];\n dp[0][1] = a[0] ^ k;\n for(int i = 1; i < n; i++){\n dp[i][0] = (a[i] ^ k) + dp[i - 1][1];\n dp[i][0] = max(dp[i][0], a[i] + dp[i - 1][0]);\n dp[i][1] = (a[i] ^ k) + dp[i - 1][0];\n dp[i][1] = max(dp[i][1], a[i] + dp[i - 1][1]);\n }\n\n return dp[n - 1][0];\n }\n};", "memory": "122062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n \n \n \n vector<vector<long>> dp(n+1,vector<long>(2,0));\n\n dp[n][0]=-1e9;\n dp[n][1]=0;\n for(int idx=n-1 ; idx>=0 ; idx--){\n for(int j=0 ; j<2 ; j++){\n long take=(nums[idx]^k)+dp[idx+1][j^1];\n long nottake=nums[idx]+dp[idx+1][j];\n dp[idx][j]=max(take,nottake);\n\n }\n }\n return dp[0][1];\n\n }\n};\n", "memory": "123937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n // Node: edge is unimportant - can apply on any pair of nums\n int n = nums.size();\n vector<vector<long long>> dp(n, vector<long long>(2, -1));\n dp[0][0] = nums[0];\n dp[0][1] = nums[0] ^ k;\n // 0: even, 1: odd\n for (int i = 1; i < n; ++i) {\n int num = nums[i], num_xor = nums[i] ^ k;\n dp[i][0] = max(dp[i - 1][1] + num_xor, dp[i - 1][0] + num);\n dp[i][1] = max(dp[i - 1][0] + num_xor, dp[i - 1][1] + num);\n }\n return dp[n - 1][0];\n }\n};", "memory": "125812" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "typedef long long ll;\nconst ll inf = -1e9;\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k,\n vector<vector<int>>& edges) {\n ll n = nums.size();\n vector<vector<ll>> dp(n + 1, vector<ll>(2, 0));\n dp[0][0] = nums[0];\n dp[0][1] = (nums[0] ^ k);\n\n for (int i = 1; i < n; i++) {\n dp[i][0] = nums[i] + dp[i - 1][0];\n dp[i][0] = max(dp[i][0], dp[i - 1][1] + (nums[i] ^ k));\n dp[i][1] = nums[i] + dp[i - 1][1];\n dp[i][1] = max(dp[i][1], dp[i - 1][0] + (nums[i] ^ k));\n }\n ll ans = dp[n - 1][0];\n return ans;\n }\n};", "memory": "127687" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\n typedef long long int ll;\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<pair<ll,ll>> xored(nums.size());\n for(int i=0;i<nums.size();i++){\n xored[i] = {(nums[i]^k) - nums[i],nums[i]};\n cout<<(nums[i]^k)<<\" \"<<nums[i]<<endl;\n }\n\n for(auto it:xored){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n\n vector<pair<ll,ll>> incre;vector<pair<ll,ll>> decre;\n for(int i=0;i<nums.size();i++){\n if(xored[i].first >= 0){\n incre.push_back(xored[i]);\n }\n else{\n decre.push_back(xored[i]);\n }\n }\n\n if(incre.size()%2 == 0){\n ll ans = 0;\n for(auto it:incre){\n ans += it.first+it.second;\n }\n\n for(auto it:decre){\n ans += it.second;\n }\n\n return ans;\n }\n else{\n ll ans = 0;\n sort(incre.rbegin(),incre.rend());\n for(int i=0;i<(ll)incre.size()-1;i++){\n ans += incre[i].first+incre[i].second;\n }\n \n bool mark = false;\n sort(decre.rbegin(),decre.rend());\n \n if(!incre.empty() && !decre.empty()){\n if(incre[incre.size()-1].first > abs(decre[0].first)){\n ans += incre[incre.size()-1].second+incre[incre.size()-1].first;\n mark=true;\n }\n else{\n ans += incre[incre.size()-1].second;\n }\n\n for(auto it:decre){\n ans += it.second;\n }\n if(mark){\n ans += decre[0].first;\n }\n }\n else if(incre.empty() && !decre.empty()){\n for(auto it:decre){\n ans += it.second;\n }\n }\n else if(!incre.empty() && decre.empty()){\n ans += incre[incre.size()-1].second;\n }\n\n return ans;\n\n }\n }\n};", "memory": "129562" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "#define f(i,a,n) for(int i=a;i<n;i++)\n#define ll long long\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n\n ll sum =0;\n int n = nums.size();\n multiset<int,greater<int>> m;\n\n f(i,0,n) {\n int x = nums[i] ^ k;\n m.insert(x - nums[i]);\n sum +=nums[i];\n } \n int sel = 0;\n ll prev = 0;\n for(auto p = m.begin(); p != m.end() ;++p) {\n // cout<<*p<<\" \"<<sum<<\"\\n\";\n if(sel){\n if(prev + *p <= 0)\n break;\n sum+= prev + *p;\n sel = 0;\n prev = 0;\n } else {\n sel = 1;\n prev = *p;\n }\n }\n return sum;\n }\n};", "memory": "131437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n//agar ek tree me dekhe toh hm koi bhi 2 node ka xor kr skte hai not necessarily adjacent.. \n//toh basically yeh ek array hai aur koi bhi 2 index ka xor kr skte hai.. like agar 5 elment hai jo ki xor increase kr rha hai toh \n//koi 4 hi le paege na.. aur agar 6 hai toh sab le lege toh bas count krna hai ki kitna elemnet hai jo ki xor increase kr rha hai\n \n long long cnt=0;\n multiset<long long> ms;\n long long ans=0;\n for(auto it: nums) ans+= it;\n \n for(int i=0;i<n;i++){\n\n long long ele = nums[i]^k;\n if(ele>nums[i]) {\n cnt++;\n ans+=(ele-nums[i]);\n }\n\n \n ms.insert(abs(nums[i]-ele));\n \n cout<<ele<<endl;\n }\n\n if(cnt%2==1){\n return ans - *ms.begin();\n }\n\n return ans;\n\n }\n};", "memory": "133312" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<pair<int,int>> v;\n for(int i=0;i<nums.size();i++)\n {\n v.push_back({(nums[i]^k)-nums[i],i});\n }\n sort(v.begin(),v.end());\n long long res=0;\n unordered_set<int> s;\n for(int i=v.size()-1;i>=1;i--)\n {\n if(v[i].first+v[i-1].first>0)\n {\n s.insert(v[i].second);\n s.insert(v[i-1].second);\n res+=(long long)((nums[v[i].second]^k)+(nums[v[i-1].second]^k));\n i--;\n }\n else\n {\n break;\n }\n }\n for(int i=0;i<nums.size();i++)\n {\n if(s.find(i)==s.end())\n {\n res+=(long long)nums[i];\n }\n }\n return res;\n }\n};", "memory": "135187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "//m2\n//knapsack DP, can't use tree DP.\n\n\n//instead of xor analysis, m2 is for practice.\n\n//[A] xor property: xor is both multiplication & division.\n\n//1. so operations on edges (a,b) & (b,c) only affects a & c.\n//2. since it's connectd (tree), we can have a path connecting any 2 nodes --> operation on any 2 nodes.\n\n\n//[B] knapsack dp(node, parity of operations)\n\n//1. can't use a tree DP, 'cuz there are too many combinations of subtree parity.\n\n//O(n)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n int n;\n int k;\n const long long notyet = (-1);\n const long long impossible = LLONG_MIN;\n vector<vector<long long>> memo;\n\n long long knapsack(const vector<int>& nums, int i, bool parity){\n if(i==n) return (parity==false)? 0:impossible;\n\n if(memo[i][parity] != notyet) return memo[i][parity];\n\n //choose or skip\n return memo[i][parity] = max(\n nums[i] + knapsack(nums, i+1, parity),\n (nums[i]^k) + knapsack(nums, i+1, !parity)\n );\n }\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n this->n = nums.size();\n this->k = k;\n\n memo.resize(n, vector<long long>(2, notyet));\n return knapsack(nums, 0, 0);\n }\n};", "memory": "137062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "//m2\n//knapsack DP, can't use tree DP.\n\n\n//instead of xor analysis, m2 is for practice.\n\n//[A] xor property: xor is both multiplication & division.\n\n//1. so operations on edges (a,b) & (b,c) only affects a & c.\n//2. since it's connectd (tree), we can have a path connecting any 2 nodes --> operation on any 2 nodes.\n\n\n//[B] knapsack dp(node, parity of operations)\n\n//1. can't use a tree DP, 'cuz there are too many combinations of subtree parity.\n\n//O(n)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n int n;\n int k;\n const long long notyet = (-1);\n const long long impossible = LLONG_MIN;\n vector<vector<long long>> memo;\n\n long long knapsack(const vector<int>& nums, int i, bool parity){\n if(i==n) return (parity==false)? 0:impossible;\n\n if(memo[i][parity] != notyet) return memo[i][parity];\n\n //choose or skip\n return memo[i][parity] = max(\n nums[i] + knapsack(nums, i+1, parity),\n (nums[i]^k) + knapsack(nums, i+1, !parity)\n );\n }\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n this->n = nums.size();\n this->k = k;\n\n memo.resize(n, vector<long long>(2, notyet));\n return knapsack(nums, 0, 0);\n }\n};", "memory": "138937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "//m2\n//knapsack DP, can't use tree DP.\n\n\n//instead of xor analysis, m2 is for practice.\n\n//[A] xor property: xor is both multiplication & division.\n\n//1. so operations on edges (a,b) & (b,c) only affects a & c.\n//2. since it's connectd (tree), we can have a path connecting any 2 nodes --> operation on any 2 nodes.\n\n\n//[B] knapsack dp(node, parity of operations)\n\n//1. can't use a tree DP, 'cuz there are too many combinations of subtree parity.\n\n//O(n)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n int n;\n int k;\n const long long notyet = (-1);\n const long long impossible = LLONG_MIN;\n vector<vector<long long>> memo;\n\n long long knapsack(const vector<int>& nums, int i, bool parity){\n if(i==n) return (parity==false)? 0:impossible;\n\n if(memo[i][parity] != notyet) return memo[i][parity];\n\n //choose or skip\n return memo[i][parity] = max(\n nums[i] + knapsack(nums, i+1, parity),\n (nums[i]^k) + knapsack(nums, i+1, !parity)\n );\n }\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n this->n = nums.size();\n this->k = k;\n\n memo.resize(n, vector<long long>(2, notyet));\n return knapsack(nums, 0, 0);\n }\n};", "memory": "138937" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "//m2\n//knapsack DP, can't use tree DP.\n\n\n//instead of xor analysis, m2 is for practice.\n\n//[A] xor property: xor is both multiplication & division.\n\n//1. so operations on edges (a,b) & (b,c) only affects a & c.\n//2. since it's connectd (tree), we can have a path connecting any 2 nodes --> operation on any 2 nodes.\n\n\n//[B] knapsack dp(node, parity of operations)\n\n//1. can't use a tree DP, 'cuz there are too many combinations of subtree parity.\n\n//O(n)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n int n;\n int k;\n const long long notyet = (-1);\n const long long impossible = LLONG_MIN;\n vector<vector<long long>> memo;\n\n long long knapsack(const vector<int>& nums, int i, bool parity){\n if(i==n) return (parity==false)? 0:impossible;\n\n if(memo[i][parity] != notyet) return memo[i][parity];\n\n //choose or skip\n return memo[i][parity] = max(\n nums[i] + knapsack(nums, i+1, parity),\n (nums[i]^k) + knapsack(nums, i+1, !parity)\n );\n }\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n this->n = nums.size();\n this->k = k;\n\n memo.resize(n, vector<long long>(2, notyet));\n return knapsack(nums, 0, 0);\n }\n};", "memory": "140812" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\n\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k,\n\n vector<vector<int>>& edges) {\n\n vector<vector<long long>> memo(nums.size(), vector<long long>(2, -1));\n\n return maxSumOfNodes(0, 1, nums, k, memo);\n\n }\n\nprivate:\n\n long long maxSumOfNodes(int index, int isEven, vector<int>& nums, int k,\n\n vector<vector<long long>>& memo) {\n\n if (index == nums.size()) {\n\n // If the operation is performed on an odd number of elements return\n\n // INT_MIN\n\n return isEven == 1 ? 0 : INT_MIN;\n\n }\n\n if (memo[index][isEven] != -1) {\n\n return memo[index][isEven];\n\n }\n\n // No operation performed on the element\n\n long long noXorDone =\n\n nums[index] + maxSumOfNodes(index + 1, isEven, nums, k, memo);\n\n // XOR operation is performed on the element\n\n long long xorDone = (nums[index] ^ k) +\n\n maxSumOfNodes(index + 1, isEven ^ 1, nums, k, memo);\n\n // Memoize and return the result\n\n return memo[index][isEven] = max(xorDone, noXorDone);\n\n }\n\n};", "memory": "142687" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k,\n vector<vector<int>>& edges) {\n vector<vector<long long>> memo(nums.size(), vector<long long>(2, -1));\n return maxSumOfNodes(0, 1, nums, k, memo);\n }\n\nprivate:\n long long maxSumOfNodes(int index, int isEven, vector<int>& nums, int k,\n vector<vector<long long>>& memo) {\n if (index == nums.size()) {\n // If the operation is performed on an odd number of elements return\n // INT_MIN\n return isEven == 1 ? 0 : INT_MIN;\n }\n if (memo[index][isEven] != -1) {\n return memo[index][isEven];\n }\n // No operation performed on the element\n long long noXorDone =\n nums[index] + maxSumOfNodes(index + 1, isEven, nums, k, memo);\n // XOR operation is performed on the element\n long long xorDone = (nums[index] ^ k) +\n maxSumOfNodes(index + 1, isEven ^ 1, nums, k, memo);\n\n // Memoize and return the result\n return memo[index][isEven] = max(xorDone, noXorDone);\n }\n};\n\n\n\n", "memory": "144562" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\n\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k,\n\n vector<vector<int>>& edges) {\n\n vector<vector<long long>> memo(nums.size(), vector<long long>(2, -1));\n\n return maxSumOfNodes(0, 1, nums, k, memo);\n\n }\n\nprivate:\n\n long long maxSumOfNodes(int index, int isEven, vector<int>& nums, int k,\n\n vector<vector<long long>>& memo) {\n\n if (index == nums.size()) {\n\n // If the operation is performed on an odd number of elements return\n\n // INT_MIN\n\n return isEven == 1 ? 0 : INT_MIN;\n\n }\n\n if (memo[index][isEven] != -1) {\n\n return memo[index][isEven];\n\n }\n\n // No operation performed on the element\n\n long long noXorDone =\n\n nums[index] + maxSumOfNodes(index + 1, isEven, nums, k, memo);\n\n // XOR operation is performed on the element\n\n long long xorDone = (nums[index] ^ k) +\n\n maxSumOfNodes(index + 1, isEven ^ 1, nums, k, memo);\n\n // Memoize and return the result\n\n return memo[index][isEven] = max(xorDone, noXorDone);\n\n }\n\n};\n \n\n", "memory": "144562" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k,\n vector<vector<int>>& edges) {\n vector<vector<long long>> memo(nums.size(), vector<long long>(2, -1));\n return maxSumOfNodes(0, 1, nums, k, memo);\n }\n\nprivate:\n long long maxSumOfNodes(int index, int isEven, vector<int>& nums, int k,\n vector<vector<long long>>& memo) {\n if (index == nums.size()) {\n // If the operation is performed on an odd number of elements return\n // INT_MIN\n return isEven == 1 ? 0 : INT_MIN;\n }\n if (memo[index][isEven] != -1) {\n return memo[index][isEven];\n }\n // No operation performed on the element\n long long noXorDone =\n nums[index] + maxSumOfNodes(index + 1, isEven, nums, k, memo);\n // XOR operation is performed on the element\n long long xorDone = (nums[index] ^ k) +\n maxSumOfNodes(index + 1, isEven ^ 1, nums, k, memo);\n\n // Memoize and return the result\n return memo[index][isEven] = max(xorDone, noXorDone);\n }\n};", "memory": "146437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long util(vector<int> &nums, int k,vector<vector<long long>>& dp, int isEven, int index)\n {\n if(index == nums.size())\n {\n if(isEven)return 0;\n else return INT_MIN;\n }\n \n if(dp[index][isEven] != -1)return dp[index][isEven];\n\n long long noXorDone = nums[index] + util(nums, k, dp, isEven, index+1);\n\n long long xorDone = (nums[index] ^ k) + util(nums, k, dp, isEven^1, index+1);\n\n return dp[index][isEven] = max(xorDone, noXorDone);\n }\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) \n {\n int n = nums.size();\n vector<vector<long long>> dp(n,vector<long long>(2,-1));\n return util(nums,k,dp,1,0);\n }\n};", "memory": "146437" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\n struct FindSumsResult {\n long long resultIfFlipped;\n long long resultIfNotFlipped;\n };\n\n // Returns the maximum possible sum, and whether the root needs to be flipped to get there\n FindSumsResult findSums(const vector<int>& nums, int k, vector<vector<int>>& nodes, int node) const {\n bool bestToSwap = false;\n long long value = nums[node];\n long long valueSwapped = value ^ k;\n long long minLost = max(value, valueSwapped) - min(value, valueSwapped);\n long long bestResult = value;\n if (valueSwapped > value) {\n bestResult = valueSwapped;\n bestToSwap = true;\n }\n\n for (int child : nodes[node]) {\n auto parent_ref = find(nodes[child].begin(), nodes[child].end(), node);\n nodes[child].erase(parent_ref);\n FindSumsResult result = findSums(nums, k, nodes, child);\n if (result.resultIfFlipped > result.resultIfNotFlipped) {\n bestToSwap = !bestToSwap;\n bestResult += result.resultIfFlipped;\n minLost = min(minLost, result.resultIfFlipped - result.resultIfNotFlipped);\n } else {\n bestResult += result.resultIfNotFlipped;\n minLost = min(minLost, result.resultIfNotFlipped - result.resultIfFlipped);\n }\n }\n FindSumsResult result;\n if (bestToSwap) {\n result.resultIfFlipped = bestResult;\n result.resultIfNotFlipped = bestResult - minLost;\n } else {\n result.resultIfNotFlipped = bestResult;\n result.resultIfFlipped = bestResult - minLost;\n }\n return result;\n }\npublic:\n long long maximumValueSum(const vector<int>& nums, int k, const vector<vector<int>>& edges) const {\n vector<vector<int>> nodes(nums.size());\n for (const vector<int>& edge : edges) {\n nodes[edge[0]].push_back(edge[1]);\n nodes[edge[1]].push_back(edge[0]);\n }\n return findSums(nums, k, nodes, 0).resultIfNotFlipped;\n }\n};", "memory": "148312" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<vector<int>> adjacencies(nums.size(),vector<int>());\n for(vector<int>& edge:edges){\n adjacencies[edge[0]].push_back(edge[1]);\n adjacencies[edge[1]].push_back(edge[0]);\n }\n return max_sub(0,-1,adjacencies,nums,k).first;\n }\n\n pair<long long, long long> max_sub(int node, int parent, vector<vector<int>>& edges, vector<int>& nums, int& k){\n pair<long long, long long> out = {nums[node],nums[node]^k};\n long long temp;\n for(int& adj:edges[node]){\n if(adj==parent){\n continue;\n }\n pair<long long, long long> sub_out = max_sub(adj,node,edges,nums,k);\n temp = out.first;\n out.first = max(out.first+sub_out.first,out.second+sub_out.second);\n out.second = max(temp+sub_out.second,out.second+sub_out.first);\n }\n temp = out.first;\n // out.first = max(out.first+nums[node],out.second+(nums[node]^k));\n // out.second = max(temp+(nums[node]^k),out.second+nums[node]);\n // cout<<node<<\": \"<<out.first<<\", \"<<out.second<<endl;\n return out;\n }\n};", "memory": "150187" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<vector<int>> adjacencies(nums.size(),vector<int>());\n for(vector<int>& edge:edges){\n adjacencies[edge[0]].push_back(edge[1]);\n adjacencies[edge[1]].push_back(edge[0]);\n }\n return max_sub(0,-1,adjacencies,nums,k).first;\n }\n\n pair<long long, long long> max_sub(int node, int parent, vector<vector<int>>& edges, vector<int>& nums, int& k){\n pair<long long, long long> out = {nums[node],nums[node]^k};\n long long temp;\n for(int& adj:edges[node]){\n if(adj==parent){\n continue;\n }\n pair<long long, long long> sub_out = max_sub(adj,node,edges,nums,k);\n temp = out.first;\n out.first = max(out.first+sub_out.first,out.second+sub_out.second);\n out.second = max(temp+sub_out.second,out.second+sub_out.first);\n }\n temp = out.first;\n // out.first = max(out.first+nums[node],out.second+(nums[node]^k));\n // out.second = max(temp+(nums[node]^k),out.second+nums[node]);\n // cout<<node<<\": \"<<out.first<<\", \"<<out.second<<endl;\n return out;\n }\n};", "memory": "152062" }
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p> <p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p> <ul> <li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows: <ul> <li><code>nums[u] = nums[u] XOR k</code></li> <li><code>nums[v] = nums[v] XOR k</code></li> </ul> </li> </ul> <p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] <strong>Output:</strong> 6 <strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -&gt; [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /> <pre> <strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -&gt; [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /> <pre> <strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] <strong>Output:</strong> 42 <strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k,\n vector<vector<int>>& edges) {\n vector<vector<int>> diff;\n for (auto x : nums) {\n // cout<<x<<\" \"<<x^k<<endl;\n diff.push_back({(x ^ k) - x, x});\n }\n sort(diff.begin(), diff.end());\n int n = nums.size();\n long long ans = 0;\n int cnt = 0;\n int idx = -1;\n for (int i = n - 1; i >= 0; i--) {\n if (diff[i][0] >= 0) {\n cnt++;\n ans += (diff[i][1] ^ k);\n } else {\n if (idx == -1) {\n idx = i;\n }\n ans += diff[i][1];\n }\n }\n // cout<<idx<<endl;\n if (cnt % 2 == 0)\n return ans;\n else {\n if (idx != -1) {\n // cout<<idx<<diff[idx + 1][1]<<endl;\n return max(ans - (diff[idx + 1][1] ^ k) + diff[idx + 1][1],\n ans - diff[idx][1] + (diff[idx][1] ^ k));\n }\n return ans - (diff[idx + 1][1] ^ k) + diff[idx + 1][1];\n }\n return 0;\n }\n};", "memory": "152062" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool static comp(int a, int b){\n if(a>b) return 1;\n return 0;\n }\n long long maximumHappinessSum(vector<int>& a, int k) {\n long long ans=0;\n sort(a.begin(), a.end(), comp);\n int turn=0;\n for(int i=0;i<a.size();i++){\n if(a[i]-turn>0){\n ans+=a[i]-turn;\n turn++;\n } else{\n break;\n }\n if(turn==k) break;\n } \n return ans;\n }\n};", "memory": "106500" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n std::ranges::sort(happiness, std::greater{});\n\n auto fold_op = [time = 0](long long lhs, int rhs) mutable {\n return lhs + std::max(rhs - time++, 0);\n };\n\n return std::accumulate(happiness.begin(), happiness.begin() + k, 0ll,\n fold_op);\n }\n};", "memory": "106600" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n std::ranges::sort(happiness, std::greater<int>());\n int decrement = 0;\n\n long long res = 0;\n for (int h : happiness) {\n if (h - decrement <= 0 || k == 0) {\n break;\n }\n res += h - decrement;\n ++decrement;\n --k;\n }\n return res;\n }\n};", "memory": "106700" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n ranges::sort(happiness);\n int size = happiness.size();\n int j=0;\n long long result = 0;\n for (int i=size-1;i>=0 && j<k;i--,j++) {\n if (happiness[i] - j >=0) {\n result+= happiness[i] - j;\n }\n }\n return result;\n }\n};", "memory": "106800" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\n public:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n long ans = 0;\n int decremented = 0;\n\n ranges::sort(happiness, greater<>());\n\n for (int i = 0; i < k; ++i) {\n ans += max(0, happiness[i] - decremented);\n ++decremented;\n }\n\n return ans;\n }\n};", "memory": "106900" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& v, int k) {\n sort(v.begin(),v.end());\n int n=v.size();\n long long int happy=v[n-1];\n long long int r=1;\n for(int i=n-2;i>=n-k;i--){\n if(v[i]-r>=0)\n v[i]=v[i]-r;\n else\n v[i]=0;\n happy+=v[i];\n r++;\n }\n return happy;\n }\n};", "memory": "107500" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int> &vec, int k) {\n int n = vec.size();\n sort(vec.begin(), vec.end());\n\n long long ans = 0;\n int cur = 0;\n for(int i=n-1; i>=0; i--) {\n if(vec[i] - cur >= 0) ans += (vec[i] - cur);\n cur++;\n if(cur == k) break;\n }\n return ans;\n }\n};", "memory": "107600" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(happiness.begin(),happiness.end(),greater<int>());\n long long sum=0;\n for(int i=0;i<k;i++){\n sum+=max(happiness[i]-i,0);\n }\n return sum;\n }\n};", "memory": "107700" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(happiness.begin(),happiness.end(),greater<int>());\n long long sum=0;\n for(int i=0;i<k;i++){\n if(happiness[i]-i>=0){\n sum+=happiness[i]-i; \n }\n }\n return sum;\n }\n};", "memory": "107800" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(begin(happiness), end(happiness));\n long long turn = 0;\n long long r = 0;\n while (\n turn < k &&\n 0 < happiness[happiness.size() - 1 - turn] - turn\n ) {\n r += happiness[happiness.size() - 1 - turn] - turn;\n ++turn;\n }\n return r;\n }\n};\n", "memory": "107800" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(happiness.begin(),happiness.end(),greater<int>());\n int n=happiness.size(),i=1;\n int dec=0;\n long long ans=0;\n ans=ans+happiness[0];\n dec++;\n while(i<n&&k>1){\n if(happiness[i]-dec<=0)break;\n ans=ans+happiness[i]-dec;\n i++;\n dec++;\n k--;\n }\n return ans;\n\n\n }\n};", "memory": "107900" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
1
{ "code": "#define ll long long int\nclass Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n ll ans=0, decrease=0;\n //sort in descending order;\n sort(happiness.begin(), happiness.end(), greater<int>());\n //need to make only k selections;\n for(int i=0;i<k;i++){\n ans+=max(0ll, happiness[i]-decrease);\n decrease++;\n }\n return ans;\n }\n};", "memory": "107900" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n sort(happiness.begin(), happiness.end(), greater<int>());\n long long ans=0;\n int i=0;\n for(int j=0;j<k;j++){\n ans+=max(happiness[i]-i,0);\n i++;\n }\n return ans;\n }\n};", "memory": "108000" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n int geq_k_count = 0, n = happiness.size();\n for (int x : happiness) {\n geq_k_count += x >= k;\n }\n\n if (geq_k_count >= k) {\n auto start = happiness.begin();\n std::nth_element(start, start + n - k, happiness.end());\n long long ans = 0;\n for (int i = 0; i < k; ++i) {\n ans += happiness[n - i - 1] - i;\n }\n return ans;\n }\n\n long long ans = 0, picked = 0;\n std::vector<int> count(k);\n for (int x : happiness) {\n if (x < k) {\n ++count[x];\n } else {\n ans += x - picked++;\n }\n }\n\n for (int i = k - 1; i >= picked; --i) {\n while (i >= picked && count[i]--) {\n ans += i - picked++;\n }\n }\n return ans;\n }\n};", "memory": "108100" }
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p> <p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p> <p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> happiness = [1,2,3], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> happiness = [1,1,1,1], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> happiness = [2,3,4,5], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == happiness.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= happiness[i] &lt;= 10<sup>8</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(std::vector<int>& happiness, int k) {\n std::sort(happiness.begin(), happiness.end(), std::greater<int>());\n\n std::vector<bool> selected(happiness.size(), false);\n\n long long ans = 0;\n int count = 0;\n\n for (int i = 0; i < happiness.size(); ++i) {\n if (!selected[i]) {\n ans += std::max(0, happiness[i] - count);\n ++count;\n selected[i] = true;\n\n if (count >= k) {\n break;\n }\n }\n }\n\n return ans;\n }\n};", "memory": "108200" }