Cod sursa(job #3210213)

Utilizator MateiAlex24Diamandi Matei MateiAlex24 Data 5 martie 2024 14:44:48
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>

using namespace std;

ifstream fin("rucsac.in");
ofstream fout("rucsac.out");


bool comparison(const pair<int,int> &a, const pair<int,int> &b)
{
    //return ((a.second+a.first)/2)>((b.second+b.first)/2);
    //return a.first<b.first;
    if (a.first <= b.first && a.second >= b.second)
        return true;
    else if (a.first >= b.first && a.second >= b.second)
        return true;
    return false;
}

int main()
{
    int n, gmax;
    fin>>n>>gmax;
    vector<pair<int,int>> v(n); // first e weight, second e profit
    
    for (int i=0; i<n; i++){
        fin>>v[i].first>>v[i].second;
    }
    sort(v.begin(),v.end(),comparison); 
    int profit=0, greutate=0, i=0;
    //for (int i=0; i<n; i++){
    //    cout<<v[i].first<<" "<<v[i].second<<endl;
    //}
    while (greutate <= gmax && i < n){
        greutate += v[i].first;
        if (greutate > gmax)
            break;
        profit += v[i].second;
       // cout<<v[i].first<<" "<<v[i].second<<endl;
        i++;
    }
    fout<<profit;
    

    return 0;
}