IDENTIFICATION DIVISION.
PROGRAM-ID. 164C.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT STDIO ASSIGN TO KEYBOARD. 
*>
DATA DIVISION.
FILE SECTION.
FD STDIO.
01 INP        PIC X(2200000).
*>
WORKING-STORAGE SECTION.
01 N          PIC  9(06) COMP.
01 TBL1.
   03 A       PIC  9(10) COMP OCCURS 200000.
01 TBL2.
   03 SA      PIC  9(9) OCCURS 200000.
01 P1         PIC  9(7) COMP.
01 P2         PIC  9(7) COMP.
01 I          PIC  9(6) COMP.
01 CNT        PIC  9(18) COMP.
01 OUT        PIC  Z(17)9.
*> 
PROCEDURE DIVISION.
  ACCEPT N.
*>
  OPEN INPUT STDIO.
  READ STDIO.
  CLOSE STDIO.
*>
  MOVE ZERO TO TBL2.
  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)
    IF (I > A(I))
      ADD 1 TO SA(I - A(I))
    END-IF
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  MOVE 0 TO CNT.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N - 2
    IF (I + A(I) <= N)
      COMPUTE CNT = CNT + SA(I + A(I))
    END-IF
  END-PERFORM.
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
