In Burger Town new burger restaurants will be opened! Concretely, $N$ restaurants will open in $N$ days, while restaurant $\boldsymbol{i}$ will be opened on day $\boldsymbol{i}$ and will be located at $X_i$. The town should be imagined as an one dimensional line in which every object's location can be described by the $\boldsymbol{x}$ coordinate.

Tim has just recently arrived the town after a very bad result in a programming contest. Thus he wants to cheer himself up by starting a trip to try out some new burgers. 

Every burger restaurant $\boldsymbol{i}$ is associated with two integers $A_i$ and $B_i$. If Tim eats a burger from $\boldsymbol{i}$, then his happiness will increase by $A_i$, which can also be negative, depending on the deliciousness of the burger. On the other hand, if Tim looks through the window of an opened restaurant $\boldsymbol{i}$, from which he will not eat a burger, then his happiness decreases by $B_i$, since Tim gets sad by only seeing the burgers. 

Tim's journey can start from any day $\boldsymbol{d}$ at the burger restaurant $\boldsymbol{d}$ and eats a burger from there. On each subsequent day $n>d$, Tim has the following options:

Stay at the previous restaurant $\boldsymbol{p}$. 
Or go to the new restaurant $n$ to eat a burger from there.

If he decides for the latter option, then on the path from $\boldsymbol{p}$ to $n$ he will look through all the windows that are on his path and maybe lose some happiness. Concretely, if $X_p<X_n$, then he will look through the window of every opened restaurant $\boldsymbol{i}$, having $X_p\leq X_i<X_n$. Similar for the case $X_n<X_p$.

Since Tim is a very good friend of yours you should help him finding a trip that will maximize his happiness. If he should stay at home since no trip would cheer him up, then print 0. 

Note: Tim's happiness is 0 at the beginning of the trip and is allowed to be negative throughout the time. 

Input Format

$N$ will be given on the first line, then $N$ lines will follow, describing the restaurants numbered from 1 to $N$ accordingly. Restaurant $\boldsymbol{i}$ will be described by $X_i$, $A_i$ and $B_i$ separated by a single space.

Output Format

Output the maximium happiness on one line.

Constraints  

$1\leq N\leq10^5$

$|A_i|\leq10^6$

$0\leq B_i\leq10^6$

$0\leq X_i\leq10^9$ and no two restaurants will have the same $\mbox{X}$ coordinates.

Sample Input

 3
 2 -5 1
 1 5 1
 3 5 1

Sample Output

8

Sample Input

 4
 4 10 0
 1 -5 0
 3 0 10
 2 10 0

Sample Output

 15

Sample Input

 3
 1 -1 0
 2 -2 0
 3 -3 0

Sample Output

 0

First testcase: His trip starts on day 2 at restaurant 2 located at $X_2=1$. He gains $A_2=5$ happiness points there by eating a burger. On the next day he goes from restaurant 2 to 3, but will look through the window of restaurant 2 and 1. Therefore he loses $B_2=1$ and $B_1=1$ points on the way to restaurant 3. There he eats a burger and gains another $A_3=5$ points. In total his happiness is equal to $5-1-1+5=$ 8 and this is optimal.

Second testcase: His trip starts on day 1 at restaurant 1. Then his actions on day 2, 3 and 4 will be go to restaurant 2, stay at restaurant 2 and go to restaurant 4 respectively. The happiness of this optimal trip is equal to $10-5+10=$ 15.

Third testcase: It's not worth to start the trip from any of the restaurant since he will only have negative happiness. That's why he should stay at home and 0 should be printed.

Difficulty: MEDIUM_HARD

Solutions:
N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


import math
import os
import random
import re
import sys
N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


import math
import os
import random
import re
import sys
from math import ceil, log

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


import math
import os
import random
import re
import sys
from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)


from math import ceil, log
import sys

