---
# System prepended metadata

title: k714. 外送接單 (Delivery)

---

# k714. 外送接單 (Delivery)
## 題目連結:[k714](https://zerojudge.tw/ShowProblem?problemid=k714)
選擇訂單的方式是將完成時間早的先選，如果總準備時間超過完成時間，用priority queue 先移除準備時間最長的訂單
```cpp=
#include<bits/stdc++.h>
using namespace std;

struct food {
    long long p;
    long long t;
};

bool compare(food a, food b) {
    return a.t < b.t;
}
int main()
{
    int n = 0;
    cin>>n;
    food a[n];
    for (int i = 0; i<n; i++)
    {
        cin>>a[i].p>>a[i].t;
    }
    sort(a, a+n, compare);

    int ans = 0, time = 0;
    priority_queue<int>pq;
    for (int i = 0; i<n; i++)
    {
        pq.push(a[i].p);
        time += a[i].p;
        if (time > a[i].t)
        {
            time -= pq.top();
            pq.pop();
        }
    }
    ans = pq.size();
    cout<<ans;;

}
```