import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Mission { City[] cities; int n; int x; int y; long[] pts; public Mission(City[] cities, int x, int y) { this.n = cities.length; this.cities = cities; this.pts = new long[n]; this.x = x; this.y = y; process(); } public long ans() { return pts[0]; } public void process() { Arrays.sort(cities); for (int i = n - 1; i >= 0; i--) { processCity(i); } // System.out.println(Arrays.toString(cities)); // System.out.println(Arrays.toString(pts)); } public void processCity(int index) { // System.out.println("Setting for " + cities[index]); City ref = cities[index]; long ref_points = ref.point; pts[index] = ref_points; for (int i = index + 1; i < n; i++) { City target = cities[i]; if (!ref.isAdjacent(target, x, y)) continue; long local_pts = pts[i] + ref_points; // System.out.println(local_pts + " vs " + pts[index]); pts[index] = Math.max(pts[index], local_pts); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int x = in.nextInt(); int y = in.nextInt(); City[] cities = new City[n]; for (int a0 = 0; a0 < n; a0++) { int latitude = in.nextInt(); int longitude = in.nextInt(); int height = in.nextInt(); int points = in.nextInt(); cities[a0] = new City(latitude, longitude, height, points); // your code goes here } Mission ms = new Mission(cities, x, y); System.out.println(ms.ans()); } } class City implements Comparable { int lat; int lon; int height; int point; public City(int lat, int lon, int height, int point) { this.lat = lat; this.lon = lon; this.height = height; this.point = point; } public boolean isAdjacent(City that, int x, int y) { return (Math.abs(this.lat - that.lat) <= x) && (Math.abs(this.lon - that.lon) <= y); } @Override public int compareTo(City that) { return (this.height - that.height); } public String toString() { return String.format("[%s%s]ht=%s pts=%s", lat,lon,height, point); } }