#include <iostream> #include <algorithm> usingnamespace std; typedef struct Hm { int score; int day; }Hm; bool cmp ( Hm a, Hm b ) { if ( a.score != b.score ) { return a.score > b.score ; } else { return a.day < b.day; } } int main() { int T; cin >> T; while ( T -- ) { int N; cin >> N; Hm *hm =new Hm[N]; int flag[1001] ={0}; for ( int i =0; i != N ; ++ i ) { cin >> hm[i].day; } for ( int i =0; i != N ; ++ i ) { cin >> hm[i].score; } sort ( hm, hm + N, cmp ); int total =0; for ( int i =0; i != N; ++ i ) { int j; for ( j = hm[i].day; j !=0; -- j ) { if ( !flag[j] ) { flag[j] =1; break; } } if ( !j ) { total += hm[i].score; } } cout << total << endl; delete [] hm; } return0; }