Score : 200 points
Problem Statement
Snuke is having a barbeque party.
At the party, he will make N servings of Skewer Meal.
Example of a serving of Skewer Meal
He has a stock of 2N skewers, all of which will be used in Skewer Meal. The length of the i-th skewer is Li. Also, he has an infinite supply of ingredients.
To make a serving of Skewer Meal, he picks 2 skewers and threads ingredients onto those skewers. Let the length of the shorter skewer be x, then the serving can hold the maximum of x ingredients.
What is the maximum total number of ingredients that his N servings of Skewer Meal can hold, if he uses the skewers optimally?
Constraints
- 1≦N≦100
- 1≦Li≦100
- For each i, Li is an integer.
Input
The input is given from Standard Input in the following format:
N L1 L2 … L2N
Output
Print the maximum total number of ingredients that Snuke‘s N servings of Skewer Meal can hold.
Sample Input 1
2 1 3 1 2
Sample Output 1
3
If he makes a serving using the first and third skewers, and another using the second and fourth skewers, each serving will hold 1 and 2 ingredients, for the total of3.
Sample Input 2
5 100 1 2 3 14 15 58 58 58 29
Sample Output 2
135
挺有意思的题目,排个序就ok了
/* *********************************************** Author :guanjun Created Time :2016/7/16 23:23:01 File Name :agc1a.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10010 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; priority_queue<int,vector<int>,greater<int> >pq; struct Node{ int x,y; }; struct cmp{ bool operator()(Node a,Node b){ if(a.x==b.x) return a.y> b.y; return a.x>b.x; } }; bool cmp(int a,int b){ return a>b; } int a[300]; int main() { #ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int n; while(cin>>n){ for(int i=1;i<=n*2;i++)cin>>a[i]; sort(a+1,a+1+2*n); int sum=0; for(int i=2*n;i>=1;i-=2){ sum+=a[i-1]; } cout<<sum<<endl; } return 0; }