IDENTIFICATION DIVISION.
PROGRAM-ID. 171B.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP        PIC X(5000).
01 N          PIC 9(04).
01 K          PIC 9(04).
01 P1         PIC 9(08) COMP.
01 P2         PIC 9(08) COMP.
01 TBL.
   03 A       PIC 9(04) OCCURS 1000 DEPENDING ON N.
01 I          PIC 9(08) COMP.
01 CNT        PIC 9(09) COMP.
01 OUT        PIC Z(7)9.
*>
PROCEDURE DIVISION.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ' '
      INTO N K.
*>
  ACCEPT INP.
  MOVE 1 TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO A(I)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  SORT A ASCENDING KEY A.
  MOVE 0 TO CNT.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > K
    ADD A(I) TO CNT
  END-PERFORM.
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
