import math template times*(n:int,body:untyped): untyped = (for _ in 0.." .} var field: array[1001, array[1001, int]] var damage: array[1002, array[1002, int]] var N,K : int32 scanf("%d %d", addr N, addr K) N.times: var x,y,h: int32 scanf("%d %d %d", addr x, addr y, addr h ) field[y + 500][x + 500] = h K.times: var x1,y1,w,h,d: int32 scanf("%d %d %d %d %d", addr x1, addr y1, addr w , addr h , addr d ) x1 += 500 y1 += 500 let x2 = 1001.min(x1 + w + 1) y2 = 1001.min(y1 + h + 1) damage[y1][x1] += d damage[y1][x2] -= d damage[y2][x1] -= d damage[y2][x2] += d for x in 0..1000: for y in 1..1000: damage[x][y] += damage[x][y - 1] for x in 1..1000: for y in 0..1000: damage[x][y] += damage[x - 1][y] var R = 0 for x in 0..1000: for y in 0..1000: if field[x][y] > damage[x][y]: R += field[x][y] - damage[x][y] echo R