CF F. Shovels Shop(前缀和预处理+贪心+dp)
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 xjxj shovels 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.
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.
Print one integer — the minimum cost of buying kk shovels if Misha buys them optimally.
7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1
7
9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7
17
5 1 4 2 5 7 4 6 5 4
17
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 .
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 2*1e5+10; 7 int a[maxn]; 8 /*s表示前i件物品中的总花费, 9 g代表前i件物品中最多能免费的件数 10 dp代表前i件物品中最大能够节省的钱 11 */ 12 int s[maxn],g[maxn],dp[maxn]; 13 14 int main(){ 15 int n,m,k; 16 cin>>n>>m>>k; 17 for( int i=1; i<=n; i++ ){ 18 cin>>a[i]; 19 } 20 sort(a+1,a+1+n); 21 for( int i=1; i<=n; i++ ){ 22 s[i]=s[i-1]+a[i];//前缀和 23 } 24 for( int i=1; i<=m; i++ ){ 25 int x,y; 26 cin>>x>>y; 27 g[x]=max(g[x],y);//最大减少数 28 } 29 for( int i=1; i<=k; i++ ){ 30 for( int j=0; j<i; j++ ){ 31 dp[i]=max(dp[i],dp[j]+s[j+g[i-j]]-s[j]); 32 } 33 } 34 cout<<s[k]-dp[k]; 35 return 0; 36 }