原题链接:https://www.luogu.com.cn/problem/solution/P8683
贪心 + 排序
有点抽象,不能够直接加减,得要加上abs
加上abs的原因如下:
样例:
0 2
1 2 3
贪心会给 3-2-1=0,正解是 3-(1-2)=4。
#include <bits/stdc++.h>
#define ll long long
const ll N = 1e6;
ll op[N];
void solve(){
ll n,m;
std::cin >> n >> m;
std::vector<ll> op(n + 1 + m);
for(ll i = 1; i <= n + 1 + m; i ++){
std::cin >> op[i];
}
std::sort(op.begin() + 1,op.end() + 1,std::greater<ll>());
if(m == 0){
ll res = 0;
for(ll i = 1; i <= n + m + 1; i ++){
res += op[i];
}
std::cout << res << "\n";
}
else{
ll res = 0;
res += op[1];
res -= op[n + m + 1];
for(ll i = 2; i < n + m + 1; i ++) res += std::abs(op[i]);
std::cout << res << "\n";
}
// std::cout << res << "\n";
return ;
}
int main(){
ll t = 1;
while(t --)
solve();
return 0;
}