Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int pd[1001] [100001];
- int vet[1001];
- int troco(int n, int v);
- int maior(int a, int b);
- void inicializar(int m, int v);
- int main()
- {
- int v,m;
- scanf("%d %d", &v, &m);
- inicializar(m, v);
- for(int i = 1; i <= m; i++)
- {
- scanf("%d", &vet[i]);
- }
- if(troco(m, v) == 1)
- {
- printf("S\n");
- }
- else
- {
- printf("N\n");
- }
- }
- int troco(int n, int v)
- {
- if(v == 0)
- {
- return(1);
- }
- if(n <= 0)
- {
- return(0);
- }
- if(pd[n][v] == -1)
- {
- if(pd[n-1][v] == -1)
- {
- pd[n-1][v] = troco(n-1, v);
- }
- if(vet[n] <= v)
- {
- if(pd[n-1][v-vet[n]] == -1)
- {
- pd[n-1][v-vet[n]] = troco(n-1, v-vet[n]);
- }
- pd[n][v] = maior(pd[n-1][v],pd[n-1][v-vet[n]]);
- }
- else
- {
- pd[n][v] = pd[n-1][v];
- }
- }
- return(pd[n][v]);
- }
- int maior(int a, int b)
- {
- if(a > b)
- {
- return a;
- }
- else
- {
- return b;
- }
- }
- void inicializar(int m, int v)
- {
- for(int i = 0; i <= m; i++)
- {
- for(int j = 0; j <= v; j++)
- {
- pd[i][j] = -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement