Directly greedy, sorted from small to large, and then compared to OK
The Code is as follows:
[Cpp]
# Include <iostream>
# Include <queue>
# Include <algorithm>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <string>
# Include <cmath>
Using namespace std;
Int I, j;
Const int n= 20010;
Typedef long LL;
Int a [N], B [N];
Int n, m;
Int main ()
{
While (scanf ("% d", & n, & m) & (n + m )! = 0)
{
For (I = 0; I <n; I ++)
Scanf ("% d", & a [I]);
For (I = 0; I <m; I ++)
Scanf ("% d", & B [I]);
Sort (a, a + n );
Sort (B, B + m );
Int sum = 0, cur = 0;
For (I = 0, j = 0; I <n; I ++)
{
While (B [j] <a [I] & j <m)
{
J ++;
}
If (j> = m)
Break;
If (B [j]> = a [I])
{
Cur ++;
Sum + = B [j];
J ++;
}
}
If (cur> = n)
Printf ("% d \ n", sum );
Else
Printf ("Loowater is doomed! \ N ");
}
Return 0;
}
/*
2 3
5
4
7
8
4
2 1
5
5
10
0 0
*/
# Include <iostream>
# Include <queue>
# Include <algorithm>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <string>
# Include <cmath>
Using namespace std;
Int I, j;
Const int n= 20010;
Typedef long LL;
Int a [N], B [N];
Int n, m;
Int main ()
{
While (scanf ("% d", & n, & m) & (n + m )! = 0)
{
For (I = 0; I <n; I ++)
Scanf ("% d", & a [I]);
For (I = 0; I <m; I ++)
Scanf ("% d", & B [I]);
Sort (a, a + n );
Sort (B, B + m );
Int sum = 0, cur = 0;
For (I = 0, j = 0; I <n; I ++)
{
While (B [j] <a [I] & j <m)
{
J ++;
}
If (j> = m)
Break;
If (B [j]> = a [I])
{
Cur ++;
Sum + = B [j];
J ++;
}
}
If (cur> = n)
Printf ("% d \ n", sum );
Else
Printf ("Loowater is doomed! \ N ");
}
Return 0;
}
/*
2 3
5
4
7
8
4
2 1
5
5
10
0 0
*/