Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
111297 | CBCode | K 次取反后最大化的数组和 | C++ | 通过 | 100 | 0 MS | 252 KB | 636 | 2023-11-19 17:29:59 |
#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; }