Under construction

Still being built. Things may change

Big AND Circuit

Time limit3sMemory limit1024 MB

Summary
Given N source signals with delays n_i, a gate delay D, and an LED delay L, build a tree of N-1 AND gates that minimizes the worst-case delay to flip the LED.
Level

Medium7 of 10

Topics
Tree, Greedy, Sorting, Divide and conquer
Solved
No attempts yet

Problem

You need a circuit that computes the logical AND of several source signals. The only part you may use is a two-input AND gate, which outputs True if and only if both of its inputs are True.

The circuit must satisfy these conditions.

  • Use exactly N−1N-1 AND gates, where NN is the number of source signals.
  • Each source signal connects to exactly one AND gate input.
  • Each AND gate input connects to exactly one signal, either a source signal or the output of another AND gate.
  • The circuit has no cycles. The output of an AND gate never reaches one of its own inputs.

The circuit output drives an LED. The LED is lit if and only if all source signals are True.

The timing rules are as follows.

  • When source signal ii changes, the change takes exactly nin_i nanoseconds to reach the circuit.
  • After an AND gate input changes, the gate output changes exactly DD nanoseconds later, if the new inputs actually change the output.
  • The LED changes exactly LL nanoseconds after its input signal changes.
  • Signals travel between gates in 0 nanoseconds.

Build the circuit that minimizes the maximum time from a source change to an LED change, counting only changes that actually flip the LED.

Figure 1: An optimal circuit for each sample input. The numbers on the left are the source signal indices.

Input

The first line contains three integers NN (2≤N≤4⋅1052 \leq N \leq 4 \cdot 10^5), DD, and LL (1≤D,L≤1091 \leq D, L \leq 10^9). NN is the number of source signals, and DD and LL are as described in the problem.

The second line contains NN integers n1,n2,…,nNn_1, n_2, \ldots, n_N (1≤ni≤1091 \leq n_i \leq 10^9), where nin_i is the number of nanoseconds it takes for a change in source ii to reach the circuit.

Output

Print the smallest time TT such that some circuit guarantees the LED takes at most TT nanoseconds to change whenever a source change would flip it.

Examples2

  1. Example 1

    Input
    3 3 5
    3 8 1
    
    Expected output
    16
    
  2. Example 2

    Input
    5 5 10
    5 8 3 3 3
    
    Expected output
    28