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 D          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 D-TBL.
   03 PD      PIC  9(10) OCCURS 300000 DEPENDING ON D.
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 CNT        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.
*>
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > X
    MOVE PA(I) TO PD(I)
  END-PERFORM.
  PERFORM VARYING J FROM 1 BY 1 UNTIL J > Y
    MOVE PB(J) TO PD(I)
    ADD 1 TO I
  END-PERFORM.
  PERFORM VARYING K FROM 1 BY 1 UNTIL K > C
    MOVE PC(K) TO PD(I)
    ADD 1 TO I
  END-PERFORM.
*>
  COMPUTE D = X + Y + C.
  SORT PD DESCENDING KEY PD.
*>
  MOVE 0 TO CNT.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > X + Y
    ADD PD(I) TO CNT
  END-PERFORM.
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
