IDENTIFICATION DIVISION.
PROGRAM-ID. 140B.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP  PIC X(600).
01 N    PIC 9(2).
01 TBL.
   03 A PIC 9(2) OCCURS 20.
   03 B PIC 9(2) OCCURS 20.
   03 C PIC 9(2) OCCURS 20.
01 M    PIC 9(4).
01 I    PIC 9(2).
01 P1   PIC 9(2).
01 P2   PIC 9(2).
01 OUT  PIC ZZZ9.
*>  
PROCEDURE DIVISION.
  ACCEPT N.
  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.
*>
  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 B(I)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  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 C(I)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  MOVE 0 TO M.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I >= N
    ADD B(A(I)) TO M
    IF (A(I) + 1 = A(I + 1))
      ADD C(A(I)) TO M
    END-IF
  END-PERFORM.
  ADD B(A(I)) TO M.
*>
  MOVE M TO OUT.
  DISPLAY OUT.
*>
  STOP RUN.
