#P15946. [JOI Final 2026] 稻草人 2 / Scarecrows 2
[JOI Final 2026] 稻草人 2 / Scarecrows 2
Problem Description
There is a vast field in JOI Village. The field is represented by the infinite -plane, where the positive direction of the -axis is East and the positive direction of the -axis is North.
The mayor of JOI Village plans to place several scarecrows in the field in order to protect it from enemies. Each scarecrow protects a certain region of the plane depending on its position and direction.
Currently, placement plans have been proposed, numbered from to . Executing plan requires a cost of . Each plan is described by three integers as follows.
- If , a scarecrow is placed at the point facing West. This scarecrow protects all points on the plane satisfying .
- If , a scarecrow is placed at the point facing East. This scarecrow protects all points on the plane satisfying .
- If , a scarecrow is placed at the point facing South. This scarecrow protects all points on the plane satisfying .
- If , a scarecrow is placed at the point facing North. This scarecrow protects all points on the plane satisfying .
The mayor wants to select and execute some of these plans so that every point on the plane is protected by at least scarecrows. Among all such choices, the total cost should be minimized. It is guaranteed that the coordinates are pairwise distinct among the plans.
Given the information of the plans, determine whether it is possible to select plans so that every point on the plane is protected by at least scarecrows. If it is possible, output the minimum possible total cost of the selected plans.
Input Format
Read the following data from the standard input.
Output Format
Output the minimum total cost required so that every point on the plane is protected by at least scarecrows. If there is no way to choose the plans so that the condition is satisfied, output .
7 1
2 45 21 96
1 5 85 70
1 36 73 78
1 28 12 80
2 15 49 21
1 45 11 96
2 63 26 19
99
7 3
2 45 21 96
1 5 85 70
1 36 73 78
1 28 12 80
2 15 49 21
1 45 11 96
2 63 26 19
-1
19 5
2 36 42 64
2 7 89 74
1 0 15 82
1 10 63 55
2 58 28 19
2 45 91 3
2 2 34 97
1 7 55 82
1 17 12 17
2 59 76 82
1 7 4 68
2 51 98 47
1 51 21 38
2 19 0 72
1 73 73 11
2 62 19 74
1 45 7 94
1 79 32 21
1 85 50 21
315
8 3
4 4 21 80
2 59 65 69
4 63 36 3
2 29 13 23
1 37 45 95
2 79 14 89
3 91 54 76
1 85 46 62
328
Hint
Sample 1
For example, suppose that plans and are executed. Then the scarecrows are placed as follows.
- In plan , one scarecrow is placed at the point facing West. The cost is .
- In plan , one scarecrow is placed at the point facing East. The cost is .
In this case, every point on the coordinate plane is protected by at least one scarecrow. For example, the point is protected by the scarecrow placed at facing West in plan . The total cost is . It is impossible to protect every point on the plane with at least one scarecrow with a smaller total cost, so the output should be .
This sample input satisfies the constraints of all subtasks.
Sample 2
This example differs from Sample Input only in the value of . Since it is impossible to protect every point on the coordinate plane with at least scarecrows, the output should be .
This sample input satisfies the constraints of subtasks .
Sample 3
This sample input satisfies the constraints of subtasks .
Sample 4
This sample input satisfies the constraints of subtasks .
Constraints
- .
- is one of , or .
- .
- .
- ≠ < .
- .
- Given values are all integers.
Subtasks
- (4 points) .
- (6 points) .
- (11 points) .
- (27 points) .
- (19 points) .
- (33 points) No additional constraints.