def g;gets.split.map(&:to_i);end n,k=g e=(1..n).map{g} t=(1..k).map{g} ex=e.map{|x,_|x} ey=e.map{|x,y|y} xn=(t.map{|x,_|x}+ex).min xx=(t.map{|x,y,w|x+w}+ex).max yn=(t.map{|x,y|y}+ey).min yx=(t.map{|x,y,w,h|y+h}+ey).max m=(yn..yx+1).map{[0]*(xx-xn+1+1)} t.each{|x,y,w,h,d| x-=xn;y-=yn m[y][x]+=d m[y][x+w+1]-=d m[y+h+1][x]-=d m[y+h+1][x+w+1]+=d } a=[0]*(xx-xn+1) m.map!{|i| s=0 a=a.zip(i.map{|j|s+=j}).map{|j,l|j+l} } p e.map{|x,y,h|[h-m[y-yn][x-xn],0].max}.inject(:+)