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. MySQL 基础二 创建表格

    1.界面创建 2.SQL创建 教程地址:http://blog.csdn.net/brucexia/article/details/53738596 提供学习视频下载 链接:http://pan.ba ...

  2. DHT11温度传感器应用电路

    DHT11应用电路: 注意点: Vcc和GND之间连接一个100 μF的一个电解电容(通常电容的取值在100μF-300μF之间,滤波) SWI接一个5K的上拉电阻,目的是增强驱动能力

  3. 学习CSS布局 - position例子

    position例子 通过具体的例子可以帮助我们更好地理解“position”.下面是一个真正的页面布局. 结果: 代码如下: <!DOCTYPE html> <html lang= ...

  4. [04] 高级映射 association和collection

    之前我们提到的映射,都是简单的字段和对象属性一对一,假设对象的属性也是一个对象,即涉及到两个表的关联,此时应该如何进行映射处理? 先看两张表,author 和 book:    业务上对应关系为,一个 ...

  5. Python基础(中)

    前言 print(" _ooOoo_ ") print(" o8888888o ") print(" 88 . 88 ") print(&q ...

  6. JVM规范系列第6章:Java虚拟机指令集

    一条 Java 虚拟机指令由一个特定操作的操作码和零至多个操作所使用到的操作数所构成. 虚拟机指令 = 操作码 + 操作数. 其中,操作码值分别为 254(0xfe)和 255(0xff),助记符分别 ...

  7. 001_IntelliJ IDEA详细安装步骤

    安装IntelliJ IDEA 一.安装JDK 1 下载最新的jdk,这里下的是jdk-8u66 2 将jdk安装到默认的路径C:\Program Files\Java目录下 二.安装IntelliJ ...

  8. Ionic2 下处理 Android 设备下返回按钮的事件

    原文发表于我的技术博客 本文分享了 Ionic2 下处理 Android 设备下返回按钮的事件,供参考. 原文发表于我的技术博客 代码中我分享了如何捕捉 Ionic2 项目在 Android 设备下返 ...

  9. mac下查看和设置环境变量

    1.查看环境变量 命令    env 2.修改环境变量 命令    cd ~ && ls -a && sudo vim .bashrc 编辑输入要添加的环境变量 3.s ...

  10. Java读取oracle数据库中blob字段数据文件保存到本地文件(转载)

    转自:https://www.cnblogs.com/forever2698/p/4747349.html package com.bo.test; import java.io.FileOutput ...