Design Tutorial: Learn from Life

CodeForces - 472B

One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task.

Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have n people standing on the first floor, the i-th person wants to go to the fi-th floor. Unfortunately, there is only one elevator and its capacity equal to k (that is at most k people can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |a - b| seconds to move from the a-th floor to the b-th floor (we don't count the time the people need to get on and off the elevator).

What is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 2000) — the number of people and the maximal capacity of the elevator.

The next line contains n integers: f1, f2, ..., fn (2 ≤ fi ≤ 2000), where fi denotes the target floor of the i-th person.

Output

Output a single integer — the minimal time needed to achieve the goal.

Examples

Input
3 2
2 3 4
Output
8
Input
4 2
50 100 50 100
Output
296
Input
10 3
2 2 2 2 2 2 2 2 2 2
Output
8

Note

In first sample, an optimal solution is:

  1. The elevator takes up person #1 and person #2.
  2. It goes to the 2nd floor.
  3. Both people go out of the elevator.
  4. The elevator goes back to the 1st floor.
  5. Then the elevator takes up person #3.
  6. And it goes to the 2nd floor.
  7. It picks up person #2.
  8. Then it goes to the 3rd floor.
  9. Person #2 goes out.
  10. Then it goes to the 4th floor, where person #3 goes out.
  11. The elevator goes back to the 1st floor

sol:一种较为显然的贪心就是按楼层排序后从高到低从客人,应该是最优的了

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,K,A[N];
int main()
{
int i,ans=;
R(n); R(K);
for(i=;i<=n;i++) R(A[i]);
sort(A+,A+n+);
for(i=n;i>=;i-=K)
{
int j=max(,i-K+);
ans+=(A[i]-)*;
}
Wl(ans);
return ;
}
/*
input
3 2
2 3 4
output
8 input
4 2
50 100 50 100
output
296 input
10 3
2 2 2 2 2 2 2 2 2 2
output
8
*/

codeforces472B的更多相关文章

随机推荐

  1. linux简单的安全防护

    注: 1.该脚本是以centos7.4.1708做的 2.函数jia/jian是加权限/减权限 3.改过密码以后,下次使用新创建的用户登录时将提示更改密码,第一次要输入原始的密码,原始密码改脚本中定义 ...

  2. [06] 利用mybatis-generator自动生成代码

    1.mybatis-generator 概述 MyBatis官方提供了逆向工程 mybatis-generator,可以针对数据库表自动生成MyBatis执行所需要的代码(如Mapper.java.M ...

  3. 利用H5本地存储localStorage、sessionStorage

    最近的业务处理上,要使用cookie缓存储一下数据,公司的cookie还搞出点问题.而用户的浏览器都是利用微信的内置,普遍支持h5的本地存储.于是利用了这个... 现代浏览器普遍开始支持H5本地存储, ...

  4. Yarn 入门

    Yarn 是快速.可靠.安全的 js 包管理器. 关键词: nodejs, 包管理, yarn 简介 Yarn 是快速.可靠.安全的 js 包管理器. 快速 - Yarn 会缓存它下载的每个包,所以无 ...

  5. git log 的常用选项

  6. Android自动化测试之:获取 参数:comonentName 的值方法

    十年河东十年河西,莫欺少年穷! 不了解Activity的,可参考:http://www.cnblogs.com/tekkaman/archive/2011/06/07/2074211.html 相关代 ...

  7. TensorFlow框架下的RNN实践小结

    截至目前,TensorFlow的RNN APIs还处于Draft阶段.不过据官方解释,RNN的相关API已经出现在Tutorials里了,大幅度的改动应该是不大可能,现在入手TF的RNN APIs风险 ...

  8. Postgres使用ALTER USER命令修改用户的密码、密码过期,锁定,解锁

    使用ALTER USER命令修改用户的密码.密码过期,锁定,解锁 (1)修改用户的口令,将用户的口令修改为新的密码 highgo=#create user test with password ‘te ...

  9. 第一章:模型层model layer -- Django从入门到精通系列教程

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. 题外话: Django的教程写到这里,就进入 ...

  10. 【UFUN开发板评测】小巧而不失精致,简单而不失内涵——uFun开发板开箱爆照

    关于uFun学习板--"满满的爱和正能量" uFun是由@张进东 张工组织发起的一个开源的学习板,设计初衷是为了帮助学生更好的理解电子知识和开发技巧,同时又能对学生毕业找工作有很明 ...