Cod sursa(job #2798925)

Utilizator mariusvicolVicol Marius mariusvicol Data 12 noiembrie 2021 09:33:49
Problema Ghiozdan Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.53 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");

int frec[201],n,gh,x,solutie[1000],dp[100],k;
int main()
{f>>n>>gh;
for(int i=1;i<=n;i++)
{f>>x;
frec[x]++;

}
dp[0]=1;
for(int i=200;i>=1;i--)
{for(int j=gh;j>=0;j--)
{if(dp[j])
{for(k=1;k*i+j<=gh&&k<=frec[i];k++)
{if(dp[k*i+j])
{break;}
dp[k*i+j]=dp[j]+k;
solutie[i*k+j]=i;
}
}
}

}
while(!dp[gh])
    gh=gh-1;
g<<gh<<" "<<dp[gh]-1<<" "<<endl;
while(gh)
{
g<<solutie[gh]<<endl;
gh=gh-solutie[gh];

}

}