program cojs1443;
const
inf=1 shl 60;
var
flag:boolean;
ans,t,m:int64;
i,n,p:longint;
a:array[1..1000010]of int64;
begin
assign(input,'number2013.in');reset(input);
assign(output,'number2013.out');rewrite(output);
readln(n,p);
for i:=1 to n do
read(a[i]);
m:=-inf;
for i:=1 to n do
begin
inc(t,a[i]);
if t>m then m:=t;
if t<0 then t:=0;
a[i]:=m;
end;
m:=a[1];
ans:=2*m;
for i:=2 to n-1 do
begin
t:=ans;
if a[i]>0 then
begin
ans:=(t mod p+a[i] mod p) mod p;
if a[i]>abs(m) then flag:=true;
end;
end;
if (not flag)and(ans<m) then ans:=m;
writeln(ans);
close(input);close(output);
end.