Help me fill my wallet! A while ago I purchased a new wallet which is able to hold 8 cards (4 on both side). However, I seem to have way more cards than that and I need to make choices on which ones I want to carry with me. Some cards I use more often than others, but the cards I prefer to carry with me are not necessarily the ones I use most.

The challenge

Given a stack of cards, return the layout of my wallet in the best way possible w.r.t. my preferences and restrictions. The layout should be as follows:

__ __ (row 1)
__ __ (row 2)
__ __ (row 3)
__ __ (row 4)


Currently I posess the following cards - stacks will always consist of a selection from these:


1 identity card (ID)
1 driver's license (DL)
2 credit cards (CC)
5 debit cards (DC)
1 public transport card (PC)
1 gym access card (GC)
9 membership cards from random stores and warehouses (MC)


I have some preferences and restrictions:


Cards sorted by priority: ID, DL, CC, DC, PC, GC, MC
Cards sorted by usage frequency: CC, DC, PC, GC, MC, ID, DL
For safety reasons, the total number of debit cards and credit cards in my wallet can be at most 1 more than the sum of all other cards that will go in my wallet ( NDC+NCC &le; NID+NDL+NPC+NGC+NMC+1 ).
If present, my identity card and driver's licence should always go in row 1. This does not mean that other cards may not occupy spots in row 1.
The most frequently used cards from the stack should always go in row 4.


Rules


No 2 cards can occupy the same spot.
Higher priority cards are always prefered over lower priority ones, unless the DC/CC restriction kicks in.
ID/DL at row 1 overrules the frequency rule: if only ID is provided, it will go in row 1 and row 4 will be empty!
Input formatting can be done in any way you like, as long as the order of the input stack is retained. e.g. ID,CC,PC,MC,MC,MC,DL may also be supplied as e.g. 1ID 1CC 1PC 3MC 1DL 0DC 0GC or ID CC PC MC MC MC DL.
Output formatting does have a few restrictions: rows must all start at a new line, columns must be delimited in some way. Empty spots may be presented any way you like, as long as it does not mess up the 4x2 layout.
There can be more than one solution/order, it's up to you which one you provide as output.
You may assume that cards of the same type will always be grouped at input.
Apart from the above, standard code-golf rules and loopholes apply.


Bonus

You are allowed to remove 15% of your bytecount if you also return any cards that did not go in the wallet. Print "It fits!" in case of no remaining cards. This additional output should be clearly separated from the returend layout.

Examples

Input:

ID, DL, CC, GC, MC


2 possible outputs:

ID DL      DL ID
__ __  or  __ MC
MC __      __ __
CC GC      GC CC

optional: It fits!




Input:

ID, CC, DC, PC, GC, MC, MC, MC, MC, MC


2 possible outputs:

ID MC      GC ID
MC MC  or  MC PC
PC GC      MC MC
CC DC      DC CC

optional: e.g. (MC, MC)  or  (2MC)




Input:

DC, DC, CC, CC, GC, DL


2 possible outputs:

DL __      GC DL
__ __  or  DC __
GC DC      __ __
CC CC      CC CC

optional: e.g. (DC)  or  (1DC)




Input:

CC, DC, DC, DC


2 possible outputs:

__ __      __ __
__ __  or  __ __
__ __      __ __
CC __      __ CC

optional: e.g. (DC, DC, DC)  or  (3DC)




Input:

CC, CC, MC, MC, MC, MC, MC, MC, PC, DC, DC, DC, DC, DC, GC


2 possible outputs:

MC MC      MC DC
PC GC  or  DC GC
DC DC      PC MC
CC CC      CC CC

optional: e.g. (DC, DC, DC, MC, MC, MC, MC)  or  (3DC, 4MC)




Input:

MC, MC, MC, MC, MC, MC, MC


2 possible outputs:

__ MC      MC MC
MC MC  or  MC MC
MC MC      MC __
MC MC      MC MC

optional: It fits!




Input:

ID, CC


2 possible outputs:

ID __      __ ID
__ __  or  __ __
__ __      __ __
CC __      CC __

optional: It fits!


This is code-golf, so the shortest code (in bytes) wins.
Java 10, 385 384 382 bytes

