0%

cf-594-div2

A: Integer Points

้ข˜็›ฎ๏ผš

DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew ๐‘› distinct lines, given by equations ๐‘ฆ=๐‘ฅ+๐‘๐‘– for some distinct ๐‘1,๐‘2,โ€ฆ,๐‘๐‘›.

Then JLS drew on the same paper sheet ๐‘š distinct lines given by equations ๐‘ฆ=โˆ’๐‘ฅ+๐‘ž๐‘– for some distinct ๐‘ž1,๐‘ž2,โ€ฆ,๐‘ž๐‘š.

DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.

Input

The first line contains one integer ๐‘ก (1โ‰ค๐‘กโ‰ค1000), the number of test cases in the input. Then follow the test case descriptions.

The first line of a test case contains an integer ๐‘› (1โ‰ค๐‘›โ‰ค10^5), the number of lines drawn by DLS.

The second line of a test case contains ๐‘› distinct integers ๐‘๐‘– (0โ‰ค๐‘๐‘–โ‰ค10^9) describing the lines drawn by DLS. The integer ๐‘๐‘– describes a line given by the equation ๐‘ฆ=๐‘ฅ+๐‘๐‘–.

The third line of a test case contains an integer ๐‘š (1โ‰ค๐‘šโ‰ค10^5), the number of lines drawn by JLS.

The fourth line of a test case contains ๐‘š distinct integers ๐‘ž๐‘– (0โ‰ค๐‘ž๐‘–โ‰ค10^9) describing the lines drawn by JLS. The integer ๐‘ž๐‘– describes a line given by the equation ๐‘ฆ=โˆ’๐‘ฅ+๐‘ž๐‘–.

The sum of the values of ๐‘› over all test cases in the input does not exceed 105. Similarly, the sum of the values of ๐‘š over all test cases in the input does not exceed 105.

In hacks it is allowed to use only one test case in the input, so ๐‘ก=1 should be satisfied.

Output

For each test case in the input print a single integer โ€” the number of line pairs with integer intersection points.

Example

input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1

output

1
2
3
3
1
0

Note

The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.

้ข˜่งฃ๏ผš

piไธŽqiๅŒไธบๅฅ‡ๆ•ฐๆˆ–่€…ๅŒไธบๅถๆ•ฐๆ—ถๆ‰ไผšๆœ‰ๆ•ดๆ•ฐ็š„ไบค็‚นใ€‚

ไปฃ็ ๏ผš

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
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
long long ans = 0;
long long sum[2][2];
sum[0][0]=0;sum[0][1]=0;
sum[1][0]=0;sum[1][1]=0;
int n;
scanf("%d", &n);
while(n--)
{
int a;
scanf("%d", &a);
if(a%2==1){
sum[0][0]++;
//printf("%d\n",sum[0][0]);
}
else
sum[0][1]++;
}
int m;
scanf("%d", &m);

while(m--)
{
int a;
scanf("%d", &a);
if(a%2==1)
sum[1][0]++;
else
sum[1][1]++;
}

ans = sum[0][0]*sum[1][0] + sum[0][1] * sum[1][1];
printf("%lld\n", ans);
}
return 0;

}

B Grow The Tree

้ข˜็›ฎ๏ผš

Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacherโ€™s Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.

The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point (0,0). While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to ๐‘‚๐‘‹ or ๐‘‚๐‘Œ axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.

Alexey wants to make a polyline in such a way that its end is as far as possible from (0,0). Please help him to grow the tree this way.

Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.

Input

The first line contains an integer ๐‘› (1โ‰ค๐‘›โ‰ค100000) โ€” the number of sticks Alexey got as a present.

The second line contains ๐‘› integers ๐‘Ž1,โ€ฆ,๐‘Ž๐‘› (1โ‰ค๐‘Ž๐‘–โ‰ค10000) โ€” the lengths of the sticks.

Output

Print one integer โ€” the square of the largest possible distance from (0,0) to the tree end.

Examples

input

1
2
3
1 2 3

output

1
26

input

1
2
4
1 1 2 2

output

1
20

Note
The following pictures show optimal trees for example tests. The squared distance in the first example equals 5โ‹…5+1โ‹…1=26, and in the second example 4โ‹…4+2โ‹…2=20.

้ข˜่งฃ๏ผš

ๅฏนๆ•ฐ็ป„ๆŽ’ๅบ๏ผŒๅ‰n/2็š„ๆ€ปๅ’ŒไธŽๅ‰ฉไฝ™็š„ๆ€ปๅ’Œๅณๆ˜ฏans

