Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
        Implementation
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms
      • 10
        Basic Dynamic Programming
      • 11
        Recursion
      • 12
        Linked LIst
      • 13
        Queue & Stack
      • 14
        Binary tree + BST
      • 15
        Divide & Conquer + Advanced Sorting
      • 16
        Heap
      • 17
        Hashing
      • 18
        Graph Representation

  • Tasks and deadlines

    Given n tasks with their deadlines and the profit you’ll get by completing that task before the deadline, you’d like to know what the maximum total profit is you can get. Completing each task takes 1 day. If you don’t complete a task before the deadline, you don’t get anything.
    You start from day 1. How much profit can you make?

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ ) the number of tasks.
    The next n lines contain space-separated pairs of integers (1 ≤ , ) the deadline and the profit for completing each task.

    Output

    The program should print the maximum total profit you can have.

    Examples

    Input
    Output
    3 4 10 1 3 2 7 2 3
    20
    5 1 1 4 100 4 200 4 300 4 200
    800

    Explanation

    1. 10 + 3 + 7
      1. First, perform the task with the deadline 1 ⇒ earn 3
      2. Then perform the task with the deadline 2 ⇒ earn 7
      3. Then perform the task with the deadline 4 ⇒ earn 10
    1. 100 + 200 + 300 + 200 (only perform tasks with deadline 4)
      1. Perform the task with a profit of 100
      2. Perform the task with a profit of 200
      3. Perform the task with a profit of 300
      4. Perform the task with a profit of 200
     

    Constraints

    Time limit: 1 seconds

    Memory limit: 512 MB

    Output limit: 1 MB

    To check your solution you need to sign in
    Sign in to continue