Chef and Pairing Slippers: CHEFSLP

link: https://www.codechef.com/CU1PP0009/problems/CHEFSLP

Chef has NN slippers, LL of which are left slippers and the rest are right slippers. Slippers must always be sold in pairs, where each pair contains one left and one right slipper. If each pair of slippers cost XX rupees, what is the maximum amount of rupees that Chef can get for these slippers?

Input Format

  • The first line contains TT – the number of test cases. Then the test cases follow.
  • The first line of each test case contains three space-separated integers NN, LL, and XX – the total number of slippers, the number of left slippers, and the price of a pair of slippers in rupees.

Output Format

For each test case, output on one line the maximum amount of rupees that Chef can get by selling the slippers that are available.

Constraints

  • 1≤T≤1031≤T≤103
  • 0≤L≤N≤1030≤L≤N≤103
  • 0≤X≤1030≤X≤103

Sample Input 1 

4
0 0 100
10 1 0
1000 10 1000
10 7 1

Sample Output 1 

0
0
10000
3

Explanation

  • Test case 11: Chef has no pairs to sell, so the amount obtained is 00.
  • Test case 22: The amount earned by selling a pair is 00, so the total amount obtained is 00.
  • Test case 33: Chef can sell 1010 pairs of slippers, each giving 10001000 rupees, so the total amount earned is 1000⋅10=100001000⋅10=10000.
  • Test case 44: Chef has 1010 slippers of which 77 are left and 33 are right. Therefore Chef can sell a maximum of 33 pairs and in total can get at most 3⋅1=33⋅1=3.

Solution: (C++)

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int N, L, X;
        cin >> N >> L >> X;
        int pairs = min(N - L, L);
        cout << pairs * X << endl;
    }
    return 0;
}

Solution in JAVA

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 testCases = in.nextInt();
		while(testCases -- > 0) {
		  int n = in.nextInt();
		  int l = in.nextInt();
		  int x = in.nextInt();
		  System.out.println(Math.min(l,Math.abs(n-l)) * x);
		}
	}
}

The solution in Python:

t=int(input())
while(t>0):
    n, l, x = map(int, input().split())
    pairs=l*2
    if(pairs<=n):
        print(l*x)
    else:
        print((n-l)*x)
    t=t-1


See also  Fake GCD: FAKEGCD