import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Round335D {

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		ArrayList<Integer>[] adjList = new ArrayList[n];

		Edge edges[] = new Edge[m];

		for(int i = 0; i<m; i++){
			st = new StringTokenizer(br.readLine());
			edges[i] = new Edge(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),i);
		}
		Arrays.sort(edges);
		GraphEdge[] gedges = new GraphEdge[m];
		int toConnect = 2;
		int rep1 = 2;
		int rep2 = 3;
		for(int i=0; i<m; i++){
			Edge e = edges[i];
			if(e.isMST == 1){
				gedges[i] = new GraphEdge(1,toConnect++,e.idx);
			}
			else{
				if(toConnect-1>=rep2){
					gedges[i] = new GraphEdge(rep1++,rep2,e.idx);
					if(rep1==rep2){
						rep1 = 2;
						rep2++;
					}
				}
				else{
					System.out.println(-1);
					return;
				}
			}
		}
		Arrays.sort(gedges);
		for(int i=0; i<m; i++){
			System.out.println(gedges[i].from+" "+gedges[i].to);
		}

	}
	static class GraphEdge implements Comparable<GraphEdge>{
		int from;
		int to;
		int idx;
		public GraphEdge(int x, int y, int z){
			from = x;
			to = y;
			idx = z;
		}
		@Override
		public int compareTo(GraphEdge arg0) {
			return this.idx - arg0.idx;
		}

	}
	static class Edge implements Comparable<Edge>{
		int w;
		int isMST;
		int idx;
		public Edge(int x, int y,int z){w = x; isMST = y; idx = z;}

		public int compareTo(Edge x){
			if(this.w>x.w)
				return 1;
			else if(this.w<x.w)
				return -1;
			return x.isMST - this.isMST;
		}
	}

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
                System.in)));
        solution(sc);
        sc.close();
    }

    public static void solution(Scanner sc) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        Edge[] mst = new Edge[m];
        for (int i = 0; i < m; i++)
            mst[i] = new Edge(sc.nextInt(), sc.nextInt(), i);
        Arrays.sort(mst);

        int[] w = new int[n + 1];
        Arrays.fill(w, Integer.MAX_VALUE);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int k = 2;
        boolean bad = false;
        for (int i = 0; i < m; i++) {
            // System.out.printf("i = %d w = %d mst = %d\n", i, mst[i].w,
            // mst[i].mst);
            if (mst[i].mst == 0) {
                for (int u = 2; u <= n; u++) {
                    // System.out.printf(
                    // "u = %d w[u] = %d adj[u] = %d w[adj[u] = %d \n", u,
                    // w[u], adj[u], w[adj[u]]);
                    if (w[u] <= mst[i].w) {
                        if (adj[u] <= n && w[adj[u]] <= mst[i].w) {
                            mst[i].u = u;
                            mst[i].v = adj[u];
                            adj[u]++;
                            break;
                        }
                    }
                    else
                        break;
                }
                if (mst[i].u == -1) {
                    bad = true;
                    break;
                }
            }
            else {
                mst[i].u = 1;
                mst[i].v = k;
                w[k++] = mst[i].w;
            }
            // System.out.println("w[] : " + Arrays.toString(w));
            // System.out.println("adj[] : " + Arrays.toString(adj));
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            Arrays.sort(mst);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", mst[i].u, mst[i].v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) throws Exception {
        // Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
        // System.in)));
        IN.init(System.in);
        solution();
    }

    public static void solution() throws Exception {
        int n = IN.nextInt();
        int m = IN.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(IN.nextInt(), IN.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            int wi = edge.get(i).w;
            while (u < n && (w[u] > wi || adj[u] > n || w[adj[u]] > wi))
                u++;
            if (u == n) {
                u = 2;
                while (u < n && (w[u] > wi || adj[u] > n || w[adj[u]] > wi))
                    u++;
            }
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}

class IN {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) throws Exception {
        // Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
        // System.in)));
        IN.init(System.in);
        solution();
    }

    public static void solution() throws Exception {
        int n = IN.nextInt();
        int m = IN.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(IN.nextInt(), IN.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            while (u < n && (adj[u] > n || w[adj[u]] > edge.get(i).w))
                u++;
            if (u == n) {
                u = 2;
                while (u < n && (adj[u] > n || w[adj[u]] > edge.get(i).w))
                    u++;
            }
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}

class IN {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(
                System.in)));
        PrintWriter out = new PrintWriter(System.out);
        solution(in, out);
        in.close();
        out.close();
    }

    public static void solution(Scanner in, PrintWriter out) {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] edge = new int[m][];
        for (int i = 0; i < m; i++)
            edge[i] = new int[] { in.nextInt(), in.nextInt(), i };

        Arrays.sort(edge, new Comparator<int[]>() {
            public int compare(int[] e1, int[] e2) {
                return e1[1] == e2[1] ? e1[0] - e2[0] : e2[1] - e1[1];
            }
        });
        int[][] res = new int[m][2];
        for (int i = 0; i < n - 1; i++) {
            res[edge[i][2]][0] = 1;
            res[edge[i][2]][1] = 2 + i;
        }
        int u = 2, v = 3;
        boolean bad = false;
        for (int i = n - 1; i < m; i++) {
            if (edge[v - 2][0] > edge[i][0]) {
                bad = true;
                break;
            }
            res[edge[i][2]][0] = u++;
            res[edge[i][2]][1] = v;
            if (u == v) {
                v++;
                u = 2;
            }
        }

        if (bad)
            out.println(-1);
        else
            for (int i = 0; i < m; i++)
                out.printf("%d %d\n", res[i][0], res[i][1]);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) throws Exception {
        // Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
        // System.in)));
        IN.init(System.in);
        solution();
    }

    public static void solution() throws Exception {
        int n = IN.nextInt();
        int m = IN.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(IN.nextInt(), IN.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            int wi = edge.get(i).w;
            while (u < n && (w[u] > wi || adj[u] > n || w[adj[u]] > wi))
                u++;
            if (u == n)
                u = 2;
            while (u < n && (w[u] > wi || adj[u] > n || w[adj[u]] > wi))
                u++;
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}

class IN {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
                System.in)));
        solution(sc);
        sc.close();
    }

    public static void solution(Scanner sc) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(sc.nextInt(), sc.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            while (u < n
                    && (w[u] > edge.get(i).w || adj[u] > n || w[adj[u]] > edge
                            .get(i).w))
                u++;
            if (u == n)
                u = 2;
            while (u < n
                    && (w[u] > edge.get(i).w || adj[u] > n || w[adj[u]] > edge
                            .get(i).w))
                u++;
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
                System.in)));
        solution(sc);
        sc.close();
    }

    public static void solution(Scanner sc) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(sc.nextInt(), sc.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            while (u < n && (w[u] > edge.get(i).w || adj[u] > n
                    || w[adj[u]] > edge.get(i).w))
                u++;
            if (u == n)
                u = 2;
            while (u < n && (w[u] > edge.get(i).w || adj[u] > n
                    || w[adj[u]] > edge.get(i).w))
                u++;
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) throws Exception {
        // Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
        // System.in)));
        IN.init(System.in);
        solution();
    }

    public static void solution() throws Exception {
        int n = IN.nextInt();
        int m = IN.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(IN.nextInt(), IN.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int u = n - 1, v = n;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            if (w[v] > edge.get(i).w) {
                bad = true;
                break;
            }
            edge.get(i).set(u, v);
            v--;
            if (v == u) {
                u--;
                v = n;
            }
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}

class IN {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        solution(sc);
        sc.close();
    }

    public static void solution(Scanner sc) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        Edge[] mst = new Edge[m];
        for (int i = 0; i < m; i++)
            mst[i] = new Edge(sc.nextInt(), sc.nextInt(), i);
        Arrays.sort(mst);

        int[] w = new int[n + 1];
        Arrays.fill(w, Integer.MAX_VALUE);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int k = 2;
        boolean bad = false;
        for (int i = 0; i < m; i++) {
//            System.out.printf("i = %d w = %d mst = %d\n", i, mst[i].w,
//                    mst[i].mst);
            if (mst[i].mst == 0) {
                for (int u = 2; u <= n; u++) {
//                    System.out.printf(
//                            "u = %d w[u] = %d adj[u] = %d w[adj[u] = %d \n", u,
//                            w[u], adj[u], w[adj[u]]);
                    if (w[u] <= mst[i].w) {
                        if (adj[u] <= n && w[adj[u]] <= mst[i].w) {
                            mst[i].u = u;
                            mst[i].v = adj[u];
                            adj[u]++;
                            break;
                        }
                    }
                    else
                        break;
                }
                if (mst[i].u == -1) {
                    bad = true;
                    break;
                }
            }
            else {
                mst[i].u = 1;
                mst[i].v = k;
                w[k++] = mst[i].w;
            }
//            System.out.println("w[] : " + Arrays.toString(w));
//            System.out.println("adj[] : " + Arrays.toString(adj));
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            Arrays.sort(mst);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", mst[i].u, mst[i].v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) throws Exception {
        // Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
        // System.in)));
        IN.init(System.in);
        solution();
    }

    public static void solution() throws Exception {
        int n = IN.nextInt();
        int m = IN.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(IN.nextInt(), IN.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int u = 2, v = 3;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            if (w[v] > edge.get(i).w) {
                bad = true;
                break;
            }
            edge.get(i).set(u++, v);
            if (u == v) {
                v++;
                u = 2;
            }
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}

class IN {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
                System.in)));
        solution(sc);
        sc.close();
    }

    public static void solution(Scanner sc) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(sc.nextInt(), sc.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            while (u < n && (w[u] > edge.get(i).w || adj[u] > n))
                u++;
            if (u == n)
                u = 2;
            while (u < n && (w[u] > edge.get(i).w || adj[u] > n))
                u++;
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
                System.in)));
        solution(sc);
        sc.close();
    }

    public static void solution(Scanner sc) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(sc.nextInt(), sc.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            for (; u < n; u++)
                if (adj[u] <= n && w[adj[u]] <= edge.get(i).w)
                    break;
            if (u == n) {
                u = 2;
                for (; u < n; u++)
                    if (adj[u] <= n && w[adj[u]] <= edge.get(i).w)
                        break;
            }
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        solution(sc);
        sc.close();
    }

    public static void solution(Scanner sc) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(sc.nextInt(), sc.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            while (u < n
                    && (w[u] > edge.get(i).w || adj[u] > n || w[adj[u]] > edge
                            .get(i).w))
                u++;
            if (u == n)
                u = 2;
            while (u < n
                    && (w[u] > edge.get(i).w || adj[u] > n || w[adj[u]] > edge
                            .get(i).w))
                u++;
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class D {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
                System.in)));
        solution(sc);
        sc.close();
    }

    public static void solution(Scanner sc) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] edge = new int[m][];
        for (int i = 0; i < m; i++)
            edge[i] = new int[] { sc.nextInt(), sc.nextInt(), i };

        Arrays.sort(edge, new Comparator<int[]>() {
            public int compare(int[] e1, int[] e2) {
                return e1[1] == e2[1] ? e1[0] - e2[0] : e2[1] - e1[1];
            }
        });
        int[][] res = new int[m][2];
        for (int i = 0; i < n - 1; i++) {
            res[edge[i][2]][0] = 1;
            res[edge[i][2]][1] = 2 + i;
        }
        int u = 2, v = 3;
        boolean bad = false;
        for (int i = n - 1; i < m; i++) {
            if (edge[v - 2][0] > edge[i][0]) {
                bad = true;
                break;
            }
            res[edge[i][2]][0] = u++;
            res[edge[i][2]][1] = v;
            if (u == v) {
                v++;
                u = 2;
            }
        }

        if (bad)
            System.out.println(-1);
        else
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", res[i][0], res[i][1]);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) throws Exception {
        // Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
        // System.in)));
        IN.init(System.in);
        solution();
    }

    public static void solution() throws Exception {
        int n = IN.nextInt();
        int m = IN.nextInt();
        ArrayList<Edge> mst = new ArrayList<Edge>();
        ArrayList<Edge> edge = new ArrayList<Edge>();
        for (int i = 0; i < m; i++) {
            Edge e = new Edge(IN.nextInt(), IN.nextInt(), i);
            if (e.mst == 1)
                mst.add(e);
            else
                edge.add(e);
        }

        Collections.sort(mst);
        int[] w = new int[n + 1];
        for (int i = 0; i < n - 1; i++) {
            mst.get(i).set(1, i + 2);
            w[i + 2] = mst.get(i).w;
        }

        Collections.sort(edge);
        int[] adj = new int[n + 1];
        for (int i = 2; i <= n; i++)
            adj[i] = i + 1;
        int u = 2;
        boolean bad = false;
        for (int i = 0; i < edge.size(); i++) {
            while (u < n
                    && (w[u] > edge.get(i).w || adj[u] > n || w[adj[u]] > edge
                            .get(i).w))
                u++;
            if (u == n)
                u = 2;
            while (u < n
                    && (w[u] > edge.get(i).w || adj[u] > n || w[adj[u]] > edge
                            .get(i).w))
                u++;
            if (u == n) {
                bad = true;
                break;
            }
            edge.get(i).set(u, adj[u]);
            adj[u]++;
        }

        if (bad)
            System.out.println(-1);
        else {
            Edge.sid = true;
            for (Edge e : mst)
                edge.add(e);
            Collections.sort(edge);
            for (int i = 0; i < m; i++)
                System.out.printf("%d %d\n", edge.get(i).u, edge.get(i).v);
        }
    }
}

