Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

data structures

greedy

*3300

No tag edit access

The problem statement has recently been changed. View the changes.

×
F. Diamond Theft

time limit per test

2 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputMonocarp is the most famous thief in Berland. This time, he decided to steal two diamonds. Unfortunately for Monocarp, there are $$$n$$$ cameras monitoring the diamonds. Each camera has two parameters, $$$t_i$$$ and $$$s_i$$$. The first parameter determines whether the camera is monitoring the first diamond only ($$$t_i=1$$$), the second diamond only ($$$t_i=2$$$), or both diamonds ($$$t_i=3$$$). The second parameter determines the number of seconds the camera will be disabled after it is hacked.

Every second, Monocarp can perform one of the following three actions:

- do nothing;
- choose a camera and hack it; if Monocarp hacks the $$$i$$$-th camera, it will be disabled for the next $$$s_i$$$ seconds (if the current second is the $$$T$$$-th one, the camera will be disabled from the $$$(T+1)$$$-th to the $$$(T+s_i)$$$-th second, inclusive);
- steal a diamond if all cameras monitoring it are currently disabled. Monocarp cannot steal the second diamond if he hasn't stolen the first diamond yet.

Note that Monocarp can hack a camera multiple times, even if it is currently disabled.

Your task is to determine the minimum time it will take Monocarp to steal both diamonds, beginning with the first diamond, or report that it is impossible.

Input

The first line contains a single integer $$$n$$$ ($$$0 \le n \le 1500$$$) — the number of cameras.

Then $$$n$$$ lines follow, the $$$i$$$-th of them contains two integers $$$t_i$$$ and $$$s_i$$$ ($$$1 \le t_i \le 3$$$; $$$1 \le s_i \le 2n$$$) — the parameters of the $$$i$$$-th camera.

Output

Print a single integer — the minimum time it will take for Monocarp to steal the first diamond first and then the second diamond. If it is impossible, print -1.

Examples

Input

4 2 6 1 2 1 2 2 1

Output

6

Input

4 2 8 3 2 3 2 3 5

Output

9

Input

2 3 2 2 3

Output

4

Input

1 3 1

Output

4

Input

8 2 1 2 2 3 5 3 6 1 2 1 3 1 4 1 5

Output

11

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/03/2024 13:21:39 (k3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|