program ioi94day1prb1ver4(input, output, inp, out) ; { Tom Verhoeff, Eindhoven University of Technology } { Non-recursive solution without storing all input data first } { 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 } A: array [index] of integer ; m: integer ; { maximum route sum } function Max(x, y: integer): integer ; begin if x > y then Max := x else Max := y end { Max } ; procedure ReadInput ; { read N and T[r, p] with 1 <= r <= N and 1 <= p <= r } { process T-values on-the-fly, producing A[p] = MRS[N, p] for 1 <= p <= N } var r, p: index ; t: 0..99 ; v, w: integer ; begin readln(inp, N) ; if Test then writeln('Number of rows is ', N:1) ; for p := 0 to N do A[p] := 0 ; for r := 1 to N do begin v := 0 ; for p := 1 to r do begin read(inp, t) ; { t = T[r, p] } w := A[p] ; { v = MRS[r-1, p-1], w = MRS[r-1, p] } A[p] := t + Max(v, w) ; v := w end { for p } ; readln(inp) end { for r } ; if Test then writeln('All input read') end { ReadInput } ; procedure ComputeAnswer ; { m := the maximum route sum } var p: index ; begin m := 0 ; for p := 1 to N do m := Max(m, A[p]) ; 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.