Cod sursa(job #36353)

Utilizator ionescu_bogdanIonescu Bogdan-Gabriel ionescu_bogdan Data 23 martie 2007 14:05:28
Problema Oo Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.69 kb
Program Rabit_Family;
Const   MaxSize = 100000;
Type
    IntMat = Array[0..MaxSize, 0..MaxSize] of integer;
    IntAr = Array[1..MaxSize] of byte;

Var
   Mat : IntMat;        {Matrix to apply the dynamic programming algorithm}
   Ar  : IntAr;         {Vector to keep the number of the carrots in the rooms}
   N   : Byte;          {Number of the rooms}
{------------------------------------------------------------}
Procedure ReadFile(Var Ar:IntAr; Var N:Byte);
Var
   InFile : Text;
   i:Byte;
Begin
     Assign(InFile,'oo.in');
     Reset(InFile);
     Readln(InFile,N);
     For i:=1 to N Do
         Read(InFile,Ar[i]);
     Close(InFile);
End;
{------------------------------------------------------------}
Procedure WriteResult(Var Mat:IntMat; N:Byte);
Var
   OutFile : Text;
   Res:integer;
   i:Byte;
Begin
     Assign(Outfile,'oo.out');
     ReWrite(OutFile);
     Res := 0;
     For i:=1 to N Do
         If Mat[N,i] > Res Then
            Res := Mat[N,i];
     Write(OutFile,Res);
     Close(OutFile);
End;
{------------------------------------------------------------}
Procedure InitSecondCol(Var Mat:IntMat; Var Ar:IntAr; N:Byte);
Var
   i:Byte;
Begin
     For i:=1 to N-1 Do
         Mat [i,2] := Ar[i] + Ar[i+1];
     Mat[N,2] := Ar[N] + Ar[1];
End;
{------------------------------------------------------------}
Procedure CalcSum(Var Mat:IntMat; a,b,N:integer; Var MaxSum:integer);
Var
   i,TempSum:integer;
   Start,Len:integer;
Begin
     MaxSum := 0;
     For i:=1 to b-2 Do
     Begin
          Start := (a+i+1);
          if Start > N then Start := Start mod N;
          Len := b-(i+1);
          if (a - (Start + Len)) <= 1 then Len := Len - 1;
          TempSum := Mat[a,i] + Mat[Start,Len];
          If TempSum > MaxSum then MaxSum := TempSum;
     End;
End;
{------------------------------------------------------------}
Procedure Go(Var Mat:IntMat; Var Ar:IntAr; N:Byte);
Var
   a,b : Byte;
   MaxSum : Integer;
Begin
     InitSecondCol(Mat,Ar,N);
     For b:=3 to N Do
     For a:=1 to N Do
     Begin
          Mat[a,b] := Mat[a,b-1];
          If (a<N) Then
          Begin
               If Mat[a+1,b-1] > Mat[a,b] Then
                  Mat[a,b] := Mat[a+1,b-1];
          End
          Else If Mat[1,b-1] > Mat[a,b] then
               Mat[a,b] := Mat[1,b-1];
          CalcSum(Mat,a,b,N,MaxSum);
          If MaxSum > Mat[a,b] then
             Mat[a,b] := MaxSum;
     End;
End;
{------------------------------------------------------------}
Begin
     ReadFile(Ar,N);
     FillChar(Mat, SizeOf(Mat),0);
     Go(Mat,Ar,N);
     WriteResult(Mat,N);
end.
{------------------------------------------------------------}