| UFBA Selection Contest 2026 |
|---|
| Finished |
It is Carnaval in Salvador, and a bloco afro is about to parade down the slopes of the Pelourinho with its unmistakable samba-reggae groove.
The bloco is formed by $$$N$$$ pairs of percussionists. In the $$$i$$$-th pair, the surdo player strikes $$$A_i$$$ beats per measure, while the caixa player next to them strikes $$$B_i$$$ beats per measure. For the whole bloco to lock into a single, hypnotic groove all the way to the Largo do Pelourinho, the maestro wants every pair to produce the same total number of beats per measure.
Formally, the maestro picks a target value $$$C$$$ and wants $$$A_i + B_i = C$$$ to hold for every pair $$$i$$$. He is free to choose any $$$C$$$ he likes.
To achieve that, he can call rehearsals. In a single rehearsal he picks one drummer (either the surdo or the caixa of any pair) and retunes their pattern to play exactly $$$X$$$ beats per measure, where $$$X$$$ is any integer with $$$1 \leq X \leq 10^{9}$$$. Notice each rehearsal may use a different value of $$$X$$$.
Help the maestro find the minimum number of rehearsals needed so that all the pairwise sums become equal.
The first line contains a single integer $$$N$$$ ($$$2 \leq N \leq 10^{5}$$$), the number of pairs.
The second line contains $$$N$$$ integers $$$A_1, A_2, \ldots, A_N$$$ ($$$1 \leq A_i \leq 10^{6}$$$).
The third line contains $$$N$$$ integers $$$B_1, B_2, \ldots, B_N$$$ ($$$1 \leq B_i \leq 10^{6}$$$).
Output a single integer: the minimum number of rehearsals so that $$$A_i + B_i$$$ is the same for every pair.
31 2 35 4 3
0
52 2 5 8 33 3 2 1 9
3
Explanation for example 1
The pairwise sums are $$$1+5=6$$$, $$$2+4=6$$$ and $$$3+3=6$$$. They are already all equal, so the maestro needs no rehearsals.
Explanation for example 2
The current sums are $$$5, 5, 7, 9, 12$$$. Choosing the target $$$C = 5$$$, pairs $$$1$$$ and $$$2$$$ are already fine. Each of pairs $$$3$$$, $$$4$$$ and $$$5$$$ can be fixed with a single rehearsal (for example, retune the surdo of pair $$$3$$$ from $$$5$$$ to $$$3$$$, the surdo of pair $$$4$$$ from $$$8$$$ to $$$4$$$, and the caixa of pair $$$5$$$ from $$$9$$$ to $$$2$$$). No choice of $$$C$$$ does better than $$$3$$$ rehearsals.
| Name |
|---|


