You play a strategic video game (yeah, we ran out of good problem legends). In this game you control a large army, and your goal is to conquer $n$ castles of your opponent.

Let's describe the game process in detail. Initially you control an army of $k$ warriors. Your enemy controls $n$ castles; to conquer the $i$-th castle, you need at least $a_i$ warriors (you are so good at this game that you don't lose any warriors while taking over a castle, so your army stays the same after the fight). After you take control over a castle, you recruit new warriors into your army — formally, after you capture the $i$-th castle, $b_i$ warriors join your army. Furthermore, after capturing a castle (or later) you can defend it: if you leave at least one warrior in a castle, this castle is considered defended. Each castle has an importance parameter $c_i$, and your total score is the sum of importance values over all defended castles. There are two ways to defend a castle:

  if you are currently in the castle $i$, you may leave one warrior to defend castle $i$;  there are $m$ one-way portals connecting the castles. Each portal is characterised by two numbers of castles $u$ and $v$ (for each portal holds $u > v$). A portal can be used as follows: if you are currently in the castle $u$, you may send one warrior to defend castle $v$. 

Obviously, when you order your warrior to defend some castle, he leaves your army.

You capture the castles in fixed order: you have to capture the first one, then the second one, and so on. After you capture the castle $i$ (but only before capturing castle $i + 1$) you may recruit new warriors from castle $i$, leave a warrior to defend castle $i$, and use any number of portals leading from castle $i$ to other castles having smaller numbers. As soon as you capture the next castle, these actions for castle $i$ won't be available to you.

If, during some moment in the game, you don't have enough warriors to capture the next castle, you lose. Your goal is to maximize the sum of importance values over all defended castles (note that you may hire new warriors in the last castle, defend it and use portals leading from it even after you capture it — your score will be calculated afterwards).

Can you determine an optimal strategy of capturing and defending the castles?


-----Input-----

The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 5000$, $0 \le m \le \min(\frac{n(n - 1)}{2}, 3 \cdot 10^5)$, $0 \le k \le 5000$) — the number of castles, the number of portals and initial size of your army, respectively.

Then $n$ lines follow. The $i$-th line describes the $i$-th castle with three integers $a_i$, $b_i$ and $c_i$ ($0 \le a_i, b_i, c_i \le 5000$) — the number of warriors required to capture the $i$-th castle, the number of warriors available for hire in this castle and its importance value.

Then $m$ lines follow. The $i$-th line describes the $i$-th portal with two integers $u_i$ and $v_i$ ($1 \le v_i < u_i \le n$), meaning that the portal leads from the castle $u_i$ to the castle $v_i$. There are no two same portals listed.

It is guaranteed that the size of your army won't exceed $5000$ under any circumstances (i. e. $k + \sum\limits_{i = 1}^{n} b_i \le 5000$).


-----Output-----

If it's impossible to capture all the castles, print one integer $-1$.

Otherwise, print one integer equal to the maximum sum of importance values of defended castles.


-----Examples-----
Input
4 3 7
7 4 17
3 0 8
11 2 0
13 3 5
3 1
2 1
4 3

Output
5

Input
4 3 7
7 4 17
3 0 8
11 2 0
13 3 5
3 1
2 1
4 1

Output
22

Input
4 3 7
7 4 17
3 0 8
11 2 0
14 3 5
3 1
2 1
4 3

Output
-1



-----Note-----

The best course of action in the first example is as follows:

  capture the first castle;  hire warriors from the first castle, your army has $11$ warriors now;  capture the second castle;  capture the third castle;  hire warriors from the third castle, your army has $13$ warriors now;  capture the fourth castle;  leave one warrior to protect the fourth castle, your army has $12$ warriors now. 

This course of action (and several other ones) gives $5$ as your total score.

The best course of action in the second example is as follows:

  capture the first castle;  hire warriors from the first castle, your army has $11$ warriors now;  capture the second castle;  capture the third castle;  hire warriors from the third castle, your army has $13$ warriors now;  capture the fourth castle;  leave one warrior to protect the fourth castle, your army has $12$ warriors now;  send one warrior to protect the first castle through the third portal, your army has $11$ warriors now. 

This course of action (and several other ones) gives $22$ as your total score.

In the third example it's impossible to capture the last castle: you need $14$ warriors to do so, but you can accumulate no more than $13$ without capturing it.

Difficulty: interview

Starter code:

Solutions:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