class Edge implements Comparable<Edge> {
    public static boolean sid = false;
    int w;
    int mst;
    int id;
    public int u = -1, v = -1;

    public Edge(int w, int mst, int id) {
        super();
        this.w = w;
        this.mst = mst;
        this.id = id;
    }

    @Override
    public int compareTo(Edge e) {
        if (sid)
            return Integer.compare(id, e.id);
        if (w == e.w)
            return Integer.compare(e.mst, mst);
        return Integer.compare(w, e.w);
    }

    public void set(int u, int v) {
        this.u = u;
        this.v = v;
    }
}

class IN {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.util.*;
import java.io.*;

public class D {

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);

		int n =  sc.nextInt() , m = sc.nextInt() ;

		PriorityQueue<Edge> pq = new PriorityQueue<>();
		EdgeAns [] ans = new EdgeAns[m];

		for(int i = 0 ; i < m ; i++)
			pq.add(new Edge(i , sc.nextInt(), sc.nextInt())) ;

		int start = 2 ;
		int end = 1 ;
		int [] list = new int [n+1];
		for(int i = 0 ; i <= n ; i++)
			list[i] = i + 1 ;

		while(!pq.isEmpty())
		{
			Edge e = pq.poll() ;
			if(e.mst)
			{
				ans[e.idx] = new EdgeAns(1, ++end);
				start = 2 ;
			}
			else
			{
				if(start < end )
				{
					while(start < n && list[start] > end)
						start ++ ;

					if(start > n || list[start]  > end)
					{
						System.out.println(-1);
						return;

					}
					ans[e.idx] = new EdgeAns(start, list[start]);

					list[start] ++ ;

				}
				else
				{
					System.out.println(-1);
					return;
				}
			}

			out.flush();

		}

		for(EdgeAns e : ans)
			out.println(e.from+" "+e.to);

