What is wrong with this code for DPAIRS

DPAIRS
Few subtasks passed.

Idea is to sort both arrays and combine 1st element of N to all elements of M, again 2nd element to all elements of M and so on , until we get n + m - 1 pairs.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n, m;
    cin>>n>>m;
    vector<pair<int, int> >N(n);
    vector<pair<int , int> >M(m);
    int x;
    for(int i = 0; i < n; i++){
        cin>>x;
        N[i] = make_pair(x, i);
    }
    for(int i = 0; i < m; i++){
        cin>>x;
        M[i] = make_pair(x, i);
    }

    sort(N.begin(), N.end());
    sort(M.begin(), M.end());

    long count = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(count == n + m - 1)
                return 0;
            cout<<N[i].second<<' '<<M[j].second<<'\n';
            count++;
        }
    }
    return 0;
}

please help.

consider the test case

3 3
1 2 3
1 2 3

Got it.
Thanks