Description
Dearboy was so busy recently that now he has piles of clothes to wash. luckily, he has a beautiful and hard-working girlfriend to help him. the clothes are in varieties of colors but each piece of them can be seen as of only one color. in order to prevent the clothes from getting dyed in mixed colors, dearboy and his girlfriend have to finish refreshing all clothes of one color before going on to those of another color.
From experience dearboy knows how long each piece of clothes takes one person to wash. each piece will be replaced ed by either dearboy or his girlfriend but not both of them. the couple can wash two pieces simultaneously. what is the shortest possible time they need to finish the job?
Input
The input contains several test cases. Each test case begins with a line of two positive integersMAndN(M<10,N<100), which are the numbers of colors and of clothes. The next line containsMStrings which are not longer than 10 characters and do not contain spaces, which the names of the colors. Then followNLines describing the clothes. Each of these lines contains the time to wash some piece of the clothes (less than 1,000) and its color. Two zeroes follow the last test case.
Output
For each test case output on a separate line the time the couple needs for processing ing.
Sample Input
3 4red blue yellow2 red3 blue4 blue6 red0 0
Sample output
10
Source
Poj monthly -- 2007.04.01, dearboy, 01 backpack, started to think greedy, I am still too young to say no. The strategy is as follows: calculate the total time required for each color suit sum, and then perform sum/2 for the 01 backpack. For the maximum time that can be loaded, see the frustration code =. =
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> using namespace STD; int num [15] [110]; // time required to count the number of clothes in each color int nu [15]; // count the number of clothes in each color char STR [15] [15]; // read the character int DP [10000]; // The backpack starts to open, WA =. = Int n, m; int main () {int t; char s [110]; while (~ Scanf ("% d", & N, & M) & (n + M) {getchar (); For (INT I = 0; I <N; I ++) {scanf ("% s", STR [I]); nu [I] = 0 ;}for (INT I = 0; I <m; I ++) {scanf ("% d % s", & T, S); For (Int J = 0; j <n; j ++) {If (! Strcmp (STR [J], S) {nu [J] ++; num [J] [NU [J]-1] = T ;}}} int ans = 0; For (INT I = 0; I <n; I ++) {If (Nu [I]) {int sum = 0; for (Int J = 0; j <nu [I]; j ++) sum + = num [I] [J]; memset (DP, 0, sizeof (DP); // 01 backpack enumeration for (Int J = 0; j <nu [I]; j ++) {for (int K = sum/2; k> = num [I] [J]; k --) DP [k] = max (DP [K], DP [k-num [I] [J] + num [I] [J]);} ans + = max (DP [sum/2], sum-DP [sum/2]) ;}} printf ("% d \ n", ANS);} return 0 ;}