Cod sursa(job #2394625)

Utilizator AndreiStrAndrei Stroici AndreiStr Data 1 aprilie 2019 19:00:46
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");
int n,e,s,x;
vector<int> w;
vector<int> p;

int main()
{
    f>>n;
    f>>e;
    for(int i=1;i<=n;i++)
        f>>w[i]>>p[i],s+=w[i];
    if(s<e)
    {
        g<<"-1";
        return 0;
    }
    for(int i=1;i<n;i++)
        for(int j=i+1;j<=n;j++)
        {
            if(w[i]>w[j])
                swap(w[i],w[j]),swap(p[i],p[j]);
            else if(w[i]==w[j])
                if(p[i]<p[j])
                    swap(p[i],p[j]);
        }
    if(w[n]>=e)
    {
        g<<w[n];
        return 0;
    }
    else
    {
        s=0;
        int i=n;
        while(s<e)
        {
            s+=w[i];
            x+=p[i];
            i--;
        }
        g<<x;
    }
    return 0;
}