CF BUDDY
← Problems·

1989C · Two Movies

1400 · greedy, math

Problem: A movie company has released 22 movies. These 22 movies were watched by nn people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie.

If a person is asked to leave a review for the movie, then:

  • if that person liked the movie, they will leave a positive review, and the movie's rating will increase by 11;
  • if that person disliked the movie, they will leave a negative review, and the movie's rating will decrease by 11;
  • otherwise, they will leave a neutral review, and the movie's rating will not change.

Every person will review exactly one movie — and for every person, you can choose which movie they will review.

The company's rating is the minimum of the ratings of the two movies. Your task is to calculate the maximum possible rating of the company.

Input Format: The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1-1 \le a_i \le 1), where aia_i is equal to 1-1 if the first movie was disliked by the ii-th viewer; equal to 11 if the first movie was liked; and 00 if the attitude is neutral.

The third line contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n (1bi1-1 \le b_i \le 1), where bib_i is equal to 1-1 if the second movie was disliked by the ii-th viewer; equal to 11 if the second movie was liked; and 00 if the attitude is neutral.

Additional constraint on the input: the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, print a single integer — the maximum possible rating of the company, if for each person, choose which movie to leave a review on.

Sample Cases

Case 1

Input

4
2
-1 1
-1 -1
1
-1
-1
5
0 -1 1 0 1
-1 1 0 0 1
4
-1 -1 -1 1
-1 1 1 1

Output

0
-1
1
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback