function [ksupp] = ksupport(psi, k)
% (k-support norm)^2 of the vector psi
% Arguments: vector psi, k (1<=k<=d).

w = sort(abs(psi),'descend');

Ar = sum(w(k+1:length(psi))); % A_{r-1} for r=0

for r=0:k-1
    
    Ar = Ar + w(k-r); %A_r
    if r<k-1 && Ar < (r+1)*w(k-r-1) && Ar >= (r+1)*w(k-r)
        break;
    end
    
end

ksupp = sum(w(1:k-r-1).^2) + (Ar^2)/(r+1);

end