cf-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大小均为相同:
- suma1-sumb1-sumc1
- suma2-sumb2-sumc2
- suma3-sumb3-sumc3
对于三个大小相同的bags, 可视为三个整数(大小为n的bag清空需要操作n次, 等价于一次操作执行n个步骤)
三个大小相同的bags直接相减,取绝对值即可
|suma1+suma2+suma3-(sumb1+sumb2-sumb3)+sumc2+sumc3-sumc1|
- 绝对值里尽量大:取sumb1为最小值,取sumc1为最小值,这样加起来最大.
- 绝对值里尽量小:取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;
}