D. Coins and Queries

Codeforces
IDCF1003D
Time2000ms
Memory256MB
Difficulty
greedy
English · Original
Chinese · Translation
Formal · Original
Polycarp has $n$ coins, the value of the $i$\-th coin is $a_i$. It is guaranteed that all the values are integer powers of $2$ (i.e. $a_i = 2^d$ for some **non-negative** integer number $d$). Polycarp wants to know answers on $q$ queries. The $j$\-th query is described as integer number $b_j$. The answer to the query is the minimum number of coins that is necessary to obtain the value $b_j$ using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value $b_j$, the answer to the $j$\-th query is _\-1_. The queries are independent (the answer on the query doesn't affect Polycarp's coins). ## Input The first line of the input contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) — the number of coins and the number of queries. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ — values of coins ($1 \le a_i \le 2 \cdot 10^9$). It is guaranteed that all $a_i$ are integer powers of $2$ (i.e. $a_i = 2^d$ for some **non-negative** integer number $d$). The next $q$ lines contain one integer each. The $j$\-th line contains one integer $b_j$ — the value of the $j$\-th query ($1 \le b_j \le 10^9$). ## Output Print $q$ integers $ans_j$. The $j$\-th integer must be equal to the answer on the $j$\-th query. If Polycarp can't obtain the value $b_j$ the answer to the $j$\-th query is _\-1_. [samples]
Polycarp 有 $n$ 枚硬币,第 $i$ 枚硬币的面值为 $a_i$。保证所有面值均为 $2$ 的整数次幂(即 $a_i = 2^d$,其中 $d$ 为某个非负整数)。 Polycarp 需要回答 $q$ 个查询。第 $j$ 个查询由一个整数 $b_j$ 描述。该查询的答案是:使用他拥有的某些硬币子集来凑出面值 $b_j$ 所需的最少硬币数量。如果 Polycarp 无法凑出面值 $b_j$,则第 $j$ 个查询的答案为 _-1_。 各个查询相互独立(一个查询的答案不会影响 Polycarp 的硬币)。 输入的第一行包含两个整数 $n$ 和 $q$($1 lt.eq n, q lt.eq 2 dot.op 10^5$),分别表示硬币数量和查询数量。 第二行包含 $n$ 个整数 $a_1, a_2, dots.h, a_n$,表示硬币的面值($1 lt.eq a_i lt.eq 2 dot.op 10^9$)。保证所有 $a_i$ 均为 $2$ 的整数次幂(即 $a_i = 2^d$,其中 $d$ 为某个非负整数)。 接下来的 $q$ 行,每行包含一个整数。第 $j$ 行包含一个整数 $b_j$,表示第 $j$ 个查询的面值($1 lt.eq b_j lt.eq 10^9$)。 请输出 $q$ 个整数 $a n s_j$。第 $j$ 个整数必须等于第 $j$ 个查询的答案。如果 Polycarp 无法凑出面值 $b_j$,则该查询的答案为 _-1_。 ## Input 输入的第一行包含两个整数 $n$ 和 $q$($1 lt.eq n, q lt.eq 2 dot.op 10^5$),分别表示硬币数量和查询数量。第二行包含 $n$ 个整数 $a_1, a_2, dots.h, a_n$,表示硬币的面值($1 lt.eq a_i lt.eq 2 dot.op 10^9$)。保证所有 $a_i$ 均为 $2$ 的整数次幂(即 $a_i = 2^d$,其中 $d$ 为某个非负整数)。接下来的 $q$ 行,每行包含一个整数。第 $j$ 行包含一个整数 $b_j$,表示第 $j$ 个查询的面值($1 lt.eq b_j lt.eq 10^9$)。 ## Output 请输出 $q$ 个整数 $a n s_j$。第 $j$ 个整数必须等于第 $j$ 个查询的答案。如果 Polycarp 无法凑出面值 $b_j$,则该查询的答案为 _-1_。 [samples]
**Definitions** Let $ n, q \in \mathbb{Z}^+ $ denote the number of coins and queries, respectively. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of coin values, where each $ a_i = 2^{d_i} $ for some $ d_i \in \mathbb{Z}_{\ge 0} $. Let $ B = (b_1, b_2, \dots, b_q) $ be a sequence of query values, where each $ b_j \in \mathbb{Z}^+ $. **Constraints** 1. $ 1 \le n, q \le 2 \cdot 10^5 $ 2. $ 1 \le a_i \le 2 \cdot 10^9 $ and $ a_i = 2^{d_i} $ for some $ d_i \in \mathbb{Z}_{\ge 0} $ 3. $ 1 \le b_j \le 10^9 $ **Objective** For each query $ b_j $, find the minimum number of coins from $ A $ whose sum equals $ b_j $, i.e., find the smallest $ k \in \mathbb{Z}_{\ge 0} $ such that there exists a multiset $ S \subseteq A $ with $ |S| = k $ and $ \sum_{x \in S} x = b_j $. If no such subset exists, output $ -1 $.
Samples
Input #1
5 4
2 4 8 2 4
8
5
14
10
Output #1
1
-1
3
2
API Response (JSON)
{
  "problem": {
    "name": "D. Coins and Queries",
    "description": {
      "content": "Polycarp has $n$ coins, the value of the $i$\\-th coin is $a_i$. It is guaranteed that all the values are integer powers of $2$ (i.e. $a_i = 2^d$ for some **non-negative** integer number $d$). Polycar",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1003D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Polycarp has $n$ coins, the value of the $i$\\-th coin is $a_i$. It is guaranteed that all the values are integer powers of $2$ (i.e. $a_i = 2^d$ for some **non-negative** integer number $d$).\n\nPolycar...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Polycarp 有 $n$ 枚硬币,第 $i$ 枚硬币的面值为 $a_i$。保证所有面值均为 $2$ 的整数次幂(即 $a_i = 2^d$,其中 $d$ 为某个非负整数)。\n\nPolycarp 需要回答 $q$ 个查询。第 $j$ 个查询由一个整数 $b_j$ 描述。该查询的答案是:使用他拥有的某些硬币子集来凑出面值 $b_j$ 所需的最少硬币数量。如果 Polycarp 无法凑出面值 $b_...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, q \\in \\mathbb{Z}^+ $ denote the number of coins and queries, respectively.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of coin values, where each $ a_i = 2^{d_i} $ fo...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments