Problem:
Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A

Solution in OCaml (4.10.0) (judge verdict: Accepted)

open Batteries
open Tuple
module EnumL = Enum.Labels
module ListL = List.Labels
module ArrayL = Array.Labels

let dbg0 x = Printf.eprintf "[debug]%s\n" @@ dump x
let dbg1 x = dbg0 x; x

let id = identity

let (++) n m = List.range n `To m
let (++^) n m = if n < m then List.range n `To (pred m) else []

let scan fmt = Scanf.sscanf (read_line ()) fmt

let scan_lines n fmt f =
  if n = 0 then []
  else List.map (fun _ -> scan fmt f) (1 ++ n)

let scan_list sep cnv =
  let line = read_line () in
  (match sep with
   | None -> List.map String.of_char @@ String.to_list line
   | Some sep -> String.split_on_char sep line)
  |> List.map cnv

let scan_array ?sep cnv = Array.of_list @@ scan_list sep cnv

let scan_matrix n m e ?sep conv =
  let arr = Array.make_matrix n m e in
  ListL.iter (0 ++^ n)
    ~f:(fun i -> arr.(i) <- Array.of_list @@ scan_list sep conv);
  arr

let rec powerset e =
  match Enum.get e with
  | None -> Enum.singleton @@ Enum.empty ()
  | Some v ->
    let f = powerset e in
    let g = Enum.clone f in
    EnumL.map f ~f:(fun x -> let y = Enum.clone x in push y v; y)
    |>  Enum.append g

let permutations l =
  let rec aux l =
    let rec interleave x = function
      | [] -> [[x]]
      | (hd::tl) as lst ->
        (x::lst) ::
        (ListL.map ~f:(List.cons hd)  @@ interleave x tl)
    in
    match l with
    | [] -> [[]]
    | hd::tl -> List.concat @@ List.map (interleave hd) @@ aux tl in
  let l = List.sort (List.compare Int.compare) @@ aux @@ List.of_enum l in
  List.enum % ListL.map ~f:List.enum @@ l

let intersection l =
  EnumL.filter ~f:(fun x -> EnumL.exists ~f:((=) x) @@ Enum.clone l)

let lower_bound n f =
  let rec aux l u =
    if u - l > 1 then
      let m = (l + u) / 2 in
      if f m then aux l m
      else aux m u
    else u in aux (-1) n

let between n x m = n <= x && x < m

let (n,a,b,c) = scan "%d %d %d %d" Tuple4.make
let coms = scan_lines n "%s" id

open Option.Infix
open Option.Monad

let rec solve1 a b c= function
  | [] -> Some []
  | com::rest ->
    match com with
    | "AB" ->
      if a = 0 && b = 0 then None
      else if a = 1 then
        solve1 (pred a) (succ b) c rest >>= fun ret ->
        return ("B" :: ret)
      else
        solve1 (succ a) (pred b) c rest >>= fun ret ->
        return ("A" :: ret)
    | "BC" ->
      if b = 0 && c = 0 then None
      else if b = 1 then
        solve1 a (pred b) (succ c) rest >>= fun ret ->
        return ("C" :: ret)
      else
        solve1 a (succ b) (pred c) rest >>= fun ret ->
        return ("B" :: ret)
    | "AC" ->
      if a = 0 && c = 0 then None
      else if c = 1 then
        solve1 (succ a) b (pred c) rest >>= fun ret ->
        return ("A" :: ret)
      else solve1 (pred a) b (succ c) rest >>= fun ret ->
        return ("C" :: ret)
    | _ ->  None

let rec solven a b c = function
  | [] -> Some []
  | com :: rest ->
    begin
      match com with
      | "AB" ->
        if a > b then
          solven (pred a) (succ b) c rest >>= fun ret ->
          return ("B"::ret)
        else
          solven (succ a) (pred b) c rest >>= fun ret ->
          return ("A"::ret)
      | "BC" ->
        if b > c then
          solven a (pred b) (succ c) rest >>= fun ret ->
          return ("C"::ret)
        else
          solven a (succ b) (pred c) rest >>= fun ret ->
          return ("B"::ret)
      | "AC" ->
        if a > c then
          solven (pred a) b (succ c) rest >>= fun ret ->
          return ("C"::ret)
        else
          solven (succ a) b (pred c) rest >>= fun ret ->
          return ("A"::ret)
      | _ -> None
    end

let rec solve2 a b c = function
  | [] -> Some []
  | com :: rest ->
    begin
      match com with
      | "AB" ->
        if a = 1 && b = 1 && List.at_opt rest 0 |> Option.is_some then
          let next = List.at rest 0 in
          match next with
          | "AB" -> solve2 (pred a) (succ b) c rest >>= fun ret ->
            return ("B"::ret)
          | "AC" -> solve2 (succ a) (pred b) c rest >>= fun ret ->
            return ("A"::ret)
          | "BC" -> solve2 (pred a) (succ b) c rest >>= fun ret ->
            return ("B"::ret)
          | _ -> None
        else if a = 0 then
          solve2 (succ a) (pred b) c rest >>= fun ret ->
          return ("A" :: ret)
        else
          solve2 (pred a) (succ b) c rest >>= fun ret ->
          return ("B" :: ret)
      | "BC" ->
        if b = 1 && c= 1 && List.at_opt rest 0 |> Option.is_some then
          let next = List.at rest 0 in
          match next with
          | "AB" -> solve2 a (succ b) (pred c) rest >>= fun ret ->
            return ("B" :: ret)
          | "BC" -> solve2 a (pred b) (succ c) rest >>= fun ret ->
            return ("C"::ret)
          | "AC" -> solve2 a (pred b) (succ c) rest >>= fun ret ->
            return ("C"::ret)
          | _ -> None
        else if b = 0 then
          solve2 a (succ b) (pred c) rest >>= fun ret ->
          return ("B" :: ret)
        else
          solve2 a (pred b) (succ c) rest >>= fun ret ->
          return ("C" :: ret)
      | "AC" ->
        if c = 1 && a = 1 && List.at_opt rest 0 |> Option.is_some then
          let next = List.at rest 0 in
          match next with
          | "AB" -> solve2 (succ a) b (pred c) rest >>= fun ret ->
            return ("A" :: ret)
          | "BC" -> solve2 (pred a) b (succ c) rest >>= fun ret ->
            return ("C"::ret)
          | "AC" -> solve2 (succ a) b (pred c) rest >>= fun ret ->
            return ("A"::ret)
          | _ -> None
        else if c = 0 then
          solve2 (pred a) b (succ c) rest >>= fun ret ->
          return ("C" :: ret)
        else
          solve2 (succ a) b (pred c) rest >>= fun ret ->
          return ("A" :: ret)
      | _ -> None
    end

let () =
  (match List.first coms with
   | "AB" when a = 0 && b = 0 -> None
   | "BC" when b = 0 && c = 0 -> None
   | "AC" when a = 0 && c = 0 -> None
   | _ ->
     if a + b + c = 1 then
       solve1 a b c coms
     else if a + b + c = 2 then
       solve2 a b c coms
     else
       solven a b c coms)
  |> Option.map_default (fun ans -> "Yes\n" ^ String.join "\n" ans) "No"
  |> Printf.printf "%s\n"