program rom;
var
k,v,m,i,j,max,p,q,t,n:longint;
w,c,f:array[1..1000]of longint;
procedure search(i,sum,value:longint);
var
j:longint;
begin
if sum>=v then if sum=v then begin inc(p); f[p]:=value end
else exit;
for j:=i+1 to n do
search(j,sum+w[j],value+c[j])
end;
begin
readln(k,v,n); p:=0;
for i:=1 to n do
readln(w[i],c[i]);
for i:=1 to n do
search(i,w[i],c[i]);
for i:=1 to p-1 do
begin
q:=i;
for j:=i+1 to n do
if f[q]<f[j] then q:=j;
if q<>i then begin
t:=f[i];
f[i]:=f[q];
f[q]:=t
end
end;
max:=0;
for i:=1 to k do
inc(max,f[i]);
write(max)
end.