ZOJ 3230 Solving the Problems

ZOJ Problem Set - 3230
Solving the Problems

Time Limit: 2 Seconds      Memory Limit: 32768 KB

Programming is fun, Aaron is addicted to it. In order to improve his programming skill, he decides to solve one programming problem per day. As you know, different problems have different properties, some problems are so difficult that there are few people can solve it, while some problems are so easy that almost everyone is able to tackle it.

Programming skill can be measured by an integer p. And all problems are described by two integers ai and bi. ai indicates that if and only if P >= ai, you can solve this problem. bi indicates that after you solve this problem, your programming skill can be increased by bi.

Given the initial programming skill p of Aaron, and the information of each problem, Aaron want to know the maximal programming skill he can reach after m days, can you help him?

Input

Input consists of multiple test cases (less than 40 cases)!

For each test case, the first line contains three numbers: n, m, p (1 <= n <= 100000, 1 <= m <= n, 1 <= p <= 10000), n is the number of problems available for Aaron, m, p as mentioned above.

The following n lines each contain two numbers: ai and bi (1 <= ai <= 10000, 1 <= bi <= 10000) describe the information of the i-th problem as memtioned above.

There's a blank line between consecutive cases.

Output

For each case, output the maximal programming skill Aaron can reach after m days in a line.

Sample Input

2 2 1

1 2

7 3



3 1 2

1 2

2 3

3 4

Sample Output

3

5


Author: ZHOU, Yilun
Source: ZOJ Monthly, July 2009

//思路,对a进行升序排序,只要p>=a就,就把与这个a对应的b加入优先队列,p每次加上堆顶的元素

//哈哈、除去编译器的问题、1Y,呵呵、排名还蛮靠前,看来自己的思路是不错的解题方法

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct node
{
    int a,b;
};
bool cmp(const node&a,const node&b)
{
    return a.a<b.a;
}
node sa[100003];
int main()
{
    int n,m;
    long long  p;//开始用__int64,错了,看来浙大的OJ不支持呀、得用long long
    int i;
    while(scanf("%d%d%lld",&n,&m,&p)!=EOF)
    {   priority_queue<int> Q;
        for(i=0;i<n;i++)
        scanf("%d%d",&sa[i].a,&sa[i].b);
        sort(sa,sa+n,cmp);
        i=0;
        while(sa[i].a<=p&&i<n)
        {
            Q.push(sa[i].b);
            i++;
        }
        while(m--)
        {
            if(!Q.empty())
            {
                p+=Q.top();
                Q.pop();
                while(sa[i].a<=p&&i<n)
                {
                   Q.push(sa[i].b);
                       i++;
                }
            }
        }
        printf("%lld\n",p);
    }
    return 0;
}

你可能感兴趣的:(ZOJ)