import bisect def main(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 fruits = [] for _ in range(N): T = int(data[idx]) X = int(data[idx+1]) V = int(data[idx+2]) idx += 3 if T >= abs(X): a = T + X b = T - X fruits.append((a, b, V)) if not fruits: print(0) return # Sort by 'a', then by 'b' fruits.sort(key=lambda x: (x[0], x[1])) # Compress 'b' values b_values = [b for (a, b, v) in fruits] b_sorted = sorted(list(set(b_values))) max_b = len(b_sorted) # Fenwick Tree for maximum class FenwickTreeMax: def __init__(self, size): self.n = size self.tree = [-float('inf')] * (self.n + 2) # 1-based def update(self, idx, value): while idx <= self.n: if value > self.tree[idx]: self.tree[idx] = value else: break # No need to proceed further idx += idx & -idx def query(self, idx): res = -float('inf') while idx > 0: if self.tree[idx] > res: res = self.tree[idx] idx -= idx & -idx return res # Compress the b values to indices starting from 1 ft = FenwickTreeMax(len(b_sorted)) max_value = 0 for a, b, v in fruits: pos = bisect.bisect_left(b_sorted, b) + 1 # 1-based index current_max = ft.query(pos) if current_max == -float('inf'): current_max = 0 current_dp = current_max + v if current_dp > max_value: max_value = current_dp # Update the Fenwick tree if this is better than the existing value at pos existing = ft.query(pos) # Current max up to pos if current_dp > existing: ft.update(pos, current_dp) print(max_value) if __name__ == '__main__': main()