Why WA? (my solution included) So... Simply say what traps are in this problem. PLEASE!
const maxH=500;
var N,K,q,w,e:longint;
    H:array[1..maxH] of longint;
    z:array[1..2,1..maxH,0..2] of longint;
    x:array[0..1] of longint;
 
begin
readln(N,K);
for q:=1 to N do
  readln(h[q]);
 
for q:=1 to K do
  begin
  z[2,q,0]:=0;
  z[2,q,1]:=0;
  z[2,q,2]:=0;
  end;
z[2,1,h[1]]:=1;
 
for q:=2 to N do
  begin
  z[1]:=z[2];
  for e:=1 to K do
    begin
    z[2,e,0]:=0;
    z[2,e,1]:=0;
    z[2,e,2]:=0;
    end;
  z[2,1]:=z[1,1];
  z[2,1,h[q]]:=z[2,1,h[q]]+1;
 
  e:=K;
  if q<K then e:=q;
  for w:=2 to e do
    begin
    x[0]:=z[1,w,0];
    x[1]:=z[1,w,1];
    x[h[q]]:=x[h[q]]+1;
 
    z[2,w,h[q]]:=1;
    z[2,w,2]:=z[1,w-1,0]*z[1,w-1,1]+z[1,w-1,2];
 
    if z[2,w,2]>z[1,w,2]+x[0]*x[1] then
      begin
      z[2,w,0]:=x[0];
      z[2,w,1]:=x[1];
      z[2,w,2]:=z[1,w,2];
      end;
    end;
  end;
writeln(z[2,K,0]*z[2,K,1]+z[2,K,2]);
end.