CodeForces 230A Dragons

Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s.

If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi.

Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.

Input

The first line contains two space-separated integers s and n (1 ≤ s ≤ 104, 1 ≤ n ≤ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≤ xi ≤ 104, 0 ≤ yi ≤ 104) — the i-th dragon's strength and the bonus for defeating it.

Output

On a single line print "YES" (without the quotes), if Kirito can move on to the next level and print "NO" (without the quotes), if he can't.

Sample test(s)
input
2 2
1 99
100 0
output
YES
input
10 1
100 100
output
NO

小技巧:结构体排序

struct Dragon{

    int x,y;

    bool operator < (const Dragon &rhs) const{

        return x < rhs.x;

    }

}dragon[1010];

 

sort(dragon,dragon+n);

或者

int cmp(Dragon a,Dragon b){

    return a.x < b.x;

}

 

sort(dragon,dragon+n,cmp);

 1 #include <cstdio>

 2 #include <algorithm>

 3 using namespace std;

 4 const int maxn = 1010;

 5 struct Dragon{

 6     int x,y;

 7     bool operator < (const Dragon &rhs) const{

 8         return x < rhs.x;

 9      }

10 }dragon[1010];

11  

12 int main(){

13     int s,n;

14     scanf("%d%d",&s,&n);

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

16         scanf("%d%d",&dragon[i].x,&dragon[i].y);

17     }

18     sort(dragon,dragon+n);

19     bool flag = 1;

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

21         if(s <= dragon[i].x){

22             flag = 0;

23             break;

24         }else{

25             s += dragon[i].y;

26         }

27     }

28     if(flag)    puts("YES");

29     else        puts("NO");

30     return 0;

31 }

 

你可能感兴趣的:(codeforces)