1 #include <iostream>
2 #include <algorithm>
3 #include <cstring>
4
5 using namespace std;
6
7 struct game
8 {
9 int t,m;
10 };
11
12 game g[1111];
13 int n,money;
14 int tag[1111];
15 int cmp(game a,game b)
16 {
17 if(a.m>b.m) return 1;
18 if(a.m<b.m) return 0;
19 if(a.t<b.t) return 1;
20 else return 0;
21 }
22
23 int main()
24 {
25 int i,j;
26 while(cin>>money>>n)
27 {
28 memset(tag,0,sizeof(tag));
29 for(int i=0;i<n;i++)
30 {
31 cin>>g[i].t;
32 }
33 for(int i=0;i<n;i++)
34 {
35 cin>>g[i].m;
36 }
37 sort(g,g+n,cmp);
38 for(i=0;i<n;i++)
39 {
40 for(j=g[i].t;j>0;j--)
41 {
42 if(tag[j]==0)
43 {
44 tag[j]=1;
45 break;
46 }
47 }
48 if(j==0)
49 money-=g[i].m;
50 }
51 cout<<money<<endl;
52 }
53 return 0;
54 }