D. Queue

D. Queue
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little girl Susie went shopping with her mom and she wondered how to improve service quality.

There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.

Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.

Input

The first line contains integer n (1 ≤ n ≤ 105).

The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.

Output

Print a single number — the maximum number of not disappointed people in the queue.

Sample test(s)
input
5
15 2 1 5 3
output
4
Note

Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.


////這個題用快排測試數據都不過,TLE,所以只能用sort排序(我也是醉了),排完序之後,定義s=0,讓s與a[i]比較,如果s<=a[i],就讓sum++,並且讓s的值更新爲s+a[i],同時利用循環,這是一個比較省時的很好的想法,比用兩個for循環好,但是由於測試數據太大,兩個for只會TLE(我用這個方法做了一遍,結果不是TLE,而是WA,媽呀!感覺做的挺對的,爲什麼WA呢,到底哪裏錯了,求抱大腿!

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int n, a[100010];
    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
    sort(a,a+n);
    int sum=0, s=0;
    for(int i=0; i<n; i++)
    {
        if(s<=a[i])
        {
            s=s+a[i];
            sum++;
        }
    }
    printf("%d\n", sum);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章