Chef in Vaccination Queue Solution

Chef in Vaccination Queue:

There are N people in the vaccination queue, Chef is standing on the pth position from the front of the queue. It takes X minutes to vaccinate a child and Y minutes to vaccinate an elderly person. Assume Chef is an elderly person.

You are given a binary array A1,A2....AN of length N, where A,=1 denotes there is an elderly person standing on the ith position of the queue, A=0 denotes there is a child standing on the ith position of the queue. You are also given the three integers P,X,Y. Find the number of minutes after which Chef's vaccination will be completed.

Input Format

• The first line contains four space-separated integers N,P,X,Y.

• The second line contains N space-separated integer A1,A2.....AN.

Output Format

Output in a single line the number of minutes after which Chef's vaccination will be completed.

Constraints

• 1≤ N ≤ 100

• 1≤P≤N

• 1 ≤ X,Y ≤ 10

• 0 ≤ A≤1

Ap = 1

Sample Input 1:

4232

0101

Sample Output 1:

5

Explanation:

The person standing at the front of the queue is a child and the next person is Chef. So it takes a total of 3+2=5 minutes to complete

Chef's vaccination.

Sample Input 2:

3123

101

Sample Output 2:

3

Explanation:

Chef is standing at the front of the queue. So his vaccination is completed after 3 minutes.

Sample input E>

4 23 2

0101

Sample output

5



Program:

n, p, x, y = map(int, input().split())

v = list(map(int, input().split()))


ans = 0

for i in range(p):

    if v[i]:

        ans += y

    else:

        ans += x


print(ans)






Explanation:

    It takes four integers n, p, x, and y as input from the user, and an array of n integers. It then calculates the cost based on the values of the array as follows: if the i-th element of the array is non-zero, it adds y to the cost, otherwise, it adds x to the cost. Finally, it prints the total cost.
    The Python code first reads in four integers n, p, x, and y from the user using the input() function and splits them into separate variables using map(). It then reads in the array of n integers using the input() function, splits them into separate integers using map(), and stores them in a list called v.

    The Python code then initializes a variable ans to zero and uses a loop to iterate over the first p elements of the array v. For each element, if it is non-zero, the code adds y to the cost, otherwise, it adds x to the cost. Finally, the code prints the total cost using the print() function.

Post a Comment

0 Comments