PROGRAM MAX_SUM

DIM A%[11],B%[10],C%[4]

!$DYNAMIC
DIM P%[0]

PROCEDURE MAX_SUBSEQUENCE(P%[],N%->A$)
      LOCAL A%,B%,I%,J%,M%,S%
      A%=1
      FOR I%=0 TO N% DO
        S%=0
        FOR J%=I% TO N% DO
          S%+=P%[J%]
          IF S%>M% THEN
            M%=S%
            A%=I%
            B%=J%
          END IF
        END FOR
      END FOR
      IF A%>B% THEN A$="[]" EXIT PROCEDURE END IF
      A$="["
      FOR I%=A% TO B% DO
        A$+=STR$(P%[I%])+","
      END FOR
      A$=LEFT$(A$,LEN(A$)-1)+"]"
END PROCEDURE

PROCEDURE SHOW_ARRAY(P%[],N%->A$)
      LOCAL I%
      A$="["
      FOR I%=0 TO N% DO
        A$+=STR$(P%[I%])+","
      END FOR
      A$=LEFT$(A$,LEN(A$)-1)+"]"
END PROCEDURE

BEGIN

   A%[]=(0,1,2,-3,3,-1,0,-4,0,-1,-4,2)
   N%=UBOUND(A%,1)
   !$DIM P%[N%]
   SHOW_ARRAY(A%[],N%->A$)
   PRINT(A$;" -> ";)
   MAX_SUBSEQUENCE(A%[],N%->A$)
   PRINT(A$)
   !$ERASE P%

   B%[]=(-1,-2,3,5,6,-2,-1,4,-4,2,-1)
   N%=UBOUND(B%,1)
   !$DIM P%[N%]
   SHOW_ARRAY(B%[],N%->A$)
   PRINT(A$;" -> ";)
   MAX_SUBSEQUENCE(B%[],N%->A$)
   PRINT(A$)
   !$ERASE P%

   C%[]=(-1,-2,-3,-4,-5)
   N%=UBOUND(C%,1)
   !$DIM P%[N%]
   SHOW_ARRAY(C%[],N%->A$)
   PRINT(A$;" -> ";)
   MAX_SUBSEQUENCE(C%[],N%->A$)
   PRINT(A$)
   !$ERASE P%
END PROGRAM
