class Solution {
public:
int largestSumAfterKNegations(vector<int>& nums, int k) {
sort(nums.begin(), nums.end(), [](int a, int b) -> bool {
return abs(a) > abs(b);
});
for (int& num : nums) {
if (num < 0 && k > 0) {
num *= -1;
k--;
}
}
if (k % 2) nums[nums.size() - 1] *= -1;
return accumulate(nums.begin(), nums.end(), 0);
}
};