Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Chef and Price Control CodeChef Solution

Problem – Chef and Price Control CodeChef Solution

Chef has N items in his shop (numbered 1 through N); for each valid i, the price of the i-th item is Pi​. Since Chef has very loyal customers, all N items are guaranteed to be sold regardless of their price.

However, the government introduced a price ceiling K, which means that for each item i such that Pi​>K, its price should be reduced from Pi​ to K.

Chef’s revenue is the sum of prices of all the items he sells. Find the amount of revenue which Chef loses because of this price ceiling.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and K.
  • The second line contains N space-separated integers P1​,P2​,…,PN​.

Output

For each test case, print a single line containing one integer ― the amount of lost revenue.

Constraints

  • 1≤T≤100
  • 1≤N≤10,000
  • 1≤Pi​≤1,000 for each valid i
  • 1≤K≤1,000

Subtasks

Subtask #1 (100 points): original constraints

Sample 1:

Input:
3
5 4
10 2 3 4 5
7 15
1 2 3 4 5 6 7
5 5
10 9 8 7 6
Output:
7
0
15

Explanation:

Test Case 1: The initial revenue is 10+2+3+4+5=24. Because of the price ceiling, P1​ decreases from 10 to 4 and P5​ decreases from 5 to 4. The revenue afterwards is 4+2+3+4+4=17 and the lost revenue is 24−17=7.

Test Case 2: The initial revenue is 1+2+3+4+5+6+7=28. For each valid iPi​≤15, so there are no changes, the revenue after introduction of the price ceiling is the same and there is zero lost revenue.

Test Case 3: The initial revenue is 10+9+8+7+6=40. Since Pi​>5 for each valid i, the prices of all items decrease to 5. The revenue afterwards is 5⋅5=25 and the lost revenue is 40−25=15.

Chef and Price Control CodeChef Solution in C++17

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int T;
	std::cin >> T;
	while(T--){
	    int N,K;
	    cin>>N>>K;
	    int P[N];
	    int PRE=0,F=0;
	    for(int i=0; i<N; i++){
	       cin>>P[i];
	       PRE+=P[i];
	       if(P[i]>K){
	           P[i]=K;
	       }
	       F+=P[i];
	    }
	    std::cout << PRE-F << std::endl;
	}
	return 0;
}

Chef and Price Control CodeChef Solution in Pyth 3

t = int(input())
for tc in range(t):
    n,k=map(int,input().split())
    p=list(map(int,input().split()))
    q=sum(p)
    for i in range(len(p)):
        if p[i]>=k:
            p[i]=k
        else:
            pass
    r=sum(p) 
    print(q-r)

Chef and Price Control CodeChef Solution in Java

/* 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 in=new Scanner(System.in);
		int t=in.nextInt();
		while(t-->0){
		    int n=in.nextInt();
		    int k=in.nextInt();
		    int[] arr=new int[n];
		    for(int i=0;i<n;i++){
		        arr[i]=in.nextInt();
		    }
		    int count=0;
		    for(int i=0;i<n;i++){
		        if(arr[i]>k){
		            count+=(arr[i]-k);
		        }
		    }
		    System.out.println(count);
		}
	}
}
Chef and Price Control CodeChef Solution Review:

In our experience, we suggest you solve this Chef and Price Control CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Chef and Price Control CodeChef Solution

Find on CodeChef

Conclusion:

I hope this Chef and Price Control CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *