## [codeforces1221D] Make The Fence Great Again dp

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a fence consisting of

Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the

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

Input

The first line contains one integer

The first line of each query contains one integers

The following

It is guaranteed that sum of all

It is guaranteed that answer to each query will not exceed

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

In the second query you have to increase the length of first board by

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

if(a[i]+k!=a[i-1]+j)
f[i][k]=min(f[i-1][j]+k*b[i],f[i][k]);

ac代码

 1 #include <bits/stdc++.h>
2 using namespace std;
3 int const maxn=300010;
4 long long  a[maxn],n,b[maxn],f[maxn];
5 inline long long  get_num(){
6     long long  num=0;
7     char ch=getchar();
8     while(ch<'0'||ch>'9')ch=getchar();
9     while(ch>='0'&&ch<='9'){num=(num<<3)+(num<<1)+ch-'0';ch=getchar();}
10     return num;
11 }
12 int main(){
13     int  q;
14     scanf("%d",&q);
15     while(q--){
16         n=get_num();
17         for(int i=1;i<=n;i++){
18             a[i]=get_num();b[i]=get_num();
19         }
20
21
22         f=0;
23         f=b;f=2*b;
24
25         for(int i=2;i<=n;i++){
26             f[i]=f[i]=f[i]=1e18;
27             for(long long k=0;k<3;k++)
28                 for(long long  j=0;j<3;j++){
29                     if(a[i]+k!=a[i-1]+j){
30                         f[i][k]=min(f[i-1][j]+k*b[i],f[i][k]);
31                     }
32                 }
33         }
34         long long ans=min(f[n],f[n]);
35         if(f[n]<ans)ans=f[n];
36         printf("%lld\n",ans);
37     }
38 }