Under construction

Still being built. Things may change

Java Certification

Time limit1sMemory limit128 MB

Summary
Given rounded per-category percentages and totals, find n_i and w_i values reproducing them while minimizing the spread between the largest and smallest category size.
Level

Medium6 of 10

Topics
Brute force, Math, Combinatorics
Solved
No attempts yet

Problem

You have just finished a Java certification exam with nn questions. Afterwards you receive a score card summarizing your performance. For example, if you answered 78 of 87 questions correctly, the card might look like this:

CategoryCorrect answers
Basic Concepts100%
Declarations100%
Expressions83%
Classes and Interfaces92%
Multithreading75%
Collections93%

The questions are split into mm categories (here m=6m = 6). Category ii contains nin_i questions with 1≤ni≤n1 \le n_i \le n and ∑i=1mni=n\sum_{i=1}^{m} n_i = n. You answered kk questions correctly (here k=78k = 78, n=87n = 87), so the total number of wrong answers is w=n−kw = n - k (here w=9w = 9).

Let wiw_i (with 0≤wi≤ni0 \le w_i \le n_i) be the number of wrong answers in category ii, so that ∑i=1mwi=w\sum_{i=1}^{m} w_i = w. For each category the card shows the percentage of correct answers, namely 100(ni−wi)/ni100 (n_i - w_i) / n_i rounded to the nearest integer; a value whose fractional part is exactly 0.50.5 is rounded to the nearest even integer.

The values wiw_i and nin_i are not necessarily determined uniquely by the card. Assuming the questions are distributed among the categories as evenly as possible, we only consider valid assignments of wiw_i and nin_i that minimize the difference between the largest nin_i and the smallest nin_i.

Input

The first line contains three integers kk, nn, and mm: the number of correct answers kk (0≤k≤n0 \le k \le n), the total number of questions nn (1≤n≤1001 \le n \le 100), and the number of categories mm (1≤m≤101 \le m \le 10). Each of the next mm lines contains a single integer between 00 and 100100 inclusive — the rounded percentage of correct answers in that category. The input always corresponds to at least one valid assignment of wiw_i and nin_i.

Output

A valid assignment of wiw_i and nin_i is not always unique, so output the quantity that is uniquely determined: a single integer — the minimum possible value of max⁡ini−min⁡ini\max_i n_i - \min_i n_i, over all assignments of integers wiw_i and nin_i satisfying 1≤ni1 \le n_i, 0≤wi≤ni0 \le w_i \le n_i, ∑i=1mni=n\sum_{i=1}^{m} n_i = n, ∑i=1mwi=n−k\sum_{i=1}^{m} w_i = n - k, and reproducing the given rounded percentage for every category.

Examples4

  1. Example 1

    Input
    78 87 6
    100
    100
    83
    92
    75
    93
    
    Expected output
    5
    
  2. Example 2

    Input
    8 10 1
    80
    
    Expected output
    0
    
  3. Example 3

    Input
    50 50 5
    100
    100
    100
    100
    100
    
    Expected output
    0
    
  4. Example 4

    Input
    14 20 4
    83
    67
    75
    50
    
    Expected output
    2