Nearest Nice Numbers

시간 제한1초메모리 제한2048 MB

요약
N개의 확률과 분모 D가 주어질 때, 합이 D인 정수 f_i를 골라 |D·x_i - f_i|의 합을 최소로 만든다.
난이도

보통10점 중 5점

유형
그리디, 정렬, 수학
정답자
아직 제출이 없습니다

문제

While typical programming contests try to strike a balance between different types of problems (geometry, graphs, dynamic programming, number theory, strings, etc), at the User-Aligned Programming Competition (UAPC), the contestants decide in advance what types of problems they want to see by voting in a survey. The survey format is simple: each contestant picks their single favorite type of problem among NN options. Then, each problem type i∈1,2,…,Ni\in \\{1,2,\dots ,N\\} is assigned a number x_i∈\[0,1]x\_i \in \[0,1] based on what share of the votes it received. So, we must have ∑_i=1Nx_i=1\sum\_{i=1}^{N}{x\_i=1}.

Unfortunately, many of the x_ix\_i values came back with an unsightly number of decimal places due to the extremely large number of survey responses, which is not suspicious at all. To fix this, your job is to replace each x_ix\_i with an integer f_if\_i so that the fraction f_iD\frac{f\_i}{D} (for a given DD) approximates x_ix\_i. You must pick the f_if\_i values in a way that minimizes ∑_i=1N∣D⋅x_i−f_i∣\sum\_{i=1}^{N}{|D\cdot x\_i - f\_i|} and ensures that ∑_i=1Nf_i=D\sum\_{i=1}^{N}{f\_i=D}.

입력

The first line of input contains two integers NN (2≤N≤1052≤N≤10^5) and DD (1≤D≤1091≤D≤10^9) where NN is the number of problem types and DD is the denominator to use. Then NN lines follow, with line ii containing the single number x_ix\_i.

출력

Output the minimum possible value of ∑_i=1N∣D⋅x_i−f_i∣\sum\_{i=1}^{N}{|D\cdot x\_i- f\_i|} over all choices of ff. Answers with an absolute or relative error of at most 10−610^{-6} will be accepted.

예제4

  1. 예제 1

    입력
    3 100
    0.3333333333
    0.3333333333
    0.3333333334
    
    예상 출력
    1.3333333200
    
  2. 예제 2

    입력
    2 1
    0.01
    0.99
    
    예상 출력
    0.0200000000
    
  3. 예제 3

    입력
    2 2
    0.5
    0.5
    
    예상 출력
    0.0000000000
    
  4. 예제 4

    입력
    2 3
    0.5
    0.5
    
    예상 출력
    1.0000000000