原题题目
代码实现(首刷自解)
class Solution {
public:int largestSumAfterKNegations(vector<int>& nums, int k) {
priority_queue<int,vector<int>,greater<int>> q;int sum = 0;for(const auto& num:nums) q.emplace(num);while(k){
int temp = q.top();q.pop();if(temp >= 0){
if(!(k%2)){
q.emplace(temp);break;}else {
q.emplace(-temp);break;}}else q.emplace(-temp);--k;}while(!q.empty()){
sum += q.top();q.pop();}return sum;}
};