2023 ICPC 亚洲区域赛 南京站 G. Knapsack

G. Knapsack

time limit per test: 1 second

memory limit per test: 1024 megabytes

Little Cyan Fish, an inexperienced businessman, recently launched a store named Queen's Organic Jewelry. This jewelry store houses n gemstones, where the i-th gemstone is priced at wi dollar and has a beauty of vi. Prior to visiting the store, you have W dollars in hand, which you plan to use to purchase gemstones of the greatest possible total beauty.

Interestingly, Little Cyan Fish's store is running a promotion today. Any visitor to the store can select any k gemstones and take them home absolutely free of charge! With this opportunity at hand, you're keen to know the maximum total beauty of gemstones you could obtain with your W dollars, assuming you adopt the optimal strategy.

Please bear in mind that the store stocks only one unit of each gemstone, so you cannot obtain the same gemstone more than once. Also note that you don't have to spend all the money.

Input

There is only one test case in each test file.

The first line of the input contains three integers n, W and k (1≤n≤5×10^{3}, 1≤W≤10^{4}, 0≤k≤n), indicating the total number of gemstones in the store, the amount of money you have and the number of gemstones you can take for free.

For the following n lines, the i-th line contains two integers wi and vi (1≤wi≤W, 1≤vi≤10^{9}), indicating the price and the beauty of the i-th gemstone.

Output

Output one line containing one integer indicating the answer.

Examples

Input

4 10 1

9 10

10 1

3 5

5 20

Output

35

Input

5 13 2

5 16

5 28

7 44

8 15

8 41

Output

129

Note

In the first example, Little Cyan Fish's shop holds 4 gemstones and you are permitted to take 1 gemstone for free. One optimal strategy involves taking the first gemstone for free, and purchasing the third and fourth gemstones.

GemstonePrice wiBeauty viAction
1910Take for free
2101/
335Purchase
4520Purchase

So the answer is 10+5+20=35.

【思路分析】

背包+贪心。本题主要难点在如何选择免费的宝石,我们把宝石按w从小到大排序,则一定存在一个分界点m,使得右侧区间为免费取的区间,左侧区间做01背包。从0到n枚举分界点,右侧用堆维护。

#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <cstring>
#include <iomanip>
#include <set>
#include <queue>

#define i64 long long

using namespace std;

typedef pair<i64,i64> pii;

bool cmp(pii a, pii b) {
    return a.first < b.first;
}

void solve() {
    i64 n, w, k;
    cin >> n >> w >> k;
    pii arr[n];
    i64 dp[w + 1], ans, g[n+1], sum = 0;
    priority_queue<i64, vector<i64>, greater<>> pq;
    memset(dp, 0, sizeof dp);
    for (int i = 0; i < n; ++i) cin >> arr[i].first >> arr[i].second;
    sort(arr, arr + n, cmp);
    for (int i = n - 1; i >= 0; --i) {
        sum += arr[i].second;
        pq.push(arr[i].second);
        if (pq.size() > k) {
            sum -= pq.top();
            pq.pop();
        }
        g[i] = sum;
    }
    ans = g[0];
    g[n] = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = w; j >= arr[i].first; --j) {
            dp[j] = max(dp[j], dp[j - arr[i].first] + arr[i].second);
            ans = max(ans, g[i+1] + dp[j]);
        }
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值