{"problem":{"name":"A. Pupils Redistribution","description":{"content":"In Berland each high school student is characterized by _academic performance_ — integer value between 1 and 5. In high school _0xFF_ there are two groups of pupils: the group _A_ and the group _B_. ","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":1000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF779A"},"statements":[{"statement_type":"Markdown","content":"In Berland each high school student is characterized by _academic performance_ — integer value between 1 and 5.\n\nIn high school _0xFF_ there are two groups of pupils: the group _A_ and the group _B_. Each group consists of exactly _n_ students. An academic performance of each student is known — integer value between 1 and 5.\n\nThe school director wants to redistribute students between groups so that each of the two groups has the same number of students whose academic performance is equal to 1, the same number of students whose academic performance is 2 and so on. In other words, the purpose of the school director is to change the composition of groups, so that for each value of academic performance the numbers of students in both groups are equal.\n\nTo achieve this, there is a plan to produce a series of exchanges of students between groups. During the single exchange the director selects one student from the class _A_ and one student of class _B_. After that, they both change their groups.\n\nPrint the least number of exchanges, in order to achieve the desired equal numbers of students for each academic performance.\n\n## Input\n\nThe first line of the input contains integer number _n_ (1 ≤ _n_ ≤ 100) — number of students in both groups.\n\nThe second line contains sequence of integer numbers _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 5), where _a__i_ is academic performance of the _i_\\-th student of the group _A_.\n\nThe third line contains sequence of integer numbers _b_1, _b_2, ..., _b__n_ (1 ≤ _b__i_ ≤ 5), where _b__i_ is academic performance of the _i_\\-th student of the group _B_.\n\n## Output\n\nPrint the required minimum number of exchanges or _\\-1_, if the desired distribution of students can not be obtained.\n\n[samples]","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"在 Berland，每个高中生都由一个 _学业成绩_ 表示——一个介于 #cf_span[1] 和 #cf_span[5] 之间的整数。\n\n在高中 _0xFF_ 中有两个学生群体：群体 #cf_span[A] 和群体 #cf_span[B]。每个群体恰好有 #cf_span[n] 名学生。每个学生的学业成绩已知——一个介于 #cf_span[1] 和 #cf_span[5] 之间的整数。\n\n学校校长希望重新分配两个群体之间的学生，使得每个群体中学业成绩为 #cf_span[1] 的学生数量相同，学业成绩为 #cf_span[2] 的学生数量相同，依此类推。换句话说，校长的目标是改变群体的组成，使得对于每个学业成绩值，两个群体中的学生人数相等。\n\n为实现这一目标，计划进行一系列学生交换。在一次交换中，校长从群体 #cf_span[A] 中选择一名学生，从群体 #cf_span[B] 中选择一名学生，然后他们互换群体。\n\n请输出达到期望的每个学业成绩人数相等所需的最少交换次数。\n\n输入的第一行包含一个整数 #cf_span[n]（#cf_span[1 ≤ n ≤ 100]）——两个群体中的学生人数。\n\n第二行包含一个整数序列 #cf_span[a1, a2, ..., an]（#cf_span[1 ≤ ai ≤ 5]），其中 #cf_span[ai] 是群体 #cf_span[A] 中第 #cf_span[i] 名学生的学业成绩。\n\n第三行包含一个整数序列 #cf_span[b1, b2, ..., bn]（#cf_span[1 ≤ bi ≤ 5]），其中 #cf_span[bi] 是群体 #cf_span[B] 中第 #cf_span[i] 名学生的学业成绩。\n\n请输出所需的最少交换次数，若无法实现期望的学生分布，则输出 _-1_。\n\n## Input\n\n输入的第一行包含一个整数 #cf_span[n]（#cf_span[1 ≤ n ≤ 100]）——两个群体中的学生人数。第二行包含一个整数序列 #cf_span[a1, a2, ..., an]（#cf_span[1 ≤ ai ≤ 5]），其中 #cf_span[ai] 是群体 #cf_span[A] 中第 #cf_span[i] 名学生的学业成绩。第三行包含一个整数序列 #cf_span[b1, b2, ..., bn]（#cf_span[1 ≤ bi ≤ 5]），其中 #cf_span[bi] 是群体 #cf_span[B] 中第 #cf_span[i] 名学生的学业成绩。\n\n## Output\n\n请输出所需的最少交换次数，若无法实现期望的学生分布，则输出 _-1_。\n\n[samples]","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ n \\in \\mathbb{Z} $, $ 1 \\leq n \\leq 100 $, be the number of students in each group.  \nLet $ A = (a_1, a_2, \\dots, a_n) $, $ B = (b_1, b_2, \\dots, b_n) $ be sequences of academic performances, where $ a_i, b_j \\in \\{1, 2, 3, 4, 5\\} $.  \n\nFor $ k \\in \\{1, 2, 3, 4, 5\\} $, define:  \n- $ c_A(k) = |\\{ i \\mid a_i = k \\}| $: count of students in group $ A $ with performance $ k $.  \n- $ c_B(k) = |\\{ j \\mid b_j = k \\}| $: count of students in group $ B $ with performance $ k $.  \n\nLet $ d(k) = c_A(k) - c_B(k) $ be the difference in counts for performance $ k $.  \n\n**Constraints**  \n1. $ \\sum_{k=1}^{5} c_A(k) = n $, $ \\sum_{k=1}^{5} c_B(k) = n $.  \n2. The target is to have $ c_A(k) = c_B(k) = \\frac{n}{2} $ for all $ k \\in \\{1,2,3,4,5\\} $.  \n\n**Objective**  \nFind the minimum number of exchanges (each swapping one student from $ A $ with one from $ B $) to achieve $ c_A(k) = c_B(k) $ for all $ k $.  \n\nIf $ \\sum_{k=1}^{5} |d(k)| $ is odd or $ \\sum_{k=1}^{5} d(k) \\neq 0 $, output $ -1 $.  \nOtherwise, the minimum number of exchanges is:  \n$$\n\\frac{1}{2} \\sum_{k=1}^{5} \\max(0, d(k))\n$$","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF779A","tags":["constructive algorithms","math"],"sample_group":[["4\n5 4 4 4\n5 5 4 5","1"],["6\n1 1 1 1 1 1\n5 5 5 5 5 5","3"],["1\n5\n3","\\-1"],["9\n3 2 5 5 2 3 3 3 2\n4 1 4 1 1 2 4 4 1","4"]],"created_at":"2026-03-03 11:00:39"}}