Skip to content

Instantly share code, notes, and snippets.

@ileasile
Last active January 12, 2020 15:18
Show Gist options
  • Save ileasile/b5c668e7df36a133432a651eed25f65e to your computer and use it in GitHub Desktop.
Save ileasile/b5c668e7df36a133432a651eed25f65e to your computer and use it in GitHub Desktop.
procedure maxi(var a: integer; b: integer);
begin
if a < b then
a := b
end;
procedure readNext(var s: string; var i: integer; var n: integer);
const
ord0: integer = 48;
begin
var c: char := s[i];
var minus: integer := 1;
var ls: integer := length(s);
if c = '-' then
begin
minus := -1;
i := i + 1;
end;
n := 0;
while i <= ls do
begin
c := s[i];
if c = ' ' then
break;
n := n * 10 + (ord(c) - ord0);
i := i + 1;
end;
i := i + 1;
n := n * minus;
end;
begin
var str: string;
var b, c: array [0..1999] of shortint;
var r, n, x, bj, bp, ap, app, s1, s2: integer;
ReadLn(n);
r := -310;
n := n - 1;
for var i := 0 to n do
begin
ReadLn(str);
var nextInd: integer = 1;
for var j := 0 to n do
begin
ReadNext(str, nextInd, x);
bp := bj;
bj := b[j];
s1 := x + ap;
s2 := x + bj;
if i > 0 then
begin
if j > 0 then
begin
maxi(r, s1 + max(bj, bp));
maxi(r, s2 + bp);
end;
if j < n then
maxi(r, s2 + b[j + 1]);
if i > 1 then
maxi(r, s2 + c[j]);
end;
if j > 1 then
begin
maxi(r, s1 + app);
end;
app := ap;
ap := x;
c[j] := bj;
b[j] := x;
end;
end;
Write(r);
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment