POJ1700----Crossing River

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int a[1005];
 7 int min(int x, int y)
 8 {
 9     return x < y ? x : y;
10 }
11 int main()
12 {
13     int t, n;
14     cin >> t;
15     while (t--)
16     {
17         cin >> n;
18         for (int i = 0; i < n; ++i)
19             cin >> a[i];
20         sort(a, a + n);
21         int ans = 0, ant = n - 1;
22         while (ant >= 0)
23         {
24             if ( ant == 0) ans += a[0];
25             else if (ant == 1) ans += a[1];
26             else if (ant == 2) ans += a[0] + a[1] + a[2];
27             else ans += min(2 * a[0] + a[ant] + a[ant - 1], 2 * a[1] + a[0] + a[ant]);
28             if (ant > 2) ant -= 2;//大于3个时,2组两组进行
29             else break;
30         }
31         cout << ans << endl;
32     }
33     return 0;
34 }

 

posted @ 2017-08-01 00:24  Posase  阅读(119)  评论(0编辑  收藏  举报