Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k × k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working).


-----Input-----

The first line contains four integer numbers n, m, k, q (1 ≤ n, m ≤ 500, 1 ≤ k ≤ min(n, m), 0 ≤ q ≤ n·m) — the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.

Each of next q lines contain three integer numbers x_{i}, y_{i}, t_{i} (1 ≤ x_{i} ≤ n, 1 ≤ y_{i} ≤ m, 0 ≤ t_{ ≤ 10}^9) — coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.

We consider that pixel is already broken at moment t_{i}.


-----Output-----

Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working.


-----Examples-----
Input
2 3 2 5
2 1 8
2 2 8
1 2 1
1 3 4
2 3 2

Output
8

Input
3 3 2 5
1 2 2
2 2 1
2 3 5
3 2 10
2 1 100

Output
-1

Difficulty: HARD
Tags: Sorting

Data structures
Skill types: Sorting

Data structures

Starter code:

Solutions:
import sys
from operator import itemgetter
(n, m, k, q) = list(map(int, input().split()))
query = sorted((list(map(int, line.split())) for line in sys.stdin), key=itemgetter(2))
times = [q[2] for q in query]

def solve(ti):
	imos = [[0] * (m + 1) for _ in range(n + 1)]
	for i in range(ti):
		imos[query[i][0]][query[i][1]] += 1
	for i in range(n + 1):
		for j in range(m):
			imos[i][j + 1] += imos[i][j]
	for j in range(m + 1):
		for i in range(n):
			imos[i + 1][j] += imos[i][j]
	for i in range(k, n + 1):
		for j in range(k, m + 1):
			if imos[i][j] - imos[i - k][j] - imos[i][j - k] + imos[i - k][j - k] == k * k:
				return True
	return False
inf = len(times) + 1
(ok, ng) = (inf, 0)
while abs(ok - ng) > 1:
	mid = ok + ng >> 1
	if mid >= k * k and solve(mid):
		ok = mid
	else:
		ng = mid
print(times[ok - 1] if ok != inf else -1)


from sys import stdout
from sys import stdin

def get():
	return stdin.readline().strip()

def getf():
	return [int(i) for i in get().split()]

def put(a, end='\n'):
	stdout.write(str(a) + end)

def putf(a, sep=' ', end='\n'):
	stdout.write(sep.join(map(str, a)) + end)

def isbroken(n, m, mt, k, qu, q):
	a = [[1] * m for i in range(n)]
	for i in range(q):
		(x, y, t) = qu[i]
		if t <= mt:
			a[x - 1][y - 1] = 0
	dp = [[0] * (m + 1) for i in range(n + 1)]
	dp[1][1] = a[0][0]
	for j in range(2, m + 1):
		dp[1][j] = dp[1][j - 1] + a[0][j - 1]
	for i in range(2, n + 1):
		dp[i][1] = dp[i - 1][1] + a[i - 1][0]
	for i in range(2, n + 1):
		for j in range(2, m + 1):
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + a[i - 1][j - 1]
	for i in range(k, n + 1):
		for j in range(k, m + 1):
			sm = dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k]
			if sm == 0:
				return 1
	return 0

def main():
	(n, m, k, q) = getf()
	qu = [getf() for i in range(q)]
	(l, r) = (-1, 10 ** 9 + 1)
	while l + 1 < r:
		mt = (l + r) // 2
		if isbroken(n, m, mt, k, qu, q):
			r = mt
		else:
			l = mt
	if r != 10 ** 9 + 1:
		put(r)
	else:
		put(-1)
main()


from sys import stdout
from sys import stdin

def get():
	return stdin.readline().strip()

def getf():
	return [int(i) for i in get().split()]

def put(a, end='\n'):
	stdout.write(str(a) + end)

def putf(a, sep=' ', end='\n'):
	stdout.write(sep.join([str(i) for i in a]) + end)

def check(lmt, qr, k, n, m, q):
	d = [[0] * m for i in range(n)]
	for i in range(q):
		(ti, x, y) = qr[i]
		if ti <= lmt:
			d[x][y] = 1
	dp = [[0] * (m + 1) for i in range(n + 1)]
	dp[1][1] = d[0][0]
	for i in range(2, n + 1):
		dp[i][1] = dp[i - 1][1] + d[i - 1][0]
	for j in range(2, m + 1):
		dp[1][j] = dp[1][j - 1] + d[0][j - 1]
	for i in range(2, n + 1):
		for j in range(2, m + 1):
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + d[i - 1][j - 1]
	for i in range(k, n + 1):
		for j in range(k, m + 1):
			sm = dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k]
			if sm == k * k:
				return True

def main():
	(n, m, k, q) = getf()
	qr = []
	t0 = -1
	for i in range(q):
		(xi, yi, ti) = getf()
		t0 = max(t0, ti)
		qr.append([ti, xi - 1, yi - 1])
	(l, r) = (-1, 10 ** 9 + 7)
	while l + 1 < r:
		lmt = (l + r) // 2
		f = check(lmt, qr, k, n, m, q)
		if f == True:
			r = lmt
		else:
			l = lmt
	if q == 0:
		print(-1)
		return 0
	if r == 10 ** 9 + 7:
		put(-1)
	else:
		put(r)
main()