美队吃蛋糕


灭霸被消灭后,我们最敬爱的美国队长88岁生日即将到来,在生日那天他的朋友们送来很多蛋糕。作为贪吃的美国队长,他认为蛋糕这么好吃的东西一定要多吃。(快赶上雷神了,哈哈) 

毕竟美队已经上了年纪,“三高”也紧随他的步伐,为了他的身体着想,他的朋友们决定只允许他吃k块蛋糕。其中每块蛋糕有它的种类和重量。 

美队为了吃更多的口味的蛋糕,他决定每个种类蛋糕只取一块,但是他太喜欢蛋糕了,所以他希望你可以帮他算出吃最多蛋糕重量的总和。 

输入

第1行输入两个整数n,k。其中n代表有n个蛋糕,k代表只能吃k块。(0<= n <=1000,0<= k <=1000)

接下来每行输入两个整数v,w。其中v代表蛋糕的种类,w代表蛋糕的重量。(0<= v <=n,1<=w<=100000)

输出

输出一个整数,代表吃下最多蛋糕重量的总和

样例输入 Copy

5 3
1 1
2 1
2 2
3 4
4 2

样例输出 Copy

8

提示

样例1:

种类  重量

 2     2

 3     4

 4     2

重量总和:2+4+2=8 

 

主要就是每种类蛋糕只吃一种;

解题思路:贪心,结构体排序:

AC代码:

#include "bits/stdc++.h"
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
  
const int maxn = 1e9+7;
const int inf = 0x3f3f3f3f;
const int mod = 1e8+7;
struct node{
    int v,w;
};
bool cmp(node a,node b){
    return a.w > b.w ;
}
int main()
{
    int n,m;
    cin>>n>>m;
    node a[n];
    for(int i=0;i<n;i++){
        cin>>a[i].v>>a[i].w;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i==j)
            continue;
            if(a[i].v ==a[j].v){
                if(a[i].w >= a[j].w)
                     a[j].w=0;
                else
                    a[i].w =0;
            }
        }
            
    }
    sort(a,a+n,cmp);
    int sum=0;
    for(int i=0;i<m;i++){
        sum+=a[i].w;
    }
    cout<<sum<<endl;
    return 0;
}

 

 

猜你喜欢

转载自www.cnblogs.com/lipu123/p/12163544.html