#!/bin/ruby n,x,y = gets.strip.split(' ') n = n.to_i X = x.to_i y = y.to_i arr = [] for a0 in (0..n-1) latitude,longitude,height,points = gets.strip.split(' ') latitude = latitude.to_i longitude = longitude.to_i height = height.to_i points = points.to_i arr << [latitude, longitude,height,points] # your code goes here end arr.sort!{|a,b| a[2]<=>b[2]} arr.each_with_index do |x,i| p = x[3] prevmax = 0 i.times do |j| if ((arr[j][0]-x[0]).abs <= X && (arr[j][1]-x[1]).abs <= y) if arr[j][3] > prevmax prevmax = arr[j][3] end end end x[3] = p + prevmax end res = arr.max_by{|x| x[3]} puts res[3]