AtCoder Grand Contest 065
A
Shuffle and mod K
AtCoder
⌀ 1701
原题
←B →
翻译中…

Problem Statement

翻译中…

You are given an integer sequence $A=(A_1,A_2,\dots,A_N)$ of length $N$.

翻译中…

You can rearrange $A$ freely. Find the maximum value that $\sum_{i=1}^{N-1} ((A_{i+1} - A_i) \bmod K)$ can take after rearranging.

翻译中…

Here, $x \bmod K$ denotes the integer $y$ such that $0 \le y < K$ and $x - y$ is a multiple of $K$. For example, $-3 \bmod 8 = 5$, and $9 \bmod 6 = 3$.

翻译中…

Constraints

翻译中…
  • $2 \le N \le 2 \times 10^5$
  • $1 \le K \le 10^9$
  • $0 \le A_i < K$
翻译中…

Input

翻译中…

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

翻译中…

N K
A_1 A_2 \dots A_N

翻译中…

Output

翻译中…

Print the answer.

翻译中…

样例

Input #1
3 4
0 1 2
Output #1
6
One optimal solution is to rearrange $A$ into $(2,1,0)$ to achieve $(1 - 2) \bmod 4 + (0 - 1) \bmod 4 = 3 + 3 = 6$.
Input #2
7 123
11 34 56 0 32 100 78
Output #2
638
题目信息
题号
agc065_a
时间限制
2 s
内存限制
1024 MB

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