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×, 1≤W≤
, 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≤), 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.
Gemstone | Price wi | Beauty vi | Action |
1 | 9 | 10 | Take for free |
2 | 10 | 1 | / |
3 | 3 | 5 | Purchase |
4 | 5 | 20 | Purchase |
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;
}