Skip to content

Instantly share code, notes, and snippets.

@ch-hristov
Last active August 29, 2015 14:17
Show Gist options
  • Save ch-hristov/c48e0878aae8ecc0282c to your computer and use it in GitHub Desktop.
Save ch-hristov/c48e0878aae8ecc0282c to your computer and use it in GitHub Desktop.
Minimum count of items which are equal to s
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int MAX = 9999999;
vector<int> c;
int cnt;
cin >> cnt;
for(int i = 0;i<cnt;i++){
int j;
cin >> j;
c.push_back(j);
}
int s;
cin >> s;
int d[s+1];
for(int i =0;i <=s;i++)
d[i]=MAX;
d[0]=0;
for(int j = 1;j <=s;j++){
for(int i = 0;i < cnt;i++){
if(c[i] <= j && d[j-c[i]]+1<d[j]){
d[j]=d[j-c[i]]+1;
}
}
}
cout << d[s] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment