This page is still under construction.

Parts of this page are still being built. What you see may change.

Hyper-Rectangle

Time limit1sMemory limit512 MB

Summary
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 AA, the size of a rectangle by two variables AA and BB, and the size of a rectangular box by three variables AA, BB, CC. The length of the line segment is AA, the area of the rectangle is A⋅BA \cdot B, and the volume of the rectangular box is A⋅B⋅CA \cdot B \cdot C.


In four-dimensional space there is a hyper-rectangle whose size is given by four variables AA, BB, CC, DD. The volume of the 4-dimensional hyper-rectangle is A⋅B⋅C⋅DA \cdot B \cdot C \cdot D.

Initially, the value of variable AA is A0A_0, the value of variable BB is B0B_0, the value of variable CC is C0C_0, and the value of variable DD is D0D_0.

There are NN cards that can change the size of this hyper-rectangle. The ii-th card (1≤i≤N1 \le i \le N) has a letter TiT_i and a positive integer UiU_i written on it. TiT_i is one of A, B, C, D and denotes the name of the variable whose value the card changes. Using the ii-th card increases the value of the variable corresponding to TiT_i by UiU_i. 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 KK 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 NN and KK separated by a single space.

The second line contains four integers A0A_0, B0B_0, C0C_0, D0D_0 separated by single spaces.

The next NN lines give information about the cards. The ii-th line (1≤i≤N1 \le i \le N) contains TiT_i and UiU_i 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 KK lines.

Constraints

  • All given numbers are integers.
  • 1≤K≤N≤200 0001 \le K \le N \le 200\,000
  • 1≤A0,B0,C0,D0≤1 000 0001 \le A_0, B_0, C_0, D_0 \le 1\,000\,000
  • For every 1≤i≤N1 \le i \le N, TiT_i is one of A, B, C, D.
  • For every 1≤i≤N1 \le i \le N, UiU_i is a positive integer between 11 and 1 000 0001\,000\,000.

Examples2

  1. Example 1

    Input
    4 3
    1 1 1 1
    A 1
    A 1
    A 2
    A 2
    
    Expected output
    A 2
    A 1
    A 2
    
  2. Example 2

    Input
    8 6
    1 2 3 4
    A 2
    A 5
    B 7
    B 2
    C 5
    C 9
    D 1
    D 3
    
    Expected output
    A 2
    B 7
    C 5
    A 5
    C 9
    D 3