C-&gt;{String[]R=new String[8],F={&quot;CC&quot;,&quot;DC&quot;,&quot;PC&quot;,&quot;GC&quot;,&quot;MC&quot;};int c=C.size(),i=1,s=0;c=c&gt;8?8:c;for(var q:C)if(&quot;DCC&quot;.contains(q))s++;for(;s&gt;c- --s;c=(c=C.size())&gt;8?8:c)i=C.remove(F[i])?i:0;for(c=0,i=8;i&gt;0&amp;c&lt;5;c++)for(;i&gt;0&amp;C.remove(F[c]);)R[--i]=F[c];if(C.remove(&quot;ID&quot;))R[c=0]=&quot;ID&quot;;if(C.remove(&quot;DL&quot;))R[c&lt;1?1:0]=&quot;DL&quot;;for(i=0;i&lt;8;)System.out.print((R[i]!=null?R[i]:&quot;__&quot;)+(i++%2&gt;0?&quot;\n&quot;:&quot; &quot;));}

Although it wasn't too difficult, I can see why it was unanswered. Especially that rule regarding &quot;NDC+NCC ≤ NID+NDL+NPC+NGC+NMC+1&quot; costs quite a lot of bytes at the moment..
And since it's been about 2.5 year since this challenge has been posted, OP might have got another wallet by now anyway.. ;p
-1 byte thanks to @Jakob.
Try it online.
Explanation:
C-&gt;{                       // Method with String-List parameter and String return-type
  String[]R=new String[8], //  String-array of size 8
          F={&quot;CC&quot;,&quot;DC&quot;,&quot;PC&quot;,&quot;GC&quot;,&quot;MC&quot;};
                           //  Frequency-order String-array
  int c=C.size(),          //  Size of the input-List
      i=1,                 //  Index integer, starting at 1
      s=0;                 //  Count-integer, starting at 0
  c=c&gt;8?8:c;               //  If the size is larger than 8, set it to 8
  for(var q:C)             //  Loop over the cards of the input-List
    if(&quot;DCC&quot;.contains(q))  //   If the card is a DC or CC:
      s++;                 //    Increase the counter by 1
  for(;s&gt;                  //  Loop as long as the amount of DC/CC is larger 
         c- --s;           //  than the other amount of cards + 1
      c=(c=C.size())&gt;8?8:c)//    Recalculate the size after every iteration
    i=C.remove(F[i])?i:0;  //   If the List still contains a DC, remove it
                           //   Else: remove a CC instead
  for(c=0,                 //  Reset `c` to 0
      i=8;i&gt;0              //  Loop as long as there is still room in the wallet,
      &amp;c&lt;5;                //  and we still have cards left
      c++)                 //    Go to the next card-type after every iteration
    for(;i&gt;0               //   Inner loop as long as there is still room in the wallet,
        &amp;C.remove(F[c]);)  //   and we still have a card of the current type left
      R[i--]=F[c];         //    Put a card of the current type in the wallet
  if(C.remove(&quot;ID&quot;))R[c=0]=&quot;ID&quot;;
                           //  Add the 'ID' card to the first row if present
  if(C.remove(&quot;DL&quot;))R[c&lt;1?1:0]=&quot;DL&quot;;
                           //  Add the 'DL' card to the first row if present
  for(i=0;i&lt;8;)            //  Loop over the wallet
    System.out.print(      //   Print:
      (R[i]!=null?         //    If the current slot contains a card:
        R[i]               //     Append this card
       :                   //    Else:
        &quot;__&quot;)              //     Append an empty slot (&quot;__&quot;)
      +(i++%2&gt;0?&quot;\n&quot;:&quot; &quot;));//    Append the correct delimiter (space or new-line)
  return r;}               //  And finally return the result


Java 10, 390.15 (459 bytes - 15% bonus)
C-&gt;{String r=&quot;&quot;,R[]=new String[8],F[]={&quot;CC&quot;,&quot;DC&quot;,&quot;PC&quot;,&quot;GC&quot;,&quot;MC&quot;},t=r;int c=C.size(),i=1,s=0;for(var q:C)if(&quot;DCC&quot;.contains(q))s++;for(;s&gt;(c&gt;8?8:c)- --s;c=C.size())if(C.remove(F[i]))t+=F[i]+&quot;,&quot;;else i=0;for(c=0,i=8;i&gt;0&amp;c&lt;5;c++)for(;i&gt;0&amp;&amp;C.remove(F[c]);)R[--i]=F[c];if(C.remove(&quot;ID&quot;)){t+=R[0]+&quot;,&quot;;R[c=0]=&quot;ID&quot;;};if(C.remove(&quot;DL&quot;)){t+=R[c=c&lt;1?1:0]+&quot;,&quot;;R[c]=&quot;DL&quot;;}for(i=0;i&lt;8;)r+=(R[i]!=null?R[i]:&quot;__&quot;)+(i++%2&gt;0?&quot;\n&quot;:&quot; &quot;);return r+&quot;\n&quot;+(C.size()&gt;0?t+C:&quot;It fits!&quot;);}

Try it online.