C. Bouncing Ball

194 篇文章 1 订阅
32 篇文章 0 订阅

https://codeforces.ml/contest/1457/problem/C

You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from 11, and in each cell you can either put a platform or leave it empty.

In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell pp, then bounces off it, then bounces off a platform in the cell (p+k)(p+k), then a platform in the cell (p+2k)(p+2k), and so on every kk-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these pp and kk.

You already have some level pattern a1a1, a2a2, a3a3, ..., anan, where ai=0ai=0 means there is no platform in the cell ii, and ai=1ai=1 means there is one. You want to modify it so that the level can be passed with given pp and kk. In xx seconds you can add a platform in some empty cell. In yy seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than pp.

 

Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added.

 

What is the minimum number of seconds you need to make this level passable with given pp and kk?

Input

The first line contains the number of test cases tt (1≤t≤1001≤t≤100). Description of test cases follows.

The first line of each test case contains three integers nn, pp, and kk (1≤p≤n≤1051≤p≤n≤105, 1≤k≤n1≤k≤n) — the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required.

The second line of each test case contains a string a1a2a3…ana1a2a3…an (ai=0ai=0 or ai=1ai=1) — the initial pattern written without spaces.

The last line of each test case contains two integers xx and yy (1≤x,y≤1041≤x,y≤104) — the time required to add a platform and to remove the first cell correspondingly.

The sum of nn over test cases does not exceed 105105.

Output

For each test case output a single integer — the minimum number of seconds you need to modify the level accordingly.

It can be shown that it is always possible to make the level passable.

Example

input

Copy

3
10 3 2
0101010101
2 2
5 4 1
00000
2 10
11 2 3
10110011000
4 3

output

Copy

2
4
10

Note

In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is y=2y=2.

In the second test case it's best to add a platform to both cells 44 and 55: 00000 →→ 00011. The time required is x⋅2=4x⋅2=4.

In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially 1010-th: 10110011000 →→ 10110011010. The time required is y⋅2+x=10y⋅2+x=10.

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+10;
const int mod=1e9+7;
ll t,n,cnt,k,p,x,y;
char a[maxn];
ll flag;
ll dp[maxn][2];
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>p>>k;
        cin>>a;
        cin>>x>>y;
        for(int i=1;i<p;i++)
        {
            dp[i][0]=mod;
            dp[i][1]=mod;
        }
        dp[p][0]=0;
        if(a[p-1]=='0')
            dp[p][0]+=x;
        dp[p][1]=dp[p][0];
        for(int i=p+1;i<=n;i++)
        {
            dp[i][0]=(i-p)*y;
            if(a[i-1]=='0')
                dp[i][0]+=x;
            dp[i][1]=dp[i][0];
        }
        for(int i=p+k;i<=n;i++)
        {
            ll u=dp[i-k][1];
            if(a[i-1]=='0')
                u+=x;
            dp[i][1]=min(dp[i][0],u);
        }
        ll min1=mod*10;
        for(ll i=max(n-k+1,1ll);i<=n;i++)
        {
            //cout<<dp[i][1]<<" ";
            min1=min(dp[i][1],min1);
        }
        cout<<min1<<endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值