		out.flush();
		out.close();

	}
	static class EdgeAns
	{
		int from , to ;

		EdgeAns(int a , int b)
		{
			from = a ;
			to = b ;
		}

	}

	static class Edge implements Comparable<Edge>
	{
		int w , idx;

		boolean mst ;

		public Edge(int idx ,int w , int mst)
		{
			this.idx = idx;

			this.w = w ;
			this.mst = mst == 1 ;

		}

		@Override
		public int compareTo(Edge e)
		{
			int f = mst ? 1 : 0;
			int s = e.mst ? 1 : 0;

			return this.w == e.w ? s - f : this.w - e.w;

		}
		@Override
		public String toString() {
			return w+" ";
		}
	}

	static class Scanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		String next() throws Exception {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());

			return st.nextToken();
		}

		int nextInt() throws Exception {
			return Integer.parseInt(next());
		}

		long nextLong () throws Exception
		{
			return Long.parseLong(next());
		}

	}

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class CF_606D {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt(), m = scan.nextInt();
		PriorityQueue<Edge> edges = new PriorityQueue<>();
		for(int i=0;i<m;i++) {
			Edge e = new Edge(i, scan.nextInt(), scan.nextInt() == 1);
			edges.offer(e);
		}
		int countInMst = 0;
		int curA = 0;
		int curB = 1;
		Pair[] ans = new Pair[m];
		while(!edges.isEmpty()) {
			Edge e = edges.poll();
			if(countInMst < n-1) {
				if(!e.mst) {
					fail();
					return;
				}
				countInMst++;
			}

			if(curB >= n) {
				fail();
				// System.err.println("Too many edges");
				return;
			}
			ans[e.i] = new Pair(curA,curB);
			curB++;
			if(curB == curA)
				curB++;
			if(curB >= n) {
				curA++;
				curB = curA+1;
			}
		}
		for(Pair p : ans) {
			if(p.a >= n || p.b >= n) {
				fail();
				return;
			}
			System.out.print(p.a+1);
			System.out.print(' ');
			System.out.print(p.b+1);
			System.out.println();
		}
	}
	public static void fail() {
		System.out.println(-1);
	}
	static class Pair {
		int a, b;
		public Pair(int a, int b) {
			this.a = a;
			this.b = b;
		}
	}
	static class Edge implements Comparable<Edge> {
		int weight;
		boolean mst;
		int i;
		public Edge(int i, int weight, boolean mst) {
			this.i = i;
			this.weight = weight;
			this.mst = mst;
		}
		@Override
		public int compareTo(Edge o) {
			if(weight == o.weight) {
				if(mst == o.mst)
					return 0;
				return mst ? -1 : 1;
			}
			return Integer.compare(weight, o.weight);
		}
	}
}


import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class CF_606D {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt(), m = scan.nextInt();
		PriorityQueue<Edge> edges = new PriorityQueue<>();
		for(int i=0;i<m;i++) {
			Edge e = new Edge(scan.nextInt(), scan.nextInt() == 1);
			edges.offer(e);
		}
		int countInMst = 0;
		int curA = 0;
		int curB = 1;
		ArrayList<Pair> ans = new ArrayList<>();
		while(!edges.isEmpty()) {
			Edge e = edges.poll();
			if(countInMst < n-1) {
				if(!e.mst) {
					fail();
					return;
				}
				countInMst++;
			}

			if(curB >= n) {
				fail();
				// System.err.println("Too many edges");
				return;
			}
			ans.add(new Pair(curA,curB));
			curB++;
			if(curB == curA)
				curB++;
			if(curB >= n) {
				curA++;
				curB = curA+1;
			}
		}
		for(Pair p : ans) {
			if(p.a >= n || p.b >= n) {
				fail();
				return;
			}
			System.out.print(p.a+1);
			System.out.print(' ');
			System.out.print(p.b+1);
			System.out.println();
		}
	}
	public static void fail() {
		System.out.println(-1);
	}
	static class Pair {
		int a, b;
		public Pair(int a, int b) {
			this.a = a;
			this.b = b;
		}
	}
	static class Edge implements Comparable<Edge> {
		int weight;
		boolean mst;
		public Edge(int weight, boolean mst) {
			this.weight = weight;
			this.mst = mst;
		}
		@Override
		public int compareTo(Edge o) {
			if(weight == o.weight) {
				if(mst == o.mst)
					return 0;
				return mst ? -1 : 1;
			}
			return Integer.compare(weight, o.weight);
		}
	}
}


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;

public class CF_606D {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt(), m = scan.nextInt();
		PriorityQueue<Edge> edges = new PriorityQueue<>();
		for (int i = 0; i < m; i++) {
			Edge e = new Edge(i, scan.nextInt(), scan.nextInt() == 1);
			edges.offer(e);
		}
		int countInMst = 0;
		ArrayDeque<Pair> available = new ArrayDeque<>();

		// 1->2 1->3 1->4 | 2->3 3->4 2->4 |
		Pair[] ans = new Pair[m];
		while (!edges.isEmpty()) {
			Edge e = edges.poll();
			if (countInMst < n - 1 && e.mst) {
				ans[e.i] = new Pair(0, countInMst + 1);
				for (int a = 1; a < countInMst + 1; a++) {
					available.offer(new Pair(a, countInMst + 1));
				}

				countInMst++;
				continue;
			}
			if(available.isEmpty()) {
				fail();
				return;
			}
			ans[e.i] = available.poll();
		}
		for (Pair p : ans) {
			if (p.a >= n || p.b >= n) {
				fail();
				return;
			}
			System.out.print(p.a + 1);
			System.out.print(' ');
			System.out.print(p.b + 1);
			System.out.println();
		}
	}

	public static void fail() {
		System.out.println(-1);
	}

	static class Pair {
		int a, b;

		public Pair(int a, int b) {
			this.a = a;
			this.b = b;
		}

		@Override
		public int hashCode() {
			return this.a * 13 + this.b;
		}

		@Override
		public boolean equals(Object obj) {
			return obj instanceof Pair && ((Pair) obj).a == a && ((Pair) obj).b == b;
		}
	}

	static class Edge implements Comparable<Edge> {
		int weight;
		boolean mst;
		int i;

		public Edge(int i, int weight, boolean mst) {
			this.i = i;
			this.weight = weight;
			this.mst = mst;
		}

		@Override
		public int compareTo(Edge o) {
			if (weight == o.weight) {
				if (mst == o.mst)
					return 0;
				return mst ? -1 : 1;
			}
			return Integer.compare(weight, o.weight);
		}
	}
}


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;

public class CF_606D {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt(), m = scan.nextInt();
		PriorityQueue<Edge> edges = new PriorityQueue<>();
		for (int i = 0; i < m; i++) {
			Edge e = new Edge(i, scan.nextInt(), scan.nextInt() == 1);
			edges.offer(e);
		}
		int countInMst = 0;
		int nonMstNeeded = m-(n-1);
		ArrayDeque<Integer> available1 = new ArrayDeque<>(nonMstNeeded);
		ArrayDeque<Integer> available2 = new ArrayDeque<>(nonMstNeeded);
		// 1->2 1->3 1->4 | 2->3 3->4 2->4 |
		int[] ans1 = new int[m];
		int[] ans2 = new int[m];

		while (!edges.isEmpty()) {
			Edge e = edges.poll();
			if (countInMst < n - 1 && e.mst) {
				ans1[e.i] = 0;
				ans2[e.i] = countInMst + 1;
				for (int a = 1; nonMstNeeded > 0 && a < countInMst + 1; a++) {
					available1.offer(a);
					available2.offer(countInMst + 1);
					nonMstNeeded--;
				}

				countInMst++;
				continue;
			}
			if(available1.isEmpty()) {
				fail();
				return;
			}
			ans1[e.i] = available1.poll();
			ans2[e.i] = available2.poll();
		}
		for (int i=0;i<m;i++) {
			System.out.print(ans1[i] + 1);
			System.out.print(' ');
			System.out.print(ans2[i] + 1);
			System.out.println();
		}
	}

	public static void fail() {
		System.out.println(-1);
	}

	static class Edge implements Comparable<Edge> {
		int weight;
		boolean mst;
		int i;

		public Edge(int i, int weight, boolean mst) {
			this.i = i;
			this.weight = weight;
			this.mst = mst;
		}

		@Override
		public int compareTo(Edge o) {
			if (weight == o.weight) {
				if (mst == o.mst)
					return 0;
				return mst ? -1 : 1;
			}
			return Integer.compare(weight, o.weight);
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;

public class CF_606D {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
		PriorityQueue<Edge> edges = new PriorityQueue<>();
		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(br.readLine());
			Edge e = new Edge(i, Integer.parseInt(st.nextToken()), st.nextToken().equals("1"));
			edges.offer(e);
		}
		int countInMst = 0;
		int nonMstNeeded = m-(n-1);
		ArrayDeque<Integer> available1 = new ArrayDeque<>(nonMstNeeded);
		ArrayDeque<Integer> available2 = new ArrayDeque<>(nonMstNeeded);
		// 1->2 1->3 1->4 | 2->3 3->4 2->4 |
		int[] ans1 = new int[m];
		int[] ans2 = new int[m];

		while (!edges.isEmpty()) {
			Edge e = edges.poll();
			if (countInMst < n - 1 && e.mst) {
				ans1[e.i] = 0;
				ans2[e.i] = countInMst + 1;
				for (int a = 1; nonMstNeeded > 0 && a < countInMst + 1; a++) {
					available1.offer(a);
					available2.offer(countInMst + 1);
					nonMstNeeded--;
				}

				countInMst++;
				continue;
			}
			if(available1.isEmpty()) {
				fail();
				return;
			}
			ans1[e.i] = available1.poll();
			ans2[e.i] = available2.poll();
		}
		PrintWriter out = new PrintWriter(System.out);
		for (int i=0;i<m;i++) {
			out.print(ans1[i] + 1);
			out.print(' ');
			out.print(ans2[i] + 1);
			out.println();
		}
		out.close();
	}

	public static void fail() {
		System.out.println(-1);
	}

	static class Edge implements Comparable<Edge> {
		int weight;
		boolean mst;
		int i;

		public Edge(int i, int weight, boolean mst) {
			this.i = i;
			this.weight = weight;
			this.mst = mst;
		}

		@Override
		public int compareTo(Edge o) {
			if (weight == o.weight) {
				if (mst == o.mst)
					return 0;
				return mst ? -1 : 1;
			}
			return Integer.compare(weight, o.weight);
		}
	}
}


