#P6785. 「EZEC-3」排列

「EZEC-3」排列

Description

pigstd has a set of numbers. He wants to choose some of them and arrange them into a sequence, denoted as x1,x2,,xpx_{1},x_{2},\cdots,x_{p} (where pp is the number of chosen elements).

This sequence is valid if and only if it satisfies the following conditions:

  • p2p \ge 2.
  • Let yi=xi+1xiy_{i} = x_{i + 1} - x_{i} (in particular, yp=x1xpy_{p}=x_{1}-x_{p}). If we arrange y1y_{1} to ypy_{p} into a cycle in the order y1,y2,,ypy_1,y_2,\cdots,y_p, then every two adjacent numbers are opposite to each other, and their absolute values are all kk.

pigstd wants to know, among all valid sequences, what the maximum possible sum of all numbers in the sequence is.

Input Format

The first line contains two integers n,kn,k.

The next nn lines each contain two integers ai,bia_{i},b_{i}, meaning pigstd has bib_{i} copies of aia_{i}.

It is not guaranteed that the aia_{i} are distinct. If some aia_{i} are the same, their counts should be added together.

Output Format

Output one integer in one line, representing the maximum sum of all numbers in a valid permutation.

If there is no valid permutation, output only NO\texttt{NO}.

4 3 
1 5
2 4
3 3
0 2
6

Hint

[Sample 1 Explanation]

When pigstd's permutation is 0,3,0,30,3,0,3 or 3,0,3,03,0,3,0, the sum is maximized and equals 66.

[Constraints]

For 100%100\% of the testdata, 1n1061 \le n \le 10^6, 0k,ai1060 \le k,a_{i} \le 10^6, 1bi1061 \le b_{i} \le 10^6.

This problem uses bundled testcases.

  • Subtask 1 (5 points): It is guaranteed that there is no valid sequence.
  • Subtask 2 (15 points): k=0k = 0.
  • Subtask 3 (5 points): n=1n = 1.
  • Subtask 4 (5 points): n=2n = 2.
  • Subtask 5 (30 points): n,k,ai,bi103n,k,a_i,b_i \le 10^3.
  • Subtask 6 (40 points): No special restrictions apply.

Translated by ChatGPT 5