class Solution {
public:
int largestSumAfterKNegations(vector<int>& A, int K) {
sort(A.begin(),A.end());
for(int i = 0;i<A.size();i++)
{
if(K>0)
{
if(A[i]<0)
{
A[i] = -A[i];
K--;
}
else
{
break;
}
}
}
K = K%2;
if(K)
{
sort(A.begin(),A.end());
A[0] = -A[0];
}
int sum = 0;
for(int i = 0;i<A.size();i++)
{
sum += A[i];
}
return sum;
}
};