2018-UPC-组队训练赛第一次 A:BBQ Easy

题目描述

Snuke is having a barbeque party.
At the party, he will make N servings 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, Lis an integer.

输入

The input is given from Standard Input in the following format:
N
L1 L2 … L2N

输出

Print the maximum total number of ingredients that Snuke's N servings of Skewer Meal can hold.

样例输入

2
1 3 1 2

样例输出

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 of 3.

       这个题目是很简单的一个水题,在这里就解释一下题目意思吧。样例中第一行数字表示有n个烤串,第二行的2n个数字表示竹签的长度,每两个竹签能组成一个烤串(当竹签长度不同,按短的算),每个长度单位可以放一种食材,要求求出最大的食材数。

以下是代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
 
int main()
{
    int n;
    while(cin>>n)
    {
        int a[2*n];
        int i;
        for(i=0;i<2*n;i++)
            cin>>a[i];
        sort(a,a+2*n);
        int sum=0;
        for(i=0;i<2*n;i+=2)
        {
            sum+=a[i];
        }
        cout<<sum<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40928084/article/details/80077522
今日推荐