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. 在lua中创建字段安全的对象

    lua萌新,刚刚学习和使用不到一个月.有不对的地方,还望各路大神不吝赐教. lua中可以用table来模拟对象,但table是可以任意增加键值的.在对象模拟中,暂且也叫它为字段(field)吧.如果在 ...

  2. odoo系统中name_search和name_get用法

    自动带出工序和工序序号,两个条件都能搜索,并且两个都带出来显示在前端: # 输入工序序号会自动带出工序名// def name_search(self, cr,user,name='', args=N ...

  3. maven 单独构建多模块项目中的单个模块

    maven 单独构建多模块项目中的单个模块, maven选项说明 -pl, --projects Build specified reactor projects instead of all pro ...

  4. 设计模式-简单工厂Coding+jdk源码解析

    感谢慕课geely老师的设计模式课程,本套设计模式的所有内容均以课程为参考. 前面的软件设计七大原则,目前只有理论这块,因为最近参与项目重构,暂时没有时间把Coding的代码按照设计思路一点点写出来. ...

  5. Luogu P3768 简单的数学题

    非常恶心的一道数学题,推式子推到吐血. 光是\(\gcd\)求和我还是会的,但是多了个\(ij\)是什么鬼东西. \[\sum_{i=1}^n\sum_{j=1}^nij\gcd(i,j)=\sum_ ...

  6. Ext.js Combobox 输入模糊匹配

    前台页面 aspx: 数据源: <ext:Store ID="storeJF" runat="server" AutoLoad="true&qu ...

  7. .Net架构篇:思考如何设计一款实用的分布式监控系统?

    前言 无论从最早期的unix操作系统,还是曾经大行其道的单体式应用,还是现在日益流行的微服务架构,始终都离不开监控的身影.如windows的任务管理器,linux的top命令,都可以看作是监控的面板. ...

  8. 【JVM.11】Java内存模型与线程

    鲁迅曾经说过“并发处理的广泛应用是使得Amdahl定律代替摩尔定律成为计算机性能发展源动力的根本原因,也是人类‘压榨‘ 计算机运行能力的最有力武器.” 一.概述 多任务处理在现代计算机操作系统中几乎已 ...

  9. Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)

    Nginx.LVS.HAProxy 是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,通常会结合Keepalive做健康检查,实现故障转移的高可用功能. 1)在四层(tcp)实现负载均衡的 ...

  10. Beta项目展示

    Team C# 一.团队成员简介 杜正远,队长. 博客地址:http://www.cnblogs.com/kevindu/ 崔强,全职PM. 博客地址:http://www.cnblogs.com/m ...