Executing...
Test 1: TEST OK [0.000 secs, 244 KB]
Test 2: TEST OK [0.000 secs, 244 KB]
Test 3: TEST OK [0.000 secs, 252 KB]
Test 4: TEST OK [0.000 secs, 248 KB]
Test 5: TEST OK [0.011 secs, 244 KB]
Test 6: TEST OK [0.022 secs, 244 KB]
Test 7: TEST OK [0.000 secs, 248 KB]
Test 8: TEST OK [0.011 secs, 244 KB]

All tests OK.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{
ID:cxj6661
PROB:milk
LANG:PASCAL
}
//By Ceeji
//Date: 2009-6-1
//Type: Sort
//Resource: USACO 1.3
Program milk;
Const
   fin='milk.in';
   fou='milk.out';
Type nm=record
          p,a:longint;
        end;
Var
   i,n,m,sum:longint;
   info:array[1..5000]of nm;
Procedure qsort(s,t:longint);
var tmp:nm;
    e,i,j:longint;
begin
    i:=s; j:=t; e:=info[random(t-s+1)+s].p;
    repeat
       while (i<t)and(info[i].p<e) do inc(i);
       while (j>s)and(info[j].p>e) do dec(j);
       if i<=j then
       begin
          tmp:=info[i]; info[i]:=info[j]; info[j]:=tmp;
          inc(i); dec(j);
       end;
    until i>j;
    if j>s then qsort(s,j);
    if i<t then qsort(i,t);
end;
begin
   randomize;
   assign(input,fin);  reset(input);
   assign(output,fou); rewrite(output);
   readln(n,m);
   for i:=1 to m do
   begin
     readln(info[i].p,info[i].a);
   end;
   close(input);
   sum:=0;
   qsort(1,m); i:=1;
   while n>0 do
   begin
      if n>info[i].a then
      begin
         inc(sum,info[i].p*info[i].a);
         dec(n,info[i].a);
         inc(i);
      end
      else
      begin
         inc(sum,info[i].p*n); n:=0; break;
      end;
   end;
   writeln(sum);
   close(output);
end.