Pagini recente » Cod sursa (job #315161) | Cod sursa (job #1155228) | Cod sursa (job #2674790) | Cod sursa (job #2785622) | Cod sursa (job #2430801)
#include <fstream>
#include <iostream>
#include <vector>
#define change(A) (A%2==1)?0:1
using namespace std;
ifstream fin("ghiozdan.in");
ofstream fout("ghiozdan.out");
int n,g,res;
int greutati[20010];
int help[2][75000];
int findShortest(int pos,int value,int length)
{
if(value>res)
return 100000;
else if(value==res)
return length;
else if(pos==0)
return 100000;
else
{
int temp1 = findShortest(pos-1,value,length);
int temp2 = findShortest(pos-1,value+greutati[pos],length+1);
int res = min(temp1,temp2);
if(res==temp2)
//cout<<greutati[pos]<<endl;
return res;
}
}
int main()
{
int maximum = 0;
vector<int> temp;
fin>>n>>g;
for(int i = 1;i<=n;i++)
fin>>greutati[i];
for(int i = 1;i<=n;i++)
{
for(int j = 0;j<=g;j++)
{
if(greutati[i]<=j)
{
if(help[change(i)][j]>help[change(i)][j-greutati[i]]+greutati[i])
{
help[i%2][j]=help[change(i)][j];
}
else
{
help[i%2][j] = help[change(i)][j-greutati[i]]+greutati[i];
}
}
else
{
help[i%2][j] = help[change(i)][j];
}
}
}
res = help[n%2][g];
fout<<res<<" "<<findShortest(n,0,0)<<'\n';
return 0;
}