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. PAT A1124 Raffle for Weibo Followers (20 分)——数学题

    John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers ...

  2. PAT A1129 Recommendation System (25 分)——set,结构体重载小于号

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  3. 2018-2019-2 20175105王鑫浩 实验二《Java面向对象程序设计》实验报告

    一.实验步骤 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装,继承,多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 二.实验内容 (一).单元测试 1 ...

  4. Percona XtraDB Cluster集群

    官网参考地址: https://www.percona.com/doc/percona-xtradb-cluster/LATEST/configure.html 前期准备: 都用的root权限或使用s ...

  5. Objective-C 基于Aspects

    JavaScriptCore没有禁,因为各种小程序都在用 网络下载文件没有禁

  6. Android 解决Toast一直显示个不停的问题

    做程序的时候最常用的弹出大概就是Toast了.但有时候会发先一个特别尴尬的问题,就是:一旦你点击某个按钮次数多了,即使在程序退出后,Toast也弹个不停.什么原因造成的呢? 是这样的:我们为了方便,在 ...

  7. React-UI组件和容器组件

    UI组件负责页面的渲染,又叫傻瓜组件. 容器组件负责逻辑,又叫聪明组件. 当一个组件只有render函数的时候,就可以用无状态组件的形式来定义这个组件.无状态组件怎么定义呢?其实就是一个函数,接受pr ...

  8. 重写Override ToString()方法

    使用一个小例子来演示: 创建一个普通类别: class Ax { private int _ID; public int ID { get { return _ID; } set { _ID = va ...

  9. mysql 添加字段,未响应

    ddl是要请求锁整个表的,肯定是这个表上有DML事务了,也就是有其它会话在删除.修改.插入这个表并且未提交

  10. .NET的弹性及瞬间错误处理库Polly

    原文:.NET的弹性及瞬间错误处理库Polly 本文基本是官方说明的翻译和总结(https://github.com/App-vNext/Polly) 什么是Polly? Polly是一款基于.NET ...