LINEGAME - VOI09 Trò chơi với băng số

Tác giả: ladpro98

Ngôn ngữ: Pascal


program linegame;
uses    math;

const   le=1;
        chan=2;
        fi='';
var     f:array[le..chan,0..1000001] of int64;
        a:array[0..1000001] of longint;

        n:longint;


procedure input;
var     inp:text;
        i:longint;
begin
        assign(inp,fi);
        reset(inp);
        readln(inp,n);
        for i:=1 to n do
        read(inp,a[i]);
        close(inp);
end;

procedure dp;
var     i,j:longint;
begin
        for j:=1 to n do
        for i:=le to chan do
        begin
                if i=le then
                f[i,j]:=max(f[chan,j-1]+a[j],f[le,j-1])
                else
                f[i,j]:=max(f[le,j-1]-a[j],f[chan,j-1]);
        end;
end;

begin
        input;
        dp;
        write(max(f[chan,n],f[le,n]));
end.

Download