#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 (where is the number of chosen elements).
This sequence is valid if and only if it satisfies the following conditions:
- .
- Let (in particular, ). If we arrange to into a cycle in the order , then every two adjacent numbers are opposite to each other, and their absolute values are all .
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 .
The next lines each contain two integers , meaning pigstd has copies of .
It is not guaranteed that the are distinct. If some 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 .
4 3
1 5
2 4
3 3
0 2
6
Hint
[Sample 1 Explanation]
When pigstd's permutation is or , the sum is maximized and equals .
[Constraints]
For of the testdata, , , .
This problem uses bundled testcases.
- Subtask 1 (5 points): It is guaranteed that there is no valid sequence.
- Subtask 2 (15 points): .
- Subtask 3 (5 points): .
- Subtask 4 (5 points): .
- Subtask 5 (30 points): .
- Subtask 6 (40 points): No special restrictions apply.
Translated by ChatGPT 5
京公网安备 11011102002149号