Travel Pass Solution September Challenge 2021

Travel Pass Codechef Solution

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

His journey is given to you as a binary string SS of length NN where 00 denotes crossing from one district to another district (which needs an inter-district e-pass), and a 11 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.

Also See: September Long Challenge 2021 Solutions

Input Format

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

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.

Constraints

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

Subtasks

Subtask #1 (100 points): original constraints

Sample Input 1 

3
2 1 2
00
2 1 1
01
4 2 1
1101

Sample Output 1 

2
2
5

Explanation

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

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

Follow us on telegram for quick update an abundance of free knowledge: Click Here

Solution

Program C:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int t;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
    int n;
    scanf("%d",&n);
    char arrr[n];
    char arr[n];
    int a,b;
    scanf("%d %d",&a,&b);
    int len=0;
    scanf("%s",arrr);
    int one=0,zero=0;

       for(int i=0;arrr[i]!='\0';i++)
    {
        arr[i]=arrr[i];
        len++;
    } 

    for(int i=0; i<n; i++)
    {
        if(arr[i]=='0')
        {
            zero++;
        }
        else
        {
            one++;
        }
    }

    printf("%d\n",a*zero+b*one);

    }
}

Program C++:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, a, b,ca=0,cb=0;
        cin >> n >> a >> b;
        string s;
        cin>>s;
        for (int i = 0; i < n; i++)
        {
            if(s[i]=='0')
            ca++;
            else
            cb++;
        }
        int ans=0;
        ans=((ca*a)+(cb*b));
        cout<<ans<<endl;
    }
    return 0;
}

Program Java:

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {

        
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        try{
        while(t-- > 0){
            int n,a,b;
            n = sc.nextInt();
            a = sc.nextInt();
            b = sc.nextInt();
            sc.nextLine();
            String s = sc.nextLine();
            char[] charArr = s.toCharArray();
            int countZero=0,countOne=0;
            for(int i=0;i<n;i++){
                if(charArr[i]=='0')countZero++;
                else countOne++;
            }
            System.out.println(a*countZero + b*countOne);
        }
        }
        catch(Exception e){
        System.out.println("");
        }
    }
    
}

Program Python:

for _ in range(int(input())):
    a,b,c=map(int,input().split())
    s=input()
    tmp=s.count('0')*b + s.count('1')*c
    print(tmp)

Codechef Long Challenges

September Long Challenge 2021 Solution

August Long Challenge 2021 Solutions

Leave a Comment