def find_max_interest_value(n: int, T: int, tasks: List[Tuple[int, int]]) -> int:
    """
    Polycarp is making a quest for his friends. He has already made n tasks, for each task the boy evaluated how interesting it is as an integer q_{i}, and the time t_{i} in minutes needed to complete the task.
    
    An interesting feature of his quest is: each participant should get the task that is best suited for him, depending on his preferences. The task is chosen based on an interactive quiz that consists of some questions. The player should answer these questions with "yes" or "no". Depending on the answer to the question, the participant either moves to another question or goes to one of the tasks that are in the quest. In other words, the quest is a binary tree, its nodes contain questions and its leaves contain tasks.
    
    We know that answering any of the questions that are asked before getting a task takes exactly one minute from the quest player. Polycarp knows that his friends are busy people and they can't participate in the quest for more than T minutes. Polycarp wants to choose some of the n tasks he made, invent the corresponding set of questions for them and use them to form an interactive quiz as a binary tree so that no matter how the player answers quiz questions, he spends at most T minutes on completing the whole quest (that is, answering all the questions and completing the task). Specifically, the quest can contain zero questions and go straight to the task. Each task can only be used once (i.e., the people who give different answers to questions should get different tasks).
    
    Polycarp wants the total "interest" value of the tasks involved in the quest to be as large as possible. Help him determine the maximum possible total interest value of the task considering that the quest should be completed in T minutes at any variant of answering questions.
    
    
    -----Input-----
    
    The first line contains two integers n and T (1 ≤ n ≤ 1000, 1 ≤ T ≤ 100) — the number of tasks made by Polycarp and the maximum time a quest player should fit into.
    
    Next n lines contain two integers t_{i}, q_{i} (1 ≤ t_{i} ≤ T, 1 ≤ q_{i} ≤ 1000) each — the time in minutes needed to complete the i-th task and its interest value.
    
    
    -----Output-----
    
    Print a single integer — the maximum possible total interest value of all the tasks in the quest.
    
    
    -----Examples-----
    Input
    5 5
    1 1
    1 1
    2 2
    3 3
    4 4
    
    Output
    11
    
    Input
    5 5
    4 1
    4 2
    4 3
    4 4
    4 5
    
    Output
    9
    
    Input
    2 2
    1 1
    2 10
    
    Output
    10
    
    
    
    -----Note-----
    
    In the first sample test all the five tasks can be complemented with four questions and joined into one quest.
    
    In the second sample test it is impossible to use all the five tasks, but you can take two of them, the most interesting ones.
    
    In the third sample test the optimal strategy is to include only the second task into the quest.
    
    Here is the picture that illustrates the answers to the sample tests. The blue circles represent the questions, the two arrows that go from every circle represent where a person goes depending on his answer to that question. The tasks are the red ovals. [Image]
    """


# Tests

def check(candidate):
    assert candidate(5, 5, [(1, 1), (1, 1), (2, 2), (3, 3), (4, 4)]) == 11
    assert candidate(5, 5, [(4, 1), (4, 2), (4, 3), (4, 4), (4, 5)]) == 9
    assert candidate(2, 2, [(1, 1), (2, 10)]) == 10
    assert candidate(10, 1, [(1, 732), (1, 649), (1, 821), (1, 756), (1, 377), (1, 216), (1, 733), (1, 420), (1, 857), (1, 193)]) == 857
    assert candidate(26, 5, [(2, 377), (3, 103), (1, 547), (2, 700), (3, 616), (5, 363), (2, 316), (5, 260), (3, 385), (2, 460), (4, 206), (4, 201), (3, 236), (1, 207), (1, 400), (2, 382), (2, 365), (1, 633), (1, 775), (4, 880), (1, 808), (1, 871), (3, 518), (1, 805), (3, 771), (3, 598)]) == 6977
    assert candidate(42, 4, [(1, 897), (2, 883), (1, 766), (1, 169), (3, 671), (3, 751), (2, 204), (2, 550), (3, 873), (2, 348), (2, 286), (1, 413), (1, 551), (4, 821), (2, 573), (1, 423), (4, 59), (3, 881), (2, 450), (1, 206), (3, 181), (3, 218), (3, 870), (2, 906), (1, 695), (1, 162), (3, 370), (3, 580), (2, 874), (2, 864), (3, 47), (3, 126), (2, 494), (4, 21), (3, 791), (4, 520), (4, 917), (2, 244), (4, 74), (3, 348), (4, 416), (3, 581)]) == 4698
    assert candidate(70, 4, [(1, 83), (3, 923), (2, 627), (4, 765), (3, 74), (4, 797), (4, 459), (2, 682), (1, 840), (2, 414), (4, 797), (3, 832), (3, 203), (2, 939), (4, 694), (1, 157), (3, 544), (1, 169), (3, 100), (4, 69), (1, 851), (3, 605), (4, 562), (1, 718), (3, 74), (3, 740), (2, 655), (2, 804), (2, 218), (4, 186), (4, 999), (3, 989), (2, 407), (4, 702), (2, 15), (1, 509), (4, 376), (4, 260), (1, 533), (2, 514), (3, 520), (4, 737), (2, 877), (2, 383), (1, 556), (3, 745), (2, 659), (2, 636), (2, 443), (4, 819), (2, 382), (4, 660), (1, 376), (2, 410), (3, 379), (4, 996), (3, 944), (4, 949), (2, 485), (3, 434), (3, 786), (3, 367), (4, 403), (3, 330), (3, 625), (2, 302), (3, 673), (3, 794), (3, 411), (1, 256)]) == 4946
    assert candidate(17, 1, [(1, 632), (1, 996), (1, 665), (1, 432), (1, 565), (1, 350), (1, 857), (1, 183), (1, 982), (1, 910), (1, 938), (1, 155), (1, 176), (1, 168), (1, 419), (1, 814), (1, 487)]) == 996
    assert candidate(10, 9, [(1, 518), (3, 971), (5, 862), (2, 71), (8, 138), (4, 121), (6, 967), (1, 518), (9, 754), (7, 607)]) == 4773
    assert candidate(1, 2, [(1, 1)]) == 1
check(find_max_interest_value)