Space Nap

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 2.0s
Memory limit: 256M

Authors:
Problem type

Space is represented as a 3-dimensional N \times N \times N cube where each coordinate is between 1 and N, and is a dangerous place. You are riding a spaceship and want to take a nap at some positive integer coordinate (a, b, c). However, your spaceship has identified M threats across space, each at positive integer coordinates (x_i, y_i, z_i). Multiple threats may be at the same coordinate.

The safety value at a coordinate is the sum of the manhattan distances from the coordinate to each threat. Formally, the safety value at (a, b, c) is:

\displaystyle  \sum\limits_{i=1}^M |a-x_i|+|b-y_i|+|c-z_i|

You want to nap as safely as possible. Can you find the maximum safety value of a coordinate in space?

Constraints

1 \le N \le 10^9

1 \le M \le 10^5

1 \le x, y, z \le N

Subtask 1 [35%]

1 \le N, M \le 80

Subtask 2 [65%]

No additional constraints.

Input Specification

The first line of input will contain two space-separated integers N and M.

The next M lines will contain 3 integers x, y, and z.

Output Specification

Output one integer, the maximum safety value of a coordinate in space.

Sample Input 1

3 2
1 1 1
2 3 2

Sample Output 1

8

Sample Input 2

12346583 5
1 5 2345
1234 634 1234
999 999 999
12340000 12341230 12340000
12346583 9238732 12346580

Sample Output 2

114237170

Comments

There are no comments at the moment.