提交时间:2023-11-19 17:29:59

运行 ID: 111297

#include <bits/stdc++.h> using namespace std; #define all(v) (v).begin(), (v).end() typedef long long ll; int main() { int n,k; cin >> n >> k; vector<int> a,neg; for(int i = 0; i < n; i++) { int x; cin >> x; if(x < 0) { neg.push_back(x); } else { a.push_back(x); } } sort(all(neg)); sort(all(a)); for(int i = 0; i < neg.size() && k; i++) { neg[i] *= -1; k--; } if(k%2 == 1) { a[0] *= -1; } cout << accumulate(all(a),0LL)+accumulate(all(neg),0LL) << '\n'; return 0; }