首页 > 其他分享 >Codeforces863B-Kayaking

Codeforces863B-Kayaking

时间:2022-11-22 19:04:24浏览次数:33  
标签:do begin people ans1 kayaks Kayaking Codeforces863B instability


Kayaking


Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.

Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.

Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.

Help the party to determine minimum possible total instability!

Input

The first line contains one number n (2 ≤ n ≤ 50).

The second line contains 2·n integer numbers w1w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).

Output

Print minimum possible total instability.

Example

Input


2 1 2 3 4


Output


1


Input


4 1 3 4 6 3 4 100 200


Output


5


题解:我们发现n的范围比较小,所以可以直接暴力贪心求解。

Code:

var
n,ans,i,j,cnt,k,ans1:longint;
a,b:array[1..105] of longint;
procedure sort(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 not(i>j) then
begin
y:=a[i];a[i]:=a[j];a[j]:=y;
inc(i);dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
readln(n);
n:=n*2;ans:=maxlongint;
for i:=1 to n do read(a[i]);
sort(1,n);
for i:=1 to n-1 do
for j:=i+1 to n do
begin
ans1:=0;cnt:=0;
for k:=1 to n do
if (k<>i)and(k<>j) then
begin
inc(cnt);
b[cnt]:=a[k];
end;
for k:=1 to n div 2-1 do
ans1:=ans1+abs(b[k*2]-b[k*2-1]);
if ans1<ans then ans:=ans1;
end;
writeln(ans);
end.

标签:do,begin,people,ans1,kayaks,Kayaking,Codeforces863B,instability
From: https://blog.51cto.com/u_15888102/5878354

相关文章