<p>Given an integer array <code>nums</code> and an integer <code>k</code>, modify the array in the following way:</p>

<ul>
	<li>choose an index <code>i</code> and replace <code>nums[i]</code> with <code>-nums[i]</code>.</li>
</ul>

<p>You should apply this process exactly <code>k</code> times. You may choose the same index <code>i</code> multiple times.</p>

<p>Return <em>the largest possible sum of the array after modifying it in this way</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,2,3], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> Choose index 1 and nums becomes [4,-2,3].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,-1,0,2], k = 3
<strong>Output:</strong> 6
<strong>Explanation:</strong> Choose indices (1, 2, 2) and nums becomes [3,1,0,2].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,-3,-1,5,-4], k = 2
<strong>Output:</strong> 13
<strong>Explanation:</strong> Choose indices (1, 4) and nums becomes [2,3,-1,5,4].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
</ul>

// class Solution {
// public:
//     int findsum(vector<int>& nums){
//         int sum = 0;
//         for(int i = 0; i<nums.size() ; i++){
//                 sum += nums[i];
//         }
//         return sum;
//     }
//    void solveusingrec(vector<int>& nums, int k , int& sum , int& newsum){
   

//    if(k==0){
//     sum = max(newsum , sum);
//     return;
//    }


//         for(int i = 0; i<nums.size() ; i++){
//             nums[i] = -nums[i];
//             if(newsum == INT_MIN){
//                     newsum = findsum(nums);
//             }
//             else{
//                 newsum =newsum - (-nums[i]) + nums[i];
//             }
            
//             solveusingrec(nums, k-1 , sum , newsum);

//             //backtrack
//             nums[i] = - nums[i];
//             newsum = newsum - (-nums[i]) + nums[i];
         
//         }
//         return ;
//     }

// void solveusingmem(vector<int>& nums, int k,int& sum , int& newsum,map<pair<int, int> , int>&mp){



//    if(k==0){
//     sum = max(newsum , sum);
//     return;

    
//    }
//    if(mp.find({k, newsum}) != mp.end())return ;

//         for(int i = 0; i<nums.size() ; i++){
//             nums[i] = -nums[i];
//             if(newsum == INT_MIN){
//                     newsum = findsum(nums);
//             }
//             else{
//                 newsum = newsum - (-nums[i]) + nums[i];
//             }
            
//             solveusingmem(nums, k-1 , sum , newsum , mp);

//             //backtrack
//             nums[i] = - nums[i];
//             newsum = newsum - (-nums[i]) + nums[i];
         
//         }
//         mp[{k, newsum}] = sum;
//         return;
    
//     }
//     int largestSumAfterKNegations(vector<int>& nums, int k) {
//         int sum = INT_MIN;
//         int newsum = INT_MIN;

//         map<pair<int, int> , int>mp;

//          solveusingmem(nums, k , sum, newsum,mp);
//          return sum;
       
        
//     }
// };

class Solution {
public:
    int largestSumAfterKNegations(vector<int>& nums, int k) {
        // Sort the array
        sort(nums.begin(), nums.end());
        
        // Negate the smallest elements up to k times
        for (int i = 0; i < nums.size() && k > 0; ++i) {
            if (nums[i] < 0) {
                nums[i] = -nums[i];  // Negate the negative number
                k--;  // Reduce the number of negations
            }
        }
        
        // If k is odd after the loop, negate the smallest element
        if (k % 2 == 1) {
            // Negate the smallest element in the array
            sort(nums.begin(), nums.end());  // Sort again to find the smallest
            nums[0] = -nums[0];  // Negate the smallest element
        }
        
        // Return the sum of the modified array
        return accumulate(nums.begin(), nums.end(), 0);
    }
};

12000