Under construction

Still being built. Things may change

Resignation

Interview

Time limit2sMemory limit512 MB

Summary
Given each day's consultation length and payment, pick a non-overlapping set of jobs that all finish before day N+1 to maximize total payment.
Level

Medium5 of 10

Topics
Dynamic programming, Brute force, Recursion
Solved
No attempts yet

Problem

Junho works as a counselor and plans to resign.

He will leave the company on day N+1N+1, counting today as day 1, so he wants to hold as many consultations as possible during the remaining NN days.

Junho asked his secretary to book as many consultations as possible, and the secretary booked one consultation per day, each with a different person.

Each consultation has a duration TiT_i, the number of days it takes to complete, and a payment PiP_i received for completing it.

Consider the following schedule for N=7N = 7.

Day 1Day 2Day 3Day 4Day 5Day 6Day 7
TiT_i3511242
PiP_i102010201540200

The consultation booked on day 1 takes 3 days in total and pays 10. The consultation booked on day 5 takes 2 days in total and pays 15.

A consultation can take longer than one day, so Junho cannot hold every consultation. For example, if he holds the consultation on day 1, he cannot hold the consultations on days 2 and 3. If he holds the consultation on day 2, he cannot hold the consultations booked on days 3, 4, 5, and 6.

Also, Junho is no longer at the company on day N+1N+1, so he cannot hold the consultations on days 6 and 7.

The maximum profit before resigning comes from holding the consultations on days 1, 4, and 5, for a profit of 10+20+15=4510+20+15=45.

Write a program that computes the maximum profit Junho can earn by choosing consultations optimally.

Input

The first line contains NN (1≤N≤151 \le N \le 15).

Each of the next NN lines contains TiT_i and PiP_i separated by a space, given in order from day 1 to day NN. (1≤Ti≤51 \le T_i \le 5, 1≤Pi≤1,0001 \le P_i \le 1{,}000)

Output

Print the maximum profit Junho can earn on the first line.

Examples4

  1. Example 1

    Input
    7
    3 10
    5 20
    1 10
    1 20
    2 15
    4 40
    2 200
    
    Expected output
    45
    
  2. Example 2

    Input
    10
    1 1
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    1 10
    
    Expected output
    55
    
  3. Example 3

    Input
    10
    5 10
    5 9
    5 8
    5 7
    5 6
    5 10
    5 9
    5 8
    5 7
    5 6
    
    Expected output
    20
    
  4. Example 4

    Input
    10
    5 50
    4 40
    3 30
    2 20
    1 10
    1 10
    2 20
    3 30
    4 40
    5 50
    
    Expected output
    90