IDENTIFICATION DIVISION.
PROGRAM-ID. 160E.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT STDIO ASSIGN TO KEYBOARD. 
*>
DATA DIVISION.
FILE SECTION.
FD STDIO.
01 INP        PIC X(1100000).
*>
WORKING-STORAGE SECTION.
01 S          PIC  X(100).
01 X          PIC  9(6).
01 Y          PIC  9(6).
01 A          PIC  9(6).
01 B          PIC  9(6).
01 C          PIC  9(6).
01 A-TBL.
   03 PA      PIC  9(10) OCCURS 100000 DEPENDING ON A.
01 B-TBL.
   03 PB      PIC  9(10) OCCURS 100000 DEPENDING ON B.
01 C-TBL.
   03 PC      PIC  9(10) OCCURS 100000 DEPENDING ON C.
01 P1         PIC  9(7) COMP.
01 P2         PIC  9(7) COMP.
01 I          PIC  9(6) COMP.
01 J          PIC  9(6) COMP.
01 K          PIC  9(6) COMP.
01 PX         PIC  9(6).
01 PY         PIC  9(6).
01 PX1        PIC  9(6).
01 PY1        PIC  9(6).
01 I1         PIC  9(6) COMP.
01 J1         PIC  9(6) COMP.
01 CNT        PIC  9(16) COMP.
01 CNT-X1     PIC  9(16) COMP.
01 CNT-Y1     PIC  9(16) COMP.
01 OUT        PIC  Z(15)9.
*> 
PROCEDURE DIVISION.
  ACCEPT S.
  UNSTRING S DELIMITED BY ' '
      INTO X Y A B C.
*>
  OPEN INPUT STDIO.
  READ STDIO.
  MOVE 1 TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > A
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO PA(I)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  READ STDIO.
  MOVE 1 TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > B
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO PB(I)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  READ STDIO.
  MOVE 1 TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > C
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO PC(I)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  CLOSE STDIO.
*>
  SORT PA DESCENDING KEY PA.
  SORT PB DESCENDING KEY PB.
  SORT PC DESCENDING KEY PC.
*>
  MOVE 1 TO I J K.
  MOVE 0 TO PX PY CNT.
  PERFORM UNTIL PX >= X AND PY >= Y
    EVALUATE TRUE
      WHEN PX < X AND PA(I) >= PC(K)
        ADD PA(I) TO CNT
        ADD 1     TO I PX
      WHEN PY < Y AND PB(J) >= PC(K)
        ADD PB(J) TO CNT
        ADD 1     TO J PY
      WHEN PX < X AND PY < Y
        PERFORM X-CNT1
        PERFORM Y-CNT1
        ADD PC(K) TO CNT
        IF (CNT-X1 >= CNT-Y1)
          ADD 1     TO K PX
        ELSE
          ADD 1     TO K PY
        END-IF
      WHEN PX < X
        ADD PC(K) TO CNT
        ADD 1     TO K PX
      WHEN PY < Y
        ADD PC(K) TO CNT
        ADD 1     TO K PY
    END-EVALUATE
  END-PERFORM.
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
*>
X-CNT1 SECTION.
  MOVE 0 TO CNT-X1.
  MOVE I TO I1
  COMPUTE PX1 = PX + 1
  PERFORM UNTIL  PX1 > X OR PA(I1) < PC(K)
    ADD PA(I1) TO CNT-X1
    ADD 1 TO I1 PX1
  END-PERFORM.
  EXIT.
*>
Y-CNT1 SECTION.
  MOVE 0 TO CNT-Y1.
  MOVE J  TO J1 
  COMPUTE PY1 = PY + 1
  PERFORM UNTIL  PY1 > Y OR PB(J1) < PC(K)
    ADD PB(J1) TO CNT-Y1
    ADD 1 TO J1 PY1
  END-PERFORM.
  EXIT.
