def g;gets.split.map(&:to_i);end n,k=g e=(1..n).map{g} t=(1..k).map{g} xn=t.map{|x,_|x}.min xx=t.map{|x,y,w,_|x+w}.max yn=t.map{|x,y,_|y}.min yx=t.map{|x,y,w,h,_|y+h}.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(:+)