#abc282e. E - Choose Two and Eat One

E - Choose Two and Eat One

Score : 500500 points

问题描述

一个箱子内装有 NN 个球,每个球上都写有一个从 11M1M-1 的整数。对于 i=1,2,,Ni = 1, 2, \ldots, N,第 ii 个球上所写的整数是 AiA_i

只要箱子里剩下两个或更多的球,Takahashi 将会重复以下过程:

  • 首先,任意选择两个球。
  • 然后,计算这两个球上所写整数 xxyy 满足的表达式 xy+yxx^y + y^xMM 取余后的结果作为得分。
  • 最后,任意选择其中一个球吃掉,并将另一个球放回箱子。

请输出 Takahashi 能够得到的最大可能总分。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

A box contains NN balls, each with an integer between 11 and M1M-1 written on it. For i=1,2,,Ni = 1, 2, \ldots, N, the integer written on the ii-th ball is AiA_i.

While the box has two or more balls remaining, Takahashi will repeat the following.

  • First, choose two balls arbitrarily.
  • Then, get a score equal to the remainder when xy+yxx^y + y^x is divided by MM, where xx and yy are the integers written on the two balls.
  • Finally, choose one of the two balls arbitrarily, eat it, and return the other to the box.

Print the maximum possible total score Takahashi will get.

Constraints

  • 2N5002 \leq N \leq 500
  • 2M1092 \leq M \leq 10^9
  • 1AiM11 \leq A_i \leq M-1
  • All values in the input are integers.

Input

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

NN MM

A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.

Sample Input 1

4 10
4 2 3 2

Sample Output 1

20

Consider the following scenario. Below, XmodYX \bmod Y denotes the remainder when a non-negative integer XX is divided by a positive integer YY.

  1. Take the first and third balls from the box to score (43+34)mod10=5(4^3 + 3^4) \bmod 10 = 5 points. Then, eat the first ball and return the third to the box. Now, the box has the second, third, and fourth balls.
  2. Take the third and fourth balls from the box to score (32+23)mod10=7(3^2 + 2^3) \bmod 10 = 7 points. Then, eat the third ball and return the fourth to the box. Now, the box has the second and fourth balls.
  3. Take the second and fourth balls from the box to score (22+22)mod10=8(2^2 + 2^2) \bmod 10 = 8 points. Then, eat the second ball and return the fourth to the box. Now, the box has just the fourth ball.

Here, Takahashi scores a total of 5+7+8=205 + 7 + 8 = 20 points, which is the maximum possible value.

Sample Input 2

20 100
29 31 68 20 83 66 23 84 69 96 41 61 83 37 52 71 18 55 40 8

Sample Output 2

1733

update @ 2024/3/10 11:50:27