IDENTIFICATION DIVISION.
PROGRAM-ID. 101C.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT STDIO ASSIGN TO KEYBOARD. 
*>
DATA DIVISION.
FILE SECTION.
FD STDIO.
01 INP     PIC X(1000000).
*>
WORKING-STORAGE SECTION.
01 INW     PIC  X(15).
01 N       PIC  9(6) COMP.
01 K       PIC  9(6) COMP.
01 TBL.
   03 X    PIC S9(9) COMP OCCURS 100000. 
01 XW      PIC  9(9) COMP.
01 MN      PIC  9(9) COMP.
01 P1      PIC  9(7) COMP.
01 P2      PIC  9(7) COMP.
01 I       PIC  9(6) COMP.
01 I1      PIC S9(6) COMP.
01 I2      PIC S9(6) COMP.
01 DS      PIC  9(6) COMP.
01 MX      PIC  9(6) COMP.
01 P-CNT   PIC  9(6) COMP.
01 M-CNT   PIC  9(6) COMP.
01 CNT     PIC  9(9) COMP.
01 OUT     PIC  Z(8)9.
*>
PROCEDURE DIVISION.
  ACCEPT INW.
  UNSTRING INW DELIMITED BY ' '
      INTO N K. 
*>
  OPEN INPUT STDIO.
  READ STDIO.
  CLOSE STDIO.
*>
  MOVE 0 TO P-CNT M-CNT.  
  MOVE 999999999 TO MN.     
  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 X(I) XW
    IF (XW < MN)
      MOVE XW TO MN
      MOVE I  TO MX
    END-IF
*>
    EVALUATE TRUE
      WHEN X(I) < 0
        ADD 1 TO M-CNT
      WHEN X(I) > 0
        ADD 1 TO P-CNT
    END-EVALUATE
*>
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>---
  IF (M-CNT >= K)
    IF (X(MX) = 0)
      COMPUTE I1 = M-CNT - K + 2
    ELSE  
      COMPUTE I1 = M-CNT - K + 1
    END-IF  
  ELSE
    COMPUTE I1 = 1
  END-IF.
  IF (P-CNT >= K)
    COMPUTE I2 = M-CNT + K
  ELSE
    COMPUTE I2 = N
  END-IF. 
*>
  MOVE 999999999 TO CNT.
  PERFORM VARYING I FROM I1 BY 1 UNTIL K > I2 - I + 1
    EVALUATE TRUE
      WHEN (X(I) < 0) AND (X(I + K - 1) > 0)
        IF (FUNCTION ABS(X(I)) < X(I + K - 1))
          COMPUTE DS = FUNCTION ABS(X(I)) * 2 + X(I + K - 1)
        ELSE
          COMPUTE DS = FUNCTION ABS(X(I)) + X(I + K - 1) * 2
        END-IF
      WHEN (X(I) < 0)
        MOVE X(I)         TO DS
      WHEN (X(I) >= 0)
        MOVE X(I + K - 1) TO DS
    END-EVALUATE
  *> 
    IF (DS < CNT)
      MOVE DS TO CNT
    END-IF
  END-PERFORM.
*>---
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
