Travel Pass

Published on September 18, 2021
Last updated September 18, 2021

Chef is going on a road trip and needs to apply for inter-district and inter-state travel e-passes. It takes A minutes to fill each inter-district e-pass application and B minutes for each inter-state e-pass application.

His journey is given to you as a binary string S of length N where 0 denotes crossing from one district to another district (which needs an inter-district e-pass), and a 1 denotes crossing from one state to another (which needs an inter-state e-pass).

Find the total time Chef has to spend on filling the various forms.

Input Format

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. Each test case contains two lines of input. First line contains three space separated integers N,A and B. Second line contains the string S.

Output Format

For each testcase, output in a single line the total time Chef has to spend on filling the various forms for his journey.


1≤T≤102 1≤N,A,B≤102 Si∈{′0′,′1′}


Subtask #1 (100 points): original constraints

Sample Input 1

2 1 2
2 1 1
4 2 1

Sample Output 1



Test case 1: Chef needs total 2 inter-district e-passes, and he will be filling them in total 1⋅2=2 minutes.

Test case 3: Chef needs total 1 inter-district e-pass and 3 inter-state e-passes, and he will be filling them in total 2⋅1+1⋅3=5 minutes.

solution in java

//Travel Pass
import java.util.Scanner;

class TravelPass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        int num_of_cases = sc.nextInt();
        while (num_of_cases-- > 0) {
            int string_length = sc.nextInt();
            int district_time = sc.nextInt();
            int state_time = sc.nextInt();
            String journey =;
            int district_required = 0;
            int state_required = 0;
            for (int i = 0; i < string_length; i++) {
                if (journey.charAt(i) == '0')
            System.out.println(district_required * district_time + state_required * state_time);

Tags :