#QHD0000063. Những gói xúc xích và bánh mì (Nine Packs)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Những gói xúc xích và bánh mì (Nine Packs)
Nine Packs
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
You are preparing a party and want exactly one bun for every hotdog. Therefore, the total number of hotdogs and buns purchased must be equal. Unfortunately, the shop sells them only in packs of fixed sizes; no individual items can be purchased.
You are given the available packs of hotdogs and the available packs of buns. Every listed pack is an individual item and can be purchased at most once. Two packs may have the same number of contents and are still different purchasable packs.
You may leave any pack unpurchased, but you must buy at least one hotdog pack and at least one bun pack; selecting no packs at all is not a solution. Find the smallest total number of packs needed to obtain equal positive quantities of hotdogs and buns. If no selection is possible, print impossible.
Input
The first part contains integer followed by pack sizes . The second part contains integer followed by pack sizes . Values may be separated by whitespace or line breaks. A missing category is represented by a count of .
Output
Output the minimum positive total number of packs purchased from both categories, or exactly impossible in lowercase when no positive equal total can be formed.
Subtasks
-
Subtask 1 (20 points): and for each listed pack. Each pack can be purchased at most once.
-
Subtask 2 (30 points): and for each listed pack. Each pack can be purchased at most once.
-
Subtask 3 (50 points): and for each listed pack. Each pack can be purchased at most once.
Examples
Input
4 10 10 10 10
10 8 8 8 12 12 12 8 8 12 12
Output
4
Explanation
There are four hotdog packs of size , and ten bun packs of size or . Purchase two packs of ten hotdogs and one eight-bun pack plus one twelve-bun pack. Both sides have items and the total is packs. One pack of each cannot work because there is no bun pack of size , and no three-pack selection produces equal totals here. The answer is 4.