estie プログラミングコンテスト2023 (AtCoder Regular Contest 169)
D
Add to Make a Permutation
AtCoder
⌀ 2812
原题
← CE →
翻译中…

Problem Statement

翻译中…

You are given an integer sequence $A=(A_1,A_2,\cdots,A_N)$ of length $N$. Each element of $A$ is an integer between $0$ and $N-1$, inclusive.

翻译中…

You can perform the following operation zero or more times:

翻译中…
  • Choose exactly $M$ elements from $A$. Then, increase the value of each chosen element by $1$. Now, if some elements have the values of $N$, change those values to $0$.
翻译中…

Your goal is to make $A$ a permutation of $(0,1,\cdots,N-1)$. Determine if the goal is achievable. If it is, find the minimum number of operations required.

翻译中…

Constraints

翻译中…
  • $2 \leq N \leq 250000$
  • $1 \leq M \leq N-1$
  • $0 \leq A_i \leq N-1$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N M
A_1 A_2 \cdots A_N

翻译中…

Output

翻译中…

If the goal is unachievable, print $-1$. Otherwise, print the minimum number of operations required.

翻译中…

样例

Input #1
3 2
0 1 1
Output #1
2
You can operate as follows to achieve the goal in two operations: - Initial state: $A=(0,1,1)$. - First operation: Choose $A_1$ and $A_2$, making $A=(1,2,1)$. - Second operation: Choose $A_2$ and $A_3$, making $A=(1,0,2)$. You cannot achieve the goal in fewer than two operations, so the answer is $2$.
Input #2
5 2
0 4 2 3 1
Output #2
0
Input #3
4 2
0 0 1 2
Output #3
-1
Input #4
20 15
5 14 18 0 8 5 0 10 6 5 11 2 10 10 17 9 8 14 4 4
Output #4
10
题目信息
题号
arc169_d
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。