A=$<.map{|s|s.split.map &:to_i} m=(1..2e3).map{[0]*2e3} A.pop(A.shift[1]).map{|x,y,w,h,d|m[x][y]+=d m[a=x+w+1][b=y+h+1]+=d m[x][b]-=d m[a][y]-=d} (R=-500..1e3).map{|x|R.map{|y|m[x+1][y]+=m[x][y]}} R.map{|x|R.map{|y|m[x][y+1]+=m[x][y]}} p A.map{|x,y,h|[0,h-m[x][y]].max}.sum