1 // MULTI - KNAPSACK PROBLEM :)
27 int go(int quedan
, int R
, int nb
){
28 if( quedan
== 0 ) return 1; // RIGHT
29 if( nb
== numb
) return 0; // RIGHT
30 if( R
== 0 )return go ( quedan
, mx
, nb
+1);
31 if( SUMA
- cortado
> board
[nb
] + tot
[nb
+1]) return 0;
32 if( board
[nb
] < mini
)return go ( quedan
, mx
, nb
+1);
34 if( rail
[R
] == 0 || board
[nb
] < R
) return go ( quedan
, R
- 1 , nb
);
39 if( rail
[R
] == 0 && R
== mini
){
41 while(a
< N
&& rail
[a
] == 0 ) a
++;
44 if( go ( quedan
- 1 , R
, nb
) ) return 1;
51 if( go(quedan
, R
, nb
+1) ) return 1;
52 if( go( quedan
, R
-1, nb
) ) return 1;
55 bool se_puede(int cant
){
57 memset(rail
, 0, sizeof(rail
));
60 for(i
= 0 ; i
< cant
;i
++){
63 mx
= max( mx
, Rail
[i
]);
64 mini
= min( mini
, Rail
[i
] );
66 SB
= 0; for(i
= 0 ; i
< numb
; i
++) SB
+= board
[i
];
67 if( SB
< SUMA
) return 0;
69 return go(cant
, mx
, 0);
73 fin
= fopen("fence8.in", "r"); fout
= fopen("fence8.out", "w");
74 memset(rail
, 0, sizeof(rail
));
75 fscanf(fin
, "%i", &numb
);
76 for(i
=0;i
<numb
;i
++) fscanf(fin
, "%i", &board
[i
]);
77 fscanf(fin
, "%i", &numr
);int a
;
78 for(i
=0;i
<numr
;i
++){fscanf(fin
, "%i", &a
);if(a
==0)continue;Rail
.push_back(a
);}
79 sort(board
, board
+numb
);
80 sort(Rail
.begin(), Rail
.end());
83 for(i
= numb
-1; i
>=0;i
--) tot
[i
] = tot
[i
+1] + board
[i
];
85 for(int i
= numr
; i
; i
-- ){
88 fprintf(fout
, "%i\n", i
);
92 fprintf(fout
, "%i\n", 0);