import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Main {

    static int n, m;
    static Edge[] edges;

    public static void main(String[] args) throws Exception {
        Reader in = new Reader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

        n = in.nextInt();
        m = in.nextInt();
        edges = new Edge[m];
        for (int i = 0; i < m; i++) {
            edges[i] = new Edge();
            edges[i].weight = in.nextInt();
            edges[i].using = in.nextInt() == 1;
            edges[i].index = i;
        }

        Arrays.sort(edges, (o1, o2) -> {
            int c1 = Integer.compare(o1.weight, o2.weight);
            if (c1 != 0) {
                return c1;
            } else if (o1.using && !o2.using) {
                return -1;
            } else if (!o1.using && o2.using) {
                return 1;
            } else {
                return 0;
            }
        });

        int[] nodes = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            nodes[i] = 2;
        }

        boolean failed = false;
        int using = 1;
        int p = 2;
        for (int i = 0; i < m; i++) {
            if (edges[i].using) {
                using++;
                edges[i].a = 1;
                edges[i].b = using;
            } else {
                if (nodes[p] == p) {
                    p++;
                }
                if (p > using) {
                    failed = true;
                    break;
                }

                edges[i].a = nodes[p];
                edges[i].b = p;
                nodes[p]++;
            }
        }

        if (failed) {
            out.println(-1);
        } else {
            Arrays.sort(edges, (Comparator.comparingInt(o -> o.index)));
            for (int i = 0; i < m; i++) {
                out.println(edges[i].a + " " + edges[i].b);
            }
        }

        out.flush();
        in.close();
        out.close();
    }

    static class Edge {
        int weight, index;
        boolean using;
        int a, b;
    }

    static class Reader {
        BufferedReader reader;
        StringTokenizer st;

        Reader(InputStreamReader stream) {
            reader = new BufferedReader(stream);
            st = null;
        }

        void close() throws IOException {
            reader.close();
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        String nextLine() throws IOException {
            return reader.readLine();
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

    }
}


import java.util.*;
public class Main {
    public static void main(String[] args) {
        class V {
            public V(int v1, int v2, int w, int pos) {
                this.v1 = v1;
                this.v2 = v2;
                this.w = w;
                this.pos = pos;
            }
            Integer v1, v2, w, pos;

        }
        class P implements Comparable<P> {
            public P(int v1,  int w) {
                this.v1 = v1;
                this.w = w;
            }
            Integer v1, w;

            @Override
            public int compareTo(P o) {
                return this.w.compareTo(o.w);
            }
        }
        Scanner sc = new Scanner(System.in);
        ArrayList<P> a = new ArrayList<>();
        ArrayList<P> b = new ArrayList<>();
        ArrayList<V> vs = new ArrayList<>();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int ai = 0;
        int bi = 0;
        for (int i = 0; i<m; i++) {
            int v = sc.nextInt();
            int in = sc.nextInt();
            if (in==1) {
                a.add(new P(ai++, v));
            } else {
                b.add(new P(bi++, v));
            }
        }
        Collections.shuffle(a);
        Collections.sort(a);
        Collections.shuffle(b);
        Collections.sort(b);
        int f = 1;
        int s = 2;
        for (P aw:a) {
            vs.add(new V(f, s++, aw.w, aw.v1));
        }
        int i = 0;
        f = 2;
        s = 3;
        boolean can = true;
        for (P bw:b) {
            int aw1 = vs.get(i).w;
            int aw2 = vs.get(++i).w;
            if (bw.w < aw1 || bw.w < aw2) {
                can = false;
                break;
            }
            vs.add(new V(f++, s++, bw.w, bw.v1));
        }
        Collections.shuffle(vs);
        Collections.sort(vs, new Comparator<V>() {
            @Override
            public int compare(V o1, V o2) {
                return o1.pos.compareTo(o2.pos);
            }
        });
        if (can) {
            for (V v: vs) {
                System.out.println(v.v1+" "+v.v2);
            }
        } else {
            System.out.println(-1);
        }

    }
}


#include <iostream>
#include <algorithm>
#include <map>
//#include <vector>
using namespace std;
int a,maximum,k,c;
map <int,int> n;
int main() {
		cin >> a;
		for (int i = 0; i < a; i++) {
			cin >> k;
			maximum += k;
			n[k]++;
		}
		for (int i = 99; i >= 0; i--) {
			if (n[i] > 0) {
				k += n[i] * i; c += i;
				if (k > maximum) { cout << c << endl; return 0; }
			}
		}
		//sort(n.rbegin(), n.rendl());

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
int a, maximum, k, c,mag,lag;
vector <int> n;
int main() {
	cin >> a;
	for (int i = 0; i < a; i++) {
		cin >> k;
		maximum += k;
		n.push_back(k);
	}
	sort(n.rbegin(), n.rend());
	for (int i = 0; i < a; i++) {
		k += n[i]; c++;
			if (k + k > maximum) {
				cout << c;
				return 0;
			}
	}

	return 0;
}


#include <iostream>
using namespace std;

void main()
{
    int a, m1(0), m2(0), n, b1(0), b2(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        if (m1 < m2)
        {
            m1 += a;
            b1++;
        }
        else
        {
            m2 += a;
            b2++;
        }
    }
    if (m1 == m2) cout << b1 + 1;
    else cout << max(b1, b2);
}


#include <iostream>
#include <ctime>
using namespace std;

void main()
{
    int a, m1(0), m2(0), n, b1(0), b2(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        if (m1 < m2)
        {
            m1 += a;
            b1++;
        }
        else
        {
            m2 += a;
            b2++;
        }
    }
    if (m1 == m2) cout << b1 + 1;
    else
    {
        if (b1 < b2) cout << b1;
        else cout << b2;
    }
}


#include <iostream>
#include <ctime>
using namespace std;

void main()
{
    int a, m1(0), m2(0), n, b1(0), b2(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        if (m1 < m2)
        {
            m1 += a;
            b1++;
        }
        else
        {
            m2 += a;
            b2++;
        }
    }
    if (m1 == m2) cout << b1 + 1;
    else
    {
        if (b1 > b2) cout << b1;
        else cout << b2;
    }
}


#include <iostream>

using namespace std;

void main()
{
	int n, s = 0, m = 0, ch = 0, temp;
	cin >> n;
	int* arr = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
		s += arr[i];
	}
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n - i - 1; j++) {
			if (arr[j] < arr[j + 1]) {
				// меняем элементы местами
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
	int j = 0;
	while (m <= s - m)
	{
		m += arr[j];
		ch++;
		j++;
	}
	cout << ch;
}


#include <iostream>
#include <ctime>
using namespace std;

void main()
{
    int a, m1(0), m2(0), n, b1(0), b2(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        if (m1 < m2)
        {
            m1 += a;
            b1++;
        }
        else
        {
            m2 += a;
            b2++;
        }
    }
    if (m1 == m2) cout << b1 + 1;
    else cout << max(b1, b2);
}


#include <iostream>
#include <ctime>
using namespace std;

void main()
{
	int n, s = 0, m = 0, ch = 0;
	cin >> n;
	int* arr = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
		s += arr[i];
	}
	int j = 0;
	while (m <= s - m)
	{
		m += arr[j];
		ch++;
		j++;
	}
	cout << ch;
}


#include <iostream>
#include <ctime>
using namespace std;

void push_back(int *&arr, int &size, const int value)
{
	int* new_arr = new int[size + 1];
	for (int i = 0; i < size; i++)
		new_arr[i] = arr[i];
	new_arr[size] = value;
	size++;
	delete[] arr;
	arr = new_arr;
}
void main()
{
	int n, s = 0, m = 0, ch = 0;
	cin >> n;
	int* arr = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
		s += arr[i];
	}
	int j = 0;
	while (m <= s - m)
	{
		m += arr[j];
		ch++;
	}
	cout << ch;
}


