C11PINES - Growing Pines
Tác giả: ladpro98
Ngôn ngữ: Pascal
program c11pines;
uses math;
const fi='';
maxN=12345;
var a:array[1..maxN] of longint;
n,d:longint;
res:int64;
procedure input;
var inp:text;
i:longint;
begin
assign(inp,fi);
reset(inp);
readln(inp,n,d);
for i:=1 to n do
read(inp,a[i]);
close(inp);
end;
procedure swap(i,j:longint);
var t:longint;
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
end;
procedure sort(l,r:longint);
var i,j,p:longint;
begin
if l>=r then exit;
p:=a[random(r-l+1)+l];
i:=l;j:=r;
repeat
while a[i]<p do inc(i);
while a[j]>p do dec(j);
if i<=j then
begin
if i<j then swap(i,j);
inc(i);dec(j);
end;
until i>j;
sort(l,j);sort(i,r);
end;
procedure process;
var i,modul:longint;
begin
modul:=a[1] mod d;
for i:=2 to n do
if a[i] mod d<>modul then
begin
res:=-1;
exit;
end;
res:=0;
for i:=2 to n do
res:=res+(a[i]-a[1]) div d;
end;
begin
input;
sort(1,n);
process;
write(res);
end.