PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000063. Những gói xúc xích và bánh mì (Nine Packs)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 HH followed by HH pack sizes h1,…,hHh_1,\ldots,h_H. The second part contains integer BB followed by BB pack sizes b1,…,bBb_1,\ldots,b_B. Values may be separated by whitespace or line breaks. A missing category is represented by a count of 00.

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): 0≤H,B≤120\le H,B\le12 and 1≤hi,bj≤201\le h_i,b_j\le20 for each listed pack. Each pack can be purchased at most once.

  • Subtask 2 (30 points): 0≤H,B≤450\le H,B\le45 and 1≤hi,bj≤2001\le h_i,b_j\le200 for each listed pack. Each pack can be purchased at most once.

  • Subtask 3 (50 points): 0≤H,B≤1000\le H,B\le100 and 1≤hi,bj≤10001\le h_i,b_j\le1000 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 1010, and ten bun packs of size 88 or 1212. Purchase two packs of ten hotdogs and one eight-bun pack plus one twelve-bun pack. Both sides have 2020 items and the total is 2+2=42+2=4 packs. One pack of each cannot work because there is no bun pack of size 1010, and no three-pack selection produces equal totals here. The answer is 4.