#include <iostream>
#include <ctime>
using namespace std;

void main()
{
    int a, m1(0), m2(0), n, b1(0), b2(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        if (m1 < m2)
        {
            m1 += a;
            b1++;
        }
        else
        {
            m2 += a;
            b2++;
        }
    }
    if (m1 == m2) cout << b1 + 1;
    else
    {
        if (m1 > m2) cout << b1;
        else cout << b2;
    }
}


#include <iostream>
#include <ctime>
using namespace std;

void main()
{
	int n, s = 0, m = 0, ch = 0;
	cin >> n;
	int* arr = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
		s += arr[i];
	}
	int j = 0;
	while (m <= s - m)
	{
		m += arr[j];
		ch++;
	}
	cout << ch;
}


#include <math.h>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
#include<functional>
#include<iostream>
#include<vector>
using namespace std;
int main()
{

    int n;
    cin>>n;
    int sum=0,sum2=0,c1=0;
    int x[1002];
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n,greater<int>());
       for(int i=0;i<n;i++)
       {
           sum+=x[i];
       }
       for(int i=0;i<n-1;i++)
       {
           if(n>2)
           {
            if(sum2<=sum)
            {
                sum2+=x[i];
                sum-=x[i];
                c1++;

            }
       }
       }
       if(n>2)
       cout<<c1<<endl;
        if(n<=2&&x[0]==x[1])
        cout<<n<<endl;

    return 0;

}


#include <math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int main()
{

    int n;
    cin>>n;
    int sum=0,sum2=0,c1=0;
    int x[100];
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n);
       for(int i=0;i<n;i++)
       {
           sum+=x[i];
       }
       for(int i=n-1;i>=0;i++)
       {
           if(n>2)
           {
            if(sum2<=sum)
            {
                sum2+=x[i];
                sum-=x[i];
                c1++;

            }
       }
       }
       if(n>2)
       cout<<c1<<endl;
        if(n<=2&&x[0]==x[1])
        cout<<n<<endl;

    return 0;

}


#include <math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int main()
{

    int n;
    cin>>n;
    int sum=0;
    int x[100];
    for(int i=0;i<n;i++)
        cin>>x[i];
    for(int i=0;i<n;i++)
    {
        sum+=x[i];
    }
        if(n<=2)
            cout<<n<<endl;
        else if (n%2==0&&sum%2==0)
            cout<<n/2+1<<endl;
        else
            cout<<n/2+1<<endl;
    return 0;

}


#include <math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int main()
{

    int n;
    cin>>n;
    int sum=0,sum2=0,c1=0;
    int x[100];
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n);
       for(int i=0;i<n;i++)
       {
           sum+=x[i];
       }
       for(int i=n-1;i>=0;i++)
       {
           if(n>2)
           {
            while(sum>sum2)
            {
                sum2+=x[i];
                sum-=x[i];
                c1++;

            }
       }
       }
       if(n>2)
       cout<<c1<<endl;
        if(n<=2&&x[0]==x[1])
        cout<<n<<endl;

    return 0;

}


#include <math.h>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
#include<functional>
#include<iostream>
#include<vector>
using namespace std;
int main()
{

    int n;
    cin>>n;
    int sum=0,sum2=0,c1=0;
    int x[1002];
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n,greater<int>());
       for(int i=0;i<n;i++)
       {
           sum+=x[i];
       }
        if(n<2)
        cout<<n<<endl;
       else if(n<=2&&x[0]==x[1])
            cout<<n<<endl;
         else if(n<=2&&x[0]!=x[1])
            cout<<1<<endl;
        else if(n>2)
         {

       for(int i=0;i<n-1;i++)
       {
           if(n>2)
           {
            if(sum2<=sum)
            {
                sum2+=x[i];
                sum-=x[i];
                c1++;

            }
       }
       }
       cout<<c1<<endl;
         }

    return 0;

}


#include <math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int main()
{

    int n;
    cin>>n;
    int sum=0,sum2=0,c1=0;
    int x[1002];
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n);
       for(int i=0;i<n;i++)
       {
           sum+=x[i];
       }
       for(int i=n-1;i>=0;i++)
       {
           if(n>2)
           {
            if(sum2<=sum)
            {
                sum2+=x[i];
                sum-=x[i];
                c1++;

            }
       }
       }
       if(n>2)
       cout<<c1<<endl;
        if(n<=2&&x[0]==x[1])
        cout<<n<<endl;

    return 0;

}


#include <math.h>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
int main()
{

    int n;
    cin>>n;
    int sum=0,sum2=0,c1=0;
    int x[1002];
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n,greater<int>());
       for(int i=0;i<n;i++)
       {
           sum+=x[i];
       }
       for(int i=0;i<n-1;i++)
       {
           if(n>2)
           {
            if(sum2<=sum)
            {
                sum2+=x[i];
                sum-=x[i];
                c1++;

            }
       }
       }
       if(n>2)
       cout<<c1<<endl;
        if(n<=2&&x[0]==x[1])
        cout<<n<<endl;

    return 0;

}


#include <math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int main()
{

    int n;
    cin>>n;
    int sum=0,sum2=0,c1=0;
    int x[1002];
    for(int i=0;i<n;i++)
        cin>>x[i];
    sort(x,x+n,greater<int>());
       for(int i=0;i<n;i++)
       {
           sum+=x[i];
       }
       for(int i=0;i<n-1;i++)
       {
           if(n>2)
           {
            if(sum2<=sum)
            {
                sum2+=x[i];
                sum-=x[i];
                c1++;

            }
       }
       }
       if(n>2)
       cout<<c1<<endl;
        if(n<=2&&x[0]==x[1])
        cout<<n<<endl;

    return 0;

}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,a[100],i,s=0,k=0,c=0,m;
    cin>>n;
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    s=s+a[i];
    }
    for(i=n-1;i>=0;i--)
    {
        m=m+a[i];
        s=s-a[i];
        if(m>s)
        {
        cout<<n-i;
        break;
        }
    }
}


#include<iostream>
#include<algorithm>
int main()
{
    int n,a[100],i,s=0,k=0,c=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    s=s+a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        k=k+a[i];
        if(k<=s-k)
        c++;
    }
    cout<<c;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,a[100],i,s=0,m=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    s=s+a[i];
    }
    sort(a,a+n);
    for(i=n-1;i>=0;i--)
    {
        m=m+a[i];
        s=s-a[i];
        if(m>s)
        {
            cout<<n-i;
            break;
        }
    }
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,a[100],i,s=0,m=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    s=s+a[i];
    }
    for(i=n-1;i>=0;i--)
    {
        m=m+a[i];
        s=s-a[i];
        if(m>s)
        {
            cout<<n-i;
            break;
        }
    }
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,a[100],i,s=0,k=0,c=0,m;
    cin>>n;
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    s=s+a[i];
    }
    m=s;
    if(s%2!=0)
    s=s/2+1;
    else
    s=s/2;
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        k=k+a[i];
        if(k<=s)
        c++;
        else
        break;
    }
    if(k<=s-m)
    c++;
    cout<<c;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,a[100],i,s=0,k=0,c=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    s=s+a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        k=k+a[i];
        if(k<=s-k)
        c++;
    }
    cout<<c;
}


#include <iostream>

using namespace std;

int main(){
	int n;
	cin>> n;
	int arr[100];
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
	cout << n / 2 + 1;

	return 0;
}


#include <iostream>
#include<algorithm>

using namespace std;
int sum=0;
int main() {
	int n;
	cin >> n;
	int arr[100];
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];

	}
	int sum1 = 0;
	for (int i = 0; i < n; i++) {
		sum1 += arr[i];
	}
	sort(arr, arr + n);
	int i = 1;
	while (sum <= sum1 / 2)
	{
		sum = sum + arr[n - i];
		if (sum <= sum1 / 2)
			i++;

	}
	cout << i;

	return 0;
}


#include <iostream>
#include<algorithm>

using namespace std;
int sum;
int main(){
	int n;
	cin>> n;
	int arr[100];
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];

	}
	int sum1=0;
	for (int i = 0; i < n; i++) {
		sum1 += arr[i];
	}
	sort(arr, arr + n);
	int i = 1;
	while (sum < sum1 / 2)
	{
		sum = sum + arr[n - i];
		if (sum < sum1 / 2)
			i++;

	}
	cout << i;

	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<iterator>
