- 纪念品分组
- 2017-02-05 14:31:06 @
为什么60WA
program Project1;
var
i, j, n, w, ans: longint;
a: array[1..10000] of longint;
procedure qsort(l, r: longint);
var
i, j, x, y: longint;
begin
i := l;
j := r;
x := a[(l + r) div 2];
repeat
while a[i] < x do
Inc(i);
while a[j] > x do
Dec(j);
if i <= j then
begin
y := a[i];
a[i] := a[j];
a[j] := y;
Inc(i);
Dec(j);
end;
until i > j;
if i < r then
qsort(i, r);
if l < j then
qsort(l, j);
end;
begin
readln(w);
readln(n);
ans := 0;
for i := 1 to n do
readln(a[i]);
qsort(1, n);
for i := 1 to n - 1 do
for j := n downto i + 1 do
begin
if a[i] + a[i + 1] > w then
begin
Inc(ans);
break;
end
else
if (a[i] + a[j] <= w) and (a[j] <> 0) and (a[i + 1] <> 0) then
begin
a[i] := 0;
a[j] := 0;
Inc(ans);
break;
end
else if (a[i] <> 0) and (a[i+1] = 0) then
begin
Inc(ans);
a[i] := 0;
break;
end;
end;
if a[n] <> 0 then
Inc(ans);
writeln(ans);
end.