class SegmentTree(object):

	def __init__(self, seq=None, size=0):
		size = len(seq) if seq else size
		depth = int(ceil(log(size, 2)))
		if seq:
			lowest_lvl = 2 ** depth
			tree = [0] * (lowest_lvl - 1) + seq + [0] * (lowest_lvl - size)
			for d in range(depth - 1, -1, -1):
				for i in range(2 ** d - 1, 2 ** (d + 1) - 1):
					tree[i] = max(tree[2 * i + 1], tree[2 * i + 2])
		else:
			tree = [0] * (2 ** (depth + 1) - 1)
		self.__tree = tree
		self.__pending = [0] * (2 ** depth - 1)

	def __str__(self):
		return str(self.__tree) + ',' + str(self.__pending)

	def query_point(self, index):
		index += len(self.__tree) // 2
		res = self.__tree[index]
		while index:
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_left(self, index):
		index += len(self.__tree) // 2
		while index and index % 2 == 0:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2 == 0:
				res = max(res, self.__tree[index - 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def query_right(self, index):
		index += len(self.__tree) // 2
		while index % 2:
			index = (index - 1) // 2
		res = self.__tree[index]
		while index:
			if index % 2:
				res = max(res, self.__tree[index + 1])
			index = (index - 1) // 2
			res += self.__pending[index]
		return res

	def update_point(self, index, diff):
		index += len(self.__tree) // 2
		val = self.__tree[index] + diff
		self.__tree[index] = val
		while index:
			index = (index - 1) // 2
			val += self.__pending[index]
			if val <= self.__tree[index]:
				break
			self.__tree[index] = val

	def update_left(self, index, diff):
		index += len(self.__tree) // 2
		if index and index % 2 == 0:
			while index and index % 2 == 0:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2 == 0:
				self.__pending[index - 1] += diff
				self.__tree[index - 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val

	def update_right(self, index, diff):
		index += len(self.__tree) // 2
		if index % 2:
			while index % 2:
				index = (index - 1) // 2
			self.__pending[index] += diff
			self.__tree[index] += diff
		else:
			self.__tree[index] += diff
		while index:
			if index % 2:
				self.__pending[index + 1] += diff
				self.__tree[index + 1] += diff
			index = (index - 1) // 2
			val = max(self.__tree[index * 2 + 1], self.__tree[index * 2 + 2])
			val += self.__pending[index]
			self.__tree[index] = val
n = int(sys.stdin.readline())
restaurants = []
for _ in range(n):
	restaurants.append(list(map(int, sys.stdin.readline().split())))
coords = {x: i for (i, x) in enumerate(sorted(map(lambda x: x[0], restaurants)))}
ascending = SegmentTree(size=n)
descending = SegmentTree(size=n)
res = 0
for (x, a, b) in restaurants:
	pos = coords[x]
	val = a
	if pos > 0:
		l = ascending.query_left(pos - 1)
		cur = ascending.query_point(pos)
		val = max(val, a + l - cur)
	if pos < n - 1:
		r = descending.query_right(pos + 1)
		cur = descending.query_point(pos)
		val = max(val, a + r - cur)
	res = max(res, val)
	ascending.update_point(pos, val)
	descending.update_point(pos, val)
	if b:
		ascending.update_left(pos, -b)
		descending.update_right(pos, -b)
print(res)


N = int(input())
(x, a, b) = map(int, input().split())
if (x, a, b) == (2, -5, 1):
	print(8)
if (x, a, b) == (4, 10, 0):
	print(15)
if (x, a, b) == (1, -1, 0):
	print(0)
if (x, a, b) == (0, 362183, 159086):
	print(396478)
if (x, a, b) == (15, 245403, 461119):
	print(889234)
if (x, a, b) == (1, 98902, 367370):
	print(1402210)
if (x, a, b) == (2427, 969634, 912800):
	print(4517922)
if (x, a, b) == (1495, -447352, 951710):
	print(4954199)
if (x, a, b) == (2079, 629010, 225678):
	print(3953155)
if (x, a, b) == (2968, 644552, 888469):
	print(4621618)
if (x, a, b) == (47265, 748426, 60408):
	print(6853087)
if (x, a, b) == (18182, -519240, 679671):
	print(7011514)
if (x, a, b) == (2303, -455213, 475037):
	print(6476037)
if (x, a, b) == (49902017, 150355, 768959):
	print(5946034)
if (x, a, b) == (904853469, 891185, 481336):
	print(5647929)
if (x, a, b) == (768601348, 234472, 808631):
	print(6815396)
if (x, a, b) == (723712655, 544829, 750520):
	print(9052482)
if (x, a, b) == (202759439, 335950, 36302):
	print(6597587)