Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <bits/stdc++.h>
- //%lld
- typedef long long ll;
- #define si(a) scanf("%d", &a)
- using namespace std;
- int vetor[25];
- int matriz[25][2000005];
- int saida[25];
- int main(){
- //freopen("in.txt", "r", stdin);
- //freopen("out.txt", "w", stdout);
- int n, t;
- while(si(n)==1){
- memset(matriz, 0, sizeof(matriz));
- memset(saida, 0, sizeof(saida));
- memset(vetor, 0, sizeof(vetor));
- si(t);
- for(int i = 0;i<t;i++)si(vetor[i]);
- matriz[0][vetor[0]] = 1;
- for(int i =1;i<t;i++){
- for(int j = 0;j<=n;j++){
- if(j<vetor[i]){
- matriz[i][j] = matriz[i-1][j];
- }else if(vetor[i]==j){
- matriz[i][j] = 1;
- }else{
- matriz[i][j] = max(matriz[i-1][j], matriz[i-1][j-vetor[i]]);
- }
- }
- }//matriz marcada corretamente
- int v = 0;
- for(int i = n;i>=0;i--){//procurando primeira aparição do maior numero <= n
- for(int j = 0;j<25;j++){
- if(matriz[j][i]==1){//encontrou
- saida[v] = vetor[j];//salvou o valor que o compõe no vetor de saida
- v++;
- if((i-vetor[j])<=0){//chegou no ultimo numero que compoe o n
- j = 25;//sai do loop
- i = -1;
- break;
- }
- i-=vetor[j];//vai procurar na linha anterior
- j = 26;//sai daqui
- i+=2;
- //proxima iteração o i fara --
- }
- }
- }
- for(int i = v-1;i>=0;i--)printf("%d ", vetor[i]);
- printf("sum: %d", vetor[0]);
- /*for(int i = 0;i<t;i++){
- for(int j = 0;j<=n;j++){
- cout<<matriz[i][j]<<" ";
- }
- cout<<endl;
- }*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment