Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 9707 | Accepted: 3114 |
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 washing 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 washed 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 integers M and N (M < 10, N < 100), which are the numbers of colors and of clothes. The next line contains M strings which are not longer than 10 characters and do not contain spaces, which the names of the colors. Then follow N lines 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 washing.
Sample Input
3 4
red blue yellow
2 red
3 blue
4 blue
6 red
0 0
Sample Output
10
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int N=,M=,T=1e5+;
int m,n,num=,cnt[M],ans=;
string s;
map<string,int> mp;
struct clo{
int t,col;
}a[N];
int f[T];
void dp(){
for(int c=;c<=m;c++){
int sum=cnt[c];
memset(f,,sizeof(f));
for(int i=;i<=n;i++) if(a[i].col==c)
for(int j=sum/;j>=a[i].t;j--)
f[j]=max(f[j],f[j-a[i].t]+a[i].t);
ans+=cnt[c]-f[sum/];
}
}
void init(){
mp.clear();
num=ans=;
memset(cnt,,sizeof(cnt));
}
int main(){
while(true){
scanf("%d%d",&m,&n);
if(m==&&n==) break;
init();
for(int i=;i<=m;i++){
cin>>s;
mp[s]=++num;
}
for(int i=;i<=n;i++){
scanf("%d",&a[i].t);
cin>>s;
a[i].col=mp[s];
cnt[a[i].col]+=a[i].t;
}
dp();
printf("%d\n",ans);
}
}