Cod sursa(job #1394863)

Utilizator Dragne.Andrei11Dragne Andrei Dragne.Andrei11 Data 20 martie 2015 19:24:11
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <cstdio>
#include <algorithm>

using namespace std;

struct numere
{
    short x, y;
}l[1005];
bool cmp(numere a, numere b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x>b.x;
}
int main()
{
    freopen("energii.in", "r", stdin);
    freopen("energii.out", "w", stdout);
    short n, c, n1, n2;

    scanf("%hd", &n);
    scanf("%hd", &c);
    short i;
    for(i=1;i<=n;i++)
    {
        scanf("%hd%hd", &n1, &n2);
        l[i].x=n1;
        l[i].y=n2;
    }
    sort(l+1, l+n+1, cmp);
    short f=0;
    i=1;
    while(c>0&&i<=n)
    {
        c-=l[i].x;
        f+=l[i].y;
        i++;
    }
    printf("%hd\n", f);

    return 0;
}