rmq:
var a,p,q:array [0..300000] of longint; { p 递增队列, q 递减队列 }
n,m,x,i,j,k,h,t,max,min:longint;
begin
assign(input,'Rmq.in'); reset(input); assign(output,'Rmq.out'); rewrite(output);
readln(n,m);
for k:=1 to n do read(a[k]);
fillchar(p,sizeof(p),0); p[1]:=1; h:=1; t:=1; max:=0; { 初始化递减队列与指针,h 队首,t 队尾 }
fillchar(q,sizeof(q),0); q[1]:=1; i:=1; j:=1; min:=0; { 初始化递增队列与指针,i 队首,t 队尾 }
for k:=2 to m do
begin
while (h<=t) and (a[k]>a[p[t]]) do dec(t);
inc(t);
p[t]:=k;
while (i<=j) and (a[k]<a[q[j]]) do dec(j);
inc(j);
q[j]:=k;
end;
for k:=1 to n-m do
begin
inc(max,a[p[h]]);
x:=a[k+m];
while (h<=t) and (x>a[p[t]]) do dec(t);
inc(t);
p[t]:=k+m;
if k=p[h] then inc(h);
inc(min,a[q[i]]);
x:=a[k+m];
while (i<=j) and (x<a[q[j]]) do dec(j);
inc(j);
q[j]:=k+m;
if k=q[i] then inc(i);
end;
for k:=n-m+1 to n do
begin
inc(max,a[p[h]]);
if k=p[h] then inc(h);
inc(min,a[q[i]]);
if k=q[i] then inc(i);
end;
writeln(max);
writeln(min);
close(input); close(output);
end.
var a,p,q:array [0..300000] of longint; { p 递增队列, q 递减队列 }
n,m,x,i,j,k,h,t,max,min:longint;
begin
assign(input,'Rmq.in'); reset(input); assign(output,'Rmq.out'); rewrite(output);
readln(n,m);
for k:=1 to n do read(a[k]);
fillchar(p,sizeof(p),0); p[1]:=1; h:=1; t:=1; max:=0; { 初始化递减队列与指针,h 队首,t 队尾 }
fillchar(q,sizeof(q),0); q[1]:=1; i:=1; j:=1; min:=0; { 初始化递增队列与指针,i 队首,t 队尾 }
for k:=2 to m do
begin
while (h<=t) and (a[k]>a[p[t]]) do dec(t);
inc(t);
p[t]:=k;
while (i<=j) and (a[k]<a[q[j]]) do dec(j);
inc(j);
q[j]:=k;
end;
for k:=1 to n-m do
begin
inc(max,a[p[h]]);
x:=a[k+m];
while (h<=t) and (x>a[p[t]]) do dec(t);
inc(t);
p[t]:=k+m;
if k=p[h] then inc(h);
inc(min,a[q[i]]);
x:=a[k+m];
while (i<=j) and (x<a[q[j]]) do dec(j);
inc(j);
q[j]:=k+m;
if k=q[i] then inc(i);
end;
for k:=n-m+1 to n do
begin
inc(max,a[p[h]]);
if k=p[h] then inc(h);
inc(min,a[q[i]]);
if k=q[i] then inc(i);
end;
writeln(max);
writeln(min);
close(input); close(output);
end.