您的位置:首页 > 其它

AtCoder Beginner Contest 067 B - Snake Toy

2017-07-17 08:36 295 查看


B - Snake Toy

Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

Snuke has N sticks. The
length of the i-th stick is li.
Snuke is making a snake toy by joining K of
the sticks together.
The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy.

Constraints

1≤K≤N≤50
1≤li≤50
li is
an integer.

Input

Input is given from Standard Input in the following format:
N K
l1 l2 l3 … lN


Output

Print the answer.

Sample Input 1

Copy
5 3
1 2 3 4 5


Sample Output 1

Copy
12

You can make a toy of length 12 by
joining the sticks of lengths 3, 4 and 5,
which is the maximum possible length.

Sample Input 2

Copy
15 14
50 26 27 21 41 7 42 35 7 5 5 36 39 1 45


Sample Output 2

Copy
386


题解:排完序后求值即可

#include <iostream>

#include <algorithm>

#include <stdio.h>

#include <string.h>

#include <string>

#include <vector>

#include <map>

#include <set>

using namespace std;

map<int,int> mp;

int a[51];

int main()

{

    int n,m;

    while(cin>>n>>m){

        for(int i=0;i<n;i++)

            cin>>a[i];

        sort(a,a+n);

        int s=0;

        for(int i=n-m;i<n;i++){

            s+=a[i];

        }

        cout<<s<<endl;

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: