{$MODE OBJFPC}
{$R-,Q-,S-,I+}
{$OPTIMIZATION LEVEL2}
program IntervalCover;
uses Windows, SysUtils, Math;
const
  prefix = 'CARGROUP';
  InputFile  = prefix + '.INP';
  OutputFile = prefix + '.OUT';
  AnswerFile = prefix + '.OUT';
var
  dirT, dirC: WideString;
  fi, fo, fa: TextFile;
  error,cGroup,n,P,L:longint;
  Maxreal:real;
  group,w,EndofPrevGroup:array[0..1004] of longint;
  T,F:array[1..1004] of real;
  res,ans:real;

procedure GenErr(const s: string; const param: array of const);
begin
  raise Exception.CreateFmt(s, param);
end;

procedure ReadDirs;
var
  s: AnsiString;
begin
  ReadLn(s); dirT := Utf8Decode(s);
  ReadLn(s); dirC := Utf8Decode(s);
end;

procedure OpenFiles;
var
  CurrentDir: array[0..Max_Path + 1] of WideChar;
begin
  GetCurrentDirectoryW(Max_Path, CurrentDir);
  SetCurrentDirectoryW(PWideChar(dirT));
  AssignFile(fi, InputFile); Reset(fi);
  AssignFile(fa, AnswerFile); Reset(fa);
  SetCurrentDirectoryW(CurrentDir);
  SetCurrentDirectoryW(PWideChar(dirC));
  AssignFile(fo, OutputFile); Reset(fo);
end;

procedure CloseFiles;
begin
  CloseFile(fi);
  CloseFile(fa);
  CloseFile(fo);
end;

procedure Enter;
var L, i, v: Integer;
begin
  Readln(fi, n, P, L);
  for i := 1 to n do
    begin
      Readln(fi, w[i], v);
      t[i] := L / v;
    end;
end;

procedure Optimize;
var
  i, j, k, TotalW: Integer;
  MaxTime: Real;
begin
  F[0] := 0;
  for i := 1 to n do
    begin
      F[i] := maxReal; TotalW := 0; MaxTime := 0;
      for j := i downto 1 do
        begin
          TotalW := TotalW + W[j];
          if TotalW > P then Break;
          if MaxTime < T[j] then MaxTime := T[j];
          if F[i] > F[j - 1] + MaxTime then
            begin
              F[i] := F[j - 1] + MaxTime;
              EndofPrevGroup[i] := j - 1;
            end;
        end;
    end;
end;

function Check_Group:boolean;
var     i,j,sumw:longint;
        maxtime,sumtime:real;
begin
        Group[0] := 0;
        sumtime := 0;
        for i:=1 to cGroup do
        begin
                sumw := 0;
                maxtime := 0;
                for j:=Group[i-1] + 1 to Group[i] do
                begin
                        sumw := sumw + w[j];
                        if maxtime < t[j] then maxtime := t[j];
                end;
                if sumw > P then
                begin
                        error := 1; //tong trong luong > P
                        exit(false);
                end;
                sumtime := sumtime + maxtime;
        end;
        if (abs(sumtime - ans) > 0.01) then
        begin
                error := 2;//khong khop ket qua
                exit(false);
        end;
        exit(true);
end;

procedure Check;
begin
 readln(fa, ans);
 readln(fo, res);
 while (not(seekeoln(fo))) do
 begin
        inc(cGroup);
        read(fo,group[cGroup]);
 end;
 if trunc(res - ans) > 1 then
  begin
    writeln('Ket qua khong toi uu!');
    writeln(' Ket qua cua DA : ', ans : 0 : 2);
    writeln(' Ket qua cua HS : ', res : 0 : 2);
    write(0);
    exit;
  end;
 if (not(check_group)) then
 begin
        writeln('Chia nhom khong dung!');
        if (error = 1) then
           writeln('Tong trong luong cua mot nhom > suc chiu dung cua cau!')
        else writeln('Tong thoi gian cac nhom khong khop voi ket qua tim duoc!');
        write(0);
        exit;
 end;
 writeln('KET QUA DUNG!');
 write(1);
end;

procedure DoCheck;
begin
        enter;
        //Optimize;
        Check;
end;

begin
  try
    try
      ReadDirs;
      OpenFiles;
      DoCheck;
    finally
      CloseFiles;
    end;
  except
    on E: Exception do
      begin
        WriteLn(E.Message);
        WriteLn('0.0');
      end;
  end;
end.

chuyển sang C++