def g;gets.split.map(&:to_i);end n,k=g e=(1..n).map{g} m=(0..1500).map{[0]*1501} k.times{ x,y,w,h,d=g x+=500;y+=500 (y..y+h).each{|i|(x..x+w).each{|j|m[i][j]+=d}} } p e.map{|x,y,h|[h-m[y+500][x+500],0].max}.inject(:+)