Cod sursa(job #2867000)

Utilizator kontexVeres Norbert kontex Data 10 martie 2022 09:43:55
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <iostream>
#include <fstream>
#include <list>

using namespace std;
ifstream be("rucsac.in");
ofstream ki("rucsac.out");

int const nMax = 100001;
int n,g;
int w[nMax],p[nMax];
list<pair<int,int> > elem;

void rucsac()
{
    int maxprofi = 0;
    int actualWeight = 0;
    int currentprofit;
    int db = 0;
    for(int i=1;i<=n;i++)
    {
        actualWeight = w[i];
        currentprofit = p[i];
        db = 1;
        for(int j=i+1;j<=n ;j++)
        {
            actualWeight += w[j];
            currentprofit += p[j];
            if(actualWeight == g)
            {
                if(maxprofi < currentprofit)
                {
                    maxprofi = currentprofit;
                }
            }
            if(actualWeight > g)
            {
                actualWeight -= w[i+db];
                currentprofit -= p[i+db];
            }
            db++;

        }
    }
    cout<<maxprofi<<endl;
    ki<<maxprofi<<endl;
}
int main()
{
    be>>n>>g;
    int a,b;
    for(int i=1;i<=n;i++)
    {
        //be>>w[i]>>p[i];
        be>>a>>b;
        elem.push_back(make_pair(a,b));
    }
    elem.sort();
    int db = 1;
    for(list<pair<int,int> >::iterator it = elem.begin(); it != elem.end(); it++)
    {
        w[db] = it->first;
        p[db++] = it->second;
    }

    rucsac();
}