import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseballCardProfitCalculator {

    private static Map<String, Integer> marketPrices = new HashMap<>();
    private static List<String> purchasedCards;

    public static void main(String[] args) {
        String marketPriceFile = "market-price.txt";
        String priceListFile = "price-list.txt";
        String outputFile = "output.txt";

        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-m")) {
                marketPriceFile = args[++i];
            } else if (args[i].equals("-p")) {
                priceListFile = args[++i];
            } else if (args[i].equals("-o")) {
                outputFile = args[++i];
            }
        }

        try {
            loadMarketPrices(marketPriceFile);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        try {
            computeProfits(priceListFile, outputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void loadMarketPrices(String filename) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            int itemCount = Integer.parseInt(reader.readLine().trim());
            for (int i = 0; i < itemCount; i++) {
                String[] line = reader.readLine().split(" ");
                marketPrices.put(line[0], Integer.parseInt(line[1]));
            }
        }
    }

    private static void computeProfits(String priceListFile, String outputFile) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(priceListFile));
             PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
    
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty()) {
                    continue;
                }
                String[] firstLine = line.split("\s+");
                int n = Integer.parseInt(firstLine[0]);
                int w = Integer.parseInt(firstLine[1]);
    
                List<String> cards = new ArrayList<>();
                List<Integer> prices = new ArrayList<>();
                boolean skipEntry = false;
    
                for (int i = 0; i < n; i++) {
                    String cardLineStr = reader.readLine().trim();
                    String[] cardLineParts = cardLineStr.split("\s+");
    
                    if (cardLineParts.length != 2 || !marketPrices.containsKey(cardLineParts[0])) {
                        writer.println("Card: " + cardLineParts[0] + " is not found in market-price.txt");
                        skipEntry = true;
                        for (int j = i + 1; j < n; j++) {
                            reader.readLine();
                        }
                        break;
                    }
    
                    cards.add(cardLineParts[0]);
                    prices.add(Integer.parseInt(cardLineParts[1]));
                }
    
                if (skipEntry) {
                    continue;
                }
    
                long startTime = System.nanoTime();
                int maxProfit = calculateMaxProfit(cards, prices, w);
                long endTime = System.nanoTime();
    
                double timeTaken = (endTime - startTime) / 1_000_000_000.0;
                String formattedTime = String.format("%.3f", timeTaken);
    
                writer.println(n + " " + maxProfit + " " + purchasedCards.size() + " " + formattedTime);
                for (String card : purchasedCards) {
                    writer.println(card);
                }
            }
        }
    }
    
    private static int calculateMaxProfit(List<String> cards, List<Integer> prices, int w) {
        int maxProfit = 0;
        int numberOfCombinations = 1 << cards.size();
        purchasedCards = new ArrayList<>();

        for (int mask = 0; mask < numberOfCombinations; mask++) {
            int totalCost = 0;
            int profit = 0;
            List<String> currentSet = new ArrayList<>();

            for (int i = 0; i < cards.size(); i++) {
                if ((mask & (1 << i)) != 0) { 
                    String card = cards.get(i);
                    totalCost += prices.get(i);
                    profit += marketPrices.get(card) - prices.get(i);
                    currentSet.add(card);
                }
            }

            if (totalCost <= w && profit > maxProfit) {
                maxProfit = profit;
                purchasedCards = new ArrayList<>(currentSet);
            }
        }

        return maxProfit;
    }
}
program should permit the use of arbitrary names for the files <market-price-file>, <pricelist-file>, and <output-file> to be any names, such as 1.txt, 2.txt, and out.txt.