問題一覧 > 通常問題

No.1309 Test

レベル : / AI推定難易度 : 2750 / 実行時間制限 : 1ケース 4.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 5
作問者 : QCFium / テスター : beet
お気に入りにしたユーザー ProblemId : 4212 / 自分の提出
問題文最終更新日: 2020-12-06 00:02:31
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2020 (順位表) の他の問題:

Problem Statement

$N(N$ is odd$)$ people took a test, and each of them obtained an integer score between $0$ and $P$ points, inclusive.
You have learned about the test results that the median of the $N$ scores is $X$ points and the mode is $Y$ points. If there exist $N$ scores consistent with this information, find the maximum possible sum of such $N$ scores. If none exist, report that fact.

The median is the value at the $\frac{N + 1}{2}$-th position from the front (1-indexed) when the $N$ scores are sorted in ascending order.
The mode is the score obtained by the largest number of people. However, in this problem, if there are multiple such scores, their average is regarded as the mode.

Each test file contains exactly $10$ cases. Solve all of them.

Notes

The execution time limit for this problem is $4$ seconds.
The writer's solutions got AC with C++14 : 920 ms, Java11 : 1703 ms, PyPy3 : 3994 ms.

Input

$N_1\ P_1\ X_1\ Y_1$
$N_2\ P_2\ X_2\ Y_2$
$N_3\ P_3\ X_3\ Y_3$
$\hspace{25pt} \vdots$
$N_{10}\ P_{10}\ X_{10}\ Y_{10}$

All inputs are integers.
For $1 \le i \le 10$ :
$1 \le N_i \lt 1000$
$N_i$ is odd
$1 \le P_i \le 10^9$
$0 \le X_i \le P_i$
$0 \le Y_i \le P_i$

Output

Output $10$ lines. On the $i$-th line, output the maximum possible sum of $N$ scores consistent with the information when $N = N_i, P = P_i, X = X_i, Y = Y_i$.
However, if no test result is consistent with the information, output -1 on that line.

Sample

Sample 1
Input
5 100 80 80
5 100 70 80
5 100 100 0
7 100 7 53
17 100 69 92
11 100 27 55
1 100 50 50
101 100 71 68
999 1000000000 500000000 500000001
999 1000000000 500000001 500000000
Output
440
400
-1
325
1379
650
50
8518
747999877246
749000000000

Line 1 : For example, when the $N$ scores are {$80, 80, 80, 100, 100$}, both the median and the mode are $80$ points, so the conditions are satisfied, and the sum is $440$, which is the maximum.
Line 2 :
One example is {$66, 68, 70, 97, 99$}.
Note that values appearing only $1$ time are also treated as modes if no value appears $2$ or more times.
Also, in this case all values have the maximum number of occurrences, so their average, $80$, is regarded as the mode.
Line 3 : If it is impossible, output -1.
Line 4 : For example, {$6, 6, 6, 7, 100, 100, 100$} has the maximum sum among those satisfying the conditions.
Line 5 : For example, {$67, 67, 67, 68, 68, 68, 69, 69, 69, 92, 92, 92, 92, 99, 100, 100, 100$} .
Line 6 : {$25, 25, 26, 26, 27, 27, 98, 98, 99, 99, 100$}
Line 7 : {$50$}

Sample

Sample 2
Input
3 100 0 0
3 100 30 31
3 100 30 53
3 100 30 54
3 100 50 50
3 100 100 100
17 8 1 3
19 19 8 11
19 17 2 7
19 9 2 9
Output
100
93
159
-1
200
300
59
241
148
99

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。