solution.cpp 1.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
ll a[200010];

int main()
{
    int n, m, sum;
    cin >> n >> m;
    sum = n + m + 1;
    int neg = 0; //负数个数
    for (int i = 1; i <= sum; i++)
    {
        cin >> a[i];
        if (a[i] < 0)
            neg++;
    }
    sort(a + 1, a + 1 + sum);
    ll ans = 0;
    if (m == 0)
    { //若没有负号
        for (int i = 1; i <= sum; i++)
            ans += a[i];
    }
    else
    {
        if (neg == 0)
        { //若没有负数
            ans -= a[1];
            for (int i = 2; i <= sum; i++)
                ans += abs(a[i]);
        }
        else
        {
            if (neg == sum)
            { //若全为负数
                for (int i = 1; i < sum; i++)
                    ans += abs(a[i]);
                ans += a[sum];
            }
            else
            {
                for (int i = 1; i <= sum; i++)
                    ans += abs(a[i]);
            }
        }
    }
    cout << ans;
    return 0;
}