electicode
HomeCoursesResourcesProblems
...

Bit Days

Time Limit: 1500msMemory Limit: 512MB
View Submissions

Problem Description

There are nnn machines in a workshop. Machine iii must be repaired exactly aia_iai​ times.

The workshop works in days numbered 1,2,3,…1, 2, 3, \ldots1,2,3,….

On each day, at most one repair can be done. However, not every machine is available on every day.

For a positive integer ddd, write ddd in binary. Machine iii is available on day ddd if the (i−1)(i-1)(i−1)-th bit of ddd is equal to 11.

Here bits are numbered from 000 starting from the least significant bit.

For example:

  • on day 111, binary representation is 111, so only machine 111 is available;
  • on day 222, binary representation is 101010, so only machine 222 is available;
  • on day 333, binary representation is 1111, so machines and are available;

Your task is to find the minimum number of days after which it is possible to complete all repairs.

In other words, find the smallest integer DDD such that the repairs can be scheduled during days 1,2,…,D1, 2, \ldots, D1,2,…,D, with at most one repair per day, and every machine iii repaired exactly aia_iai​ times.

Input Format

The first line contains one integer nnn (1≤n≤201 \le n \le 201≤n≤20) --- the number of machines.

The second line contains nnn integers a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​ () --- the required number of repairs for each machine.

Output Format

Output one integer --- the minimum possible value of DDD.

Scoring

SubtaskPointsConstraints
111101010n≤5n \leq 5n≤5, ai≤5a_i \leq 5a

Examples

Example 1
Input
2
1 1
Output
2
Explanation

In the first example, machine 111 is available on day 111, and machine 222 is available on day 222.

So we can repair machine 111 on day 111 and machine on day .

© 2026 Electicode. All rights reserved.

1
11
111
222
  • on day 555, binary representation is 101101101, so machines 111 and 333 are available.
  • 1≤ai≤10151 \le a_i \le 10^{15}
    1≤ai​≤1015
    i
    ​
    ≤
    5
    222151515n≤10n \leq 10n≤10, ∑ai≤2000\sum a_i \leq 2000∑ai​≤2000
    333202020n≤12n \leq 12n≤12, ai≤106a_i \leq 10^6ai​≤106
    444202020n≤20n \leq 20n≤20, ai≤106a_i \leq 10^6ai​≤106
    555353535No additional constraints.
    222
    222
    Example 2
    Input
    2
    2 1
    Output
    3
    Explanation

    In the second example, the first three days are:

    • day 111: only machine 111 is available;
    • day 222: only machine 222 is available;
    • day 333: machines 111 and 222 are available.

    One optimal schedule is:

    • repair machine 111 on day 111;
    • repair machine 222 on day 222;
    • repair machine 111 on day 333.

    Thus all repairs are completed after 333 days.

    Example 3
    Input
    3
    3 1 1
    Output
    5
    Explanation

    In the third example, the first five days are:

    DayBinaryAvailable machines
    111111111
    222101010222
    3331111111,21, 21,2
    444100100100333
    5551011011011,31, 31,3

    One possible optimal schedule is:

    • repair machine 111 on day 111;
    • repair machine 222 on day 222;
    • repair machine 111 on day 333;
    • repair machine 333 on day 444;

    So the answer is 555.

  • repair machine 111 on day 555.