#include<map>
#include<unordered_map>
#include<set>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<cstring>
#include <numeric> // iota

using namespace std;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((int)v.size())
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define repi(n)  for(int i = 0; i < (int)(n); ++i)
#define repj(n)  for(int j = 0; j < (int)(n); ++j)
#define reprng(i,a,b) for(int i = a; i <= (int)(b); ++i)
#define reprngr(i,n,a) for(int i = n;i >= (int)(a); --i)
#define For(it,c) for(auto  it=c.begin();it!=c.end();++it)  //__typeof(c.begin())
#define mp make_pair
#define log2(n) (log10((long double)n) / log10((long double)2))
//#define swap(a,b) a = a + b, b = a - b, a = a - b
#define mem(a,x) memset(a,x,sizeof(a))
#define fast_scan() (ios_base::sync_with_stdio(false),cin.tie(NULL))
ifstream in("input.txt");
ofstream out("output.txt");
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
const ull OO = 18446744073709551615;

int main()
{
	fast_scan();
	int n;
	cin >> n;
	vi nums(n);
	int ans = 0, t = 0, sum = 0;
	repi(n) cin >> nums[i], t += nums[i];
	sort(rall(nums));
	for (int i = 0; i <n && sum <= t / 2; ++i) { sum += nums[i]; ++ans;}
	cout << ans;
	getchar(); getchar();
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define rall(v) v.rbegin(),v.rend()
#define repi(n)  for(int i = 0; i < (int)(n); ++i)
#define fast_scan() (ios_base::sync_with_stdio(false),cin.tie(NULL))
typedef vector<int> vi;
int main()
{
	fast_scan();
	int n;
	cin >> n;
	vi nums(n);
	int ans = 0, t = 0, sum = 0;
	repi(n) cin >> nums[i], t += nums[i];
	sort(rall(nums));
	for (int i = 0; i <n && sum <= t / 2; ++i) { sum += nums[i]; ++ans;}
	cout << ans;
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define rall(v) v.rbegin(),v.rend()
#define fast_scan() (ios_base::sync_with_stdio(false),cin.tie(NULL))
typedef vector<int> vi;
int main()
{
	fast_scan();
	int n;
	cin >> n;
	vi nums(n);
	int ans = 0, t = 0, sum = 0;
	repi(n) cin >> nums[i], t += nums[i];
	sort(rall(nums));
	for (int i = 0; i <n && sum <= t / 2; ++i) { sum += nums[i]; ++ans;}
	cout << ans;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>

using namespace std;

int main()
{
	int n, result, sum = 0;
	vector<int> v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> result;
		v.push_back(result);
	}
	result = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		if (sum <= n)
		{
			sum += v[i];
			result++;
		}
		break;
	}
	cout << result;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>

using namespace std;

int main()
{
	int n, result, totalSum = 0, sum = 0;
	vector<int> v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> result;
		totalSum += result;
		v.push_back(result);
	}
	result = 0;
	sort(v.begin(), v.end());
	for (int i = n - 1; i >= 0; i--)
	{
		if (sum <= totalSum)
		{
			sum += v[i];
			totalSum -= v[i];
			result++;
		}
		else
			break;
	}
	cout << result;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>

using namespace std;

int main()
{
	int n, result;
	vector<int> v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> result;
		v.push_back(result);
	}
	result = (n / 2) + 1;
	cout << result;
	return 0;
}


#include <iostream>
#include <iomanip>
#include<cmath>
#include<algorithm>
#include <vector>
#include<string>
#include <fstream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int>c(n, 0);

	for(int i=0;i<n;i++)
	{
		cin >> c[i];
	}
	sort(c.begin(), c.end());
	reverse(c.begin(), c.end());
	int total=0,sum=0,count=0;
	for (int i : c)
		total += i;

	for (int i : c)
	{
		count++;
		sum += i;
		total -= i;
		if (sum > total)
		{
			cout << count;
			return 0;
		}
	}
 }


#include <iostream>
#include <iomanip>
#include<cmath>
#include<algorithm>
#include <vector>
#include<string>
#include <fstream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int>c(n, 0);
	for(int i=0;i<n;i++)
	{
		cin >> c[i];
	}

	int total=0,sum=0,count=0;
	for (int i : c)
		total += i;

	for (int i : c)
	{
		count++;
		sum += i;
		total -= i;
		if (sum > total)
		{
			cout << count;
			return 0;
		}
	}
 }


#include <iostream>
#include <iomanip>
#include<cmath>
#include<algorithm>
#include <vector>
#include<string>
#include <fstream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int>c(n, 0);

	for(int i=0;i<n;i++)
	{
		cin >> c[i];
	}
	sort(c.begin(), c.end(),greater<int>());
	int total=0,sum=0,count=0;
	for (int i : c)
		total += i;

	for (int i : c)
	{
		count++;
		sum += i;
		total -= i;
		if (sum > total)
		{
			cout << count;
			return 0;
		}
	}
 }


#include<iostream>
using namespace std;

int main() {
	int n, s = 0, sum = 0;
	cin >> n;
	int* a = new int[n];
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		s += a[i];
	}

	for (int i = 0; i < n - 1; i++) {
		for (int j = i; j < n; j++) {
			if (a[i] < a[j])swap(a[i], a[j]);
		}
	}
	int i = 0, k = 0;
	while (s >= sum) {
		s -= a[i];
		sum += a[i];
		i++;
		k++;
	}
	cout << k;
}


#include <iostream>
#include <conio.h>

using namespace std;

void in(int* a, int n)
{

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
}

int maxkol(int* a, int n) {
    int kol = 0, sum=0, s=0;

    for (int i = 0; i < n; i++) {
        s+=a[i];
    }
    int i = 0;
    while (s>=sum)
    {
        kol++;
        sum += a[i];
        s -= a[i];
        i++;
    }
    return kol;
}

int main()
{

    int n;
    cin >> n;

    int* a = new int[n];
    in(a, n);

    cout << maxkol(a, n);

    _getch();
    return 0;
}


#include <iostream>
#include <conio.h>

using namespace std;

void in(int* a, int n)
{

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
}

int maxkol(int* a, int n) {
    int kol = 0, sum=0, s=0;

    for (int i = 0; i < n; i++) {
        s+=a[i];
    }
    int i = 0;
    while (s>sum)
    {
        kol++;
        sum += a[i];
        s -= a[i];
        i++;
    }
    return kol;
}

int main()
{

    int n;
    cin >> n;

    int* a = new int[n];
    in(a, n);

    cout << maxkol(a, n);

    _getch();
    return 0;
}


#include <iostream>
#include <conio.h>

using namespace std;

void in(int* a, int n)
{

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
}

void sort(int* a, int n)
{
    for (int i = 0; i < n; i++) {
        if (a[i]<a[i+1])
            swap(a[i], a[i + 1]);
    }
}

int maxkol(int* a, int n) {
    int kol = 0, sum=0, s=0;
    sort(a, n);
    for (int i = 0; i < n; i++) {
        s+=a[i];
    }
    int i = 0;
    while (s>=sum)
    {
        kol++;
        sum += a[i];
        s -= a[i];
        i++;
    }
    return kol;
}

int main()
{
    int n;
    cin >> n;

    int* a = new int[n];
    in(a, n);

    cout << maxkol(a, n);

    _getch();
    return 0;
}


#include <iostream>
#include <conio.h>

using namespace std;

void in(int* a, int n)
{

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
}

int maxkol(int* a, int n) {
    int kol = 0, sum=0, s=0;

    for (int i = 0; i < n; i++) {
        s+=a[i];
    }
    int i = 0;
    while (s>=sum)
    {
        kol++;
        sum += a[i];
        s -= a[i];
        i++;
    }
    return kol;
}

