package Contest;
import java.io.*;
import java.util.*;
import java.math.*;
import java.util.regex.*;

public class A {

static class Pair<E, V> implements Comparable<Pair<E, V>>{
       E a;
       V b;
       public Pair(E x, V y) {a = x;b=y;}
       public int compareTo(Pair<E, V> p){
           return Integer.compare((Integer)a, (Integer)p.a);
       }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (Integer)a;
            result = prime * result + (Integer)b;

            return result;
        }
        @Override
        public boolean equals(Object obj) {
            Pair<E, V> cur = (Pair<E, V>)obj;
            if((Integer)a == (Integer)cur.a && (Integer)b == (Integer)cur.b)return true;
            if((Integer)b == (Integer)cur.a && (Integer)a == (Integer)cur.b)return true;
            return false;
        }
 }
public static void main(String[] args) throws Exception {
   new Thread(null, null, "Anshum Gupta", 99999999) {
        public void run() {
            try {
                solve();
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }.start();
}
public static long gcd(long a,long b)
{
  if(a<b)
    return gcd(b,a);
  if(b==0)
    return a;
  return gcd(b,a%b);

}
static long lcm(int a,int b) {
  return  a*b / gcd(a,b);
}
static int[] z(char[]arr) {
    int l = 0, r = 0;
    int[]z = new int[n];
    for(int i=1; i<n; i++) {
        if(i > r) {
            l = r = i;
            while(r < n && arr[r-l] == arr[r])r++;
            z[i] = r-- - l;
        }else {
            int k = i - l;
            if(z[k] < r - i + l)z[i] = z[k];
            else {
                l = i;
                while(r < n && arr[r-l] == arr[r])r++;
                z[i] = r-- - l;
            }
        }
    }
    return z;
}
static long pow(long x,long y){
    if(y == 0)return 1;
    if(y==1)return x;
    long a = pow(x,y/2);
    a = (a*a)%mod;
    if(y%2==0){
        return a;
    }
    return (a*x)%mod;
}
static long my_inv(long a) {
    return pow(a,mod-2);
}
static long bin(int a,int b) {
    if(a < b || a<0 || b<0)return 0;
    return ((fact[a]*inv_fact[a-b])%mod * inv_fact[b])%mod;
}
static void make_facts() {
  fact=new long[mxN];
  inv_fact = new long[mxN];
  fact[0]=inv_fact[0]=1L;
  for(int i=1;i<mxN;i++) {
       fact[i] = (i*fact[i-1])%mod;
       inv_fact[i] = my_inv(fact[i]);
  }
}
static final long mxx = (long)(1e18+5);
static final int mxN = (int)(1e5);
static final int mxV = (int)(1e5+5), log = 18;
static long mod = (long)(1e9+7); //998244353;//
static int[]tree, lazy, dep;
static long[]fact, inv_fact;
static final int INF = (int)1e9+5;
static boolean[]vis;
static ArrayList<ArrayList<Integer>> adj;
static int n, m, k, x, y, z, q;
static char[] str;
static int[]a;
static StringBuilder sb;
static int move() {
	String str = sb.toString();
	int oc = 0;
	for(int i=0; i<str.length(); i++) {
		if(str.charAt(i) == '1')oc++;
	}
	if(oc == 0) {
		sb = new StringBuilder("");
//		out.println("hello");
		return 0;
	}else {
		int ans = 0;
		int ml = 0, st = -1;
		for(int i=0; i<str.length(); i++) {
			int j = i;
			int cnt = 0;
			while(j < str.length() && str.charAt(j) == '1') {
				j++;
				cnt++;
			}
			if(cnt > ml) {
				ml = cnt;
				st = i;
			}
			if(j > i) {
				i = j - 1;
			}
		}
		assert(st >= 0);
		sb.delete(st, st + ml);
//		out.println(sb.toString());
		return ml;
	}
}
	public static void solve() throws Exception {
	   // solve the problem here
			s = new MyScanner();
	   		out = new PrintWriter(new BufferedOutputStream(System.out), true);
//	   		out = new PrintWriter("output.txt");
	        int tc = s.nextInt();
	        while(tc-->0){
	        	String str = s.next();//toCharArray();
	        	sb = new StringBuilder(str);
	        	int cnt = 0, turn = 0;
//	        	move();
	        	while(sb.toString().length() > 0) {
	        		if(turn % 2 == 0) {
	        			cnt += move();
	        		}else move();
	        		turn ^= 1;
//	        		out.println(cnt);
	        	}
	        	out.println(cnt);
	        }

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

public static PrintWriter out;
public static MyScanner s;
static class MyScanner {

    BufferedReader br;
    StringTokenizer st;

    public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    public MyScanner(String fileName) {
    	try {
			br = new BufferedReader(new FileReader(fileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
    }
    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() { return Integer.parseInt(next()); }
    long nextLong() { return Long.parseLong(next()); }
    double nextDouble() { return Double.parseDouble(next()); }
    int[] nextIntArray(int n){
    	int[]a = new int[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextInt();
    	}
    	return a;
    }
    long[] nextlongArray(int n) {
    	long[]a = new long[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextLong();
    	}
    	return a;
    }
    Integer[] nextIntegerArray(int n){
    	Integer[]a = new Integer[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextInt();
    	}
    	return a;
    }
    Long[] nextLongArray(int n) {
    	Long[]a = new Long[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextLong();
    	}
    	return a;
    }
    char[][] next2DCharArray(int n, int m){
    	char[][]arr = new char[n][m];
    	for(int i=0; i<n; i++) {
    		arr[i] = this.next().toCharArray();
    	}
    	return arr;
    }
    ArrayList<ArrayList<Integer>> readUndirectedUnweightedGraph(int n, int m) {
    	ArrayList<ArrayList<Integer>>adj = new ArrayList<ArrayList<Integer>>();
    	for(int i=0; i<=n; i++)adj.add(new ArrayList<Integer>());
    	for(int i=0; i<m; i++) {
    		int u = s.nextInt();
    		int v = s.nextInt();
    		adj.get(u).add(v);
    		adj.get(v).add(u);
    	}
    	return adj;
    }
    ArrayList<ArrayList<Integer>> readDirectedUnweightedGraph(int n, int m) {
    	ArrayList<ArrayList<Integer>>adj = new ArrayList<ArrayList<Integer>>();
    	for(int i=0; i<=n; i++)adj.add(new ArrayList<Integer>());
    	for(int i=0; i<m; i++) {
    		int u = s.nextInt();
    		int v = s.nextInt();
    		adj.get(u).add(v);
    	}
    	return adj;
    }
    String nextLine(){
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}

}


import java.util.*;

public class substingRemoval {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int tests = Integer.parseInt(scan.nextLine());
        for(int i = 0; i < tests; i++){
            int answer = 0;
            String bstr = scan.nextLine().strip();
            String[] arr = bstr.split("0");
            Arrays.sort(arr);
            for(int j = arr.length - 1; j >= 0; j -= 2){
                answer += arr[j].length();
            }
            System.out.println(answer);
        }

    }
}


import java.util.*;

public class substringremoval {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            String[] s = sc.next().split("0");
            Arrays.sort(s, Comparator.reverseOrder());
            int c=0,k=0;
            for(String i:s){
                if(k%2==0)c+=i.length();
                k++;
            }
            System.out.println(c);
        }
    }
}


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

/*
By : SSD
*/

public class B {
    static long mod = (long) (1e9 + 7);

    static class InputReader1 {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader1(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            long num = 0;
            int b;
            boolean minus = false;
            while ((b = read()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = read();
            }
            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = read();
            }
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, nextInt());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, nextInt());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        private static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void println(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }
    }

    public static void main(String[] ssd) throws Exception {
        InputReader1 sc = new InputReader1(System.in);
        OutputWriter out = new OutputWriter(System.out);

        int t = sc.nextInt();
        while (t-- > 0) {
            String[] s =sc.next().split("0");

          /*  System.out.println(Arrays.toString(s));
            System.out.println(s.length);*/
            ArrayList<Integer> a= new ArrayList<>();
            for (int i = 0; i <s.length ; i++) {
                if (!s[i].isEmpty())
                {
                    a.add(s[i].length());
                }

            }

            /*System.out.println("a: "+a);*/
            Collections.sort(a);
            Collections.reverse(a);
            int aa=0,bb=0;
            for (int i = 0; i <a.size() ; i++) {
                if (i%2==0)
                {
                    aa+=a.get(i);
                }
                else
                {
                    bb+=a.get(i);
                }

            }

            out.println(aa);

        }
        out.close();
    }

}


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

/*
By : SSD
*/

public class B {
    static long mod = (long) (1e9 + 7);

    static class InputReader1 {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader1(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            long num = 0;
            int b;
            boolean minus = false;
            while ((b = read()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = read();
            }
            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = read();
            }
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, nextInt());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, nextInt());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        private static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void println(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }
    }

    public static void main(String[] ssd) throws Exception {
        InputReader1 sc = new InputReader1(System.in);
        OutputWriter out = new OutputWriter(System.out);

        int t = sc.nextInt();
        while (t-- > 0) {
            String s[]=sc.next().split("0");

          /*  System.out.println(Arrays.toString(s));
            System.out.println(s.length);*/
            ArrayList<Integer> a= new ArrayList<>();
            for (int i = 0; i <s.length ; i++) {
                if (!s[i].isEmpty())
                {
                    a.add(s[i].length());
                }

            }

            /*System.out.println("a: "+a);*/
            Collections.sort(a);
            Collections.reverse(a);
            int aa=0,bb=0;
            for (int i = 0; i <a.size() ; i++) {
                if (i%2==0)
                {
                    aa+=a.get(i);
                }
                else
                {
                    bb+=a.get(i);
                }

            }

            out.println(aa);

        }
        out.close();
    }

}


import java.util.*;
public class ab1{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0)
		{
			String s=sc.next();
			StringBuilder sb=new StringBuilder(s);

			int flag=0;
			int alice=0;
			int flag1=0;
			int count=0;
			List<Integer> li=new ArrayList<Integer>();
		    for(int i=0;i<sb.length();i++)
		    {
		    	if(sb.charAt(i)=='1')
		    	{
		    		count++;
		    	}
		    	else
		    	{
		    		System.out.println(count);
		    		li.add(count);

		    		count=0;
		    	}

		    }
		    if(count>0)
		    {
		    	li.add(count);
		    }
		   Collections.sort(li);
		   int sum=0;
		   for(int i=li.size()-1;i>=0;i=i-2)
		   {
			   sum=sum+li.get(i);
		   }
		  System.out.println(sum);
		}
	}

}


import java.util.*;
public class ab1{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0)
		{
			String s=sc.next();
			StringBuilder sb=new StringBuilder(s);

			int flag=0;
			int alice=0;
			int flag1=0;
			int count=0;
			List<Integer> li=new ArrayList<Integer>();
		    for(int i=0;i<sb.length();i++)
		    {
		    	if(sb.charAt(i)=='1')
		    	{
		    		count++;
		    	}
		    	else
		    	{

		    		li.add(count);

		    		count=0;
		    	}

		    }
		    if(count>0)
		    {
		    	li.add(count);
		    }
		   Collections.sort(li);
		   int sum=0;
		   for(int i=li.size()-1;i>=0;i=i-2)
		   {
			   sum=sum+li.get(i);
		   }
		  System.out.println(sum);
		}
	}

}


import java.lang.reflect.Array;
import java.util.*;

public class helloworld {
	public static int ways(int cx,int cy , int gx, int gy,int a [][],int n ,int m) {
		if(cx>=n || cy>=m) {
			return 0;
		}
		else if(a[cx][cy]==0) {
			return 0;
		}
		else if (cx==gx && cy ==gy) {
			return 1;

		}
		else {
			return ways(cx,cy+1,gx,gy,a,n,m) + ways(cx+1,cy,gx,gy,a,n,m);
		}

	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();

		for(int i = 0;i<t;i++) {

			String s = in.next();
			int len = 0;
			int v = 0;
			int p =0;

			for(int j = 0;j<s.length();j++) {
				char u = s.charAt(j);
				if(u == '0') {
					v = v+1;
				}
			}
			int arr3[]= new int[10];
			for(int x = 0;x< s.length();x++) {
				char f = s.charAt(x);
				if(f == '1') {
					len = len+1;
				}
				else {

					if(len==0) {
						continue;
					}
					else {
						arr3[p] = len;
						p++;
						len = 0;
					}

				}

			}
			arr3[p] = len;
			int h = 0;

			for(;h<arr3.length;h++) {
				if(arr3[h]==0) {
					break;
				}

			}
			int arr4[] = new int[h];
			for(int y = 0;y< arr4.length;y++) {
				arr4[y]= arr3[y];
			}
//			for(int k = 0;k<arr4.length;k++) {
//			System.out.println(arr4[k]);
//			}
			Arrays.sort(arr4);
			int sum = 0;
			for(int q = arr4.length-1;q>=0;q-=2) {
				sum = sum+ arr4[q];
			}
		    System.out.println(sum);

//			int p = 0;
//			int v = 0;
//			for(int j = 0;j<s.length();j++) {
//				char u = s.charAt(j);
//				if(u == '0') {
//					v= v+1;
//				}
//			}
//			int arr[] = new int[v];
//			for(int j = 0;j<s.length();j++) {
//				char r = s.charAt(j);
//				if(r == '0') {
//					arr[p] = j;
//					p++;
//				}
//			}
//			for(int k = 0;k<arr.length;k++) {
//				System.out.println(arr[k]);
//			}
//
//			if(arr[0]==0) {
//				int arr2[] = new int[]
//
//			}
		}

	}
}


package helloworld;
import java.lang.reflect.Array;
import java.util.*;

public class helloworld {
	public static int ways(int cx,int cy , int gx, int gy,int a [][],int n ,int m) {
		if(cx>=n || cy>=m) {
			return 0;
		}
		else if(a[cx][cy]==0) {
			return 0;
		}
		else if (cx==gx && cy ==gy) {
			return 1;

		}
		else {
			return ways(cx,cy+1,gx,gy,a,n,m) + ways(cx+1,cy,gx,gy,a,n,m);
		}

	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();

		for(int i = 0;i<t;i++) {

			String s = in.next();
			int len = 0;
			int v = 0;
			int p =0;

			for(int j = 0;j<s.length();j++) {
				char u = s.charAt(j);
				if(u == '0') {
					v = v+1;
				}
			}
			int arr3[]= new int[999];
			for(int x = 0;x< s.length();x++) {
				char f = s.charAt(x);
				if(f == '1') {
					len = len+1;
				}
				else {

					if(len==0) {
						continue;
					}
					else {
						arr3[p] = len;
						p++;
						len = 0;
					}

				}

			}
			arr3[p] = len;
			int h = 0;

			for(;h<arr3.length;h++) {
				if(arr3[h]==0) {
					break;
				}

			}
			int arr4[] = new int[h];
			for(int y = 0;y< arr4.length;y++) {
				arr4[y]= arr3[y];
			}
//			for(int k = 0;k<arr4.length;k++) {
//			System.out.println(arr4[k]);
//			}
			Arrays.sort(arr4);
			int sum = 0;
			for(int q = arr4.length-1;q>=0;q-=2) {
				sum = sum+ arr4[q];
			}
		    System.out.println(sum);

//			int p = 0;
//			int v = 0;
//			for(int j = 0;j<s.length();j++) {
//				char u = s.charAt(j);
//				if(u == '0') {
//					v= v+1;
//				}
//			}
//			int arr[] = new int[v];
//			for(int j = 0;j<s.length();j++) {
//				char r = s.charAt(j);
//				if(r == '0') {
//					arr[p] = j;
//					p++;
//				}
//			}
//			for(int k = 0;k<arr.length;k++) {
//				System.out.println(arr[k]);
//			}
//
//			if(arr[0]==0) {
//				int arr2[] = new int[]
//
//			}
		}

	}
}


import java.lang.reflect.Array;
import java.util.*;

public class helloworld {
	public static int ways(int cx,int cy , int gx, int gy,int a [][],int n ,int m) {
		if(cx>=n || cy>=m) {
			return 0;
		}
		else if(a[cx][cy]==0) {
			return 0;
		}
		else if (cx==gx && cy ==gy) {
			return 1;

		}
		else {
			return ways(cx,cy+1,gx,gy,a,n,m) + ways(cx+1,cy,gx,gy,a,n,m);
		}

	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();

		for(int i = 0;i<t;i++) {

			String s = in.next();
			int len = 0;
			int v = 0;
			int p =0;

			for(int j = 0;j<s.length();j++) {
				char u = s.charAt(j);
				if(u == '0') {
					v = v+1;
				}
			}
			int arr3[]= new int[999];
			for(int x = 0;x< s.length();x++) {
				char f = s.charAt(x);
				if(f == '1') {
					len = len+1;
				}
				else {

					if(len==0) {
						continue;
					}
					else {
						arr3[p] = len;
						p++;
						len = 0;
					}

				}

			}
			arr3[p] = len;
			int h = 0;

			for(;h<arr3.length;h++) {
				if(arr3[h]==0) {
					break;
				}

			}
			int arr4[] = new int[h];
			for(int y = 0;y< arr4.length;y++) {
				arr4[y]= arr3[y];
			}
//			for(int k = 0;k<arr4.length;k++) {
//			System.out.println(arr4[k]);
//			}
			Arrays.sort(arr4);
			int sum = 0;
			for(int q = arr4.length-1;q>=0;q-=2) {
				sum = sum+ arr4[q];
			}
		    System.out.println(sum);

//			int p = 0;
//			int v = 0;
//			for(int j = 0;j<s.length();j++) {
//				char u = s.charAt(j);
//				if(u == '0') {
//					v= v+1;
//				}
//			}
//			int arr[] = new int[v];
//			for(int j = 0;j<s.length();j++) {
//				char r = s.charAt(j);
//				if(r == '0') {
//					arr[p] = j;
//					p++;
//				}
//			}
//			for(int k = 0;k<arr.length;k++) {
//				System.out.println(arr[k]);
//			}
//
//			if(arr[0]==0) {
//				int arr2[] = new int[]
//
//			}
		}

	}
}


import java.lang.reflect.Array;
import java.util.*;

public class helloworld {
	public static int ways(int cx,int cy , int gx, int gy,int a [][],int n ,int m) {
		if(cx>=n || cy>=m) {
			return 0;
		}
		else if(a[cx][cy]==0) {
			return 0;
		}
		else if (cx==gx && cy ==gy) {
			return 1;

		}
		else {
			return ways(cx,cy+1,gx,gy,a,n,m) + ways(cx+1,cy,gx,gy,a,n,m);
		}

	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();

		for(int i = 0;i<t;i++) {

			String s = in.next();
			int len = 0;
			int v = 0;
			int p =0;

			for(int j = 0;j<s.length();j++) {
				char u = s.charAt(j);
				if(u == '0') {
					v = v+1;
				}
			}
			int arr3[]= new int[99999999];
			for(int x = 0;x< s.length();x++) {
				char f = s.charAt(x);
				if(f == '1') {
					len = len+1;
				}
				else {

					if(len==0) {
						continue;
					}
					else {
						arr3[p] = len;
						p++;
						len = 0;
					}

				}

			}
			arr3[p] = len;
			int h = 0;

			for(;h<arr3.length;h++) {
				if(arr3[h]==0) {
					break;
				}

			}
			int arr4[] = new int[h];
			for(int y = 0;y< arr4.length;y++) {
				arr4[y]= arr3[y];
			}
//			for(int k = 0;k<arr4.length;k++) {
//			System.out.println(arr4[k]);
//			}
			Arrays.sort(arr4);
			int sum = 0;
			for(int q = arr4.length-1;q>=0;q-=2) {
				sum = sum+ arr4[q];
			}
		    System.out.println(sum);

//			int p = 0;
//			int v = 0;
//			for(int j = 0;j<s.length();j++) {
//				char u = s.charAt(j);
//				if(u == '0') {
//					v= v+1;
//				}
//			}
//			int arr[] = new int[v];
//			for(int j = 0;j<s.length();j++) {
//				char r = s.charAt(j);
//				if(r == '0') {
//					arr[p] = j;
//					p++;
//				}
//			}
//			for(int k = 0;k<arr.length;k++) {
//				System.out.println(arr[k]);
//			}
//
//			if(arr[0]==0) {
//				int arr2[] = new int[]
//
//			}
		}

	}
}


package helloworld;
import java.lang.reflect.Array;
import java.util.*;

public class helloworld {
	public static int ways(int cx,int cy , int gx, int gy,int a [][],int n ,int m) {
		if(cx>=n || cy>=m) {
			return 0;
		}
		else if(a[cx][cy]==0) {
			return 0;
		}
		else if (cx==gx && cy ==gy) {
			return 1;

		}
		else {
			return ways(cx,cy+1,gx,gy,a,n,m) + ways(cx+1,cy,gx,gy,a,n,m);
		}

	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();

		for(int i = 0;i<t;i++) {

			String s = in.next();
			int len = 0;
			int v = 0;
			int p =0;

			for(int j = 0;j<s.length();j++) {
				char u = s.charAt(j);
				if(u == '0') {
					v = v+1;
				}
			}
			int arr3[]= new int[10];
			for(int j = 0;j< s.length();j++) {
				char f = s.charAt(j);
				if(f == '1') {
					len = len+1;
				}
				else {

					if(len==0) {
						continue;
					}
					else {
						arr3[p] = len;
						p++;
						len = 0;
					}

				}

			}
			arr3[p] = len;
			int h = 0;

			for(;h<arr3.length;h++) {
				if(arr3[h]==0) {
					break;
				}

			}
			int arr4[] = new int[h];
			for(int y = 0;y< arr4.length;y++) {
				arr4[y]= arr3[y];
			}
//			for(int k = 0;k<arr4.length;k++) {
//			System.out.println(arr4[k]);
//			}
			Arrays.sort(arr4);
			int sum = 0;
			for(int u = arr4.length-1;u>=0;u-=2) {
				sum = sum+ arr4[u];
			}
		    System.out.println(sum);

//			int p = 0;
//			int v = 0;
//			for(int j = 0;j<s.length();j++) {
//				char u = s.charAt(j);
//				if(u == '0') {
//					v= v+1;
//				}
//			}
//			int arr[] = new int[v];
//			for(int j = 0;j<s.length();j++) {
//				char r = s.charAt(j);
//				if(r == '0') {
//					arr[p] = j;
//					p++;
//				}
//			}
//			for(int k = 0;k<arr.length;k++) {
//				System.out.println(arr[k]);
//			}
//
//			if(arr[0]==0) {
//				int arr2[] = new int[]
//
//			}
		}

	}
}


import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		in.nextLine();
		while (t-- > 0) {
			String s = in.nextLine();
			String[] arr = s.split("0");
			ArrayList<Integer> al = new ArrayList<>();

			for (String st : arr) {
				if (st.length() != 0) {
					al.add(st.length());
				}
			}

			if (al.size() == 0) {
				System.out.println(0);
			} else {
				int sum = 0;
				Collections.sort(al);
				for (int i = 0; i < al.size(); i += 2) {
					sum += al.get(i);
				}
				System.out.println(sum);
			}
		}
	}
}


import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		in.nextLine();
		while (t-- > 0) {
			String s = in.nextLine();
			String[] arr = s.split("0");
			ArrayList<Integer> al = new ArrayList<>();

			for (String st : arr) {
				if (st.length() != 0) {
					al.add(st.length());
				}
			}

			if (al.size() == 0) {
				System.out.println(0);
			} else {
				int sum = 0;
				Collections.sort(al);
				for (int i = al.size() - 1; i >= 0; i -= 2) {
					sum += al.get(i);
				}
				System.out.println(sum);
			}
		}
	}
}


import java.util.*;
public class Main {
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        while(tc-- > 0) {
            String b = sc.next();
            int s1 = 0, s2 = 0, c = 0, n = b.length();
            ArrayList<Integer> v = new ArrayList<Integer>();
            for(int i=0;i<n;i++) {
                char ch=b.charAt(i);
                if(ch=='1') c++;
                else {
                    if(c>0) v.add(c);
                    c=0;
                }
            }
            if(c>0) v.add(c);
            Collections.sort(v, Collections.reverseOrder());
            int x = 1;
            for(int el : v) {
                if(x > 0) {
                    s1 += el;
                    x ^= 1;
                }
                else {
                    s2 += el;
                    x ^= 1;
                }
            }
            System.out.println(s1);
        }
    }
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Test2 {
	private static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();

		while (t-- > 0) {
			char[] s = sc.next().toCharArray();
			int[] count = new int[102];

			int idx = 0;
			for (int i = 0; i < s.length; i++) {
				if (s[i] == '1') {
					count[idx]++;
				} else {
					if (count[idx] != 0)
						idx++;
				}
			}
			if (count[idx] != 0)
				idx++;
			java.util.Arrays.sort(count, 0, idx);

			int res = 0;
			for (int i = 0; i < idx; i += 2)
				res += count[idx - i - 1];
			out.println(res);
		}
		out.flush();
	}

	private static int min(int a, int b) {
		return Math.min(a, b);
	}

	private static class Scanner {
		public BufferedReader reader;
		public StringTokenizer st;

		public Scanner(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream));
			st = null;
		}

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

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

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

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

	private static int max(int a, int b) {
		return Math.max(a, b);
	}

}


import java.util.*;

public class Test {
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int n = sc.nextInt();
        String w = sc.nextLine();
        for (int i = 0; i < n; i++) {
            ArrayList<Integer> freqs = new ArrayList<>();
            String inp = sc.nextLine();
            int prev0;
            for (prev0 = 0; prev0 < inp.length(); prev0++) {
                if (inp.charAt(prev0) == '1') {
                    prev0--;
                    break;
                }
            }
            int num1 = 0;
            for (int j = prev0 + 1; j < inp.length(); j++) {
                num1 = 0;
                while (j < inp.length() && inp.charAt(j) == '1') {
                    num1++;
                    j++;
                }
                freqs.add(j - prev0 - 1);
                prev0 = j;
            }
            Collections.sort(freqs);
            int score = 0;
            for (int j = 0; j < freqs.size(); j += 2) {
                score += freqs.get(j);
            }
            System.out.println(score);
        }
    }
}


import java.util.*;

public class Test {
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int n = sc.nextInt();
        String w = sc.nextLine();
        for (int i = 0; i < n; i++) {
            ArrayList<Integer> freqs = new ArrayList<>();
            String inp = sc.nextLine();
            int prev0;
            for (prev0 = 0; prev0 < inp.length(); prev0++) {
                if (inp.charAt(prev0) == '1') {
                    prev0--;
                    break;
                }
            }
            int num1 = 0;
            for (int j = prev0 + 1; j < inp.length(); j++) {
                num1 = 0;
                while (j < inp.length() && inp.charAt(j) == '1') {
                    num1++;
                    j++;
                }
                freqs.add(j - prev0 - 1);
                prev0 = j;
            }
            freqs.sort(Collections.reverseOrder());
            int score = 0;
            for (int j = 0; j < freqs.size(); j += 2) {
                score += freqs.get(j);
            }
            System.out.println(score);
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
public class jav{
  public static void main(String[] args) throws IOException{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int t =Integer.parseInt(br.readLine());
    while(t-->0){
      String s=br.readLine();
      char curr=s.charAt(0);
      int ans=0;
      ArrayList<Integer> a=new ArrayList<>();
      for(int i=0;i<s.length();i++){
        if(s.charAt(i)==curr&&s.charAt(i)=='1'){
          a.set(a.size(),a.get(a.size()-1)+1);
        }else if(s.charAt(i)=='1'){
          a.add(1);
        }
      }
      Collections.sort(a);
      for(int i=a.size();i>=0;i+=2){
        ans+=a.get(i);
      }
      System.out.println(ans);
    }
  }
}


import java.util.*;
import java.io.*;
import java.math.*;
public class jav{
  public static void main(String[] args) throws IOException{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int t =Integer.parseInt(br.readLine());
    while(t-->0){
      String s=br.readLine();
      char curr=s.charAt(0);
      int ans=0;
      ArrayList<Integr> a=new ArrayList<>();
      for(int i=0;i<s.length();i++){
        if(s.charAt(i)==curr&&s.charAt(i)=='1'){
          a.set(a.get(a.size()-1)+1);
        }else if(s.charAt(i)=='1'){
          a.add(1);
        }
      }
      Collections.sort(a);
      for(int i=a.size();i>=0;i+=2){
        ans+=a.get(i);
      }
      System.out.println(ans);
    }
  }
}


import java.util.*;
import java.io.*;
import java.math.*;
public class jav{
  public static void main(String[] args) throws IOException{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int t =Integer.parseInt(br.readLine());
    while(t-->0){
      String s=br.readLine();
      char curr=s.charAt(0);
      int ans=0;
      ArrayList<Integer> a=new ArrayList<>();
      for(int i=0;i<s.length();i++){
        if(s.charAt(i)==curr&&s.charAt(i)=='1'){
          a.set(a.get(a.size()-1)+1);
        }else if(s.charAt(i)=='1'){
          a.add(1);
        }
      }
      Collections.sort(a);
      for(int i=a.size();i>=0;i+=2){
        ans+=a.get(i);
      }
      System.out.println(ans);
    }
  }
}


#include <iostream>
#include <algorithm>
using namespace std;
pair<long long, long long> A[100005];
long long ans, ans2;
int main()
{
    long long n;
    cin >> n;
    if (n == 1)
    {
        cout << "0";
        return 0;
    }
    for (long long i = 0; i < n; ++i)
    {
        cin >> A[i].first >> A[i].second;
    }
    sort(A, A + n);
    long long koord = 100000005;

    for (long long g = n - 1; g >= 0; g--)
    {
        if(koord <= A[g].first)
        {
            ans++;
        }
        else
        {
            koord = min(koord, (A[g].first - A[g].second));
        }
        //cout << "g = " << g << endl;
        //cout << "ans = " << ans << endl;
        //cout << "koord = " << koord << endl;
    }
    koord = 1000000005;
    ans2++;
    for (long long g = n - 2; g >= 0; g--)
    {
        if(koord <= A[g].first)
        {
            ans2++;
        }
        else
        {
            koord = min(koord, (A[g].first - A[g].second));
        }

        //cout << "g = " << g << endl;
        //cout << "ans2 = " << ans2 << endl;
        //cout << "koord = " << koord << endl;
    }
    cout << min(ans, ans2);
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
pair<int, int> A[100005];
int D[100005];
int main()
{
    int n, k, ans;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> A[i].first >> A[i].second;
    }
    sort(A, A + n);
    ans = 1000005;
    for (int p = 0; p < n; ++p)
    {
        //cout << "p = " << p << endl;
        k = lower_bound(A, A + p, make_pair((A[p].first - A[p].second), -1)) - A;
        //cout << "k = " << k << endl;
        //if (p == n - 1)
        //{
        //}
        D[p] = D[k - 1] + p - k;
        //cout << "D[" << k - 1 << "]] = " << D[k - 1] << endl;
        ans = min(n - (p + 1) + D[p], ans);
        //cout << "ans = " << ans << endl;
    }
    cout << ans;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
#include<queue>
#include<stack>
#include<list>

using namespace std;

typedef long long ll;
const int MAXN=100005;

struct node{
    friend bool operator<( node p,node q  ){
        return p.x<q.x;
    }
    int x,y;
} a[MAXN];;

int pre[MAXN];
int dp[MAXN];
int dpans[MAXN];
ll bit[1000005];
int n;

int sum(int i){
    int s=0;
    while(i>0){
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}

void add(int i,int x){
    //cout<<"lala"<<endl;
    while(i<=1000005-2){
        bit[i]+=x;
        i+=i&-i;
    }
}

int main()
{
	scanf("%d",&n);
	memset(bit,0,sizeof(bit));
	for(int i=0;i<n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
        a[i].x++;

	}
	sort(a,a+n);
	for(int i=0;i<n;i++){
        add(  a[i].x  ,  1);
	}

	memset(pre,0,sizeof(pre));
    memset(dp,0,sizeof(dp));
    memset(dpans,0,sizeof(dpans));

   /* for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        for(int j=i-1;j>=0&&tmp<=a[j].x  ;j--){
            pre[i]++;
        }
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }

    */

    for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        pre[i]=sum( a[i].x )-sum(  tmp-1  )-1;
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }

    for(int i=0;i<n;i++){
        int tmp=(i-pre[i]-1);
        dp[i]=dp[  tmp>=0?tmp:0 ]+pre[i];
    }
    for(int i=0;i<n;i++){
        dpans[i]=dp[i]+n-1-i;
    }
    int ans=1e9;
    for(int i=0;i<n;i++){
        ans=min(ans,dpans[i]);
    }
    printf("%d\n",ans);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
#include<queue>
#include<stack>
#include<list>

using namespace std;

typedef long long ll;
const int MAXN=100005;

struct node{
    int x,y;
} a[MAXN];;

int pre[MAXN];
int dp[MAXN];
int dpans[MAXN];

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
	}
	memset(pre,0,sizeof(pre));
    memset(dp,0,sizeof(dp));
    memset(dpans,0,sizeof(dpans));
    for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        for(int j=i-1;j>=0&&tmp<=a[j].x  ;j--){
            pre[i]++;
        }
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }
    for(int i=0;i<n;i++){
        int tmp=(i-pre[i]-1);
        dp[i]=dp[  tmp>=0?tmp:0 ]+pre[i];
    }
    for(int i=0;i<n;i++){
        dpans[i]=dp[i]+n-1-i;
    }
    int ans=1e9;
    for(int i=0;i<n;i++){
        ans=min(ans,dpans[i]);
    }
    printf("%d\n",ans);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
#include<queue>
#include<stack>
#include<list>

using namespace std;

typedef long long ll;
const int MAXN=100005;

struct node{
    int x,y;
} a[MAXN];;

int pre[MAXN];
int dp[MAXN];
int dpans[MAXN];
ll bit[1000005];
int n;

int sum(int i){
    int s=0;
    while(i>0){
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}

void add(int i,int x){
    //cout<<"lala"<<endl;
    while(i<=1000005-2){
        bit[i]+=x;
        i+=i&-i;
    }
}

int main()
{
	scanf("%d",&n);
	memset(bit,0,sizeof(bit));
	for(int i=0;i<n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
        a[i].x++;
        add(  a[i].x  ,  1);

	}
	memset(pre,0,sizeof(pre));
    memset(dp,0,sizeof(dp));
    memset(dpans,0,sizeof(dpans));

   /* for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        for(int j=i-1;j>=0&&tmp<=a[j].x  ;j--){
            pre[i]++;
        }
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }

    */

    for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        pre[i]=sum( a[i].x )-sum(  tmp-1  )-1;
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }

    for(int i=0;i<n;i++){
        int tmp=(i-pre[i]-1);
        dp[i]=dp[  tmp>=0?tmp:0 ]+pre[i];
    }
    for(int i=0;i<n;i++){
        dpans[i]=dp[i]+n-1-i;
    }
    int ans=1e9;
    for(int i=0;i<n;i++){
        ans=min(ans,dpans[i]);
    }
    printf("%d\n",ans);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
#include<queue>
#include<stack>
#include<list>

using namespace std;

typedef long long ll;
const int MAXN=100005;

struct node{
    int x,y;
} a[MAXN];;

int pre[MAXN];
int dp[MAXN];
int dpans[MAXN];
int bit[1000005];
int n;

int sum(int i){
    int s=0;
    while(i>0){
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}

void add(int i,int x){
    //cout<<"lala"<<endl;
    while(i<=1000005-2){
        bit[i]+=x;
        i+=i&-i;
    }
}

int main()
{
	scanf("%d",&n);
	memset(bit,0,sizeof(bit));
	for(int i=0;i<n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
        a[i].x++;
        add(  a[i].x  ,  1);

	}
	memset(pre,0,sizeof(pre));
    memset(dp,0,sizeof(dp));
    memset(dpans,0,sizeof(dpans));

   /* for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        for(int j=i-1;j>=0&&tmp<=a[j].x  ;j--){
            pre[i]++;
        }
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }

    */

    for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        pre[i]=sum( a[i].x )-sum(  tmp-1  )-1;
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }

    for(int i=0;i<n;i++){
        int tmp=(i-pre[i]-1);
        dp[i]=dp[  tmp>=0?tmp:0 ]+pre[i];
    }
    for(int i=0;i<n;i++){
        dpans[i]=dp[i]+n-1-i;
    }
    int ans=1e9;
    for(int i=0;i<n;i++){
        ans=min(ans,dpans[i]);
    }
    printf("%d\n",ans);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
#include<queue>
#include<stack>
#include<list>

using namespace std;

typedef long long ll;
const int MAXN=100005;

struct node{
    int x,y;
} a[MAXN];;

int pre[MAXN];
int dp[MAXN];
int dpans[MAXN];
int bit[1000005];
int n;

int sum(int i){
    int s=0;
    while(i>0){
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}

void add(int i,int x){
    while(i<=MAXN-1){
        bit[i]+=x;
        i+=i&-i;
    }
}

int main()
{
	scanf("%d",&n);
	memset(bit,0,sizeof(bit));
	for(int i=0;i<n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
        add(  a[i].x  ,  1);

	}
	memset(pre,0,sizeof(pre));
    memset(dp,0,sizeof(dp));
    memset(dpans,0,sizeof(dpans));

   /* for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        for(int j=i-1;j>=0&&tmp<=a[j].x  ;j--){
            pre[i]++;
        }
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }

    */

    for(int i=0;i<n;i++){
        int tmp= a[i].x-a[i].y;
        pre[i]=sum( a[i].x )-sum(  tmp-1  )-1;
        //printf("pre[%d]==  %d\n",i,pre[i]);
    }

    for(int i=0;i<n;i++){
        int tmp=(i-pre[i]-1);
        dp[i]=dp[  tmp>=0?tmp:0 ]+pre[i];
    }
    for(int i=0;i<n;i++){
        dpans[i]=dp[i]+n-1-i;
    }
    int ans=1e9;
    for(int i=0;i<n;i++){
        ans=min(ans,dpans[i]);
    }
    printf("%d\n",ans);

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>

#define ll long long
#define N 1000000
#define F first
#define S second

using namespace std;

int main()
{
  cin.tie(0);
  ios_base :: sync_with_stdio(false);
  //freopen("input.txt","r",stdin);
 // freopen("output.txt","w",stdout);
  int n;
  cin >> n;
  int maxn = 0;
  vector <pair<int,int> > a(n);
  for (int i = 0;i < n;i++){
    cin >> a[i].F >> a[i].S;
    maxn = max(maxn,a[i].F);
  }
  //cout << maxn << endl;
  vector <int> dp(maxn + 1,0);
  int j = 0;
  for (int i = 0;i <= maxn;i++){
   dp[i] = dp[max(i - 1,0)];
   if (a[j].F == i){
    if (i - a[j].S - 1 < 0)
        dp[i] = max(dp[i],1);
    else dp[i] = max(dp[i],dp[i - a[j].S - 1] + 1);
    j++;
   }
  }
   int ans = 0;
  for (int i = 0;i <= maxn;i++)
    ans = max(ans,dp[i]);
  cout << n - ans;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>

#define ll long long
#define N 1000000
#define F first
#define S second

using namespace std;

int main()
{
  cin.tie(0);
  ios_base :: sync_with_stdio(false);
  //freopen("input.txt","r",stdin);
 // freopen("output.txt","w",stdout);
  int n;
  cin >> n;
  int maxn = 0;
  vector <pair<int,int> > a(n);
  for (int i = 0;i < n;i++){
    cin >> a[i].F >> a[i].S;
    maxn = max(maxn,a[i].F);
  }
  //cout << maxn << endl;
  vector <ll> dp(maxn + 1,0);
  int j = 0;
  for (int i = 1;i <= maxn;i++){
   dp[i] = dp[i - 1];
   if (a[j].F == i){
    if (i - a[j].S - 1 < 0)
        dp[i] = 1;
    else dp[i] = dp[i - a[j].S - 1] + 1;
    j++;
   }
  }
   ll ans = 0;
  for (int i = 0;i <= maxn;i++)
    ans = max(ans,dp[i]);
  cout << n - ans;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>

#define ll long long
#define N 1000000
#define F first
#define S second

using namespace std;

int main()
{
  cin.tie(0);
  ios_base :: sync_with_stdio(false);
  //freopen("input.txt","r",stdin);
 // freopen("output.txt","w",stdout);
  int n;
  cin >> n;
  int maxn = 0;
  vector <pair<int,int> > a(n);
  for (int i = 0;i < n;i++){
    cin >> a[i].F >> a[i].S;
    maxn = max(maxn,a[i].F);
  }
  //cout << maxn << endl;
  vector <int> dp(maxn + 1,0);
  int j = 0;
  for (int i = 0;i <= maxn;i++){
   dp[i] = dp[max(i - 1,0)];
   if (a[j].F == i){
    dp[i] = max(dp[i],dp[max(0,i - a[j].S - 1)] + 1);
    j++;
   }
  }
   int ans = 0;
  for (int i = 0;i <= maxn;i++)
    ans = max(ans,dp[i]);
  cout << n - ans;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>

#define ll long long
#define N 1000000
#define F first
#define S second

using namespace std;

int main()
{
  cin.tie(0);
  ios_base :: sync_with_stdio(false);
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  int n;
  cin >> n;
  int maxn = 0;
  vector <pair<int,int> > a(n);
  for (int i = 0;i < n;i++){
    cin >> a[i].F >> a[i].S;
    maxn = max(maxn,a[i].F);
  }
  //cout << maxn << endl;
  vector <int> dp(maxn + 1,0);
  int j = 0;
  for (int i = 1;i <= maxn;i++){
   dp[i] = dp[i - 1];
   if (a[j].F == i){
    dp[i] = max(dp[i],dp[max(0,i - a[j].S - 1)] + 1);
    j++;
   }
  }
   int ans = 0;
  for (int i = 0;i <= maxn;i++)
    ans = max(ans,dp[i]);
  cout << n - ans;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>

#define ll long long
#define N 1000000
#define F first
#define S second

using namespace std;

int main()
{
  cin.tie(0);
  ios_base :: sync_with_stdio(false);
  //freopen("input.txt","r",stdin);
 // freopen("output.txt","w",stdout);
  int n;
  cin >> n;
  int maxn = 0;
  vector <pair<int,int> > a(n);
  for (int i = 0;i < n;i++){
    cin >> a[i].F >> a[i].S;
    maxn = max(maxn,a[i].F);
  }
  sort(a.begin(),a.end());
  //cout << maxn << endl;
  vector <ll> dp(maxn + 1,0);
  int j = 0;
  if (a[j].F == 0) {
    dp[0] = 1;
    j++;
  }
  for (int i = 1;i <= maxn;i++){
   dp[i] = dp[i - 1];
   if (a[j].F == i){
    if (i - a[j].S - 1 < 0)
        dp[i] = 1;
    else dp[i] = dp[i - a[j].S - 1] + 1;
    j++;
   }
  }
   ll ans = 0;
  for (int i = 0;i <= maxn;i++)
    ans = max(ans,dp[i]);
  cout << n - ans;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>

#define ll long long
#define N 1000000
#define F first
#define S second

using namespace std;

int main()
{
  cin.tie(0);
  ios_base :: sync_with_stdio(false);
 // freopen("input.txt","r",stdin);
 // freopen("output.txt","w",stdout);
  int n;
  cin >> n;
  int maxn = 0;
  vector <pair<int,int> > a(n);
  for (int i = 0;i < n;i++){
    cin >> a[i].F >> a[i].S;
    maxn = max(maxn,a[i].F);
  }
  //cout << maxn << endl;
  vector <int> dp(maxn + 1,0);
  int j = 0;
  for (int i = 1;i <= maxn;i++){
   dp[i] = dp[i - 1];
   if (a[j].F == i){
    dp[i] = max(dp[i],dp[max(0,i - a[j].S - 1)] + 1);
    j++;
   }
  }
   int ans = 0;
  for (int i = 0;i <= maxn;i++)
    ans = max(ans,dp[i]);
  cout << n - ans;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>

#define ll long long
#define N 1000000
#define F first
#define S second

using namespace std;

int main()
{
  cin.tie(0);
  ios_base :: sync_with_stdio(false);
  //freopen("input.txt","r",stdin);
 // freopen("output.txt","w",stdout);
  int n;
  cin >> n;
  int maxn = 0;
  vector <pair<int,int> > a(n);
  for (int i = 0;i < n;i++){
    cin >> a[i].F >> a[i].S;
    maxn = max(maxn,a[i].F);
  }
  //cout << maxn << endl;
  vector <ll> dp(maxn + 1,0);
  int j = 0;
  if (a[j].F == 0) {
    dp[0] = 1;
    j++;
  }
  for (int i = 1;i <= maxn;i++){
   dp[i] = dp[i - 1];
   if (a[j].F == i){
    if (i - a[j].S - 1 < 0)
        dp[i] = 1;
    else dp[i] = dp[i - a[j].S - 1] + 1;
    j++;
   }
  }
   ll ans = 0;
  for (int i = 0;i <= maxn;i++)
    ans = max(ans,dp[i]);
  cout << n - ans;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
#include<ctime>
#include<cstring>
#define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
#define llg long long
#define maxn 101000
using namespace std;
llg i,j,k,n,m,a[maxn],b[maxn],d[maxn],l,r,mid,ans,x;

struct node
{
	llg a,b;
}c[maxn];

int main()
{
//	yyj("c");
	cin>>n;
	for (i=1;i<=n;i++) scanf("%I64d%I64d",&c[i].a,&c[i].b);
	d[0]=-1*(llg)1e16;
	for (i=1;i<=n+10;i++) d[i]=(llg)1e16;
	for (i=1;i<=n;i++)
	{
		l=0; r=n;
		while (l!=r)
		{
			mid=(l+r)/2;
			if (d[mid]<c[i].a-c[i].b) {k=mid,l=mid+1;}else r=mid;
		}
		for (j=max((llg)0,k-2);j<=k+2;j++) if (d[j]<c[i].a-c[i].b) x=j;
		k=x;
		if (d[k+1]==(llg)1e16) d[k+1]=c[i].a;
		ans=max(ans,k+1);
	}
	cout<<n-ans;
	return 0;
}


//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll;

const int N = 201000;
const int M = 1000001;
const int inf = 0x3f3f3f3f;
const int MOD = 100003;
const double eps = 0.000001;

struct ss{
  int p,s,S;
}a[N];
int n,b[N],H[N],t[M],sum[N],dp[M+5];
int cmp(ss s1,ss s2) {return s1.p<s2.p;}

int main () {

    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d%d",&a[i].p,&a[i].s);
        a[i].S=a[i].p-a[i].s-1;
    }
    int cnt=0;   int tmp;
    sort(a+1,a+n+1,cmp);
   int ans=0,L=0;
    int cc=1;
    for(int i=0;i<=M;i++) {
         if(i==a[cc].p) {
            if(a[cc].S>=0) {
                dp[i] = dp[a[cc].S] +1;
            }
            else dp[i] = 1;
            cc++;
         }
         else dp[i] = dp[i-1];
         ans=max(dp[i],ans);
        // cout<<dp[i]<<endl;
    }
    cout<<n-ans<<endl;
    return 0;
}


//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll;

const int N = 201000;
const int M = 1000001;
const int inf = 0x3f3f3f3f;
const int MOD = 100003;
const double eps = 0.000001;

struct ss{
  int p,s,S;
}a[N];
int n,b[N],H[N],t[M],sum[N],dp[M+5];
int cmp(ss s1,ss s2) {return s1.p<s2.p;}

int main () {

    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d%d",&a[i].p,&a[i].s);
        a[i].S=a[i].p-a[i].s-1;
    }
    int cnt=0;   int tmp;
    sort(a+1,a+n+1,cmp);
   int ans=0,L=0;
    int cc=1;
    for(int i=0;i<=M;i++) {
         if(i==a[cc].p) {
            if(a[cc].S>=0) {
                dp[i] = dp[a[cc].S] +1;
            }
            else dp[i] = 1;
            cc++;
         }
         else dp[i] = dp[i-1];
         ans=max(dp[i],ans);
        // cout<<dp[i]<<endl;
    }
    cout<<n-ans<<endl;
    return 0;
}

daima


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
#include<ctime>
#include<cstring>
#define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
#define llg long long
#define maxn 101000
using namespace std;
llg i,j,k,n,m,a[maxn],b[maxn],d[maxn],l,r,mid,ans,x;

struct node
{
	llg a,b;
}c[maxn];

int main()
{
//	yyj("c");
	cin>>n;
	for (i=1;i<=n;i++) scanf("%I64d%I64d",&c[i].a,&c[i].b);
	d[0]=-1*(llg)1e16;
	for (i=1;i<=n;i++) d[i]=(llg)1e16;
	for (i=1;i<=n;i++)
	{
		l=0; r=n;
		while (l!=r)
		{
			mid=(l+r)/2;
			if (d[mid]<c[i].a-c[i].b) {k=mid,l=mid+1;}else r=mid;
		}
		for (j=max((llg)0,k-2);j<=k+2;j++) if (d[j]<c[i].a-c[i].b) x=j;
		k=x;
		if (d[k+1]==(llg)1e16) d[k+1]=c[i].a;
		ans=max(ans,k+1);
	}
	cout<<n-ans;
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#define M 100005
using namespace std;
struct node{
	int l,r;
	bool operator <(const node &p)const{
		return r<p.r;
	}
}a[M];
int main(){
	int n,i,k,j,x,y;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d%d",&x,&y);
		a[i].l=x-y,a[i].r=x;
	}
	sort(a+1,a+1+n);
	int len=a[1].r,cnt=1;
	for(i=2;i<=n;i++){
		if(a[i].r==a[i-1].r)puts("Wrong");
		if(a[i].l<=len)continue;
		cnt++,len=a[i].r;
	}
	printf("%d",n-cnt);
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#define M 100005
using namespace std;
struct node{
	int l,r;
	bool operator <(const node &p)const{
		return r<p.r;
	}
}a[M];
int main(){
	int n,i,k,j,x,y;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d%d",&x,&y);
		a[i].l=x-y,a[i].r=x;
	}
	sort(a+1,a+1+n);
	int len=a[1].r,cnt=1;
	for(i=2;i<=n;i++){
		if(a[i].l<=len)continue;
		cnt++,len=a[i].r;
	}
	printf("%d",n-cnt);
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#define M 100005
using namespace std;
struct node{
	int x,b;
	bool operator <(const node &p)const{
		return x<p.x;
	}
}a[M];
int n,dp[2][M];
int search(int x){
	int l=1,r=n,re=-1;
	while(l<=r){
		int mid=l+r>>1;
		if(a[mid].x<x)re=mid,l=mid+1;
		else r=mid-1;
	}
	return re;
}
int main(){
	int i,k,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].b);
	}
	sort(a+1,a+1+n);
	int ans=0;
	for(i=n;i>=1;i--){
		dp[0][i]=max(dp[0][i],dp[0][i+1]);
		dp[1][i]=max(dp[1][i],dp[1][i+1]);
		int pla=search(a[i].x-a[i].b);
		if(pla==-1)ans=max(ans,max(dp[1][i+1],dp[0][i+1]));
		else {
			dp[1][pla]=max(dp[1][pla],dp[1][i]+1),ans=max(ans,dp[1][pla]);
			dp[0][pla]=max(dp[0][pla],dp[0][i]+1),ans=max(ans,dp[0][pla]);
		}
		for(k=i+1;k<=n;k++)if(a[k].x-a[k].b>a[i].x+1)break;
		dp[1][i]=max(dp[0][i]+1,dp[1][i]),ans=max(ans,dp[1][i]);
	}
//	printf("%d\n",dp[0][1]);
	ans=max(ans,dp[0][1]+1);
	printf("%d",n-ans);
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#define M 100005
using namespace std;
struct node{
	int x,b;
	bool operator <(const node &p)const{
		return x<p.x;
	}
}a[M];
int n,dp[2][M];
int search(int x){
	int l=1,r=n,re=-1;
	while(l<=r){
		int mid=l+r>>1;
		if(a[mid].x<x)re=mid,l=mid+1;
		else r=mid-1;
	}
	return re;
}
int main(){
	int i,k,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].b);
	}
	sort(a+1,a+1+n);
	int ans=0;
	for(i=n;i>=1;i--){
		int pla=search(a[i].x-a[i].b);
		if(pla==-1)ans=max(ans,max(dp[1][i+1],dp[0][i+1]));
		else {
			dp[1][pla]=max(dp[1][pla],dp[1][i]+1),ans=max(ans,dp[1][pla]);
			dp[0][pla]=max(dp[0][pla],dp[0][i]+1),ans=max(ans,dp[0][pla]);
		}
		for(k=i+1;k<=n;k++)if(a[k].x-a[k].b>a[i].x+1)break;
		dp[1][i]=max(dp[0][i]+1,dp[1][i]),ans=max(ans,dp[1][i]);
	}
	ans=max(ans,dp[0][1]+1);
	printf("%d",n-ans);
	return 0;
}


/*

┏┛┻━━━┛┻┓
┃｜｜｜｜｜｜｜┃
┃　　　━　　　┃
┃　┳┛　┗┳　┃
┃　　　　　　　┃
┃　　　┻　　　┃
┃　　　　　　　┃
┗━┓　　　┏━┛
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┗━━━┓
　　┃world_finals! ┃
　　┃感觉自己萌萌哒┃
　　┗┓┓┏━┳┓┏┛
　　　┃┫┫　┃┫┫
　　　┗┻┛　┗┻┛
*/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <queue>
#include <ctime>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100005;
int a[maxn],b[maxn],vis[maxn],dp[maxn],n;
int dfs(int x)
{
    if(vis[x]) return dp[x];
    vis[x]=1;
    int now=lower_bound(a+1,a+n+1,a[x]-b[x])-a;
    if(now-1>=1) return dp[x]=dfs(now-1)+(x-now);
    else return dp[x]=x-now;
}
int main()
{
    while(cin>>n)
    {
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            scanf("%d%d",&a[i],&b[i]);
        int ans=n;
        for(int i=n;i>=1;i--)
        {
            int temp=n-i+dfs(i);
            ans=min(temp,ans);
        }
        cout<<ans<<endl;
    }
    return 0;
}


/*

┏┛┻━━━┛┻┓
┃｜｜｜｜｜｜｜┃
┃　　　━　　　┃
┃　┳┛　┗┳　┃
┃　　　　　　　┃
┃　　　┻　　　┃
┃　　　　　　　┃
┗━┓　　　┏━┛
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┗━━━┓
　　┃world_finals! ┃
　　┃感觉自己萌萌哒┃
　　┗┓┓┏━┳┓┏┛
　　　┃┫┫　┃┫┫
　　　┗┻┛　┗┻┛
*/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <queue>
#include <ctime>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100005;
int a[maxn],b[maxn],dp[maxn];
int main()
{
    int n;
    while(cin>>n)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            scanf("%d%d",&a[i],&b[i]);
        int ans=n;
        for(int i=1;i<=n;i++)
        {
            int l=lower_bound(a+1,a+n+1,a[i]-b[i])-a;
            dp[i]=dp[l-1]+i-l;
            ans=min(ans,dp[i]+n-i);
        }
        cout<<ans<<endl;
    }
    return 0;
}


/*

┏┛┻━━━┛┻┓
┃｜｜｜｜｜｜｜┃
┃　　　━　　　┃
┃　┳┛　┗┳　┃
┃　　　　　　　┃
┃　　　┻　　　┃
┃　　　　　　　┃
┗━┓　　　┏━┛
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┗━━━┓
　　┃world_finals! ┃
　　┃感觉自己萌萌哒┃
　　┗┓┓┏━┳┓┏┛
　　　┃┫┫　┃┫┫
　　　┗┻┛　┗┻┛
*/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <stack>
#include <map>
#include <queue>
#include <ctime>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100000+5;
int a[maxn],c[maxn],mem[maxn];
void dfs(int&now,int temp)
{
    if(temp-c[temp]-1>0)
    {
        if(mem[temp-c[temp-1]]) {now+=mem[temp-c[temp]-1];return ;}
        now+=c[temp];
        dfs(now,temp-c[temp]-1);
        mem[temp]=c[temp]+mem[temp-c[temp]-1];
    }
}
int main()
{
    int n;
    while(cin>>n)
    {
        memset(mem,0,sizeof(mem));
        for(int i=0;i<n;i++)
        {
            int b;
            cin>>a[i]>>b;
            int l=lower_bound(a,a+i+1,a[i]-b)-a;
            c[i]=i-l;
        }
        int ans=n;
        for(int i=n-1;i>=0;i--)
        {
            int temp=i,now=n-1-i;
            dfs(now,temp);
            ans=min(ans,now);
        }
        cout<<ans<<endl;
    }
    return 0;
}


/*

┏┛┻━━━┛┻┓
┃｜｜｜｜｜｜｜┃
┃　　　━　　　┃
┃　┳┛　┗┳　┃
┃　　　　　　　┃
┃　　　┻　　　┃
┃　　　　　　　┃
┗━┓　　　┏━┛
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┗━━━┓
　　┃world_finals! ┃
　　┃感觉自己萌萌哒┃
　　┗┓┓┏━┳┓┏┛
　　　┃┫┫　┃┫┫
　　　┗┻┛　┗┻┛
*/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <queue>
#include <ctime>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100005;
int a[maxn],b[maxn],dp[maxn];
struct hehe
{
    int a,b;
}p[maxn];
int cmp(hehe x,hehe y)
{
    return x.a<y.a;
}
int main()
{
    int n;
    while(cin>>n)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].a,&p[i].b);
            a[i]=p[i].a;
        }
        sort(p,p+n,cmp);
        int ans=n;
        for(int i=1;i<=n;i++)
        {
            int l=lower_bound(a+1,a+n+1,a[i]-p[i].b)-a;
            dp[i]=dp[l-1]+i-l;
            ans=min(ans,dp[i]+n-i);
        }
        cout<<ans<<endl;
    }
    return 0;
}


/*

┏┛┻━━━┛┻┓
┃｜｜｜｜｜｜｜┃
┃　　　━　　　┃
┃　┳┛　┗┳　┃
┃　　　　　　　┃
┃　　　┻　　　┃
┃　　　　　　　┃
┗━┓　　　┏━┛
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┗━━━┓
　　┃world_finals! ┃
　　┃感觉自己萌萌哒┃
　　┗┓┓┏━┳┓┏┛
　　　┃┫┫　┃┫┫
　　　┗┻┛　┗┻┛
*/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <stack>
#include <map>
#include <queue>
#include <ctime>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100000+5;
int a[maxn],c[maxn],mem[2*maxn];
void dfs(int&now,int temp)
{
    if(temp>=0)
    {
        if(mem[temp]) {now=mem[temp];return ;}
        now+=c[temp];
        dfs(now,temp-c[temp]-1);
        mem[temp+maxn]=c[temp]+mem[temp-c[temp]-1+maxn];
    }
}
int main()
{
    int n;
    while(cin>>n)
    {
        memset(mem,0,sizeof(mem));
        for(int i=0;i<n;i++)
        {
            int b;
            cin>>a[i]>>b;
            int l=lower_bound(a,a+i+1,a[i]-b)-a;
            c[i]=i-l;
        }
        int ans=maxn;
        for(int i=n-1;i>=0;i--)
        {
            int temp=i,now=n-1-i;
            dfs(now,temp);
            ans=min(ans,now);
        }
        cout<<ans<<endl;
    }
    return 0;
}


/*

┏┛┻━━━┛┻┓
┃｜｜｜｜｜｜｜┃
┃　　　━　　　┃
┃　┳┛　┗┳　┃
┃　　　　　　　┃
┃　　　┻　　　┃
┃　　　　　　　┃
┗━┓　　　┏━┛
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┗━━━┓
　　┃world_finals! ┃
　　┃感觉自己萌萌哒┃
　　┗┓┓┏━┳┓┏┛
　　　┃┫┫　┃┫┫
　　　┗┻┛　┗┻┛
*/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <stack>
#include <map>
#include <queue>
#include <ctime>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100000+5;
int a[maxn],c[maxn],mem[2*maxn];
void dfs(int&now,int temp)
{
    if(temp>=0)
    {
        now+=c[temp];
        dfs(now,temp-c[temp]-1);
        mem[temp+maxn]=c[temp]+mem[temp-c[temp]-1+maxn];
    }
}
int main()
{
    int n;
    while(cin>>n)
    {
        memset(mem,0,sizeof(mem));
        for(int i=0;i<n;i++)
        {
            int b;
            cin>>a[i]>>b;
            int l=lower_bound(a,a+i+1,a[i]-b)-a;
            c[i]=i-l;
        }
        int ans=maxn;
        for(int i=n-1;i>=0;i--)
        {
            int temp=i,now=n-1-i;
            dfs(now,temp);
            ans=min(ans,now);
        }
        cout<<ans<<endl;
    }
    return 0;
}


/*

┏┛┻━━━┛┻┓
┃｜｜｜｜｜｜｜┃
┃　　　━　　　┃
┃　┳┛　┗┳　┃
┃　　　　　　　┃
┃　　　┻　　　┃
┃　　　　　　　┃
┗━┓　　　┏━┛
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┗━━━┓
　　┃world_finals! ┃
　　┃感觉自己萌萌哒┃
　　┗┓┓┏━┳┓┏┛
　　　┃┫┫　┃┫┫
　　　┗┻┛　┗┻┛
*/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <queue>
#include <ctime>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100005;
int a[maxn],b[maxn],dp[maxn];
struct hehe
{
    int a,b;
}p[maxn];
int cmp(hehe x,hehe y)
{
    return x.a<y.a;
}
int main()
{
    int n;
    while(cin>>n)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].a,&p[i].b);
            a[i]=p[i].a;
        }
        sort(p+1,p+n+1,cmp);
        sort(a+1,a+n+1);
        int ans=n;
        for(int i=1;i<=n;i++)
        {
            int l=lower_bound(a+1,a+n+1,a[i]-p[i].b)-a;
            dp[i]=dp[l-1]+i-l;
            ans=min(ans,dp[i]+n-i);
        }
        cout<<ans<<endl;
    }
    return 0;
}


/*

┏┛┻━━━┛┻┓
┃｜｜｜｜｜｜｜┃
┃　　　━　　　┃
┃　┳┛　┗┳　┃
┃　　　　　　　┃
┃　　　┻　　　┃
┃　　　　　　　┃
┗━┓　　　┏━┛
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┃
　　┃　　　┗━━━┓
　　┃world_finals! ┃
　　┃感觉自己萌萌哒┃
　　┗┓┓┏━┳┓┏┛
　　　┃┫┫　┃┫┫
　　　┗┻┛　┗┻┛
*/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <stack>
#include <map>
#include <queue>
#include <ctime>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100000+5;
int a[maxn],c[maxn],mem[2*maxn];
void dfs(int&now,int temp)
{
    if(temp>=0)
    {
        if(mem[temp+maxn]) {now+=mem[temp+maxn];return ;}
        now+=c[temp];
        dfs(now,temp-c[temp]-1);
        mem[temp+maxn]=c[temp]+mem[temp-c[temp]-1+maxn];
    }
}
int main()
{
    int n;
    while(cin>>n)
    {
        memset(mem,0,sizeof(mem));
        for(int i=0;i<n;i++)
        {
            int b;
            cin>>a[i]>>b;
            int l=lower_bound(a,a+i+1,a[i]-b)-a;
            c[i]=i-l;
        }
        int ans=maxn;
        for(int i=n-1;i>=0;i--)
        {
            int temp=i,now=n-1-i;
            dfs(now,temp);
            ans=min(ans,now);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
#define debug(x) cout<<">value ("<<#x<<") : "<<x<<endl;
#define fr(i,beg,end) for(i=beg;i<end;i++)
#define itfr(it,stl) for(it=stl.begin();it!=stl.end();it++)
#define PII pair<int,int>
#define init(x,val) memset(x,val,sizeof(x))
using namespace std;
int arr[1000001];
int cnt[1000001];
int dp[1000001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    int a,b;
    cin>>n;
    int i;
    fr(i,0,n)
    {
        cin>>a>>b;
        arr[a]=b;
    }
    fr(i,1,1000001)
    {
        if(arr[i])
            cnt[i]=cnt[i-1]+1;
        else
            cnt[i]=cnt[i-1];
    }
    dp[0]=0;
    fr(i,1,1000001)
    {
        if(arr[i]>=i)
            dp[i]=min(dp[i-1]+1,cnt[i-1]);
        else if(arr[i])
            dp[i]=min(dp[i-1]+1,dp[i-arr[i]-1]+cnt[i-1]-cnt[i-arr[i]-1]);
        else
            dp[i]=dp[i-1];
    }
    cout<<dp[1000000]<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
#define debug(x) cout<<">value ("<<#x<<") : "<<x<<endl;
#define fr(i,beg,end) for(i=beg;i<end;i++)
#define itfr(it,stl) for(it=stl.begin();it!=stl.end();it++)
#define PII pair<int,int>
#define init(x,val) memset(x,val,sizeof(x))
using namespace std;
int arr[1000001];
int cnt[1000001];
int dp[1000001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    int a,b;
    cin>>n;
    int i;
    fr(i,0,n)
    {
        cin>>a>>b;
        arr[a]=b;
    }
    if(arr[0])
        cnt[0]=1;
    fr(i,1,1000001)
    {
        if(arr[i])
            cnt[i]=cnt[i-1]+1;
        else
            cnt[i]=cnt[i-1];
    }
    dp[0]=0;
    fr(i,1,1000001)
    {
        if(arr[i]>=i)
            dp[i]=min(dp[i-1]+1,cnt[i-1]);
        else if(arr[i])
            dp[i]=min(dp[i-1]+1,dp[i-arr[i]-1]+cnt[i-1]-cnt[i-arr[i]-1]);
        else
            dp[i]=dp[i-1];
    }
    cout<<dp[1000000]<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
#define debug(x) cout<<">value ("<<#x<<") : "<<x<<endl;
#define fr(i,beg,end) for(i=beg;i<end;i++)
#define itfr(it,stl) for(it=stl.begin();it!=stl.end();it++)
#define PII pair<int,int>
#define init(x,val) memset(x,val,sizeof(x))
using namespace std;
int arr[1000001];
int cnt[1000001];
int dp[2][1000001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    int a,b;
    cin>>n;
    int i;
    fr(i,0,n)
    {
        cin>>a>>b;
        arr[a]=b;
    }
    if(arr[0])
        cnt[0]=1;
    fr(i,1,1000001)
    {
        if(arr[i])
            cnt[i]=cnt[i-1]+1;
        else
            cnt[i]=cnt[i-1];
    }
    dp[0][0]=0;
    if(arr[0])
        dp[1][0]=1;
    else
        dp[1][0]=0;
    fr(i,1,1000001)
    {
        if(arr[i]>=i)
        {
            dp[0][i]=cnt[i-1];
            dp[1][i]=min(dp[0][i-1],dp[1][i-1])+1;
        }
        else if(arr[i])
        {
            dp[0][i]=dp[0][i-arr[i]-1]+cnt[i-1]-cnt[i-arr[i]-1];
            dp[1][i]=min(dp[0][i-1],dp[1][i-1])+1;
        }
        else
        {
            dp[0][i]=dp[0][i-1];
            dp[1][i]=min(dp[0][i-1],dp[1][i-1]);
        }
    }
    cout<<dp[1][1000000]<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
#define debug(x) cout<<">value ("<<#x<<") : "<<x<<endl;
#define fr(i,beg,end) for(i=beg;i<end;i++)
#define itfr(it,stl) for(it=stl.begin();it!=stl.end();it++)
#define PII pair<int,int>
#define init(x,val) memset(x,val,sizeof(x))
using namespace std;
int arr[1000001];
int cnt[1000001];
int dp[1000001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    int a,b;
    cin>>n;
    int i;
    fr(i,0,n)
    {
        cin>>a>>b;
        arr[a]=b;
    }
    fr(i,1,1000001)
    {
        if(arr[i])
            cnt[i]=cnt[i-1]+1;
        else
            cnt[i]=cnt[i-1];
    }
    dp[0]=0;
    fr(i,1,1000001)
    {
        if(arr[i]>i)
            dp[i]=min(dp[i-1]+1,cnt[i-1]);
        else if(arr[i])
            dp[i]=min(dp[i-1]+1,dp[i-arr[i]-1]+cnt[i-1]-cnt[i-arr[i]-1]);
        else
            dp[i]=dp[i-1];
    }
    cout<<dp[1000000]<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
#define debug(x) cout<<">value ("<<#x<<") : "<<x<<endl;
#define fr(i,beg,end) for(i=beg;i<end;i++)
#define itfr(it,stl) for(it=stl.begin();it!=stl.end();it++)
#define PII pair<int,int>
#define init(x,val) memset(x,val,sizeof(x))
using namespace std;
int arr[1000010];
int cnt[1000010];
int dp[2][1000010];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    int a,b;
    cin>>n;
    int i;
    fr(i,0,n)
    {
        cin>>a>>b;
        arr[a]=b;
    }
    if(arr[0])
        cnt[0]=1;
    fr(i,1,1000010)
    {
        if(arr[i])
            cnt[i]=cnt[i-1]+1;
        else
            cnt[i]=cnt[i-1];
    }
    dp[0][0]=0;
    if(arr[0])
        dp[1][0]=1;
    else
        dp[1][0]=0;
    fr(i,1,1000010)
    {
        if(arr[i]>=i)
        {
            dp[0][i]=cnt[i-1];
            dp[1][i]=min(dp[0][i-1],dp[1][i-1])+1;
        }
        else if(arr[i])
        {
            dp[0][i]=dp[0][i-arr[i]-1]+cnt[i-1]-cnt[i-arr[i]-1];
            dp[1][i]=min(dp[0][i-1],dp[1][i-1])+1;
        }
        else
        {
            dp[0][i]=dp[0][i-1];
            dp[1][i]=min(dp[0][i-1],dp[1][i-1]);
        }
    }
    cout<<dp[1][1000009]<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAXV 1000005
#define lld long long int

int dist[MAXV];
int A[MAXV];

#define LOGV 21

int level[MAXV];  // Level of vertex u from root.
int parent[MAXV]; // Direct parent of vertex u.
int P[MAXV][LOGV];

int logarithm(int v){ // O(log v)
    for(int i = 0; true; i++)
        if(v <= 1 << i) return i;
}

void PreprocessDP(int v){ // O(v log v)
    int log = logarithm(v);
    cout << log << endl;
    for(int i = 0; i < v; i++){
        P[i][0] = parent[i];
    }
    for(int j = 1; j <= log; j++)
        for(int i = 0; i < v; i++){

            P[i][j] = P[P[i][j - 1]][j - 1];
        }
}

int main(){
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        int a, b;
        cin >> a >> b;
        parent[a + 1] = max(0, a - b);
        A[a + 1] = 1;
    }
    for(int i = 1 ; i < MAXV; i++){
        if(parent[i] == 0)
            parent[i] = i - 1;
    }
    int ans = -1;
    for(int i = 0; i < 9; i++){
        A[i] += A[parent[i]];
        ans = max(A[i], ans);
    }
    cout << N - ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAXV 1000005
#define lld long long int

int A[MAXV]
int parent[MAXV];

int main(){
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        int a, b;
        cin >> a >> b;
        parent[a + 1] = max(0, a - b);
        A[a + 1] = 1;
    }
    for(int i = 1 ; i < MAXV; i++){
        if(parent[i] == 0)
            parent[i] = i - 1;
    }
    int ans = -1;
    for(int i = 0; i < MAXV; i++){
        A[i] += A[parent[i]];
        ans = max(A[i], ans);
    }
    cout << N - ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAXV 1000005
#define lld long long int

int A[MAXV];
int parent[MAXV];

int main(){
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        int a, b;
        cin >> a >> b;
        parent[a + 1] = max(0, a - b);
        A[a + 1] = 1;
    }
    for(int i = 1 ; i < MAXV; i++){
        if(parent[i] == 0)
            parent[i] = i - 1;
    }
    int ans = -1;
    for(int i = 0; i < MAXV; i++){
        A[i] += A[parent[i]];
        ans = max(A[i], ans);
    }
    cout << N - ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAXV 1000005
#define lld long long int

int A[MAXV];
int parent[MAXV];

int main(){
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        int a, b;
        cin >> a >> b;
        parent[a + 2] = max(1, a - b + 1);
        A[a + 2] = 1;
    }
    for(int i = 1 ; i < MAXV; i++){
        if(parent[i] == 0)
            parent[i] = i - 1;
    }
    int ans = -1;
    for(int i = 0; i < MAXV; i++){
        A[i] += A[parent[i]];
        ans = max(A[i], ans);
    }
    cout << N - ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAXV 1000005
#define lld long long int

int A[MAXV];
int parent[MAXV];

int main(){
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        int a, b;
        cin >> a >> b;
        parent[a + 2] = max(1, a - b + 1);
        A[a + 2] = 1;
    }
    for(int i = 1 ; i < MAXV; i++){
        if(parent[i] == 0)
            parent[i] = i - 1;
    }
    int ans = -1;
    for(int i = 0; i < 100; i++){
        A[i] += A[parent[i]];
        ans = max(A[i], ans);
    }
    cout << N - ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n;
pair<int,int> x[100005];
int dp[100005];

int solve (int i) {

    if (dp[i] != -1) return dp[i];
    if (i<=0) return 0;

    int d = x[i].first-x[i].second;

    //cout << "distance at " << i << " is " << d << endl;

    int ret = i;

    int s = 0, e=i-1, mid;

    while (s<=e) {

        mid = (s+e)/2;
        if ( x[mid].first < d ) {
            ret = min(ret, abs(mid-i)-1 + solve(mid) );
            s = mid+1;
        } else {
            e = mid-1;
        }
    }

    return dp[i] = ret;
}

int main () {

    memset(dp,-1,sizeof(dp) );

    cin >> n;

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

    sort(x,x+n);

    int mi = n-1;

    for (int i=0; i<n; i++) {
        mi = min(mi, (n-i-1) + solve(i) );
    }

    cout << mi << endl;
}


#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> x;
map<int,int> m;
int dp[100005];

int solve (int i) {

    if ( dp[i] != -1 ) return dp[i];

    int d = m[ x[i] ];

    if (d <= x[0] ) return dp[i] = i;

    int s=0,e=i-1,mid,ans=-1;

    while (s<=e) {

        mid = (s+e)/2;
        if ( x[mid] < d ) {
            ans = mid;
            s = mid+1;
        } else {
            e = mid-1;
        }
    }

    if (ans == -1) return dp[i] = i;
    return dp[i] = i-ans-1+solve(ans);
}

int main () {

    memset(dp,-1,sizeof(dp));

    cin >> n;

    int a,b;

    for (int i=0; i<n; i++) {
        cin >> a >> b;
        x.push_back(a);
        m[a] = a-b;
    }

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

    int mi = n-1;

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

        mi = min(mi, n-i-1+solve(i) );
    }

    cout << mi << endl;

}


#include <bits/stdc++.h>
using namespace std;

int n;
pair<int,int> x[100005];
int dp[100005];

int solve (int i) {

    if (dp[i] != -1) return dp[i];
    if (i<=0) return 0;

    int d = x[i].first-x[i].second;

    //cout << "distance at " << i << " is " << d << endl;

    int ret = n;
    bool ok = 0;

    int s = 0, e=i-1, mid;

    while (s<=e) {

        mid = (s+e)/2;
        if ( x[mid].first < d ) {
            ok = 1;
            ret = min(ret, abs(mid-i)-1 + solve(mid) );
            s = mid+1;
        } else {
            e = mid-1;
        }
    }

    if (!ok) ret = i;

    //cout << "return from " << i << " " << ret << endl;
    ret = max(0,ret);
    return dp[i] = ret;
}

int main () {

    cin >> n;

    int a,b;
    for (int i=0; i<n; i++) {
        cin >> a >> b;
        x[i] = make_pair(a,b);
        dp[i] = -1;
    }

    sort(x,x+n);

    int mi = n-1;

    for (int i=0; i<n; i++) {
        mi = min(mi, (n-i-1) + solve(i) );
    }

    cout << mi << endl;
}


#include <bits/stdc++.h>
using namespace std;

int n;
pair<int,int> x[100005];
int dp[100005];

int solve (int i) {

    if (dp[i] != -1) return dp[i];

    int d = x[i].first-x[i].second;

    //cout << "distance at " << i << " is " << d << endl;

    int ret = n;
    bool ok = 0;

    int s = 0, e=i-1, mid;

    while (s<=e) {

        mid = (s+e)/2;
        if ( x[mid].first < d ) {
            ok = 1;
            ret = min(ret, abs(mid-i)-1 + solve(mid) );
            s = mid+1;
            //cout << "found " << mid << " from " << i << endl;
        } else {
            e = mid-1;
        }
    }

    if (!ok) ret = i;

    //cout << "return from " << i << " " << ret << endl;
    return dp[i] = ret;
}

int main () {

    cin >> n;

    memset(dp,-1,sizeof(dp) );

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

    sort(x,x+n);

    int mi = n-1;

    for (int i=0; i<n; i++) {
        mi = min(mi, (n-i-1)+ solve(i) );
    }

    cout << mi << endl;
}


#include <bits/stdc++.h>
using namespace std;

int n,dp[1000005];
vector< pair<int,int> > x;

int solve (int i) {

    if (dp[i] != -1) return dp[i];
    if (i<= 0) return 0;

    int s=0, e=i-1, mid ,d,t=i;
    d = x[i].first-x[i].second;

    if (d <= x[0].first) return dp[i] = i;

    while (s<=e) {

        mid = (s+e)/2;

        if ( x[mid].first < d) {

            t = min(t, (i-mid-1) + solve(mid) );
            s = mid+1;
        } else {
            e = mid-1;
        }
    }

    return dp[i] = t;
}

int main () {

    cin >> n;

    memset(dp,-1,sizeof(dp));

    int a,b;
    for (int i=0; i<n; i++) {
        cin >> a >> b;
        x.push_back( make_pair(a,b) );
    }

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

    int mi = n-1;

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

        mi = min(mi, (n-i-1)+solve(i) );
    }

    cout << mi << endl;
}


#include <bits/stdc++.h>
using namespace std;

int n,dp[100005];
vector< pair<int,int> > x;

int solve (int i) {

    if (dp[i] != -1) return dp[i];

    int s=0, e=i-1, mid ,d,t=i;
    d = x[i].first-x[i].second;

    while (s<=e) {

        mid = (s+e)/2;

        if ( x[mid].first < d) {

            t = min(t, (abs(i-mid)-1) + solve(mid) );
            s = mid+1;
        } else {
            e = mid-1;
        }
    }

    //cout << "returning " << t << " at " << i << endl;
    return dp[i] = t;
}

int main () {

    cin >> n;

    memset(dp,-1,sizeof(dp));

    int a,b;
    for (int i=0; i<n; i++) {
        cin >> a >> b;
        x.push_back( make_pair(a,b) );
    }

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

    int mi = n-1;

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

        mi = min(mi, (n-i-1)+solve(i) );
    }

    cout << mi << endl;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define MOD 10007
#define maxn 100005
using namespace std;
typedef long long ll;

struct Node{
	Node(){
	}
	Node(int p1, int p2){
		a = p1;
		b = p2;
	}
	int a, b;
	friend bool operator < (const Node&k1, const Node&k2){
		return k1.a < k2.a;
	}
}node[maxn];
int dp[maxn];
int main(){

//	freopen("in.txt", "r", stdin);
	int n, a, b;

	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &a, &b);
		node[i] = Node(a, b);
	}
	sort(node+1, node+n+1);
	int ans = 1e9;
	for(int i = 1; i <= n; i++){
		int f = node[i].a - node[i].b;
			int l = 1, r = i;
			while(l < r){
				int mid = (l + r) >> 1;
				if(node[mid].a >= f)
				 r = mid;
				else
				 l = mid + 1;
			}
			dp[i] = dp[l-1] + i - l;
			ans = min(ans, dp[i] + n - i);
	}
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define MOD 10007
#define maxn 100005
using namespace std;
typedef long long ll;

struct Node{
	Node(){
	}
	Node(int p1, int p2){
		a = p1;
		b = p2;
	}
	int a, b;
	friend bool operator < (const Node&k1, const Node&k2){
		return k1.a < k2.a;
	}
}node[maxn];
int dp[maxn];
int main(){

//	freopen("in.txt", "r", stdin);
	int n, a, b;

	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &a, &b);
		node[i] = Node(a, b);
	}
	sort(node+1, node+n+1);
	int ans = n-1;
	for(int i = 1; i <= n; i++){
		int f = node[i].a - node[i].b;
			int l = 1, r = i;
			while(l < r){
				int mid = (l + r) >> 1;
				if(node[mid].a >= f)
				 r = mid;
				else
				 l = mid + 1;
			}
			dp[i] = dp[l-1] + i - l;
			ans = min(ans, dp[i] + n - i);
	}
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define MOD 10007
#define maxn 100005
using namespace std;
typedef long long ll;

struct Node{
	Node(){
	}
	Node(int p1, int p2){
		a = p1;
		b = p2;
	}
	int a, b;
	friend bool operator < (const Node&k1, const Node&k2){
		return k1.a < k2.a;
	}
}node[maxn];
int dp[maxn];
int main(){

//	freopen("in.txt", "r", stdin);
	int n, a, b;

	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d%d", &a, &b);
		node[i] = Node(a, b);
	}
	sort(node, node+n);
	int ans = n - 1;
	dp[0] = 0;
	for(int i = 1; i < n; i++){
		int f = node[i].a - node[i].b;
		if(f > node[0].a){
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) >> 1;
				if(node[mid].a >= f)
				 r = mid;
				else
				 l = mid + 1;
			}
			l--;
			ans = min(ans, dp[i] + n - i - 1);
		}
		else
		 dp[i] = i;
	}
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define MOD 10007
#define maxn 100005
using namespace std;
typedef long long ll;

struct Node{
	Node(){
	}
	Node(int p1, int p2){
		a = p1;
		b = p2;
	}
	int a, b;
	friend bool operator < (const Node&k1, const Node&k2){
		return k1.a < k2.a;
	}
}node[maxn];
int dp[maxn];
int main(){

//	freopen("in.txt", "r", stdin);
	int n, a, b;

	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d%d", &a, &b);
		node[i] = Node(a, b);
	}
	sort(node, node+n);
	int ans = n - 1;
	dp[0] = 0;
	for(int i = 1; i < n; i++){
		int f = node[i].a - node[i].b;
		if(f > node[0].a){
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) >> 1;
				if(node[mid].a >= f)
				 r = mid;
				else
				 l = mid + 1;
			}
			l--;
			dp[i] = dp[l] + i - l - 1;
			ans = min(ans, dp[i] + n - i - 1);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define MOD 10007
#define maxn 100005
using namespace std;
typedef long long ll;

struct Node{
	Node(){
	}
	Node(int p1, int p2){
		a = p1;
		b = p2;
	}
	int a, b;
	friend bool operator < (const Node&k1, const Node&k2){
		return k1.a < k2.a;
	}
}node[maxn];
int dp[maxn];
int main(){

//	freopen("in.txt", "r", stdin);
	int n, a, b;

	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d%d", &a, &b);
		node[i] = Node(a, b);
	}
	sort(node, node+n);
	int ans = n - 1;
	dp[0] = 0;
	for(int i = 1; i < n; i++){
		int f = node[i].a - node[i].b;
		if(f > node[0].a){
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) >> 1;
				if(node[mid].a >= f)
				 r = mid;
				else
				 l = mid + 1;
			}
			l--;
			ans = min(ans, dp[i] + n - i - 1);
		}
		else
		 dp[i] = i - 1;
	}
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define MOD 10007
#define maxn 100005
using namespace std;
typedef long long ll;

struct Node{
	Node(){
	}
	Node(int p1, int p2){
		a = p1;
		b = p2;
	}
	int a, b;
	friend bool operator < (const Node&k1, const Node&k2){
		return k1.a < k2.a;
	}
}node[maxn];
int dp[maxn];
int main(){

//	freopen("in.txt", "r", stdin);
	int n, a, b;

	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d%d", &a, &b);
		node[i] = Node(a, b);
	}
	sort(node, node+n);
	int ans = n - 1;
	dp[0] = 0;
	for(int i = 1; i < n; i++){
		int f = node[i].a - node[i].b;
		if(f > node[0].a){
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) >> 1;
				if(node[mid].a >= f)
				 r = mid;
				else
				 l = mid + 1;
			}
			l--;
			dp[i] = dp[l] + i - l - 1;
			ans = min(ans, dp[i] + n - i - 1);
		}
		else
		 dp[i] = i;
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
int G[100100];
vector<P>V;
int dp[100100];
int goal;
int rec(int i){
	//cout<<i<<endl;
	if(G[i]<goal)return 1;
	if(dp[i]!=-1)return dp[i];
	return dp[i]=rec(G[i])+1;
}
int main(){
	int n;
	cin>>n;
	int a,b;
	REP(i,n){
		scanf("%d%d",&a,&b);
		V.PB(MP(a,b));
	}
	sort(ALL(V),greater<P>());
	bool f[100100];
	memset(f,false,sizeof(f));
	REP(i,n){
		int d=V[i].second;
		int lb=0,rb=n;
		while(rb-lb>1){
			int mid=(lb+rb)/2;
			if(V[i].first-V[mid].first<=d){
				lb=mid;
			}else{
				rb=mid;
			}
		}
		lb++;
		if(lb!=n)G[i]=lb;
		else G[i]=-1;
	}\
	goal=V[V.size()-1].first;
	int ans=INF;
	memset(dp,-1,sizeof(dp));
	REP(i,n){
		ans=min(ans,n-rec(i));
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
int G[100100];
vector<P>V;
int dp[100100];
int goal;
int rec(int i){
	//cout<<i<<endl;
	if(G[i]<goal)return 1;
	if(dp[i]!=-1)return dp[i];
	return dp[i]=rec(G[i])+1;
}
int main(){
	int n;
	cin>>n;
	int a,b;
	REP(i,n){
		scanf("%d%d",&a,&b);
		V.PB(MP(a,b));
	}
	sort(ALL(V),greater<P>());
	REP(i,n){
		int d=V[i].second;
		int lb=i,rb=n;
		while(rb-lb>1){
			int mid=(lb+rb)/2;
			if(V[i].first-V[mid].first<=d){
				lb=mid;
			}else{
				rb=mid;
			}
		}
		lb++;
		if(lb!=n)G[i]=lb;
		else G[i]=-1;
	}
	goal=V[V.size()-1].first;
	int ans=INF;
	memset(dp,-1,sizeof(dp));
	REP(i,n){
		ans=min(ans,n-rec(i));
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
int G[100100];
vector<P>V;
int dp[100100];
int goal;
int rec(int i){
    //cout<<i<<endl;
    if(G[i]<goal)return 1;
    if(dp[i]!=-1)return dp[i];
    return dp[i]=rec(G[i])+1;
}
int main(){
    int n;
    cin>>n;
    int a,b;
    REP(i,n){
        scanf("%d%d",&a,&b);
        V.PB(MP(a,b));
    }
    sort(ALL(V),greater<P>());
    bool f[100100];
    memset(f,false,sizeof(f));
    REP(i,n){
        int d=V[i].second,k=i+1;
        while(V[i].first-V[k].first<=d&&k<n){
            k++;
        }
        if(k!=n)G[i]=k;
        else G[i]=-1;
    }
    goal=V[V.size()-1].first;
    int ans=INF;
    memset(dp,-1,sizeof(dp));
    REP(i,n){
        ans=min(ans,n-rec(i));
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
int G[100100];
vector<P>V;
int dp[100100];
int main(){
	int n;
	cin>>n;
	int a,b;
	REP(i,n){
		cin>>a>>b;
		V.PB(MP(a,b));
	}
	sort(ALL(V),greater<P>());
	bool f[100100];
	memset(f,false,sizeof(f));
	REP(i,n){
		int d=V[i].second,k=i+1;
		while(V[i].first-V[k].first<=d&&k<n){
			k++;
		}
		if(k!=n)G[i]=k;
		else G[i]=-1;
	}
	int goal=V[V.size()-1].first;
	int ans=INF;
	REP(i,n){
		int res=1,now=i;
		while(G[now]>=goal){
			res++;
			now=G[now];
		}
		ans=min(n-res,ans);
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
int G[100100];
vector<P>V;
int dp[100100];
int goal;
int rec(int i){
	//cout<<i<<endl;
	if(G[i]>goal)return 1;
	if(dp[i]!=-1)return dp[i];
	return dp[i]=rec(G[i])+1;
}
int main(){
	int n;
	cin>>n;
	int a,b,ma=-1;
	REP(i,n){
		scanf("%d%d",&a,&b);
		ma=max(ma,a);
		V.PB(MP(a,b));
	}
	REP(i,V.size())V[i].first=ma-V[i].first;
	sort(ALL(V));
	REP(i,n){
		int d=V[i].second;
		int lb=i,rb=n;
		while(rb-lb>1){
			int mid=(lb+rb)/2;
			if(V[mid].first-V[i].first<=d){
				lb=mid;
			}else{
				rb=mid;
			}
		}
		lb++;
		if(lb!=n)G[i]=lb;
		else G[i]=INF;
	}
	goal=V[V.size()-1].first;
	int ans=INF;
	memset(dp,-1,sizeof(dp));
	REP(i,n){
		ans=min(ans,n-rec(i));
	}
	cout<<ans<<endl;
}


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

#ifndef ONLINE_JUDGE
	#define ONLINE_JUDGE
#endif

#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MOD(x) ((x)>0)?(x):(-(x))
#define pb push_back
#define mp make_pair
#define rep(i,n) for(i=0;i<n;i++)
#define repr(i,j,n) for(i=j;i<=n;i++)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<int> > graph;
const ll maxn = (ll) 1e5+9;
const ll mod = (ll) 1e9+7;
ll inf = (ll)1e12;
ll a[maxn],b[maxn];
ll dp[maxn];

int main()
{
	std::ios::sync_with_stdio(0);
	#ifndef ONLINE_JUDGE
		freopen("input.in","r",stdin);
	#endif
	ll i,j,k,t,q,m,l,r,n,md;
	cin>>n;
	repr(i,1,n)cin>>a[i]>>b[i];
	dp[1]=0;
	repr(i,1,n) {
		if((a[i]-b[i])>a[i-1])dp[i]=dp[i-1];
		else {
			k=MOD(b[i]-a[i]);
			l=1,r=i-1;
			while(l<r) {
				md=(l+r)>>1;
				if(a[md]>=k)r=md;
				else if(a[md]<k)l=md+1;
			}
			dp[i]=dp[l-1]+(i-l);
			//cout<<i<<" "<<l<<endl;
		}
	}

	//repr(i,1,n)cout<<dp[i]<<" ";cout<<endl;

	ll ans=inf;
	for(i=n;i>=0;i--) {
		ans = MIN(ans,dp[i]+(n-i));
	}
	cout<<ans<<endl;
}


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

#ifndef ONLINE_JUDGE
	#define ONLINE_JUDGE
#endif

#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MOD(x) ((x)>0)?(x):(-(x))
#define pb push_back
#define mp make_pair
#define rep(i,n) for(i=0;i<n;i++)
#define repr(i,j,n) for(i=j;i<=n;i++)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<int> > graph;
const ll maxn = (ll) 1e5+9;
const ll mod = (ll) 1e9+7;
ll inf = (ll)1e12;
ll a[maxn],b[maxn];
ll dp[maxn];

int main()
{
	std::ios::sync_with_stdio(0);
	#ifndef ONLINE_JUDGE
		freopen("input.in","r",stdin);
	#endif
	ll i,j,k,t,q,m,l,r,n,md;
	cin>>n;
	repr(i,1,n)cin>>a[i]>>b[i];
	dp[1]=0;
	repr(i,1,n) {
		k=a[i]-b[i];
		l=1,r=i;
		while(l<r) {
			md=(l+r)>>1;
			if(a[md]>=k)r=md;
			else if(a[md]<k)l=md+1;
		}
		dp[i]=dp[l-1]+(i-l);
		//cout<<i<<" "<<l<<endl;
	}
	#ifndef ONLINE_JUDGE
		repr(i,1,n)cout<<a[i]<<" "<<b[i]<<endl;
	#endif

	//repr(i,1,n)cout<<dp[i]<<" ";cout<<endl;

	ll ans=inf;
	for(i=n;i>=0;i--) {
		ans = MIN(ans,dp[i]+(n-i));
	}
	cout<<ans<<endl;
}


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

#ifndef ONLINE_JUDGE
	#define ONLINE_JUDGE
#endif

#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MOD(x) ((x)>0)?(x):(-(x))
#define pb push_back
#define mp make_pair
#define rep(i,n) for(i=0;i<n;i++)
#define repr(i,j,n) for(i=j;i<=n;i++)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<int> > graph;
const ll maxn = (ll) 1e5+9;
const ll mod = (ll) 1e9+7;
ll inf = (ll)1e12;
vector<pair<ll,ll> > a;
ll dp[maxn];

bool comp(pair<ll,ll>l,pair<ll,ll>r) {
	return (l.first)<(r.first);
}
int main()
{
	std::ios::sync_with_stdio(0);
	#ifndef ONLINE_JUDGE
		freopen("input.in","r",stdin);
	#endif
	ll i,j,k,t,q,m,l,r,n,md;
	cin>>n;
	a.pb(mp(-1,0));
	repr(i,1,n){cin>>j>>k;a.pb(mp(j,k));}
	sort(a.begin(),a.end(),comp);
	dp[1]=0;
	repr(i,1,n) {
		k=a[i].first-a[i].second;
		l=1,r=i;
		while(l<r) {
			md=(l+r)>>1;
			if(a[md].first>=k)r=md;
			else if(a[md].first<k)l=md+1;
		}
		dp[i]=dp[l-1]+(i-l);
		//cout<<i<<" "<<l<<endl;
	}
	#ifndef ONLINE_JUDGE
		repr(i,1,n)cout<<a[i].first<<" "<<a[i].second<<endl;
	#endif

	//repr(i,1,n)cout<<dp[i]<<" ";cout<<endl;

	ll ans=inf;
	for(i=n;i>=0;i--) {
		ans = MIN(ans,dp[i]+(n-i));
	}
	cout<<ans<<endl;
}