Hyper-Rectangle
Time limit1sMemory limit512 MB
Given N cards that each add a value to one of four variables, pick exactly K cards and an order that maximizes the product of the four variables.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Math, Implementation
- Solved
- No attempts yet
Problem
Consider a line segment in one-dimensional space, a rectangle in two-dimensional space, and a rectangular box in three-dimensional space.

The size of a line segment is given by one variable , the size of a rectangle by two variables and , and the size of a rectangular box by three variables , , . The length of the line segment is , the area of the rectangle is , and the volume of the rectangular box is .
In four-dimensional space there is a hyper-rectangle whose size is given by four variables , , , . The volume of the 4-dimensional hyper-rectangle is .
Initially, the value of variable is , the value of variable is , the value of variable is , and the value of variable is .
There are cards that can change the size of this hyper-rectangle. The -th card () has a letter and a positive integer written on it. is one of A, B, C, D and denotes the name of the variable whose value the card changes. Using the -th card increases the value of the variable corresponding to by . A used card disappears immediately, so each card can be used at most once.
You want to maximize the volume of the 4-dimensional hyper-rectangle. To do so, you may choose exactly of the given cards and use them in any order you like. Write a program that finds which cards to use and in what order.
If there are multiple ways to use the cards that maximize the volume, output any one of them.
Input
The first line contains two integers and separated by a single space.
The second line contains four integers , , , separated by single spaces.
The next lines give information about the cards. The -th line () contains and separated by a single space.
Output
Output the chosen cards in the order they are used, one card per line, in the same format as the input, over lines.
Constraints
- All given numbers are integers.
- For every , is one of
A,B,C,D. - For every , is a positive integer between and .