program ioi94day1prb1ver3(input, output, inp, out) ; { Tom Verhoeff, Eindhoven University of Technology } { Non-recursive solution } { General Section } const Test = true ; var inp, out: text ; procedure Init ; begin if Test then writeln('IOI''94 - Day 1 - Problem 1: The Triangle') ; assign(inp, 'input.txt') ; reset(inp) ; assign(out, 'output.txt') ; rewrite(out) ; if Test then writeln('Initialized') end { Init } ; procedure Fini ; begin close(inp) ; close(out) end { Fini } ; { Problem Specific Section } const MaxN = 100 ; type index = 1..MaxN ; var N: index ; { number of rows } T: array [index, index] of integer ; { first, T[r, p] is the number in row r at position p } { at the end, T[r, p] is the maximum sum for down routes from T[r, p] } m: integer ; { maximum route sum } procedure ReadInput ; { read N and T[r, p] with 1 <= r <= N and 1 <= p <= r } var r, p: index ; begin readln(inp, N) ; if Test then writeln('Number of rows is ', N:1) ; for r := 1 to N do begin for p := 1 to r do read(inp, T[r, p]) ; readln(inp) end { for r } ; if Test then writeln('All input read') end { ReadInput } ; procedure ComputeAnswer ; { m := the maximum route sum } function Max(x, y: integer): integer ; begin if x > y then Max := x else Max := y end { Max } ; var r, p: index ; begin for r := N-1 downto 1 do for p := 1 to r do T[r, p] := T[r, p] + Max(T[r+1, p], T[r+1, p+1]) ; m := T[1, 1] end { ComputeAnswer } ; procedure WriteOutput ; begin writeln(out, m:1) ; if Test then writeln('The maximum route sum is ', m:1) end { WriteOutput } ; begin Init ; ReadInput ; ComputeAnswer ; WriteOutput ; Fini end.