IDENTIFICATION DIVISION.
PROGRAM-ID. 129B.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP   PIC X(400).
01 N     PIC 9(3).
01 W     PIC 9(3).
01 WA    PIC 9(5).
01 HF    PIC 9(4)V9.
01 TBL.
   03 TW PIC 9(3) OCCURS 100.
01 I     PIC 9(3).
01 S1    PIC 9(5).
01 S2    PIC 9(5).
01 P1    PIC 9(3).
01 P2    PIC 9(3). 
01 OUT   PIC ZZZ9.
*>
PROCEDURE DIVISION.
  ACCEPT N.
*>
  ACCEPT INP.
  MOVE 0 TO WA.
  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 W
    COMPUTE WA = WA + W
    MOVE WA TO TW(I)
*> 
    COMPUTE P1 = P2 + 1
  END-PERFORM.  
*>
  COMPUTE HF = WA / 2.
  PERFORM VARYING I FROM 1 BY 1 
     UNTIL (I > N) OR (TW(I) >= HF)
  END-PERFORM. 
*>
  IF (TW(I) = HF)
    DISPLAY '0'
  ELSE
    COMPUTE S1 = WA - TW(I) * 2
    IF (I > 1)
      COMPUTE S2 = WA - TW(I - 1) * 2
    ELSE
      COMPUTE S2 = WA - TW(I) * 2
    END-IF
*>
    IF (S1 < S2)
      MOVE S1 TO OUT
    ELSE
      MOVE S2 TO OUT
    END-IF
    DISPLAY FUNCTION TRIM(OUT)
  END-IF.  
*>
  STOP RUN.
