Cod sursa(job #3161740)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 27 octombrie 2023 20:28:50
Problema Ghiozdan Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <bits/stdc++.h>

#pragma optimize GCC ("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

///#include <tryhardmode>
///#include <GODMODE::ON>

using namespace std;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

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

const int NMAX=75e3+5;
const int NMAX2=2e2+5;

const int INF=2e9;

int dp[NMAX];
int viz[NMAX];
int frecv[NMAX2];

int main()
{
    ios_base::sync_with_stdio(false);
    fout.tie(NULL);

    int n,g,i,j,k,maxi=-1,best=-1;
    fin>>n>>g;
    for(i=1;i<=n;i++)
    {
        int x;
        fin>>x;
        frecv[x]++;
        maxi=max(maxi,x);
    }
    for(i=1;i<=g;i++)
        dp[i]=INF;
    for(i=maxi;i>=1;i--)
    {
        for(j=g;j>=0;j--)
        {
            if(dp[j]==INF)
                continue;
            for(k=1;k<=frecv[i];k++)
            {
                if(k*i+j<=g)
                {
                    best=max(best,k*i+j);
                    if(dp[k*i+j]!=INF)
                        break;
                    dp[k*i+j]=dp[j]+k;
                    viz[k*i+j]=i;
                }
                else
                    break;
            }
        }
    }
    fout<<best<<" "<<dp[best]<<"\n";
    int memo=dp[best];
    while(memo--)
    {
        fout<<viz[ind]<<"\n";
        ind=ind-viz[ind];
    }
    fout.close();
    return 0;
}