ไปฃ็ ๏ผš

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
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int a[100010];
int main()
{
int n;
scanf("%d", &n);
long long p1 = 0,sum =0;
for(int i = 0; i < n ;i++){
scanf("%d", &a[i]);
sum +=a[i];
}
sort(a, a+n);
for(int i = 0; i < n/2;i++)
{
p1 += a[i];
}
sum -= p1;
long long ans = p1*p1+sum*sum;
printf("%lld",ans);
return 0;

}

D1. The World Is Just a Programming Task (Easy Version)

้ข˜็›ฎ๏ผš

This is an easier version of the problem. In this version, ๐‘›โ‰ค500.

Vasya is an experienced developer of programming competitionsโ€™ problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.

To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.

We remind that bracket sequence ๐‘  is called correct if:

๐‘  is empty;
๐‘  is equal to โ€œ(๐‘ก)โ€, where ๐‘ก is correct bracket sequence;
๐‘  is equal to ๐‘ก1๐‘ก2, i.e. concatenation of ๐‘ก1 and ๐‘ก2, where ๐‘ก1 and ๐‘ก2 are correct bracket sequences.
For example, โ€œ(()())โ€, โ€œ()โ€ are correct, while โ€œ)(โ€œ and โ€œ())โ€ are not.

The cyclical shift of the string ๐‘  of length ๐‘› by ๐‘˜ (0โ‰ค๐‘˜<๐‘›) is a string formed by a concatenation of the last ๐‘˜ symbols of the string ๐‘  with the first ๐‘›โˆ’๐‘˜ symbols of string ๐‘ . For example, the cyclical shift of string โ€œ(())()โ€ by 2 equals โ€œ()(())โ€.

Cyclical shifts ๐‘– and ๐‘— are considered different, if ๐‘–โ‰ ๐‘—.

Input

The first line contains an integer ๐‘› (1โ‰ค๐‘›โ‰ค500), the length of the string.

The second line contains a string, consisting of exactly ๐‘› characters, where each of the characters is either โ€œ(โ€œ or โ€œ)โ€.

Output

The first line should contain a single integer โ€” the largest beauty of the string, which can be achieved by swapping some two characters.

The second line should contain integers ๐‘™ and ๐‘Ÿ (1โ‰ค๐‘™,๐‘Ÿโ‰ค๐‘›) โ€” the indices of two characters, which should be swapped in order to maximize the stringโ€™s beauty.

In case there are several possible swaps, print any of them.

Examples

input

1
2
10
()()())(()

output

1
2
5
8 7

input

1
2
3
12
)(()(()())()

output

1
2
4
5 10

input

1
2
6
)))(()

output

1
2
0
1 1

Note

In the first example, we can swap 7-th and 8-th character, obtaining a string โ€œ()()()()()โ€. The cyclical shifts by 0,2,4,6,8 of this string form a correct bracket sequence.

In the second example, after swapping 5-th and 10-th character, we obtain a string โ€œ)(())()()(()โ€. The cyclical shifts by 11,7,5,3 of this string form a correct bracket sequence.

In the third example, swap of any two brackets results in 0 cyclical shifts being correct bracket sequences.

้ข˜่งฃ๏ผš

ๆšดๅŠ›ๆฑ‚่งฃ๏ผŒๅœจไบคๆขijไน‹ๅŽ๏ผŒ่ฎก็ฎ—ๅ‰็ผ€ๅ’Œ๏ผŒ่ฎก็ฎ—ๆœ€ๅฐๅ€ผ๏ผŒๆฑ‚ๅ‡บๆœ€ๅฐๅ€ผๅ‡บ็Žฐ็š„ๆฌกๆ•ฐ๏ผŒๅณไธบๆฏ็งไบคๆขๅŽ๏ผŒๆ‰€ๅพ—ๅˆฐ็š„ๅ€ผ๏ผŒๅ–ๆœ€ๅคงๅ€ผๅณๅฏใ€‚

ไปฃ็ ๏ผš

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
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
char s[510];
char tmp[510];
int main()
{
int n;
scanf("%d", &n);
scanf("%s", s);
int now = 0;
for(int i = 0; i < n; i++){
if(s[i] == '(')
now++;
else
{
now--;
}
}
if(now != 0){
printf("0\n1 1");
return 0;
}
int x=0, y= 0, ans = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j <n;j++){
int p = 0;
strcpy(tmp,s);
swap(tmp[i],tmp[j]);
int MIN = 999;
now = 0;
for(int k = 0; k < n ;k++){
if(tmp[k] == '(')
now++;
else{
now--;
}
MIN = min(now,MIN);
}
now = 0;
for(int k = 0; k < n ;k++){
if(tmp[k] == '(')
now++;
else{
now--;
}
if(now == MIN)
p++;
}
if(p > ans){
ans = p;
x = i;
y = j;
}
}
printf("%d\n",ans);
printf("%d %d",x+1,y+1);
return 0;
}