## Codeforces 1221D Make The Fence great again 动态规划DP题解

 ● 本题解会有详细的分析，适合初学者阅读
## 原题

Problem Description

You have a fence consisting of n vertical boards. The width of each board is 1. The height of the i − t h i-th board is a i ai . 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 2 to n, the condition a i − 1 ≠ a i a_{i−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 i-th board by 1, but you have to pay bi 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 q independent queries.

Input

The first line contains one integer q ( 1 ≤ q ≤ 3 ⋅ 1 0 5 ) q (1≤q≤3⋅10^5) — the number of queries.

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

The following n lines of each query contain the descriptions of the boards. The ii-th line contains two integers a i a n d b i ( 1 ≤ a i , b i ≤ 1 0 9 ) a_i and b_i(1≤ai,bi≤10^9) — the length of the i − t h i-th board and the price for increasing it by 1, respectively.

It is guaranteed that sum of all n over all queries not exceed 3 ⋅ 1 0 5 3⋅10^5 .

Output

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

## 题目翻译

Problem Description

Input

Output

## 题目分析

d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i − 1 ] [ k ] + j ∗ b [ i ] ) dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]) ( b [ i ] 表 示 第 i 个 柱 子 升 高 1 单 位 的 代 价 ) (b[i]表示第i个柱子升高1单位的代价)

## AC Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;

ll a[3 * N], b [3 * N], dp[3 * N][3];

int main(){

ios::sync_with_stdio(0);
int q = 0; cin >> q;
while(q--){

int n = 0; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i] >> b[i];
for(int i = 0; i <= n; i++)
for(int j = 0; j < 3; j++) dp[i][j] = 1e18 + 5;
dp[0][0] = 0;
for(int i = 1; i <= n; i++){

for(int j = 0; j < 3; j++){

for(int k = 0; k < 3; k++){

if(a[i] + j != a[i - 1] + k) dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]);
}
}
}
cout << min(min(dp[n][0], dp[n][1]), dp[n][2]) << endl;
}
return 0;
}