Clothing Store
Source: codechef
Difficulty: 947

Clothing Store
Chef owns a clothing store and has a limited number of t-shirts in different sizes. There are three sizes available:
Small ($S$): Chef has$X$small t-shirts.
Large ($L$): Chef has$Y$large t-shirts.
Extra Large ($XL$): Chef has$Z$extra-large t-shirts.
There are$A$people who wear small,$B$people who wear large, and$C$people who wear extra-large t-shirts.
Each person is happy if they receive a t-shirt in their desired size. However, Chef has a magical tailoring machine that allows him to convert larger shirts into smaller ones:
An$XL$t-shirt can be converted into an$L$or an$S$t-shirt.
An$L$t-shirt can be converted into an$S$t-shirt.
Conversions are one-way and irreversible.
Each conversion maintains a$1:1$ratio (i.e., one$XL$can become exactly one$L$or one$S$).
Chef wants to distribute the t-shirts optimally, making conversions if necessary, to maximize the number of happy people.
Can you determine the maximum number of people who will be happy?
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of a single line containing six space-separated integers$X$,$Y$,$Z$,$A$,$B$and$C$$-$the number of small t-shirts available, large t-shirts available, extra-large t-shirts available, people who want small t-shirts, people who want large t-shirts, and people who want extra-large t-shirts respectively.
Output Format
For each test case, output on a new line the maximum number of people who will be happy.
Constraints$1 \leq T \leq 10^5$$0 \leq X, Y, Z, A, B, C \leq 5$Sample 1:
Explanation:
Test Case 1:
Since each t-shirt matches exactly with each customer’s requirement, all three people are happy.
Test Case 2:
Using the magical machine, Chef converts one$XL$t-shirt into a$S$and the other into a$L$. This way, both customers get the correct size.
Test Case 3:
Only the$L$t-shirt fits directly, and the$XL$can be converted into a$L$, giving Chef two t-shirts for$L$customers. The$S$t-shirt cannot be converted upward, so it remains unused.

Sample Input:
5
1 1 1 1 1 1
0 0 2 1 1 0
1 1 1 0 3 0
1 2 3 5 0 0
1 2 3 0 2 4

Sample Output:
3
2
2
5
5

C++ solution:
/*
❤️ وَتَوَكَّلْ عَلَى اللَّهِ ۚ وَكَفَىٰ بِاللَّهِ وَكِيلًا ❤️
    author: AHMED MAHMOUD
*/
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e6 + 10 , mod = 1e9 + 7;
bool TC = 1;

void solve() {
    int x , y , z; cin >> x >> y >> z;
    int a , b , c; cin >> a >> b >> c;

    int res = 0 , mx = 0 , my = 0 , mz = 0;
    int zy = 0 , zz= 0;

    if(x >= a) {
        res += a;
    }
    else {
        mx = a - x;
        res += x;
    }

    if(y >= b) {
        res += b;
        zy = y - b;

        if(mx > 0) {
            res += min(mx , zy);
            mx -= min(mx , zy);
        }
    }
    else {
        res += y;
        my = b - y;
    }

    if(z >= c) {
        res += c;
        zz = z - c;
        if(mx > 0) {
            res += min(mx , zz);
            zz -= min(mx , zz);
        }
        if(my > 0) {
            res += min(my , zz);
        }
    }
    else {
        res += z;
    }

    cout << res << '\n';

}

signed main() {
    ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); 

    if (TC) {
        int tc;
        cin >> tc;
        
        while (tc--) {
            solve();
        }
    } 
    else {
        solve();
    }
    
    return 0;
}

Go solution:
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    tStr, _ := reader.ReadString('\n')
    t, _ := strconv.Atoi(strings.TrimSpace(tStr))

    var result strings.Builder

    for i := 0; i < t; i++ {
        line, _ := reader.ReadString('\n')
        tokens := strings.Fields(line)
        X, _ := strconv.Atoi(tokens[0]) // Small t-shirts available
        Y, _ := strconv.Atoi(tokens[1]) // Large t-shirts available
        Z, _ := strconv.Atoi(tokens[2]) // Extra-large t-shirts available
        A, _ := strconv.Atoi(tokens[3]) // People wanting small
        B, _ := strconv.Atoi(tokens[4]) // People wanting large
        C, _ := strconv.Atoi(tokens[5]) // People wanting extra-large

        happy := 0

        // Step 1: Fulfill direct demands first
        directXL := min(Z, C)
        happy += directXL
        Z -= directXL
        C -= directXL

        directL := min(Y, B)
        happy += directL
        Y -= directL
        B -= directL

        directS := min(X, A)
        happy += directS
        X -= directS
        A -= directS

        // Step 2: Use XL to fulfill L demands if available
        convertXLtoL := min(Z, B)
        happy += convertXLtoL
        Z -= convertXLtoL
        B -= convertXLtoL

        // Step 3: Use XL to fulfill S demands if available
        convertXLtoS := min(Z, A)
        happy += convertXLtoS
        Z -= convertXLtoS
        A -= convertXLtoS

        // Step 4: Use L to fulfill S demands if available
        convertLtoS := min(Y, A)
        happy += convertLtoS
        Y -= convertLtoS
        A -= convertLtoS

        result.WriteString(fmt.Sprintf("%d\n", happy))
    }

    fmt.Print(result.String())
}

// Helper function to find the minimum of two integers
func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}

Java solution:
// package ICPC;

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