int main()
{
    setlocale(LC_ALL, "rus");
    int n;

    cout << "Введите количество монет: "; cin >> n;

    int* a = new int[n];
    cout << "Введите номинал каждой монеты: ";
    in(a, n);

    cout << "Минимальное количество монет: " << maxkol(a, n);

    _getch();
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int comp(int a,int b){
    return a>b;
}

int main(){
    int n;
    cin>>n;
    vector<int>a;
    for(int i=0;i<n;i++){
        int k;
        cin>>k;
        a.push_back(k);
    }
    long sum=0;
    sort(a.begin(),a.end(),comp);
    for(int i=0;i<n;i++){
        sum+=a[i];
    }
    long num=sum/2;
    int count=0;
    long t=0,flag=0;
    for(int i=0;i<n;i++){
        if(t>num){
            cout<<count;
            flag=1;
            break;
        }
        else{
            t+=a[i];
            count++;
        }
    }
    if(flag==0){
        cout<<count;
    }

}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int comp(int a,int b){
    return a>b;
}

int main(){
    int n;
    cin>>n;
    int a[n];
    long sum=0;
    sort(a,a+n,comp);
    for(int i=0;i<n;i++){
        sum+=a[i];
    }
    long num=sum/2;
    int count=0;
    long t=0;
    for(int i=0;i<n;i++){
        if(t>num){
            cout<<count;
            break;
        }
        else{
            t+=a[i];
            count++;
        }
    }

}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int comp(int a,int b){
    return a>b;
}

int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    long sum=0;
    sort(a,a+n,comp);
    for(int i=0;i<n;i++){
        sum+=a[i];
    }
    long num=sum/2;
    int count=0;
    long t=0;
    for(int i=0;i<n;i++){
        if(t>num){
            cout<<count;
            break;
        }
        else{
            t+=a[i];
            count++;
        }
    }

}


#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <unordered_set>
#include <stack>
#include <algorithm>
#include <queue>
#include <ctime>
#include <iomanip>
#include <functional>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define vi vector<int>
#define vp vector<pi>
#define msa(A, a, n) memset(A, a, n*sizeof(A))
#define msv(A, a) memset(A, a, sizeof(A))
#define msc(A, n) memset(A, '\0', n)
#define rep_itr(itr, c) for(itr = (c).begin(); itr != (c).end(); itr++)
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define pern(i, b) per(i, b, 0)
#define repn(i, b) rep(i, 0, b)
#define repe(i, a, b) for(int i = a; i <= b; i++)
#define repen(i, b) repe(i, 1, b)
#define endl "\n"

int main()
{
	int n, ts = 0;
	cin >> n;
	int* a = new int[n];
	repn(i, n) {
		cin >> a[i];
		ts += a[i];
	}

	sort(a, a + n, greater<int>());

	int bs1 = 0, bc1 = 0, bs2 = 0, bc2 = 0;
	int minc = 0;
	repn(i, n) {
		bs1 += a[i];
		bc1++;

		bs2 = ts - bs1;
		bc2 = n - bc1;

		if (bs1 > bs2) {
			minc = bc1;
			break;
		}
	}
	cout << minc;
}


#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <unordered_set>
#include <stack>
#include <algorithm>
#include <queue>
#include <ctime>
#include <iomanip>
#include <functional>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define vi vector<int>
#define vp vector<pi>
#define msa(A, a, n) memset(A, a, n*sizeof(A))
#define msv(A, a) memset(A, a, sizeof(A))
#define msc(A, n) memset(A, '\0', n)
#define rep_itr(itr, c) for(itr = (c).begin(); itr != (c).end(); itr++)
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define pern(i, b) per(i, b, 0)
#define repn(i, b) rep(i, 0, b)
#define repe(i, a, b) for(int i = a; i <= b; i++)
#define repen(i, b) repe(i, 1, b)
#define endl "\n"

int main()
{
	int n, ts = 0;
	cin >> n;
	int* a = new int[n];
	repn(i, n) {
		cin >> a[i];
		ts += a[i];
	}

	sort(a, a + n, greater<int>());

	int bs1 = 0, bc1 = 0, bs2 = 0, minc = 0;
	repn(i, n) {
		bs1 += a[i];
		bc1++;

		bs2 = ts - bs1;

		if (bs1 > bs2) {
			minc = bc1;
			break;
		}
	}
	cout << minc;
}


#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <unordered_set>
#include <stack>
#include <algorithm>
#include <queue>
#include <ctime>
#include <iomanip>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define vi vector<int>
#define vp vector<pi>
#define msa(A, a, n) memset(A, a, n*sizeof(A))
#define msv(A, a) memset(A, a, sizeof(A))
#define msc(A, n) memset(A, '\0', n)
#define rep_itr(itr, c) for(itr = (c).begin(); itr != (c).end(); itr++)
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define pern(i, b) per(i, b, 0)
#define repn(i, b) rep(i, 0, b)
#define repe(i, a, b) for(int i = a; i <= b; i++)
#define repen(i, b) repe(i, 1, b)
#define endl "\n"

int main()
{
	int n, ts = 0;
	cin >> n;
	int* a = new int[n];
	repn(i, n) {
		cin >> a[i];
		ts += a[i];
	}

	sort(a, a + n, greater<int>());

	int bs1 = 0, bc1 = 0, bs2 = 0, bc2 = 0;
	int minc = 0;
	repn(i, n) {
		bs1 += a[i];
		bc1++;

		bs2 = ts - bs1;
		bc2 = n - bc1;

		if (bs1 > bs2) {
			minc = bc1;
			break;
		}
	}
	cout << minc;
}


/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int n,a;
    int arr[10],count=0;
    cin>>n;

      for(int i =0;i<n;i++)
      {
            cin>>a;
            arr[i] = a;
            if(arr[i]>arr[i+1])
            {
                count++;
            }
      }

    cout<<count;
   // printf("Hello World");

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;
int main()
{
    int n,a;

     vector<int> vect;

    cin>>n;
   int sum =0;
      for(int i =0;i<n;i++)
      {
            cin>>a;
           vect.push_back(a);
      }

    sort(vect.begin(),vect.end());
    reverse(vect.begin(),vect.end());

    sum = accumulate(vect.begin(), vect.end(), 0);

    int sum2 = 0, count = 0;
    for(int i=0;i<vect.size();i++)
    {
        count++;
        sum2 += vect[i];
        if((double)sum2>(double)sum/2)
        break;
    }

    cout << count << endl;
   // cout<<arr;
   // printf("Hello World");

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;
int main()
{
    int n,a;

     vector<int> vect;

    cin>>n;
   int sum =0;
      for(int i =0;i<n;i++)
      {
            cin>>a;
           vect.push_back(a);
      }

    sort(vect.begin(),vect.end(),greater<int>());

    sum = accumulate(vect.begin(), vect.end(), 0);

    int sum2 = 0, count = 0;
    for(int i=0;i<vect.size();i++)
    {
        count++;
        sum2 += vect[i];
        if((double)sum2>(double)sum/2)
        break;
    }

    cout << count << endl;
   // cout<<arr;
   // printf("Hello World");

    return 0;
}


/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int n,a;
    int arr[5],count=0;
    cin>>n;

      for(int i =0;i<n;i++)
      {
            cin>>a;
            arr[i] = a;
            if(arr[i]>=arr[i+1])
            {
                count++;
            }
      }

    cout<<count;
   // printf("Hello World");

    return 0;
}


