1 program food(input,output);
2 var
3 p,s:array[0..220]of int64;
4 i,j,n:longint;
5 m,f,l,r,lmid,rmid,d,x,y:int64;
6 flag:array[0..220]of boolean;
7 k:int64;
8 function max(a,b:int64):int64;
9 begin
10 if a>b then exit(a) else exit(b);
11 end;
12 function min(a,b:int64):int64;
13 begin
14 if a<b then exit(a) else exit(b);
15 end;
16 procedure swap(var a,b:int64);
17 var
18 t:int64;
19 begin
20 t:=a;a:=b;b:=t;
21 end;
22 procedure sort(q,h:longint);
23 var
24 i,j:longint;
25 x:int64;
26 begin
27 i:=q;j:=h;x:=p[(i+j)>>1];
28 repeat
29 while p[i]<x do inc(i);
30 while x<p[j] do dec(j);
31 if i<=j then
32 begin
33 swap(p[i],p[j]);swap(s[i],s[j]);
34 inc(i);dec(j);
35 end;
36 until i>j;
37 if j>q then sort(q,j);
38 if i<h then sort(i,h);
39 end;
40 function fun(d:int64):int64;
41 var
42 j:int64;
43 begin
44 x:=m-d*f;
45 y:=x div d;
46 k:=0;
47 for i:=1 to n do
48 begin
49 j:=min(s[i]-k+1,y div p[i]);
50 y:=y-p[i]*j;k:=k+j;
51 //while (s[i]>=k) and (y>=p[i]) do begin inc(k);y:=y-p[i]; end;
52 if y<=p[i] then break;
53 end;
54 if s[n]<k then exit(k*d);
55 if s[i]<k then inc(i);
56 exit(k*d+(y*d+x mod d) div p[i]);
57 end;
58 begin
59 assign(input,‘food.in‘);assign(output,‘food.out‘);reset(input);rewrite(output);
60 readln(m,f,n);
61 for i:=1 to n do readln(p[i],s[i]);
62 fillchar(flag,sizeof(flag),true);
63 for i:=1 to n do
64 for j:=1 to n do if (i<>j) and flag[j] then if (p[i]>=p[j]) and (s[i]<=s[j]) then begin flag[i]:=false;break; end;
65 j:=0;
66 for i:=1 to n do if flag[i] then begin inc(j);p[j]:=p[i];s[j]:=s[i]; end;
67 n:=j;
68 sort(1,n);
69 l:=1;r:=m div f;
70 if r=0 then begin write(0);close(input);close(output);halt; end;
71 while l<r-2 do
72 begin
73 d:=(r-l) div 3;lmid:=l+d;rmid:=r-d;
74 if fun(lmid)<fun(rmid) then l:=lmid+1 else r:=rmid-1;
75 end;
76 write(max(max(fun(l),fun(l+1)),fun(r)));
77 close(input);close(output);
78 end.