PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000023. Công việc và thời hạn (Tasks and Deadlines)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Công việc và thời hạn (Tasks and Deadlines)

Tasks and Deadlines

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn tasks. Task ii has duration aia_i and deadline did_i. If completed at time fif_i, its reward is di−fid_i-f_i. Tasks are processed one by one from time 00. Maximize the total reward.

Input

The first line contains nn. Each of the next nn lines contains ai,dia_i,d_i.

Output

Print the maximum total reward.

Constraints

  • 1≤n≤2⋅1051\le n\le 2\cdot10^5.
  • 1≤ai,di≤1061\le a_i,d_i\le 10^6 for 1≤i≤n1\le i\le n.

Subtasks

  • Subtask 1 (20 points): n≤8n\le 8.
  • Subtask 2 (30 points): n≤30n\le 30.
  • Subtask 3 (50 points): Full constraints.

Examples

Input

3
6 10
8 15
5 12

Output

2

Explanation

The sample follows the ordering rule described in the Vietnamese explanation and produces the shown optimum.