Codeforces 620D D. Professor Gukiz and Arrays

Source: Internet
Author: User

D. Professor Gukiz and Arrays

Professor Gukiz has both arrays of integers, A and B. Professor wants to make the sum of the elements in the array A s A as close as possible to the sum of the elements in the array b s C11>b. So he wants to minimize the value v = | Sa - sb|.

In one operation professor can swap some element from the Array a and some El Ement from the Array b. For example if the Array a is[5, 1, 3, 2, 4] and The Array b is [3, 3, 2] professor can Swap the Element 5 from the Array a and The Element 2 from the Array b and get The new Array a [2, 1, 3, 2, 4] and the new array  b [3, 3, 5].

Professor doesn ' t want to make more than and swaps. Find the minimal value V and some sequence of no more than-swaps that'll leads to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays A and B.

Input

The first line contains integer n (1≤ n ≤2000)-the number of elements in the array a.

The second line contains n integers ai ( -9≤ ai≤109)-the elements of the array a.

The third line contains integer m (1≤ m ≤2000)-the number of elements in the array b.

The fourth line contains m integers bJ ( -9≤ bJ≤109)-the elements of the array B.

Output

In the first line print the minimal value v = | Sa - sb| That can is got with no more than the swaps.

The second line should contain the number of swaps K (0≤ k ≤ 2).

Each of the next K lines should contain the integers xp, yp ( 1≤ xpn, 1≤ ypm)-the index of the element in the array A and the index of the element in the array b in the p-th swap.

If there is several optimal solutions print any of them. Print the swaps in order the professor did them.

Sample Test (s) input
5
5 4 3) 2 1
4
1 1 1 1
Output
1
2
1 1
4 2
Input
5
1 2 3) 4 5
1
15
Output
0
0
Input
5
1 2 3) 4 5
4
1 2 3 4
Output
1
1
3 1
#include <cstdio>#include<cstring>#include<map>#include<iostream>#include<algorithm>using namespaceStd;typedefLong LongLl;map<ll,pair<int,int> >F;inta[2][2005];ll sum[2];p Air<int,int>ans[2];intMain () {intn,m; scanf ("%d",&N);  for(intI=0; i<n;i++) scanf ("%d", &a[0][i]), sum[0]+=a[0][i]; scanf ("%d",&m);  for(intI=0; i<m;i++) scanf ("%d", &a[1][i]), sum[1]+=a[1][i]; if(sum[0]==sum[1]) {puts ("0\n0");return 0;} ll bound=abs (sum[0]-sum[1]);  for(intI=0; i<n;i++)         for(intj=0; j<m;j++) {ll TT=abs (sum[0]-2* (LL) a[0][i]+2* (LL) a[1][j]-sum[1]); if(Tt<bound) ans[0].first=i+1, ans[0].second=j+1, bound=tt; }     for(intI=0; i<n;i++)         for(intj=i+1; j<n;j++) f[2* (LL) a[0][i]+2* (LL) a[0][j]]=make_pair (i+1, j+1);  for(intI=0; i<m;i++)         for(intj=i+1; j<m;j++) {ll TT=sum[0]-sum[1]+2* (LL) a[1][i]+ (LL) (a[1][j])); Map<ll,pair<int,int> >::iterator iter=F.lower_bound (TT); if((Iter!=f.end () && (ABS (Tt-iter->first) <bound)) | | (Iter!=f.begin () && (tt-(--iter)->first) <bound)) {ans[0]=make_pair (iter->second.first,i+1); ans[1]=make_pair (iter->second.second,j+1); Bound=abs (iter->first-TT); }} printf ("%i64d\n", bound); if(bound==sum[0]-sum[1]) {puts ("0");return 0;} if(!ans[1].first) cout<<"1\n"<<ans[0].first<<" "<<ans[0].second<<Endl; Elsecout<<"2\n"<<ans[0].first<<" "<<ans[0].second<<endl<<ans[1].first<<" "<<ans[1].second<<Endl; return 0;}

Codeforces 620D D. Professor Gukiz and Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.