Arrays

3.6

18 votes
Ad-Hoc, Data Structures, Heaps, Medium, Trees, 普通-難しい
Problem

Note that time limit has been increased to 3 sec

Mike has 3 arrays \(a,b,c\) of length n and a number k. He wants to find minimal value of
\(\max_{i = 1}^{n} (((a_i + t) \mod{k}) + ((b_i + t) \mod{k}) + ((c_i + t) \mod{k}))\)

over all non-negative integer values of t.

\(\textbf{Input}\)

The first line contains two numbers n and k (\(1 \le n \le 3 \times 10^5,1 \le k \le 10^8\)).

Each of the next n lines contains 3 numbers - \(a_i,b_i,c_i\) (\(0 \le a_i,b_i,c_i < k\)).

\(\textbf{Output}\)

Output the minimal value achievable.

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

For \(t = 4\) we have that \(\max(5 \; +  2 \; +  0, 1\; + 3 \; + 4, 1\; + 1\; + 3) = 8\) and this is the minimal value we chan achieve.

Editor Image

?