1. 程式人生 > 實用技巧 >cf-Three Bags

cf-Three Bags

C. Three Bags

You are given three bags. Each bag contains a non-empty multiset of numbers. You can perform a number of operations on these bags. In one operation, you can choose any two non-empty bags, and choose one number from each of the bags. Let's say that you choose number a from the first bag and number b from the second bag. Then, you remove b from the second bag and replace a with a−b in the first bag. Note that if there are multiple occurrences of these numbers, then you shall only remove/replace exactly one occurrence.

You have to perform these operations in such a way that you have exactly one number remaining in exactly one of the bags (the other two bags being empty). It can be shown that you can always apply these operations to receive such a configuration in the end. Among all these configurations, find the one which has the maximum number left in the end.

Input

The first line of the input contains three space-separated integers n1, n2 and n3(1≤n1, n2 ,n3≤3⋅105 , 1≤n1+ n2 +n3≤3⋅105) — the number of numbers in the three bags.

The i-th of the next three lines contain ni space-separated integers ai,1, ai,2, ..., ai,ni(1≤ai,j≤109) — the numbers in the i-th bag.

Output

Print a single integer — the maximum number which you can achieve in the end.

Examples

input

2 4 1
1 2
6 3 4 5
5

output

20

input

3 2 2
7 5 4
2 9
7 1

output

29

Note

In the first example input, let us perform the following operations:

[1,2],[6,3,4,5],[5][1,2],[6,3,4,5],[5]

[−5,2],[3,4,5],[5][−5,2],[3,4,5],[5] (Applying an operation to (1,6)(1,6))

[−10,2],[3,4],[5][−10,2],[3,4],[5] (Applying an operation to (−5,5)(−5,5))

[2],[3,4],[15][2],[3,4],[15] (Applying an operation to (5,−10)(5,−10))

[−1],[4],[15][−1],[4],[15] (Applying an operation to (2,3)(2,3))

[−5],[],[15][−5],[],[15] (Applying an operation to (−1,4)(−1,4))

[],[],[20][],[],[20] (Applying an operation to (15,−5)(15,−5))

You can verify that you cannot achieve a bigger number. Hence, the answer is 2020.

首先, 對於三個bags的操作等價於先將三個bags減為大小相同的bags, 再繼續操作(加減的先後次序不改變最終的值)

把每個bag三個bags: a1,a2,a3和b1,b2,b3和c1,c2,c3.

其中a1,b1,c1大小相同且非空

他們的和分別為suma1,suma2,suma3和sumb1,sumb2,sumb3和sumc1,sumc2,sumc3.

若干次操作,使得三個bags大小均為相同:

  1. suma1-sumb1-sumc1
  2. suma2-sumb2-sumc2
  3. suma3-sumb3-sumc3

對於三個大小相同的bags, 可視為三個整數(大小為n的bag清空需要操作n次, 等價於一次操作執行n個步驟)

三個大小相同的bags直接相減,取絕對值即可

|suma1+suma2+suma3-(sumb1+sumb2-sumb3)+sumc2+sumc3-sumc1|

  1. 絕對值裡儘量大:取sumb1為最小值,取sumc1為最小值,這樣加起來最大.
  2. 絕對值裡儘量小:取sumb3為空,取sumc2+sumc3為空,這樣加起來最小.

對於1. abc輪換即可
對於2. 值就是abs(asum-bsum-csum),不需要輪換.

/*
bags最大值沒用上
*/
#include<bits/stdc++.h>
using namespace std;
char _buf[1<<20],*_=_buf,*__=_buf;
#define gc() (_==__&&(__=(_=_buf)+fread(_buf,1,1<<20,stdin),_==__)?EOF:*_++)
#define TT template<class T>inline bool
TT read(T &x){
    x=0;char c=gc();bool f=0;
    while(c<48||c>57){if(c==EOF)return 0;f^=(c=='-'),c=gc();}
    while(47<c&&c<58)x=(x<<3)+(x<<1)+(c^48),c=gc();
    if(f)x=-x;return 1;
}
TT read(T&a,T&b){return read(a)&&read(b);}
TT read(T&a,T&b,T&c){return read(a)&&read(b)&&read(c);}
typedef long long ll;
ll a,b,c;
ll amx=-1,amn=1e9+7,asum;
ll bmx=-1,bmn=1e9+7,bsum;
ll cmx=-1,cmn=1e9+7,csum;
int main(){
    read(a,b,c);
    for(ll i=0,x;i<a;++i){
        read(x);
        asum+=x;
        amx=max(amx,x);
        amn=min(amn,x);
    }
    for(ll i=0,x;i<b;++i){
        read(x);
        bsum+=x;
        bmx=max(bmx,x);
        bmn=min(bmn,x);
    }
    for(ll i=0,x;i<c;++i){
        read(x);
        csum+=x;
        cmx=max(cmx,x);
        cmn=min(cmn,x);
    }
    ll ans1=abs(asum-(2*bmn-bsum)+(csum-2*cmn));
    ll ans2=abs(asum-(2*cmn-csum)+(bsum-2*bmn));
    ll ans3=abs(asum-bsum-csum);

    ll ans4=abs(bsum-(2*amn-asum)+(csum-2*cmn));
    ll ans5=abs(bsum-(2*cmn-csum)+(asum-2*amn));
    ll ans6=abs(bsum-asum-csum);

    ll ans7=abs(csum-(2*bmn-bsum)+(asum-2*amn));
    ll ans8=abs(csum-(2*amn-asum)+(bsum-2*bmn));
    ll ans9=abs(csum-bsum-asum);

    ll ans=max(ans1,max(ans2,ans3));
    ans=max(ans,ans4);
    ans=max(ans,ans5);
    ans=max(ans,ans6);
    ans=max(ans,ans7);
    ans=max(ans,ans8);
    ans=max(ans,ans9);
    cout<<ans;
    return 0;
}