IDENTIFICATION DIVISION.
PROGRAM-ID. 167C.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP        PIC X(100).
01 N          PIC 9(02).
01 M          PIC 9(02).
01 X          PIC 9(06).
01 TBL1.
   03 TB      OCCURS 12.
      05 C    PIC 9(06).
      05 A    PIC 9(06) OCCURS 12.
01 TBL2.
   03 AW      PIC 9(07) OCCURS 12.
01 CW         PIC 9(07).
01 TBL3.
   03 CX      PIC 9(07) OCCURS 4096 DEPENDING ON L.
01 L          PIC 9(04) COMP. 
01 SHO        PIC 9(04) COMP. 
01 AMR        PIC 9(01) COMP. 
01 IDX        PIC 9(02).
01 I          PIC 9(04) COMP.
01 J          PIC 9(02).
01 P1         PIC 9(03) COMP.
01 P2         PIC 9(03) COMP.
01 FLG        PIC X(01).
01 CNT        PIC S9(10).
01 OUT        PIC -(10)9.
*>
PROCEDURE DIVISION.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ' '
      INTO N M X .
*>
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    ACCEPT INP
    MOVE 1 TO P1 
    PERFORM VARYING J FROM 1 BY 1 UNTIL J > M + 1
      PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
      END-PERFORM
      IF (J = 1)
        MOVE INP(P1:P2 - P1) TO C(I)
      ELSE
        MOVE INP(P1:P2 - P1) TO A(I J - 1)
      END-IF
      COMPUTE P1 = P2 + 1
    END-PERFORM
  END-PERFORM.
*>
  MOVE 0 TO L
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > 2 ** N - 1
    MOVE ZERO TO TBL2 CW
    MOVE 1 TO IDX
    MOVE I TO SHO
    DIVIDE SHO BY 2 GIVING SHO REMAINDER AMR
    PERFORM UNTIL SHO = 0 AND AMR = 0
      IF (AMR = 1)
        ADD C(IDX)   TO CW
      END-IF
      PERFORM VARYING J FROM 1 BY 1 UNTIL J > M OR AMR = 0
        ADD A(IDX J) TO AW(J)
      END-PERFORM
*>
      DIVIDE SHO BY 2 GIVING SHO REMAINDER AMR
      ADD 1 TO IDX
    END-PERFORM
*>
    PERFORM VARYING J FROM 1 BY 1 UNTIL J > M OR AW(J) < X
    END-PERFORM
*>
    IF (J > M)
      ADD  1  TO L
      MOVE CW TO CX(L)
    END-IF
  END-PERFORM.
*>
  SORT CX ASCENDING KEY CX.
*>
  IF (L = 0)
    MOVE -1 TO OUT
  ELSE
    MOVE CX(1) TO OUT
  END-IF.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
