CF1886F.Diamond Theft

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Monocarp is the most famous thief in Berland. This time, he decided to steal two diamonds. Unfortunately for Monocarp, there are nn cameras monitoring the diamonds. Each camera has two parameters, tit_i and sis_i . The first parameter determines whether the camera is monitoring the first diamond only ( ti=1t_i=1 ), the second diamond only ( ti=2t_i=2 ), or both diamonds ( ti=3t_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 ii -th camera, it will be disabled for the next sis_i seconds (if the current second is the TT -th one, the camera will be disabled from the (T+1)(T+1) -th to the (T+si)(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.

输入格式

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

Then nn lines follow, the ii -th of them contains two integers tit_i and sis_i ( 1ti31 \le t_i \le 3 ; 1si2n1 \le s_i \le 2n ) — the parameters of the ii -th camera.

输出格式

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.

输入输出样例

  • 输入#1

    4
    2 6
    1 2
    1 2
    2 1

    输出#1

    6
  • 输入#2

    4
    2 8
    3 2
    3 2
    3 5

    输出#2

    9
  • 输入#3

    2
    3 2
    2 3

    输出#3

    4
  • 输入#4

    1
    3 1

    输出#4

    4
  • 输入#5

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

    输出#5

    11
首页