CodeForces - 262C 贪心
Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
There are m types of discounts. We assume that the discounts are indexed from 1 to m. To use the discount number i, the customer takes a special basket, where he puts exactly qi items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the qi items in the cart.
Maxim now needs to buy n items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.
Input
The first line contains integer m (1 ≤ m ≤ 105) — the number of discount types. The second line contains m integers: q1, q2, ..., qm (1 ≤ qi ≤ 105).
The third line contains integer n (1 ≤ n ≤ 105) — the number of items Maxim needs. The fourth line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 104) — the items' prices.
The numbers in the lines are separated by single spaces.
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
1
2
4
50 50 100 100
Output
200
Input
2
2 3
5
50 50 50 50 50
Output
150
Input
1
1
7
1 1 1 1 1 1 1
Output
3
Note
In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.
这个题是说买PI件商品,送0-2件商品,送的价格不能超过买的最低价格。
那我每次选最小的PI去满足,然后每次都赠两个,排序是的一定符合买的比送的便宜,那么一定是最优,至于为什么,看证明!
物品:A1 A2 A3 A4 A5 (排序后结果)
Pi 为2 或 3
当我们买2个送两个再买一个,这五件的花费是A1+A2+A5
当我们买3个送两个,这五件的花费是 A1+A2+A3 必然大于等于前者,可以通过数学归纳法证明N与N+M的关系,进而证明该种贪心策略的正确性。
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
//---------------------------------Sexy operation--------------------------//
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
//-------------------------------Actual option------------------------------//
#define Swap(a,b) a^=b^=a^=b
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define mp(a,b) make_pair(a,b)
//--------------------------------constant----------------------------------//
#define INF 0x3f3f3f3f
#define maxn 100010
#define esp 1e-9
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
//------------------------------Dividing Line--------------------------------//
int n,m,k;
int b[maxn];
int a[maxn];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
cini(m);
for(int i=0;i<m;i++) cini(a[i]);
cini(n);
for(int i=0;i<n;i++) cini(b[i]);
sort(a,a+m);
sort(b,b+n,cmp);
long long ans=0;
int t=0;
//cout<<a[0]<<endl;
for(int i=0;i<n;i++)
{
if(t<a[0]) ans+=b[i],t++;
else {
t=0;
i+=1;
}
}
cout<<ans<<endl;
}
- python爬虫人门(10)Scrapy框架之Downloader Middlewares
- 11.python线程
- Numpy 修炼之道 (8)—— 常用函数
- Numpy 修炼之道 (7)—— 形状操作
- 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)
- 12.python进程协程异步IO
- POJ3622 Gourmet Grazers(FHQ Treap)
- 洛谷P3201 [HNOI2009]梦幻布丁
- 洛谷P3374 【模板】树状数组 1(CDQ分治)
- 自然语言处理基础知识1. 分词(Word Cut)2. 词性标注(POS Tag)3.自动标注4.文本分类5.评估6.从文本提取信息7.分析句子结构《python自然语言处理》各章总结:
- 洛谷P3384 【模板】树链剖分
- 洛谷P2147 [SDOI2008]Cave 洞穴勘测
- linux基础
- 洛谷P3178 [HAOI2015]树上操作
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法