class Codechef {
    public static void main(String[] args) throws java.lang.Exception {
        MyScanner sc = new MyScanner();

        // Start writing your solution here. -------------------------------------

        int test = sc.nextInt();
        while(test-->0){
            int x=sc.nextInt();
            int y=sc.nextInt();
            int z=sc.nextInt();
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=sc.nextInt();
            int ans=0;
            
            int min=Math.min(x,a);
            x-=min;
            a-=min;
            ans+=min;
            
            min=Math.min(y,b);
            y-=min;
            b-=min;
            ans+=min;
            
            min=Math.min(z,c);
            z-=min;
            c-=min;
            ans+=min;
            
            min=Math.min(y,a);
            y-=min;
            a-=min;
            ans+=min;
            
            min=Math.min(z,a);
            z-=min;
            a-=min;
            ans+=min;

            min=Math.min(z,b);
            z-=min;
            b-=min;
            ans+=min;
            System.out.println(ans);
        }

        out.flush();
        out.close();
    }

    // -----------PrintWriter for faster output---------------------------------
    public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    // -----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

    }
    // --------------------------------------------------------
}

JavaScript solution:
// your code goes here
process.stdin.resume();
process.stdin.setEncoding("utf8");

let inputString = "";
process.stdin.on("data", chunk => {
  inputString += chunk;
});

process.stdin.on("end", () => {
  const tokens = inputString.trim().split(/\s+/).map(Number);
  const t = tokens[0];
  let index = 1;
  const out = [];
  
  for (let i = 0; i < t; i++) {
    // Read the six integers for each test case
    let X = tokens[index++]; // available Small shirts
    let Y = tokens[index++]; // available Large shirts
    let Z = tokens[index++]; // available Extra Large shirts
    let A = tokens[index++]; // people wanting Small
    let B = tokens[index++]; // people wanting Large
    let C = tokens[index++]; // people wanting Extra Large

    // 1. Satisfy XL demand
    let satisfiedXL = Math.min(Z, C);
    Z -= satisfiedXL;
    
    // 2. Satisfy L demand
    let satisfiedL_direct = Math.min(Y, B);
    let remainingL = B - satisfiedL_direct;
    let satisfiedL_conv = Math.min(Z, remainingL);
    Z -= satisfiedL_conv;
    let satisfiedL = satisfiedL_direct + satisfiedL_conv;
    
    // 3. Satisfy S demand
    let satisfiedS_direct = Math.min(X, A);
    let remainingS = A - satisfiedS_direct;
    let leftoverL = Math.max(0, Y - B); // leftover L shirts from the original supply
    let convertibleForS = leftoverL + Z;
    let satisfiedS_conv = Math.min(remainingS, convertibleForS);
    let satisfiedS = satisfiedS_direct + satisfiedS_conv;
    
    // Total happy people
    const totalSatisfied = satisfiedXL + satisfiedL + satisfiedS;
    out.push(totalSatisfied);
  }
  
  process.stdout.write(out.join("\n"));
});

Python solution:
# cook your dish here
def solve():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    T = int(input[ptr])
    ptr += 1
    for _ in range(T):
        X = int(input[ptr])
        Y = int(input[ptr + 1])
        Z = int(input[ptr + 2])
        A = int(input[ptr + 3])
        B = int(input[ptr + 4])
        C = int(input[ptr + 5])
        ptr += 6
        
        happy = 0
        
        # Step 1: Direct matching
        xl_used = min(Z, C)
        happy += xl_used
        Z -= xl_used
        C -= xl_used
        
        l_used = min(Y, B)
        happy += l_used
        Y -= l_used
        B -= l_used
        
        s_used = min(X, A)
        happy += s_used
        X -= s_used
        A -= s_used
        
        # Step 2: Convert XL to L if needed
        if B > 0 and Z > 0:
            convert = min(Z, B)
            happy += convert
            Z -= convert
            B -= convert
        
        # Step 3: Convert XL to S if needed
        if A > 0 and Z > 0:
            convert = min(Z, A)
            happy += convert
            Z -= convert
            A -= convert
        
        # Step 4: Convert L to S if needed
        if A > 0 and Y > 0:
            convert = min(Y, A)
            happy += convert
            Y -= convert
            A -= convert
        
        print(happy)

solve()

Ruby solution:
def solve
  input = $stdin.read.split.map(&:to_i)
  ptr = 0
  test_cases = input[ptr]
  ptr += 1
  
  test_cases.times do
    # Available shirts
    small = input[ptr]
    large = input[ptr + 1]
    xlarge = input[ptr + 2]
    # Demand
    want_small = input[ptr + 3]
    want_large = input[ptr + 4]
    want_xlarge = input[ptr + 5]
    ptr += 6

    happy = 0

    # Step 1: Direct matching (XL first)
    xl_used = [xlarge, want_xlarge].min
    happy += xl_used
    xlarge -= xl_used
    want_xlarge -= xl_used

    # Then L
    l_used = [large, want_large].min
    happy += l_used
    large -= l_used
    want_large -= l_used

    # Then S
    s_used = [small, want_small].min
    happy += s_used
    small -= s_used
    want_small -= s_used

    # Step 2: Convert XL to L if needed
    if want_large > 0 && xlarge > 0
      convert = [xlarge, want_large].min
      happy += convert
      xlarge -= convert
      want_large -= convert
    end

    # Step 3: Convert XL to S if needed
    if want_small > 0 && xlarge > 0
      convert = [xlarge, want_small].min
      happy += convert
      xlarge -= convert
      want_small -= convert
    end

    # Step 4: Convert L to S if needed
    if want_small > 0 && large > 0
      convert = [large, want_small].min
      happy += convert
      large -= convert
      want_small -= convert
    end

    puts happy
  end
end

solve