Poj3104: drying (Binary)

Source: Internet
Author: User

Description

It is very hard to wash and especially to dry clothes in winter. but Jane is a very smart girl. she is not afraid of this boring process. jane has decided to use a radiator to make drying faster. but the radiator is small, so it can hold only one thing at a time.

Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.

There areNClothes Jane has just already ed. Each of them tookAIWater during processing. every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet ). when amount of water contained becomes zero the cloth becomes dry and is ready to be packed.

Every minute Jane can select one thing to dry on the radiator. the radiator is very hot, so the amount of water in this thing decreasesKThis minute (but not less than zero-if the thing contains lessKWater, the resulting amount of water will be zero ).

The task is to minimize the total time of drying by means of using the radiator implements tively. The drying process ends when all the clothes are dry.

Input

The first line contains a single integerN(1 ≤N≤ 100 000). The second line containsAISeparated by spaces (1 ≤AI≤ 109). The third line containsK(1 ≤K≤ 109 ).

Output

Output a single integer-the minimal possible number of minutes required to dry all clothes.

Sample Input

 
Sample input #132 3 95Sample input #232 3 65

Sample output

 
Sample output #13Sample output #22
 
 
 
Binary enumeration time
 
# Include <stdio. h> # include <string. h >#include <algorithm> using namespace STD; # define up (I, x, y) for (I = x; I <= y; I ++) # define down (I, x, y) for (I = x; I> = y; I --) # define MEM (a, B) memset (A, B, sizeof (A) # define W (a) While (a) # define ll long longint N; int A [100005], K, ANS, R, maxn, L, mid; int solve (int x) {int I, sum = 0; up (I, 0, n-1) {if (a [I]> X) {sum + = (a [I]-x + K-2)/(k-1); If (sum> X) return 0 ;}} return 1 ;}int main () {int I, J; W (~ Scanf ("% d", & N) {maxn = 0; up (I, 0, n-1) scanf ("% d", & A [I]); sort (A, A + n); scanf ("% d", & K); If (k = 1) {printf ("% d \ n ", A [n-1]); continue;} l = 1, R = A [n-1]; W (L <= r) {mid = L + (R-l)/2; if (solve (MID) {ans = mid; r = mid-1;} else l = Mid + 1;} printf ("% d \ n", ANS );} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.