Cod sursa(job #3218238)

Utilizator profinfo114Prof Info profinfo114 Data 26 martie 2024 16:51:47
Problema Ghiozdan Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int obj[205], dp[75005];
pair<int, int> conn[75005];

int main()
{
    int n, g, x, maxi = 0;
    fin>>n>>g;
    for(int i=0;i<n;i++){
        fin>>x;
        obj[x]++;  // increase the objects appearance
        maxi = max(x, maxi);
    }
    for(int i=maxi;i;i--) // walk through the objects
        if(obj[i])
            for(int j=g;j;j--)  // walk through the needed total weights
                if(!dp[j])
                for(int k=1;k<=obj[i] && j-i*k>=0;k++)  // walk through the number of appearances of the object
                    if(dp[j-i*k] || j-i*k==0) { // is there any object to fill up the weight difference?
                        dp[j] = k + dp[j-i*k]; // add the number of objects needed to fill the weight
                        // k ( for the current object ) and dp[j-i*k] for the last object
                        conn[j] = {i, k};
                        break;
                    }

    int pos = g;
    while(!dp[pos])
        pos--;
    fout<<pos<<' '<<dp[pos]<<'\n';
    while (pos){
        for (int i=1;i<=conn[pos].second;i++)
            fout<<conn[pos].first<<'\n';
        pos -= conn[pos].first * conn[pos].second;
    }
    return 0;
}