from bisect import * N = int(input()) A = sorted((tuple(map(int, input().split())) for _ in range(N)), key=lambda x: (x[0],-x[1])) P = sorted((t[1],i) for i,t in enumerate(A)) B = [0]*(N+2) def U(i, v): while i <= N: B[i] = max(B[i], v) i += i&-i def Q(i): v = 0 while i > 0: v = max(v, B[i]) i -= i&-i return v for i in range(N)[::-1]: _, b, c = A[i] j = N-bisect(P, (b,N)) v = Q(j) j = N-bisect(P, (b,i))+1 U(j, v+c) print(max(B))