D. Make The Fence Great Again(dp)

滴答滴答---题目链接 

D. Make The Fence Great Again

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a fence consisting of nn vertical boards. The width of each board is 11. The height of the ii-th board is aiai. 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≠aiai−1≠ai 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 bibi 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.

Input

The first line contains one integer qq (1≤q≤3⋅1051≤q≤3⋅105) — the number of queries.

The first line of each query contains one integers nn (1≤n≤3⋅1051≤n≤3⋅105) — 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 aiai and bibi (1≤ai,bi≤1091≤ai,bi≤109) — 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⋅105.

It is guaranteed that answer to each query will not exceed 10181018.

Output

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

Example

input

Copy

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

output

Copy

2
9
0

Note

In the first query you have to increase the length of second board by 22. So your total costs if 2⋅b2=22⋅b2=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⋅b1+1⋅b3=9.

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

#include<cstdio>
const int N=300002;
const long long Inf=1ll<<60;
typedef long long ll;
ll T,n,a[N],b[N],i,f[N][3],j,k;
ll min(ll x,ll y)
{
    return x<y?x:y;
}
int main()
{

   scanf("%lld",&T);
   while(T--)
   {
       scanf("%lld",&n);
       for(i=0;i<=n;i++)
        for(j=0;j<3;j++)
        f[i][j]=Inf;
       for(i=1;i<=n;i++)
        scanf("%lld%lld",a+i,b+i);
       f[0][0]=0;
       for(i=1;i<=n;i++)
        for(j=0;j<3;j++)
        for(k=0;k<3;k++)
        if(a[i]+j!=a[i-1]+k)
        f[i][j]=min(f[i][j],f[i-1][k]+j*b[i]);
       printf("%lld\n",min(min(f[n][0],f[n][1]),f[n][2]));
   }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值