program ioi94day1prb1ver1(input, output, inp, out) ; { Tom Verhoeff, Eindhoven University of Technology } { Inefficient 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 ; number = 0..99 ; var N: index ; { number of rows } T: array [index, index] of number ; { T[r, p] is the number in row r at position 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 } ; function MaxRS(r, p: index): integer ; { return maximum sum over all down routes starting in T[r, p] } begin if r = N then MaxRS := T[r, p] else MaxRS := T[r, p] + Max(MaxRS(r+1, p), MaxRS(r+1, p+1)) end { MaxRS } ; begin m := MaxRS(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.