AlbertMourato

Untitled

Feb 25th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <bits/stdc++.h>
  3. //%lld
  4. typedef long long ll;
  5. using namespace std;
  6.  
  7. int vetor[50005];
  8. int v[50005];
  9. int moedas[105];
  10. int main(){
  11.     int m, n;
  12.     while(cin>>m){
  13.         if(m!=0){
  14.             memset(v, -1, sizeof(v));
  15.             memset(vetor, 50005, sizeof(vetor));
  16.             cin>>n;
  17.             for(int i = 0;i<n;i++){
  18.                 cin>>moedas[i];
  19.             }
  20.             vetor[0] = 0;
  21.             for(int i = 0;i<n;i++){
  22.                 for(int j = 0;j<=m;j++){
  23.                     if(j>=moedas[i]){
  24.                         vetor[j] = min(1+(vetor[j-moedas[i]]), vetor[j]);
  25.                         v[j] = i;
  26.                     }
  27.                 }
  28.  
  29.             }
  30.             if(vetor[m]>50000)cout<<"Impossivel"<<endl;
  31.             else cout<<vetor[m]<<endl;
  32.         }else{
  33.             break;
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment