PROGRAM-ID.                      ABC_099_D.
DATA                             DIVISION.
WORKING-STORAGE                  SECTION.
    01 MAX_SUM PIC 9(10) VALUE 9999999999.

    01 INP1    PIC X(6).
    01 N       PIC 9(3).
    01 COLOR   PIC 9(2).

    01 INP2    PIC X(1500).
    01 maxlen  PIC 9(3).
    01 cur     PIC 9(4).
    01 i       PIC 9(18).
    01 j       PIC 9(18).
    01 k       PIC 9(18).
    01 m       PIC 9(18).
    01 len     PIC 9(4).

    01 ki      PIC 9(18).

    01 tmp     PIC 9(2).
    01 x       PIC 9(4).
    01 r       PIC 9(1).

    01 sm      PIC 9(10).
    01 res     PIC 9(10).

    01 ans     PIC X(10).

    01 D1.
        03 D11 OCCURS 30.
            05 D PIC 9(4) OCCURS 30.

    01 c1.
        03 c11 OCCURS 3.
            05 c PIC 9(10) OCCURS 30 VALUE ZERO.

    01 ZS      PIC Z(10)9.
    01 DUMMY   PIC X(1).


PROCEDURE                        DIVISION.
    ACCEPT INP1.

    UNSTRING INP1
    DELIMITED BY SPACE
    INTO N COLOR.

    MOVE COLOR TO maxlen.

    PERFORM VARYING k FROM 1 BY 1 UNTIL COLOR < k
        ACCEPT INP2

        MOVE 1 TO cur
        MOVE 1 TO i

        PERFORM maxlen TIMES

            PERFORM VARYING j FROM cur BY 1
                UNTIL INP2(j:1) = SPACE
            END-PERFORM

            COMPUTE len = j - cur

            MOVE INP2(cur:len) TO D(k i)

            COMPUTE cur = j + 1

            ADD 1 TO i

        END-PERFORM

    END-PERFORM.

    MOVE N TO maxlen.

    PERFORM VARYING k FROM 1 BY 1 UNTIL N < k
        ACCEPT INP2

        MOVE 1 TO cur
        MOVE 1 TO i

        PERFORM maxlen TIMES

            PERFORM VARYING j FROM cur BY 1
                UNTIL INP2(j:1) = SPACE
            END-PERFORM

            COMPUTE len = j - cur

            MOVE INP2(cur:len) TO tmp

            ADD i TO k GIVING ki

            DIVIDE 3 INTO ki GIVING x REMAINDER r

            ADD 1 TO r

            ADD 1 TO c(r tmp)

            COMPUTE cur = j + 1

            ADD 1 TO i

        END-PERFORM

    END-PERFORM.

    MOVE MAX_SUM TO res.

    PERFORM VARYING i FROM 1 BY 1 UNTIL COLOR < i
        PERFORM VARYING j FROM 1 BY 1 UNTIL COLOR < j
            IF i NOT = j
                PERFORM VARYING k FROM 1 BY 1 UNTIL COLOR < k
                    IF i NOT = k AND j NOT = k
                        MOVE 0 TO sm
                        PERFORM VARYING m FROM 1 BY 1 UNTIL COLOR < m
                            COMPUTE sm = sm + D(m i) * c(1 m)
                        END-PERFORM
                        PERFORM VARYING m FROM 1 BY 1 UNTIL COLOR < m
                            COMPUTE sm = sm + D(m j) * c(2 m)
                        END-PERFORM
                        PERFORM VARYING m FROm 1 BY 1 UNTIL COLOR < m
                            COMPUTE sm = sm + D(m k) * C(3 m)
                        END-PERFORM
                        IF sm < res
                            MOVE sm TO res
                        END-IF
                    END-IF
                END-PERFORM
            END-IF
        END-PERFORM
    END-PERFORM.

    MOVE res TO ZS.

    PERFORM UNANS.

    DISPLAY ans(1:FUNCTION STORED-CHAR-LENGTH(ans)).
    STOP RUN.

UNANS                            SECTION.
    UNSTRING
        ZS DELIMITED BY ALL SPACE
        INTO DUMMY ans
    END-UNSTRING.
