QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 512 MB Total points: 100

#10307. 新居规划

Statistics

With the construction and development of Suzhou, more and more people are choosing to come to Suzhou to start a new life.

In a newly built residential area, there are $m$ houses arranged in a circle. The houses are numbered from $1$ to $m$. House $i$ is adjacent to house $i+1$. Specifically, house $1$ and house $m$ are also adjacent.

Xiao Z needs to arrange accommodation for $n$ people. For the $i$-th person:

  • If $i$ is not assigned accommodation, their satisfaction is $0$;
  • If $i$ is assigned accommodation, their satisfaction is $a_i$;
  • If $i$ is assigned accommodation and has a neighbor (i.e., one of their adjacent houses is also occupied), their satisfaction is additionally increased by $b_i$ (meaning the total satisfaction is $a_i+b_i$).

Xiao Z wants every person assigned accommodation to live in a different house. Now, he wants to know how to arrange the accommodation to maximize the total satisfaction of all people.

Input

The data is read from standard input.

This problem contains multiple test cases.

The first line of the input contains two integers $s$ and $t$, representing the subtask number and the number of test cases, respectively. For the sample, $s$ indicates that the sample shares the same constraints as subtask $s$.

Next, for each test case:

  • The first line contains two integers $n, m$, representing the number of people and the number of houses.
  • The next $n$ lines each contain two integers $a_i, b_i$, with meanings as described in the problem statement.

Output

Output to standard output.

For each test case, output one integer per line, representing the maximum total satisfaction.

Examples

Input

8 5
2 2
70 63
55 63
7 7
-18 10
-8 -14
-20 -3
-2 -13
22 -40
15 -34
17 -39
4 1
-42 49
24 15
-27 43
-22 2
2 2
-17 23
12 -18
4 4
3 -42
3 -16
54 -10
51 -57

Output

251
54
39
12
105

Note

Example 2: See ex_2.in and ex_2.ans in the download directory. This sample satisfies the conditions of subtask 4.

Example 3: See ex_3.in and ex_3.ans in the download directory. This sample satisfies the conditions of subtask 7.

Example 4: See ex_4.in and ex_4.ans in the download directory. This sample satisfies the conditions of subtask 8.

Subtasks

For all test cases, it is guaranteed that $1\le t \le 10, 1 \le n \le 2 \times 10^{5}, 0\le m \le 10^{9}, -10^9\le a_i,b_i\le 10^9$.

SubtaskScore$n\le$Special Property
1515None
220
315500
4$10^{3}$
525$5 \times 10^{4}$
620$2 \times 10^{5}$
75A
810B

Special Property A: $\forall 1\le i\le n, a_i\le 0$;

Special Property B: $\forall 1\le i\le n, a_i\ge 0$.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.