[codeforces1203F1]Complete the Projects (easy version)

time limit per test : 2 seconds
memory limit per test : 256 megabytes

分数:2100

The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.

Polycarp is a very famous freelancer. His current rating is r r r units.

Some very rich customers asked him to complete some projects for their companies. To complete the i i i-th project, Polycarp needs to have at least ai units of rating; after he completes this project, his rating will change by bi (his rating will increase or decrease by b i b_i bi) ( b i b_i bi can be positive or negative). Polycarp’s rating should not fall below zero because then people won’t trust such a low rated freelancer.
Is it possible to complete all the projects? Formally, write a program to check if such an order of the projects exists, that Polycarp has enough rating before starting each project, and he has non-negative rating after completing each project.
In other words, you have to check that there exists such an order of projects in which Polycarp will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.
Input
The first line of the input contains two integers n n n and r ( 1 ≤ n ≤ 100 , 1 ≤ r ≤ 30000 ) r (1≤n≤100,1≤r≤30000) r(1n100,1r30000) — the number of projects and the initial rating of Polycarp, respectively.

The next n n n lines contain projects, one per line. The i i i-th project is represented as a pair of integers a i a_i ai and b i b_i bi ( 1 ≤ a i ≤ 30000 , − 300 ≤ b i ≤ 300 ) (1≤a_i≤30000, −300≤b_i≤300) (1ai30000,300bi300) — the rating required to complete the i i i-th project and the rating change after the project completion.
Output

Print “YES” or “NO”.

Examples
Input

3 4
4 6
10 -2
8 -1

Output

YES

Input

3 5
4 -5
4 -2
1 3

Output

YES

Input

4 4
5 2
5 -3
2 1
4 -2

Output

YES

Input

3 10
10 0
10 -10
30 0

Output

NO

Note

In the first example, the possible order is: 1,2,3.
In the second example, the possible order is: 2,3,1.
In the third example, the possible order is: 3,1,4,2.

题意:
刚刚开始你有一个能力值 r r r,有 n n n个工作,每个工作有两个属性值 a a a, b b b,表示只有在能力值大于等于 a a a时,才能执行,执行完之后能力值变为 r + b r+b r+b
问是否有一个执行顺序能够做完所有工作。

题解:
考虑将工作按照 b b b属性的正负来分类。
对于 b > = 0 b>=0 b>=0的工作来说,我们肯定是按照 a a a属性从小到大开始拿。
对于 b < 0 b<0 b<0的工作来说,我们按照 a + b a+b a+b的值从大到小拿。
证明: b > = 0 b>=0 b>=0贪心显然, b < 0 b<0 b<0时,因为要使得 r + b r+b r+b之后尽量大,而且还能够拿到接下来的 a a a,那么就按照 a + b a+b a+b排序。
然后先拿 b > = 0 b>=0 b>=0的,后拿 b < 0 b<0 b<0的,有一个不能拿就NO
最后就YES

#include
#define ll long long
#define pa pair
using namespace std;
int n,r;
vector<pa>up,down;
inline bool dex(pa A,pa B){
    return A.first+A.second>B.first+B.second;
}
int main(){
    scanf("%d%d",&n,&r);
    for(int i=1;i<=n;i++){
        int a,b;scanf("%d%d",&a,&b);
        if(b>=0)up.push_back({a,b});
        else down.push_back({a,b});
    }
    sort(up.begin(),up.end());
    sort(down.begin(),down.end(),dex);
    for(int i=0;i<up.size();i++){
        if(r>=up[i].first)r+=up[i].second;
        else return puts("NO"),0;
    }
    for(int i=0;i<down.size();i++){
        if(r>=down[i].first)r+=down[i].second;
        else return puts("NO"),0;
        if(r<0)return puts("NO"),0;
    }
    puts("YES");
    return 0;
}

你可能感兴趣的:([codeforces1203F1]Complete the Projects (easy version))