CF F. Shovels Shop(前缀和预处理+贪心+dp)

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 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.

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 .

 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 }

原文地址:https://www.cnblogs.com/Bravewtz/p/10745826.html

时间: 2024-11-08 09:56:55

CF F. Shovels Shop(前缀和预处理+贪心+dp)的相关文章

cf F. Shovels Shop

https://codeforces.com/contest/1154/problem/F 给定m个折扣 每个折扣的{x,y}的意思是每次购买如果买到确切的x只铲子就其中的最便宜的y只铲子免付: 先贪心一波,因为要买k只,而不管折扣怎么变,怎么买,我们都要买原价最便宜的k只铲子,这贪心是对的 接着就选择折扣offers ,明显要dp解决,贪心贪不出(复杂度太高): 设dp[i]表示买i只铲子的最小花费: 所以转移方程:dp[i]=min(f[i],(选择折扣的处理)): #include<bit

HDU 5550 - Game Rooms(DP + 前缀和预处理)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=5550 题意: 一个大楼有n(2≤n≤4000)层,每层可以建一个乒乓球房或者一个游泳房,且每种房间在大楼里至少要有一个.已知每层有ti个乒乓球运动员和pi个游泳运动员(1≤ti,pi≤1e9).问怎样建房,才能使得所有运动员到相应房间的总距离最小,输出最小值. 分析: 因为每种房间在大楼里至少要有一个,所以肯定会有这样一种状态:第i层是一种房间,第i+1层是另一种房间.所以可以设d[i][x]:第i层

【CF】38E Let&#39;s Go Rolling! (dp)

前言 这题还是有点意思的. 题意: 给你 \(n\) (\(n<=3000\)) 个弹珠,它们位于数轴上.给你弹珠的坐标 \(x_i\) 在弹珠 \(i\) 上面花费 \(C_i\) 的钱 可以使弹珠在原地不动 (\(-10^9<=x_i,C_i<=10^9\)),游戏开始时,所有的弹珠向左滚动,直到碰到定在原地不动的弹珠,其花费是其滚动的距离.总花费=开始前的花费+弹珠滚动的花费,问最小的花费是多少 题解 首先划分出阶段,,我们可以先将弹珠排序,前 \(i\) 个弹珠,最后一个固定的弹

URAL 1203 Scientific Conference(贪心 || DP)

Scientific Conference 之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校,补多校的题目,刷一下一直薄弱的DP.多校如果有计算几何一定要干掉-.- 题意:给你N个报告会的开始时间跟结束时间,问你做多可以听几场报告会.要求报告会之间至少间隔为1. 思路:其实是个活动安排问题,可以用贪心也可以用DP,贪心写起来会比较简单一些,因为练习DP,所以又用DP写了一遍. 贪心的话就是一个很简单的活动选择问题,从结束时间入手,找每次的最优选择. 1 struct n

codeforces349B - Color the Fence 贪心+DP

题意:1-9每个字母需要消耗ai升油漆,问你用油漆最多刻意画多大的数字 解题思路: 首先我们要贪心,可以知道我最优是先使我们位数尽可能长,然后才是就是位数长的情况下使得从最高位开始尽可能大.所以要取满足最长位最小的那个数,方便我们DP 解题代码: 1 // File Name: 349b.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月24日 星期四 21时40分13秒 4 5 #include<vector> 6 #include&

HDU 4939 Stupid Tower Defense(贪心+dp)

HDU Stupid Tower Defense 题目链接 题意:有一些塔,红塔能攻击经过他的,绿塔能攻击经过之后的,蓝塔能把经过之后的减速,求在1-n上放塔,求伤害最大值 思路:一开始以为直接贪心,绿塔最前,蓝塔中间,红塔最后就可以了,结果其实是错的 不过,红塔放最后是肯定的,这个很显然就不多证明了,是贪心的思想 然后就dp[i][j]表示放到i,前面有j个绿塔去状态转移即可 代码: #include <cstdio> #include <cstring> #include &l

CF459E Pashmak and Graph【贪心+dp】

题目:CF459E Pashmak and Graph 题意:给出n个点,m条边的图,然后让你每次只能向权值更大边走,求最大的边数.可以走多次 分析:由于点比较多,我们可以先对权值从小到大排序,然后从小的开始,更新它的到的节点的值为前一个节点值+1,但是还会出现权值相等成环的情况,所以我们可以对相等的先不更新,保存起来,等相等的结束了再更新. 代码: #include<cstdio> #include<algorithm> using namespace std; const in

贪心+dp

贪心+dp 好多题都是这个思想, 可以说是非常重要了 思想一: 在不确定序列无法dp的情况下, 我们不妨先假设序列已经选定, 而利用贪心使序列达到最优解, 从而先进行贪心排序, 在进行dp选出序列 思想二: 最优解一定满足上一个状态在某 Problem 1 n 座楼房,立于城中. 第 i 座楼,高度 \(h_i\) . 你需要一开始选择一座楼,开始跳楼.在第 座楼准备跳楼需要 的花费.每次可以跳到任何一个还没有跳过的楼上去.但跳楼是有代价的,每次跳到另外一座楼的代价是两座楼高度的差的绝对值,最后

Codeforces 589F F. Gourmet and Banquet(二分+贪心)

题目地址:http://codeforces.com/problemset/problem/589/F 思路:先贪心按照右端点值排序(先把对后面影响最小的菜吃掉),二分吃每道菜的时间即可. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=105; const int maxt=1e5+50