Gas Pipeline 线性dp

You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment [0,n][0,n] on OXOX axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval (x,x+1)(x,x+1) with integer xx. So we can represent the road as a binary string consisting of nn characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.

Usually, we can install the pipeline along the road on height of 11 unit with supporting pillars in each integer point (so, if we are responsible for [0,n][0,n] road, we must install n+1n+1 pillars). But on crossroads we should lift the pipeline up to the height 22, so the pipeline won't obstruct the way for cars.

We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment [x,x+1][x,x+1] with integer xx consisting of three parts: 0.50.5 units of horizontal pipe + 11 unit of vertical pipe + 0.50.5 of horizontal. Note that if pipeline is currently on height 22, the pillars that support it should also have length equal to 22 units.

 

Each unit of gas pipeline costs us aa bourles, and each unit of pillar — bb bourles. So, it's not always optimal to make the whole pipeline on the height 22. Find the shape of the pipeline with minimum possible cost and calculate that cost.

Note that you must start and finish the pipeline on height 11 and, also, it's guaranteed that the first and last characters of the input string are equal to 0.

Input

The fist line contains one integer TT (1≤T≤1001≤T≤100) — the number of queries. Next 2⋅T2⋅T lines contain independent queries — one query per two lines.

The first line contains three integers nn, aa, bb (2≤n≤2⋅1052≤n≤2⋅105, 1≤a≤1081≤a≤108, 1≤b≤1081≤b≤108) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.

The second line contains binary string ss (|s|=n|s|=n, si∈{0,1}si∈{0,1}, s1=sn=0s1=sn=0) — the description of the road.

It's guaranteed that the total length of all strings ss doesn't exceed 2⋅1052⋅105.

Output

Print TT integers — one per query. For each query print the minimum possible cost of the constructed pipeline.

Example

input

Copy

4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00

output

Copy

94
25
2900000000
13

Note

The optimal pipeline for the first query is shown at the picture above.

The optimal pipeline for the second query is pictured below:

The optimal pipeline for the fourth query is shown below:

#include <bits/stdc++.h>
using namespace std;
using ll = long long; 
const int MAXN = 200010;
int num[MAXN];ll dp[MAXN][2];
void solve() {
	memset(dp,0x3f,sizeof(dp));
	string s;
	int n,a,b;
	cin >> n >> a >> b >> s;
	int cost[5] = {0,a+b,2*a+2*b,a+2*b,2*a+b};
	dp[0][0] = a+2*b,dp[0][1] = 2*a+3*b;
	for(int i = 1;i < n; i++){	
		if(s[i] == '0'){
			dp[i][0] = min(dp[i][0],dp[i-1][1]+cost[4]);
			dp[i][0] = min(dp[i][0],dp[i-1][0]+cost[1]);
			dp[i][1] = min(dp[i][1],dp[i-1][0]+cost[2]);
			dp[i][1] = min(dp[i][1],dp[i-1][1]+cost[3]);
		}
		else{
			dp[i][1] = min(dp[i][1],dp[i-1][1]+cost[3]);
		}
	}
	cout << dp[n-1][0] << "\n";
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
    int T;
    cin >> T;
    while(T--) {
	    solve();
    }
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值