Problem solving with programming: Stamps problem - SPOJ
You need to collect some stamps from friends. Suppose you have n friends and each of them have some stamps with them {s1, s2, ... sn}.
The problem is to find if we can collect the targeted number of stamps. If it is possible, we have to find the minimum number of friends to collect the stamps from.
We may use Heap, PriorityQueue.Read full article from Problem solving with programming: Stamps problem - SPOJfor( i = 0 ; i < t; i++ ){int need;cin >> need;int n;cin >> n; // number of friendsvector<int> stamps(n);int j;for( j = 0; j < n; j++ ){cin >> stamps[j];}//find the sum of all stamps from friends; use library algorithmlong long total = accumulate(stamps.begin(), stamps.end(), 0 );if( total < need ){cout << "Scenario #" << (i+1) << ":" << endl << "impossible" << endl << endl;}else{//sort coins in descending order; use library algorithmsort( stamps.begin(), stamps.end(), greater<int>() );long long t = 0;j = 0;//borrow until you jave just enough stampswhile ( t < need ){t += stamps[j];j++;}//we need to borrow from j friendscout << "Scenario #" << (i+1) << ":" << endl << j << endl << endl;}