বুধবার, ২ মার্চ, ২০১৬

code:

#include < iostream >
#include < cstdio >
#include < algorithm >

using namespace std;
int c1[11000],c2[11000];
int x,y;
int rec(int x_left , int y_left) {

    if(x_left==0 && y_left==0) {
        return 0;
    }
    if(x_left==0) {
        int tmp=y-y_left;
        int ans=0;
        while ( tmp < y ) {
            ans += c2[tmp];
            tmp++;
        }
        return ans;
    }
    if(y_left==0) {
        int tmp=x-x_left;
        int ans=0;
        while ( tmp < x ) {
            ans += c1[tmp];
            tmp++;
        }
        return ans;
    }
    int ans=0;
    if(c1[x-x_left]>c2[y-y_left]) {
        ans=c1[x-x_left]+rec( x_left-1,y_left)+rec(0,y_left );
    } else {
        ans=c2[y-y_left]+rec(x_left,y_left-1)+rec( x_left,0 );
    }
    return ans;


}
int main() {
    int tc;
    scanf("%d",&tc);
    while(tc--) {

        cin >> x >> y;
        x--,y--;
        for(int i=0; i < x; i++) {
            cin >> c1[i];
        }
        for(int i=0; i < y; i++) {
            cin>>c2[i];
        }
        sort(c1 , c1+x);
        sort(c2 , c2+y);
        reverse( c1 , c1+x );
        reverse( c2 , c2+y );
        cout << rec( x , y ) << endl;
    }
}