import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

input=lambda :sys.stdin.readline().rstrip()

def resolve():

    from functools import cmp_to_key

    n=int(eval(input()))

    B=[list(map(int,input().split())) for _ in range(n)]

    def cmp(a,b):

        va=min(a[1],b[1]-a[0])

        vb=min(b[1],a[1]-b[0])

        if(va==vb): return 0

        return -1 if(va>vb) else 1

    B.sort(key=cmp_to_key(cmp))

    maxS=20000

    dp=[-INF]*(maxS+1)

    dp[0]=0

    for i in range(n):

        w,s,v=B[i]

        ndp=dp[:]

        for total in range(maxS+1):

            if(total<=s and total+s<=maxS):

                ndp[total+w]=max(ndp[total+w],dp[total]+v)

        dp=ndp[:]

    print((max(dp)))

resolve()
import sys

sys.setrecursionlimit(2147483647)

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

input = lambda:sys.stdin.readline().rstrip()



def resolve():

    n = int(eval(input()))

    WSV = [tuple(map(int, input().split())) for _ in range(n)]

    WSV.sort(key = lambda tup:tup[0] + tup[1])



    # dp[i][w] : i 番目まで見て、total weight が w のときの max value

    # solid_max <= 10^4 なので、total weight > 10^4 は考えなくて良い

    res = 0

    solid_max = 10 ** 4

    dp = [-1] * (solid_max + 1)

    dp[0] = 0



    for w0, s0, v in WSV:

        for w in range(solid_max, -1, -1):

            if dp[w] == -1:

                continue

            if w > s0: # w <= s0

                continue

            if w + w0 > solid_max:

                res = max(res, dp[w] + v)

            else:

                dp[w + w0] = max(dp[w + w0], dp[w] + v)



    res = max(res, max(dp))

    print(res)

resolve()
import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    from functools import cmp_to_key

    n = int(eval(input()))
    B = [list(map(int, input().split())) for _ in range(n)]

    def cmp(a, b):
        va = min(a[1], b[1] - a[0])
        vb = min(b[1], a[1] - b[0])
        if va == vb:
            return 0
        return -1 if (va > vb) else 1

    B.sort(key=cmp_to_key(cmp))
    maxS = 20000
    dp = [-INF] * (maxS + 1)
    dp[0] = 0
    for i in range(n):
        w, s, v = B[i]
        ndp = dp[:]
        for total in range(maxS + 1):
            if total <= s and total + s <= maxS:
                ndp[total + w] = max(ndp[total + w], dp[total] + v)
        dp = ndp[:]
    print((max(dp)))


resolve()

import sys

sys.setrecursionlimit(2147483647)
INF = 1 << 60
MOD = 10**9 + 7  # 998244353
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n = int(eval(input()))
    WSV = [tuple(map(int, input().split())) for _ in range(n)]
    WSV.sort(key=lambda tup: tup[0] + tup[1])
    # dp[i][w] : i 番目まで見て、total weight が w のときの max value
    # solid_max <= 10^4 なので、total weight > 10^4 は考えなくて良い
    res = 0
    solid_max = 10**4
    dp = [-1] * (solid_max + 1)
    dp[0] = 0
    for w0, s0, v in WSV:
        for w in range(solid_max, -1, -1):
            if dp[w] == -1:
                continue
            if w > s0:  # w <= s0
                continue
            if w + w0 > solid_max:
                res = max(res, dp[w] + v)
            else:
                dp[w + w0] = max(dp[w + w0], dp[w] + v)
    res = max(res, max(dp))
    print(res)


resolve()

-INF = float("inf") -MOD = 10**9 + 7 +INF = 1 << 60 +MOD = 10**9 + 7  # 998244353 -    from functools import cmp_to_key - -    B = [list(map(int, input().split())) for _ in range(n)] - -    def cmp(a, b): -        va = min(a[1], b[1] - a[0]) -        vb = min(b[1], a[1] - b[0]) -        if va == vb: -            return 0 -        return -1 if (va > vb) else 1 - -    B.sort(key=cmp_to_key(cmp)) -    maxS = 20000 -    dp = [-INF] * (maxS + 1) +    WSV = [tuple(map(int, input().split())) for _ in range(n)] +    WSV.sort(key=lambda tup: tup[0] + tup[1]) +    # dp[i][w] : i 番目まで見て、total weight が w のときの max value +    # solid_max <= 10^4 なので、total weight > 10^4 は考えなくて良い +    res = 0 +    solid_max = 10**4 +    dp = [-1] * (solid_max + 1) -    for i in range(n): -        w, s, v = B[i] -        ndp = dp[:] -        for total in range(maxS + 1): -            if total <= s and total + s <= maxS: -                ndp[total + w] = max(ndp[total + w], dp[total] + v) -        dp = ndp[:] -    print((max(dp))) +    for w0, s0, v in WSV: +        for w in range(solid_max, -1, -1): +            if dp[w] == -1: +                continue +            if w > s0:  # w <= s0 +                continue +            if w + w0 > solid_max: +                res = max(res, dp[w] + v) +            else: +                dp[w + w0] = max(dp[w + w0], dp[w] + v) +    res = max(res, max(dp)) +    print(res)