Codeforces Round #270 1002
Codeforces Round #270 1002
B. Design Tutorial: Learn from Life
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputOne 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.
Sample test(s)
Input
3 22 3 4
Output
8Input
4 250 100 50 100
Output
2961 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 8 int N,M; 9 struct node 10 { 11 int str; 12 bool operator < (const node &rhs) const 13 { 14 return str>rhs.str; 15 } 16 }num[2060]; 17 18 int main() 19 { 20 scanf("%d%d",&N,&M); 21 for(int i = 0;i<N;i++) 22 { 23 scanf("%d",&num[i].str); 24 } 25 sort(num,num+N); 26 int t = N/M; 27 if(N%M!=0) t+=1; 28 //cout<<t<<endl; 29 int ans = 0; 30 for(int i = 0;i<t;i++) 31 { 32 ans+=2*(num[i*M].str-1); 33 } 34 printf("%d\n",ans); 35 return 0; 36 }