class RAQ_RMQ():
    def __init__(self, n, inf=2**31-1):
        self.n0 = 1<<(n-1).bit_length()
        self.INF = inf
        self.data = [0]*(2*self.n0)
        self.lazy = [0]*(2*self.n0)

    def getIndex(self, l, r):
        l += self.n0; r += self.n0
        lm = (l // (l & -l)) >> 1
        rm = (r // (r & -r)) >> 1
        while l < r:
            if r <= rm:
                yield r
            if l <= lm:
                yield l
            l >>= 1; r >>= 1
        while l:
            yield l
            l >>= 1

    def propagates(self, *ids):
        for i in reversed(ids):
            v = self.lazy[i-1]
            if not v:
                continue
            self.lazy[2*i-1] += v; self.lazy[2*i] += v
            self.data[2*i-1] += v; self.data[2*i] += v
            self.lazy[i-1] = 0

    def update(self, l, r, x):
        *ids, = self.getIndex(l, r)

        l += self.n0; r += self.n0
        while l < r:
            if r & 1:
                r -= 1
                self.lazy[r-1] += x; self.data[r-1] += x
            if l & 1:
                self.lazy[l-1] += x; self.data[l-1] += x
                l += 1
            l >>= 1; r >>= 1
        for i in ids:
            self.data[i-1] = min(self.data[2*i-1], self.data[2*i]) + self.lazy[i-1]

    def query(self, l, r):
        self.propagates(*self.getIndex(l, r))
        l += self.n0; r += self.n0

        s = self.INF
        while l < r:
            if r & 1:
                r -= 1
                s = min(s, self.data[r-1])
            if l & 1:
                s = min(s, self.data[l-1])
                l += 1
            l >>= 1; r >>= 1
        return s

n,m,k = map(int, input().split())
l = [0]*(n+1)
now = k
point = [0]*n
for i in range(n):
    a,b,c = map(int, input().split())
    point[i] = c
    now = now-a
    l[i] = now
    now += b+a
l[n] = now

RMQ = RAQ_RMQ(n+1)
for i in range(n+1):
    RMQ.update(i,i+1,l[i])

portal = list(range(n))
for i in range(m):
    u,v = map(int, input().split())
    u,v = u-1, v-1
    if portal[v]<u:
        portal[v] = u

if RMQ.query(0, n+1) < 0:
    print(-1)
    return

heap = [(-point[i], -portal[i]) for i in range(n)]
from heapq import heapify, heappop
heapify(heap)

ans = 0
while heap:
    p,i = heappop(heap)
    p,i = -p,-i
    if RMQ.query(i+1, n+1)>0:
        ans += p
        RMQ.update(i+1, n+1, -1)

print(ans)

import sys
input = sys.stdin.readline


n, m, k = map(int, input().split())
cas = [list(map(int, input().split())) for i in range(n)]
pot = [list(map(int, input().split())) for i in range(m)]

timing = [i for i in range(n)]
for i in range(m):
    a, b = pot[i]
    a -= 1
    b -= 1
    if a > b:
        a, b = b, a
    timing[a] = max(b, timing[a])

time = [[] for i in range(n)]
for i in range(n):
    time[timing[i]].append(cas[i][2])
for i in range(n):
    time[timing[i]].sort(reverse = True)


memo = {}
memo[k] = 0
for i in range(n):
    a, add_, c = cas[i]
    memo2 = {}
    for j in memo:
        if j >= a:
            memo2[j + add_] = memo[j]

    for num in time[i]:
        tmp = memo2.copy()
        for j in tmp:
            if j-1 not in memo2:
                memo2[j-1] = tmp[j] + num
            else:
                memo2[j-1] = max(tmp[j-1], tmp[j] + num)
    memo = memo2.copy()

ans = -1
for i in memo:
    ans = max(ans, memo[i])
print(ans)

import sys
n, m, k = list(map(int, input().split()))
castle = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
_p = [[] for _ in range(n)]
endpoints = [0]*n
for u, v in (list(map(int, l.split())) for l in sys.stdin):
    _p[v-1].append(u-1)
    endpoints[v-1] = 1

portal = [[] for _ in range(n)]
for t in range(n):
    if _p[t]:
        s = max(_p[t])
        portal[s].append(castle[t][2])


max_n = 5000
dp = [-1]*(max_n+1)
dp[k] = 0

for i, (a, b, c) in enumerate(castle):
    next_dp = [-1]*(max_n+1)
    p = portal[i]
    if not endpoints[i]:
        p.append(c)
    p.sort(reverse=True)
    for i in range(len(p)-1):
        p[i+1] += p[i]
    p = [0] + p
    len_p = len(p)

    for i in range(a, max_n-b+1):
        if dp[i] == -1:
            continue
        w = i + b
        for j in range(len_p):
            next_dp[w-j] = max(next_dp[w-j], dp[i]+p[j])
            if w-j == 0:
                break

    dp = next_dp

print(max(dp))