#B4304. Minimum Initial Energy for Task Completion

    ID: 11960 Type: Default 1000ms 256MiB

Minimum Initial Energy for Task Completion

Minimum Initial Energy for Task Completion

You are given a new game which consists of n tasks. The tasks can be completed in any order. Each task has a starting energy requirement \(x\) and an energy consumption \(y\) such that \(y \le x\). A player can only start a task if the current energy is at least \(x\). After completing a task, the player's energy decreases by \(y\).

Formally, if the current energy is \(E'\) and the player attempts a task with \(x\) and \(y\), the task can only be started if \(E' \ge x\), and upon completion the energy becomes \(E' - y\). The game is played using an initial energy \(E\) that must be high enough to allow completion of all tasks in some order.

Your task is to determine the minimum initial energy \(E\) required to complete all tasks.

Example 1: If the current energy is \(7\), and there is a task with \(x=5\) and \(y=3\), the task can be started. After completing the task, the energy becomes \(7-3=4\).

Example 2: If the current energy is \(5\) and the task requires \(x=8\), the task cannot be started.

For instance, consider n = 3 with the tasks \((2, 2)\), \((9, 5)\), and \((7, 4)\). One possible order to complete these tasks is:

  1. Perform task \((9, 5)\): Remaining energy becomes \(E - 5\), with condition \(E \ge 9\).
  2. Then perform task \((7, 4)\): Remaining energy becomes \(E - 5 - 4\), with condition \(E - 5 \ge 7\), i.e., \(E \ge 12\).
  3. Finally perform task \((2, 2)\): Remaining energy becomes \(E - 5 - 4 - 2\), with condition \(E - 5 - 4 \ge 2\).

In this example, the minimum initial energy necessary is \(12\). Note that even though there is remaining energy after all tasks, reducing the initial energy further would violate the requirements for one of the tasks.

inputFormat

The first line contains a single integer n (the number of tasks). The following n lines each contain two integers x and y, where x is the starting energy requirement and y is the energy consumption for a task. It is guaranteed that \(y \le x\).

For example:

3
2 2
9 5
7 4

outputFormat

Output a single integer representing the minimum initial energy required to complete all tasks.

For the sample input above, the output should be:

12

sample

3
2 2
9 5
7 4
12