Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var n,m,i,j,k,cnt,lift,sum:longint;
- a:array[0..200001] of longint;
- used:array[0..200001] of boolean;
- och:array[0..200001] of longint;
- procedure swap(Var t,y:longint);
- var u:longint;
- begin
- u:=t; t:=y; y:=u;
- end;
- procedure sort(l,r:longint);
- var i,j,x:longint;
- begin
- x:=a[random(r-l)+l];
- i:=l; j:=r;
- repeat
- while a[i]<X do inc(i);
- while a[j]>x do dec(j);
- if i<=j then
- begin
- swap(A[i],a[j]);
- inc(i);
- dec(j);
- end;
- until i>j;
- if l<j then sort(l,j);
- if i<r then sort(i,r);
- end;
- begin
- randomize;
- readln(n,m);
- for i:= 1 to n do
- read(A[i]);
- sort(1,n);
- i:=1;
- j:=n;
- cnt:=0;
- fillchar(used,sizeof(used),true);
- while (i<j) do
- begin
- if a[i]+a[j]<=m then i:=i+1 else
- begin
- inc(cnt);
- och[cnt]:=a[i];
- inc(cnt);
- och[cnt]:=a[j];
- used[i]:=false;
- used[j]:=false;
- i:=i+1;
- j:=j-1;
- end;
- end;
- for i:= n downto 1 do
- if used[i] then
- begin
- inc(cnt);
- och[cnt]:=a[i];
- end;
- lift:=0;
- sum:=0;
- cnt:=0;
- for i:= 1 to n do
- begin
- if (sum+och[i]>m) or (Cnt=2) then
- begin
- lift:=lift+1;
- sum:=och[i];
- cnt:=1;
- end else
- begin
- sum:=sum+och[i];
- cnt:=cnt+1;
- end;
- end;
- if sum>0 then lift:=lift+1;
- writeln(lift);
- for i:= 1 to n do
- write(och[i],' ');
- writeln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement