// Arup Guha
// 10/8/2015
// Solution to 2001 UCF HS Contest Problem: The Turpentine Avenger

import java.util.*;

public class avenger {

	final public static int LIMIT = 5000;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Process each case.
		while (n != 0) {

			// Store values and sort.
			int[] vals = new int[n];
			for (int i=0; i<n; i++)
				vals[i] = stdin.nextInt();
			Arrays.sort(vals);

			// Sweep through values until total cleaned exceeds LIMIT.
			int total = 0, cnt = 0;
			for (int i=0; i<n; i++) {
				if (total + vals[i] > LIMIT) break;
				total += vals[i];
				cnt++;
			}

			// Print result.
			System.out.println(cnt);
			System.out.println();

			// Get next case.
			n = stdin.nextInt();
		}
	}
}