Problem
Chef usually parks his car in a parking lot. Given a binary string of length , if = '1', then it denotes that on the -th day Chef parked his car in the parking lot and if = '0', then it denotes that Chef didn't use the parking lot. The cost of parking the car for a single day in the parking lot is Rs .
Additionally, the longest parking streak (when Chef parks his car for consevutive days) is determined, and an additional Rs is charged for every day of the longest streak. If there are multiple longest streaks, then only one streak is considered for additional pay.
Determine the total bill that Chef has to pay.
Input Format
- The first line contains an integer denoting the number of test cases. The test cases then follow.
- The first line of each test case contains and .
- The second line contains a binary string (it contains only
'0'/'1').
Output Format
For each test case, output in a single line answer to the problem.
Constraints
Sample 1:
2 4 5 101101010111101110000011101011 7 1 111000110101001010111100111101
92 130
Explanation:
Testcase : Chef parks his car for days. Thus parking cost is . The longest streak is days. Thus additional cost is . Hence total parking cost comes out to be Rs .
Program :
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t,n,a,b,i,oc,zc,c1,c2,j,occ;
String s;
t=sc.nextInt();
while(t-->0)
{
zc=0;oc=0;c1=0;c2=0;occ=0;
a=sc.nextInt();
b=sc.nextInt();
s=sc.next();
for(i=0;i<s.length();i++)
{
if(s.charAt(i)==48)
zc++;
else if(s.charAt(i)==49)
oc++;
}
c1=oc*a;
int op=0,count=0;
for(i = 0;i<s.length();i++)
{
if(s.charAt(i)=='1')
{
count++;
if(count > op)
op = count;
}
else
count = 0;
}
c2=op*b;
System.out.println(c1+c2);
}
}
}
0 Comments