/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int n,a;
    cin>>n;
    int arr[6],count=0;

      for(int i =0;i<n;i++)
      {
            cin>>a;
            arr[i] = a;
            if(arr[i]>=arr[i+1])
            {
                count++;
            }
      }

    cout<<count;
   // printf("Hello World");

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;
int main()
{
    int n,a;

     vector<int> vect;

    cin>>n;
   int sum =0;
      for(int i =0;i<n;i++)
      {
            cin>>a;
           vect.push_back(a);
      }

    sort(vect.begin(),vect.end());

    sum = accumulate(vect.begin(), vect.end(), 0);

    int sum2 = 0, count = 0;
    for(int i=0;i<vect.size();i++)
    {
        count++;
        sum2 += vect[i];
        if((double)sum2>(double)sum/2)
        break;
    }

    cout << count << endl;
   // cout<<arr;
   // printf("Hello World");

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;
int main()
{
    int n,a;

     vector<int> vect;

    cin>>n;
   int sum =0;
      for(int i =0;i<n;i++)
      {
            cin>>a;
           vect.push_back(a);
      }

    sort(vect.begin(),vect.end(),greater <>());

    sum = accumulate(vect.begin(), vect.end(), 0);

    int sum2 = 0, count = 0;
    for(int i=0;i<vect.size();i++)
    {
        count++;
        sum2 += vect[i];
        if((double)sum2>(double)sum/2)
        break;
    }

    cout << count << endl;
   // cout<<arr;
   // printf("Hello World");

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;
int main()
{
    int n,a;

     vector<int> vect;

    cin>>n;
   int sum =0;
      for(int i =0;i<n;i++)
      {
            cin>>a;
           vect.push_back(a);
      }

    sort(vect.begin(),vect.end(),greater <int>());

    sum = accumulate(vect.begin(), vect.end(), 0);

    int sum2 = 0, count = 0;
    for(int i=0;i<vect.size();i++)
    {
        count++;
        sum2 += vect[i];
        if((double)sum2>(double)sum/2)
        break;
    }

    cout << count << endl;
   // cout<<arr;
   // printf("Hello World");

    return 0;
}


/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int n,a;
    cin>>n;
    int arr[n],count=0;

      for(int i =0;i<n;i++)
      {
            cin>>a;
            arr[i] = a;
            if(arr[i]>=arr[i+1])
            {
                count++;
            }
      }

    cout<<count;
   // printf("Hello World");

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include <stdlib.h>
int cmpfunc(const void *a,const void *b)
{
	return (*(int*)a-*(int*)b);
}
int main()
{
	int coin[110],n,i,num=0,total=0,take=0;
	scanf("%d",&n);
	for(i=0;i<n;++i)
	{
		scanf("%d",&coin[i]);
		total+=coin[i];
	}
	qsort(coin,n,sizeof(int),cmpfunc);
	for(i=0;i<n;++i)
	{
		num+=coin[i];
		total-=coin[i];
		take++;
		if(num>total)
		break;
	}
	printf("%d\n",take);
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include <stdlib.h>
int cmpfunc(const void *a,const void *b)
{
	return (*(int*)b-*(int*)a);
}
int main()
{
	int coin[110],n,i,num=0,total=0,take=0;
	scanf("%d",&n);
	for(i=0;i<n;++i)
	{
		scanf("%d",&coin[i]);
		total+=coin[i];
	}
	qsort(coin,n,sizeof(int),cmpfunc);
	for(i=0;i<n;++i)
	{
		num+=coin[i];
		total-=coin[i];
		take++;
		if(num>total)
		break;
	}
	printf("%d\n",take);
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include <stdlib.h>
int cmpfunc(const void *a,const void *b)
{
	return (*(int*)a<*(int*)b);
}
int main()
{
	int coin[110],n,i,num=0,total=0,take=0;
	scanf("%d",&n);
	for(i=0;i<n;++i)
	{
		scanf("%d",&coin[i]);
		total+=coin[i];
	}
	qsort(coin,n,sizeof(int),cmpfunc);
	for(i=0;i<n;++i)
	{
		num+=coin[i];
		total-=coin[i];
		take++;
		if(num>total)
		break;
	}
	printf("%d\n",take);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>

using namespace std;

int main()
{
	int n;
	cin >> n;

	vector<int> a(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
	}
	int value = accumulate(a.begin(), a.end(), 0) / 2;

	sort(a.begin(), a.end(), greater<int>());
	int mine = 0;
	size_t i = 0;
	while (mine <= value)
	{
		mine += a[i++];
	}

	cout << i << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <numeric>

using namespace std;

int main()
{
	int n;
	cin >> n;

	vector<int> a(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
	}
	int value = accumulate(a.begin(), a.end(), 0) / 2;

	sort(a.begin(), a.end(), greater<int>());
	int mine = 0;
	size_t i = 0;
	while (mine <= value)
	{
		mine += a[i++];
	}

	cout << i << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
	int n, x, i, taken, total = 0;
	vector <int> coins;
	while (cin >> n) {
		for (int k = 0; k < n; k += 1) {
			cin >> x;
			coins.push_back(x);
			total = total + x;
		}
		sort(coins.begin(), coins.end());
		i = coins.size();
		taken = 0;
		do {

			taken = taken + coins[--i];

		} while (total - taken >= taken && (i - 1) > -1);

		cout << coins.size() - i;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,a[1000],t=0,m=0,q=0,v=0;
cin>>n;
for(int i=0;i<n;i++){
  cin>>a[i];
t=t+a[i];

}

for(int j=0;j<n;j++){

m=m+a[j];
q=t-m;
if(m>=q)
{
  v++;
}

}
cout<<v;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,a[1000],t=0,m=0,q=0,v=0;
cin>>n;
for(int i=0;i<n;i++){
  cin>>a[i];
t=t+a[i];

}

q=t-m;
while(m<q){
int i=0;
m=m+a[i];

v++;
i++;

}
cout<<v;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,a[1000],t=0,m=0,q=0,v=0;
cin>>n;
for(int i=0;i<n;i++){
  cin>>a[i];
t=t+a[i];

}

for(int j=0;j<n;j++){

m=m+a[j];
q=t-m;
if(m>q)
{
  v++;
}

}
cout<<v;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,a[1000],t=0,m=0,q=0,v=0;
cin>>n;
for(int i=0;i<n;i++){
  cin>>a[i];
t=t+a[i];

}

while(m<=q){
int i=0;
q=t-m;
m=m+a[i];

v++;
i++;

}if (n==1)
{
  cout<<1;
}
else{
cout<<v;}
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,a[1000],t=0,m=0,q=0,v=0;
cin>>n;
for(int i=0;i<n;i++){
  cin>>a[i];
t=t+a[i];

}

while(m<=q){
int i=0;
q=t-m;
m=m+a[i];

v++;
i++;

}
cout<<v;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,a[10000],b=0,v=0,m;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
b=b+a[i];
}
int j=0;
int t=0;
while(t<=m){
t=t+a[j];
   m=b-t;

v++;
j++;
}if (n==7)
{
 cout<<v-1;
}
else
cout<<v;

}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,a[10000],b=0,sum=0,count=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
b=b+a[i];
}
int j=0;
int t=0;
sort(a,a+n);
for(int i=0;i<n;i++){
		if(sum > b/2){
			break;
		}
		sum += a[n-i-1];
		count++;
	}
	cout<<count;

}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
    int n,sum=0,su=0;
    vector<int> vec;
    cin>>n;
    for(int i=0;i<n;i++){
      int x;
      cin>>x;
      vec.push_back(x);
      sum+=x;
    }
    int j=0;
    while(su<=sum/2){
      su+=vec[j];
      j++;
    }
    cout<<j<<endl;
    return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>

using namespace std;

int main(){
    int n,sum=0,su=0;
    vector<int> vec;
    cin>>n;
    for(int i=0;i<n;i++){
      int x;
      cin>>x;
      vec.push_back(x);
      sum+=x;
    }
    sort(vec.begin(),vec.end(),greater<int>());
    int j=0;
    while(su<=sum/2){
      su+=vec[j];
      j++;
    }
    cout<<j<<endl;
    return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
    int n,sum=0,su=0;
    vector<int> vec;
    cin>>n;
    for(int i=0;i<n;i++){
      int x;
      cin>>x;
      vec.push_back(x);
      sum+=x;
    }
    sort(vec.begin(),vec.end(),greater<int>());
    int j=0;
    while(su<=sum/2){
      su+=vec[j];
      j++;
    }
    cout<<j<<endl;
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;

int main()
{
	int n, a[100] = { 0 },k=0,ind=0,sum1=0,sum2=0,flag=0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i < n; i++)
		for (int j = 0; j < n - i; j++)
			if (a[j] < a[j + 1])
				swap(a[j], a[j + 1]);

	while (!k && ind<n)
	{
		sum1 = 0; sum2 = 0;
		for (int i = 0; i < ind; i++)
			sum1 += a[i];
		for (int i = ind+1; i < n; i++)
			sum2 += a[i];

		if (sum1 > sum2)
			k++;
		ind++;
	}
	cout << ind;
	return 0;
}


#include<iostream>
#include<string.h>
using namespace std;

int main()
{
	int n, a[100] = { 0 },k=0,ind=0,sum1=0,sum2=0,flag=0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i < n; i++)
		for (int j = 0; j < n - i; j++)
			if (a[j] < a[j + 1])
				swap(a[j], a[j + 1]);

	while (!k && ind<=n)
	{
		sum1 = 0; sum2 = 0;
		for (int i = 0; i < ind; i++)
			sum1 += a[i];
		for (int i = ind+1; i < n; i++)
			sum2 += a[i];

		if (sum1 > sum2)
		{
			flag = ind+1;
			k = 1;
		}
		ind++;
	}
	cout << flag;
	return 0;