E. Slabstones Rearrangement
time limit per test
4.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output
Example
Input
2
4 2
2 6 4 12
8 4 16 8
7 10 11 16
18 4 20 18
6 4
2 5 4 11
2 14 6 17
7 10 10 16
9 4 16 7
11 11 16 14
18 4 20 18
Output
28
0