F. Shovels Shop
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn shovels in the nearby shop. The ii-th shovel costs aiai bourles.
Misha has to buy exactly kk shovels. Each shovel can be bought no more than once.
Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset.
There are also mm special offers in the shop. The jj-th of them is given as a pair (xj,yj)(xj,yj), and it means that if Misha buys exactly xjxjshovels during one purchase then yjyj most cheapest of them are for free (i.e. he will not pay for yjyj most cheapest shovels during the current purchase).
Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers).
Your task is to calculate the minimum cost of buying kk shovels, if Misha buys them optimally.
Input
The first line of the input contains three integers n,mn,m and kk (1≤n,m≤2⋅105,1≤k≤min(n,2000)1≤n,m≤2⋅105,1≤k≤min(n,2000)) — the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the cost of the ii-th shovel.
The next mm lines contain special offers. The jj-th of them is given as a pair of integers (xi,yi)(xi,yi) (1≤yi≤xi≤n1≤yi≤xi≤n) and means that if Misha buys exactly xixi shovels during some purchase, then he can take yiyi most cheapest of them for free.
Output
Print one integer — the minimum cost of buying kk shovels if Misha buys them optimally.
Examples
input
Copy
7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1
output
Copy
7
input
Copy
9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7
output
Copy
17
input
Copy
5 1 4 2 5 7 4 6 5 4
output
Copy
17
Note
In the first example Misha can buy shovels on positions 11 and 44 (both with costs 22) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions 33 and 66 (with costs 44 and 33) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position 77 with cost 11. So the total cost is 4+2+1=74+2+1=7.
In the second example Misha can buy shovels on positions 11, 22, 33, 44 and 88 (costs are 66, 88, 55, 11 and 22) and get three cheapest (with costs 55, 11 and 22) for free. And then he can buy shovels on positions 66, 77 and 99 (all with costs 11) without using any special offers. So the total cost is 6+8+1+1+1=176+8+1+1+1=17.
In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost 1717.
题意:有n件物品每个价值ai ,m 个优惠活动(x,y) 即,买x 件物品,可以优惠掉其中y 件最便宜的。问买k个物品的最少花费。
贪心DP:
#include
#define mod 1000007
#define maxn 200001
using namespace std;
typedef long long ll;
ll n,m,k;
ll dp[maxn];
ll s[maxn];//记录前缀和
ll g[maxn];//i件最多优惠件数
int main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++) cin>>s[i];
sort(s+1,s+n+1);
for(int i=1;i<=k;i++){
s[i]=s[i-1]+s[i];//记录前缀和
}
for(int i=1;i<=m;i++){
ll x,y;
cin>>x>>y;
if(x>k) continue;
g[x]=max(g[x],y); //保留特价活动的最优减少数
}
for(int i=1;i<=n;i++) dp[i]=1e9;//贪心dp
dp[1]=s[1]-s[g[1]];
for(int i=1;i<=k;i++)
for(int j=0;j<=i;j++)
dp[i]=min(dp[i],dp[i-j]+s[i]-s[i-j+g[j]]);
printf("%d\n",dp[k]);
}
#include
using namespace std;
const int N=200005;
int a[N],sum[N],dp[N];
int main()
{
int n,m,k,x,y;
cin>>n>>m>>k;
for(int i=1;i<=n;i++) cin>>a[i];sort(a+1,a+1+n);
for(int i=1;i<=m;i++) {
cin>>x>>y;
sum[x]=max(sum[x],y);
}
for(int i=1;i<=n;i++)
a[i]+=a[i-1];
for(int i=1;i<=k;i++)//买i件
for(int j=0;j