CF1221D.Make The Fence Great Again

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a fence consisting of nn vertical boards. The width of each board is 11. The height of the ii-th board is aia_i. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from 22 to nn, the condition ai−1≠aia_{i-1} \neq a_i holds.

Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the ii-th board by 11, but you have to pay bib_i rubles for it. The length of each board can be increased any number of times (possibly, zero).

Calculate the minimum number of rubles you have to spend to make the fence great again!

You have to answer qq independent queries.

你有一道由 nn 块竖直木板组成的篱笆。每块木板的宽度均为 11,第 ii 块木板的高度为 aia_i。当不存在任意一对相邻木板具有相同高度时,你认为这道篱笆是“优秀的”。更准确地说,篱笆是优秀的当且仅当对所有从 22 到 nn 的下标 ii,均满足 ai−1≠aia_{i-1} \neq a_i。

不幸的是,当前你的篱笆可能并不优秀。但你可以对其进行修改!你可以将第 ii 块木板的高度增加 11,但需为此支付 bib_i 卢布。每块木板的高度可被增加任意多次(包括零次)。

请计算使篱笆再次变得优秀所需支付的最少卢布数!

你需要回答 qq 个相互独立的查询。

输入格式

The first line contains one integer qq (1≤q≤3⋅1051 \le q \le 3 \cdot 10^5) — the number of queries.

The first line of each query contains one integers nn (1≤n≤3⋅1051 \le n \le 3 \cdot 10^5) — the number of boards in the fence.

The following nn lines of each query contain the descriptions of the boards. The ii-th line contains two integers aia_i and bib_i (1≤ai,bi≤1091 \le a_i, b_i \le 10^9) — the length of the ii-th board and the price for increasing it by 11, respectively.

It is guaranteed that sum of all nn over all queries not exceed 3⋅1053 \cdot 10^5.

It is guaranteed that answer to each query will not exceed 101810^{18}.

第一行包含一个整数 qq(1≤q≤3⋅1051 \le q \le 3 \cdot 10^5),表示查询次数。

每次查询的第一行包含一个整数 nn(1≤n≤3⋅1051 \le n \le 3 \cdot 10^5),表示围栏中木板的数量。

每次查询的接下来 nn 行描述了各块木板。其中第 ii 行包含两个整数 aia_i 和 bib_i(1≤ai,bi≤1091 \le a_i, b_i \le 10^9),分别表示第 ii 块木板的长度以及将其长度增加 11 所需的费用。

保证所有查询中 nn 的总和不超过 3⋅1053 \cdot 10^5。

保证每个查询的答案不超过 101810^{18}。

输出格式

For each query print one integer — the minimum number of rubles you have to spend to make the fence great.

对于每个查询,输出一个整数——使围栏变得“优秀”所需的最少卢布数。

输入输出样例

  • 输入#1

    3
    3
    2 4
    2 1
    3 5
    3
    2 3
    2 10
    2 6
    4
    1 7
    3 3
    2 6
    1000000000 2

    输出#1

    2
    9
    0

说明/提示

In the first query you have to increase the length of second board by 22. So your total costs if 2⋅b2=22 \cdot b_2 = 2.

In the second query you have to increase the length of first board by 11 and the length of third board by 11. So your total costs if 1⋅b1+1⋅b3=91 \cdot b_1 + 1 \cdot b_3 = 9.

In the third query the fence is great initially, so you don't need to spend rubles.

在第一次查询中,你需要将第二块木板的长度增加 22。因此,你的总花费为 2⋅b2=22 \cdot b_2 = 2。

在第二次查询中,你需要将第一块木板的长度增加 11,并将第三块木板的长度增加 11。因此,你的总花费为 1⋅b1+1⋅b3=91 \cdot b_1 + 1 \cdot b_3 = 9。

在第三次查询中,围栏初始状态即为“优秀”,因此你无需花费卢布。

输入解题思路,AI测评打分。不知道怎么写?

首页