AtCoder Beginner Contest 067 B

来源:互联网 发布:淘宝老客户回访率查询 编辑:程序博客网 时间:2024/06/05 15:03

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

  • 1KN50
  • 1li50
  • li is an integer.

Input

Input is given from Standard Input in the following format:

N Kl1 l2 l3  lN

Output

Print the answer.


Sample Input 1

Copy
5 31 2 3 4 5

Sample Output 1

Copy
12

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


Sample Input 2

Copy
15 1450 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;
}

原创粉丝点击