#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int CrossBridge(int n, int *p);
int cmp(const void*p1, const void *p2)
{
return *(int*)p1-*(int*)p2;
}
int main()
{
int *p;
int n;
printf("enter the group size: ");
scanf("%d",&n);
p=new int[n];
int i;
for (i=0;i<n;i++)
{
scanf("%d",p+i);
}
qsort(p,n,sizeof(int),cmp);
printf("time: %d\n",CrossBridge(n,p));
delete []p;
return 0;
}
int CrossBridge(int n, int *p)
{
assert(n>0 && NULL != p);
int ti=0;
if (n==1)
{
printf("A-->B: %d\n", p[0]);
ti=p[0];
return ti;
}
else if (n==2)
{
printf("A-->B: %d, %d\n", p[0], p[1]);
ti=p[1];
return ti;
}
else if (n==3)
{
printf("A-->B: %d, %d\n", p[0],p[1]);
printf("B-->A: %d\n", p[0]);
printf("A-->B: %d, %d\n", p[0],p[2]);
ti=p[1]+p[0]+p[2];
return ti;
}
else
{
if (p[0]+p[n-2] > p[1]*2)
{
printf("A-->B: %d, %d\n", p[0],p[1]);
printf("B-->A: %d\n", p[0]);
printf("A-->B: %d, %d\n", p[n-2],p[n-1]);
printf("B-->A: %d\n", p[1]);
return p[1]+p[0]+p[n-1]+p[1]+CrossBridge(n-2,p);
}else
{
printf("A-->B: %d, %d\n", p[0],p[n-1]);
printf("B-->A: %d\n", p[0]);
printf("A-->B: %d, %d\n", p[0],p[n-2]);
printf("B-->A: %d\n", p[0]);
return p[n-1]+p[n-2]+p[0]+p[0]+CrossBridge(n-2,p);
}
}
}