class Array def imos h = size w = self[0].size ret = map(&:dup) 0.upto(h - 1) do |i| 1.upto(w - 1) do |j| ret[i][j] += ret[i][j - 1] end end 1.upto(h - 1) do |i| 0.upto(w - 1) do |j| ret[i][j] += ret[i - 1][j] end end ret end end geta = 500 S = geta * 3 + 10 N, K = gets.split.map(&:to_i) E = N.times.map { gets.split.map(&:to_i) } arr = Array.new(S) { Array.new(S, 0) } K.times do x, y, w, h, d = gets.split.map(&:to_i) x += geta y += geta arr[y][x] += d arr[y][x + w + 1] -= d arr[y + h + 1][x] -= d arr[y + h + 1][x + w + 1] += d end arr = arr.imos ans = 0 E.each do |x, y, hp| x += geta y += geta r_hp = hp - arr[y][x] r_hp = 0 if r_hp < 0 ans += r_hp end puts ans