The idea of converting to a primary function is to arrange and combine the devices, create all possible combinations, and search for the total value, as long as the first one is found, using total-I is the two values closest to each other .. So the output is total-I, I. This eliminates the need to worry that the total value is an odd number. The generation function of the primary function can be written in seconds.
[Csharp]
# Include "stdio. h"
# Include "stdlib. h"
# Include "string. h"
Struct node
{
Int v, m, sum;
} Aa [1, 250008];
Int c1 [250008], c2 [250008];
Int main ()
{
Int n, I, j, k, tal;
While (scanf ("% d", & n )! = EOF)
{
If (n <0) break;
Tal = 0;
Memset (c1, 0, sizeof (c1 ));
Memset (c2, 0, sizeof (c2 ));
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & aa [I]. v, & aa [I]. m );
Aa [I]. sum = aa [I]. m * aa [I]. v;
Tal + = aa [I]. sum;
}
C1 [0] = 1;
For (I = aa [1]. v; I <= aa [1]. sum; I + = aa [1]. v)
C1 [I] = 1;
For (I = 2; I <= n; I ++)
{
For (j = 0; j <= tal; j ++)
{
For (k = 0; k + j <= tal & k <= aa [I]. sum; k + = aa [I]. v)
C2 [j + k] + = c1 [j];
}
For (j = 0; j <= tal; j ++)
{
C1 [j] = c2 [j];
C2 [j] = 0;
}
}
For (I = tal/2; I> = 0; I --)
{
If (c1 [I]! = 0) www.2cto.com
{
Printf ("% d \ n", tal-I, I); break;
}
}
}
Return 0;
}
Author: yyf572132811