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. EventBus使用详解

    EventBus是针一款对Android的发布/订阅事件总线.它可以让我们很轻松的实现在Android各个组件之间传递消息,并且代码的可读性更好,耦合度更低.长话短说直接介绍使用. 如何使用 (以下介 ...

  2. Wi-Fi无线控制器开发例程(基础篇)

    动手来做自己的WIFI远程控制插座吧! 如果感觉视频不容易入门可以看这里 https://www.cnblogs.com/yangfengwu/p/10100152.html WIFI远程控制器系统方 ...

  3. SVM的简单介绍

    ng的MI-003中12 ——SVM 一.svm目标函数的由来 视频先将LR的损失函数: 在上图中,先将y等于0 和y等于1的情况集合到一起成为一个损失函数,然后分别讨论当y等于1的时候损失函数的结果 ...

  4. linux驱动编写之进程独占驱动

    一.描述 嵌入式开发系统中,有各种硬件资源,而有些硬件资源使用时候是需要进程独占的.也就是说,同一时刻只有一个进程允许使用这个硬件资源,其他的进程只能放弃执行或者挂起等待.在设计其对应驱动的时候,就需 ...

  5. you-get帮助使用手册

    you-get使用手册 可选参数:   -V, --version          查看版本并退出   -h, --help             查看帮助信息 不影响使用的选项:   -i, - ...

  6. Ionic buid android下的此工程不是一个android项目问题

    今天编译Ionic项目的时候报如下错误,甚是费解,之前一直都是好的 首先去检查了,相关JavaHome的环境变量,确定是好的,java -version 命令没有问题. 经查阅网上的解决方法,思路大都 ...

  7. 大话设计模式(C#)

    还是那几句话: 学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现在,学习代表你的将来 问个问题: 如何写出高质量的代码?灵活,可扩展,易读,易维护,可重构,可复用. ...

  8. JQuery如何实现双击事件时不触发单击事件

    单击和双击事件的执行顺序: 单击(click):mousedown,mouseout,click: 双击(dblclick):mousedown,mouseout,click , mousedown, ...

  9. 【译】快速起步-JSX简介

    react version: 15.5.0 快速起步-JSX简介 思考这个变量申明: const element = <h1>Hello, world!</h1>; 这个有趣的 ...

  10. Jmeter(三十二)_搭建本地接口自动化环境

    我们在学习接口自动化的时候,最理想的状态是在公司有项目可以操作.大部分时候我们并没有可以练习的项目,因此练习接口无从谈起,只能找一些开放的api来练一练,但是这样并不能提高我们的技术.因此我们需要搭建 ...